repo_name
stringlengths
7
71
file_path
stringlengths
5
118
context
list
import_statement
stringlengths
45
12.5k
token_num
int64
641
99.4k
cropped_code
stringlengths
44
17k
all_code
stringlengths
43
754k
next_line
stringlengths
2
330
gold_snippet_index
int64
0
68
created_at
stringlengths
25
25
level
stringclasses
9 values
codefuse-ai/CodeFuse-ModelCache
modelcache/adapter/adapter.py
[ { "identifier": "adapt_query", "path": "modelcache/adapter/adapter_query.py", "snippet": "def adapt_query(cache_data_convert, *args, **kwargs):\n chat_cache = kwargs.pop(\"cache_obj\", cache)\n scope = kwargs.pop(\"scope\", None)\n model = scope['model']\n if not chat_cache.has_init:\n raise NotInitError()\n cache_enable = chat_cache.cache_enable_func(*args, **kwargs)\n context = kwargs.pop(\"cache_context\", {})\n embedding_data = None\n cache_factor = kwargs.pop(\"cache_factor\", 1.0)\n pre_embedding_data = chat_cache.query_pre_embedding_func(\n kwargs,\n extra_param=context.get(\"pre_embedding_func\", None),\n prompts=chat_cache.config.prompts,\n )\n\n if cache_enable:\n embedding_data = time_cal(\n chat_cache.embedding_func,\n func_name=\"embedding\",\n report_func=chat_cache.report.embedding,\n )(pre_embedding_data)\n\n if cache_enable:\n cache_data_list = time_cal(\n chat_cache.data_manager.search,\n func_name=\"milvus_search\",\n report_func=chat_cache.report.search,\n )(\n embedding_data,\n extra_param=context.get(\"search_func\", None),\n top_k=kwargs.pop(\"top_k\", -1),\n model=model\n )\n cache_answers = []\n cache_questions = []\n cache_ids = []\n similarity_threshold = chat_cache.config.similarity_threshold\n similarity_threshold_long = chat_cache.config.similarity_threshold_long\n\n min_rank, max_rank = chat_cache.similarity_evaluation.range()\n rank_threshold = (max_rank - min_rank) * similarity_threshold * cache_factor\n rank_threshold_long = (max_rank - min_rank) * similarity_threshold_long * cache_factor\n rank_threshold = (\n max_rank\n if rank_threshold > max_rank\n else min_rank\n if rank_threshold < min_rank\n else rank_threshold\n )\n rank_threshold_long = (\n max_rank\n if rank_threshold_long > max_rank\n else min_rank\n if rank_threshold_long < min_rank\n else rank_threshold_long\n )\n\n if cache_data_list is None or len(cache_data_list) == 0:\n rank_pre = -1.0\n else:\n cache_data_dict = {'search_result': cache_data_list[0]}\n rank_pre = chat_cache.similarity_evaluation.evaluation(\n None,\n cache_data_dict,\n extra_param=context.get(\"evaluation_func\", None),\n )\n if rank_pre < rank_threshold:\n return\n\n for cache_data in cache_data_list:\n primary_id = cache_data[1]\n start_time = time.time()\n ret = chat_cache.data_manager.get_scalar_data(\n cache_data, extra_param=context.get(\"get_scalar_data\", None)\n )\n if ret is None:\n continue\n\n if \"deps\" in context and hasattr(ret.question, \"deps\"):\n eval_query_data = {\n \"question\": context[\"deps\"][0][\"data\"],\n \"embedding\": None\n }\n eval_cache_data = {\n \"question\": ret.question.deps[0].data,\n \"answer\": ret.answers[0].answer,\n \"search_result\": cache_data,\n \"embedding\": None,\n }\n else:\n eval_query_data = {\n \"question\": pre_embedding_data,\n \"embedding\": embedding_data,\n }\n\n eval_cache_data = {\n \"question\": ret[0],\n \"answer\": ret[1],\n \"search_result\": cache_data,\n \"embedding\": None\n }\n rank = chat_cache.similarity_evaluation.evaluation(\n eval_query_data,\n eval_cache_data,\n extra_param=context.get(\"evaluation_func\", None),\n )\n\n if len(pre_embedding_data) <= 256:\n if rank_threshold <= rank:\n cache_answers.append((rank, ret[1]))\n cache_questions.append((rank, ret[0]))\n cache_ids.append((rank, primary_id))\n else:\n if rank_threshold_long <= rank:\n cache_answers.append((rank, ret[1]))\n cache_questions.append((rank, ret[0]))\n cache_ids.append((rank, primary_id))\n cache_answers = sorted(cache_answers, key=lambda x: x[0], reverse=True)\n cache_questions = sorted(cache_questions, key=lambda x: x[0], reverse=True)\n cache_ids = sorted(cache_ids, key=lambda x: x[0], reverse=True)\n if len(cache_answers) != 0:\n return_message = chat_cache.post_process_messages_func(\n [t[1] for t in cache_answers]\n )\n return_query = chat_cache.post_process_messages_func(\n [t[1] for t in cache_questions]\n )\n return_id = chat_cache.post_process_messages_func(\n [t[1] for t in cache_ids]\n )\n # 更新命中次数\n try:\n chat_cache.data_manager.update_hit_count(return_id)\n except Exception:\n logging.info('update_hit_count except, please check!')\n\n chat_cache.report.hint_cache()\n return cache_data_convert(return_message, return_query)" }, { "identifier": "adapt_insert", "path": "modelcache/adapter/adapter_insert.py", "snippet": "def adapt_insert(*args, **kwargs):\n chat_cache = kwargs.pop(\"cache_obj\", cache)\n model = kwargs.pop(\"model\", None)\n require_object_store = kwargs.pop(\"require_object_store\", False)\n if require_object_store:\n assert chat_cache.data_manager.o, \"Object store is required for adapter.\"\n if not chat_cache.has_init:\n raise NotInitError()\n cache_enable = chat_cache.cache_enable_func(*args, **kwargs)\n context = kwargs.pop(\"cache_context\", {})\n embedding_data = None\n pre_embedding_data = chat_cache.insert_pre_embedding_func(\n kwargs,\n extra_param=context.get(\"pre_embedding_func\", None),\n prompts=chat_cache.config.prompts,\n )\n chat_info = kwargs.pop(\"chat_info\", [])\n llm_data = chat_info[-1]['answer']\n\n if cache_enable:\n embedding_data = time_cal(\n chat_cache.embedding_func,\n func_name=\"embedding\",\n report_func=chat_cache.report.embedding,\n )(pre_embedding_data)\n\n chat_cache.data_manager.save(\n pre_embedding_data,\n llm_data,\n embedding_data,\n model=model,\n extra_param=context.get(\"save_func\", None)\n )\n return 'success'" }, { "identifier": "adapt_remove", "path": "modelcache/adapter/adapter_remove.py", "snippet": "def adapt_remove(*args, **kwargs):\n chat_cache = kwargs.pop(\"cache_obj\", cache)\n model = kwargs.pop(\"model\", None)\n remove_type = kwargs.pop(\"remove_type\", None)\n require_object_store = kwargs.pop(\"require_object_store\", False)\n if require_object_store:\n assert chat_cache.data_manager.o, \"Object store is required for adapter.\"\n if not chat_cache.has_init:\n raise NotInitError()\n\n # delete data\n if remove_type == 'delete_by_id':\n id_list = kwargs.pop(\"id_list\", [])\n resp = chat_cache.data_manager.delete(id_list, model=model)\n elif remove_type == 'truncate_by_model':\n resp = chat_cache.data_manager.truncate(model)\n else:\n # resp = \"remove_type_error\"\n raise RemoveError()\n return resp" } ]
import logging import openai from modelcache.adapter.adapter_query import adapt_query from modelcache.adapter.adapter_insert import adapt_insert from modelcache.adapter.adapter_remove import adapt_remove
1,930
# -*- coding: utf-8 -*- class ChatCompletion(openai.ChatCompletion): """Openai ChatCompletion Wrapper""" @classmethod def create_query(cls, *args, **kwargs): def cache_data_convert(cache_data, cache_query): return construct_resp_from_cache(cache_data, cache_query) try: return adapt_query( cache_data_convert, *args, **kwargs ) except Exception as e: return str(e) @classmethod def create_insert(cls, *args, **kwargs): try: return adapt_insert( *args, **kwargs ) except Exception as e: return str(e) @classmethod def create_remove(cls, *args, **kwargs): try:
# -*- coding: utf-8 -*- class ChatCompletion(openai.ChatCompletion): """Openai ChatCompletion Wrapper""" @classmethod def create_query(cls, *args, **kwargs): def cache_data_convert(cache_data, cache_query): return construct_resp_from_cache(cache_data, cache_query) try: return adapt_query( cache_data_convert, *args, **kwargs ) except Exception as e: return str(e) @classmethod def create_insert(cls, *args, **kwargs): try: return adapt_insert( *args, **kwargs ) except Exception as e: return str(e) @classmethod def create_remove(cls, *args, **kwargs): try:
return adapt_remove(
2
2023-11-01 01:56:10+00:00
4k
bobby-he/simplified_transformers
run_clm.py
[ { "identifier": "model_utils", "path": "simplified_transformers/model_utils.py", "snippet": "class RMSNorm(nn.Module):\nclass myGPT2Block(nn.Module):\nclass myGPT2Attention(nn.Module):\nclass myGPT2MLP(nn.Module):\nclass MyConv1D(nn.Module):\nclass LeakyReLU(nn.Module):\n def __init__(self, d, eps=1e-8):\n def forward(self, x):\ndef convertGPT2model(gpt2_model, new_cfg):\n def __init__(self, config, layer_idx=None):\n def forward(\n self,\n hidden_states: Optional[Tuple[torch.FloatTensor]],\n layer_past: Optional[Tuple[torch.Tensor]] = None,\n attention_mask: Optional[torch.FloatTensor] = None,\n head_mask: Optional[torch.FloatTensor] = None,\n encoder_hidden_states: Optional[torch.Tensor] = None,\n encoder_attention_mask: Optional[torch.FloatTensor] = None,\n use_cache: Optional[bool] = False,\n output_attentions: Optional[bool] = False,\n ) -> Union[\n def __init__(self, config, is_cross_attention=False, layer_idx=None):\n def _attn(self, query, key, value, attention_mask=None, head_mask=None):\n def _split_heads(self, tensor, num_heads, attn_head_size):\n def _merge_heads(self, tensor, num_heads, attn_head_size):\n def forward(\n self,\n hidden_states: Optional[Tuple[torch.FloatTensor]],\n layer_past: Optional[Tuple[torch.Tensor]] = None,\n attention_mask: Optional[torch.FloatTensor] = None,\n head_mask: Optional[torch.FloatTensor] = None,\n encoder_hidden_states: Optional[torch.Tensor] = None,\n encoder_attention_mask: Optional[torch.FloatTensor] = None,\n use_cache: Optional[bool] = False,\n output_attentions: Optional[bool] = False,\n ) -> Tuple[Union[torch.Tensor, Tuple[torch.Tensor]], ...]:\n def __init__(self, intermediate_size, config):\n def forward(\n self, hidden_states: Optional[Tuple[torch.FloatTensor]]\n ) -> torch.FloatTensor:\n def __init__(\n self,\n nf,\n nx,\n resid_gain=None,\n skip_gain=None,\n trainable_gains=False,\n init_type=\"normal\",\n bias=True,\n ):\n def forward(self, x):\n def __init__(self, negative_slope: float = 1e-2, inplace: bool = False) -> None:\n def forward(self, input: torch.Tensor) -> torch.Tensor:\n def extra_repr(self) -> str:" }, { "identifier": "train_utils", "path": "simplified_transformers/train_utils.py", "snippet": "class MyTrainer(Trainer):\n def create_optimizer(self):\n def compute_loss(self, model, inputs, return_outputs=False):" } ]
import hydra import os import logging import transformers from datasets import load_dataset, DatasetDict from transformers import ( AutoTokenizer, GPT2LMHeadModel, AutoConfig, DataCollatorForLanguageModeling, TrainingArguments, ) from simplified_transformers import model_utils, train_utils
1,749
"""Script for a training run.""" log = logging.getLogger(__name__) @hydra.main(config_path="simplified_transformers/config", config_name="config") def launch(cfg): os.environ["CUDA_VISIBLE_DEVICES"] = str(cfg.gpu) transformers.set_seed(cfg.seed) ds_train = load_dataset("huggingface-course/codeparrot-ds-train", split="train") ds_valid = load_dataset( "huggingface-course/codeparrot-ds-valid", split="validation" ) raw_datasets = DatasetDict( { "train": ds_train.shuffle(seed=0).select( range(cfg.num_token_mult * 100000) ), "valid": ds_valid.shuffle(seed=0).select(range(2000)), } ) context_length = 128 tokenizer = AutoTokenizer.from_pretrained( "huggingface-course/code-search-net-tokenizer", use_fast=True ) outputs = tokenizer( raw_datasets["train"][:2]["content"], truncation=True, max_length=context_length, return_overflowing_tokens=True, return_length=True, ) print(f"Input IDs length: {len(outputs['input_ids'])}") print(f"Input chunk lengths: {(outputs['length'])}") print(f"Chunk mapping: {outputs['overflow_to_sample_mapping']}") def tokenize(element): outputs = tokenizer( element["content"], truncation=True, max_length=context_length, return_overflowing_tokens=True, return_length=True, ) input_batch = [] for length, input_ids in zip(outputs["length"], outputs["input_ids"]): if length == context_length: input_batch.append(input_ids) return {"input_ids": input_batch} tokenized_datasets = raw_datasets.map( tokenize, batched=True, remove_columns=raw_datasets["train"].column_names ) model_config = AutoConfig.from_pretrained( cfg.model.name, vocab_size=len(tokenizer), n_ctx=context_length, bos_token_id=tokenizer.bos_token_id, eos_token_id=tokenizer.eos_token_id, resid_pdrop=cfg.model.resid_pdrop, attn_pdrop=cfg.model.attn_pdrop, embd_pdrop=cfg.model.embd_pdrop, n_layer=cfg.model.n_layer, n_head=cfg.model.n_head, n_embd=cfg.model.n_embd, n_inner=int(cfg.model.n_embd * cfg.model.mlp_width_mult), initializer_range=cfg.model.initializer_range, output_attentions=cfg.report_attn_entropy, ) model = GPT2LMHeadModel(model_config) model_config.update( { "attn_block_resid_gain": cfg.model.attn_block_resid_gain, "attn_block_skip_gain": cfg.model.attn_block_skip_gain, "mlp_block_resid_gain": cfg.model.mlp_block_resid_gain, "mlp_block_skip_gain": cfg.model.mlp_block_skip_gain, "attn_mat_resid_gain": cfg.model.attn_mat_resid_gain, "attn_mat_skip_gain": cfg.model.attn_mat_skip_gain, "value_resid_gain": cfg.model.value_resid_gain, "first_layer_value_resid_gain": cfg.model.first_layer_value_resid_gain, "value_skip_gain": cfg.model.value_skip_gain, "proj_resid_gain": cfg.model.proj_resid_gain, "last_layer_proj_resid_gain": cfg.model.last_layer_proj_resid_gain, "proj_skip_gain": cfg.model.proj_skip_gain, "trainable_attn_block_gains": cfg.model.trainable_attn_block_gains, "trainable_mlp_block_gains": cfg.model.trainable_mlp_block_gains, "trainable_attn_mat_gains": cfg.model.trainable_attn_mat_gains, "trainable_value_gains": cfg.model.trainable_value_gains, "trainable_proj_gains": cfg.model.trainable_proj_gains, "norm_type": cfg.model.norm_type, "val_proj_init_std": cfg.model.val_proj_init_std, "query_init_std": cfg.model.query_init_std, "key_init_std": cfg.model.key_init_std, "centre_attn": cfg.model.centre_attn, "centre_attn_gain": cfg.model.centre_attn_gain, "val_init_type": cfg.model.val_init_type, "proj_init_type": cfg.model.proj_init_type, "activation_function": cfg.model.activation_function, "lrelu_neg_slope": cfg.model.lrelu_neg_slope, "mlp_proj_init_std": cfg.model.mlp_proj_init_std, "parallel_layers": cfg.model.parallel_layers, "norm_position": cfg.model.norm_position, "tie_valproj_init": cfg.model.tie_valproj_init, } )
"""Script for a training run.""" log = logging.getLogger(__name__) @hydra.main(config_path="simplified_transformers/config", config_name="config") def launch(cfg): os.environ["CUDA_VISIBLE_DEVICES"] = str(cfg.gpu) transformers.set_seed(cfg.seed) ds_train = load_dataset("huggingface-course/codeparrot-ds-train", split="train") ds_valid = load_dataset( "huggingface-course/codeparrot-ds-valid", split="validation" ) raw_datasets = DatasetDict( { "train": ds_train.shuffle(seed=0).select( range(cfg.num_token_mult * 100000) ), "valid": ds_valid.shuffle(seed=0).select(range(2000)), } ) context_length = 128 tokenizer = AutoTokenizer.from_pretrained( "huggingface-course/code-search-net-tokenizer", use_fast=True ) outputs = tokenizer( raw_datasets["train"][:2]["content"], truncation=True, max_length=context_length, return_overflowing_tokens=True, return_length=True, ) print(f"Input IDs length: {len(outputs['input_ids'])}") print(f"Input chunk lengths: {(outputs['length'])}") print(f"Chunk mapping: {outputs['overflow_to_sample_mapping']}") def tokenize(element): outputs = tokenizer( element["content"], truncation=True, max_length=context_length, return_overflowing_tokens=True, return_length=True, ) input_batch = [] for length, input_ids in zip(outputs["length"], outputs["input_ids"]): if length == context_length: input_batch.append(input_ids) return {"input_ids": input_batch} tokenized_datasets = raw_datasets.map( tokenize, batched=True, remove_columns=raw_datasets["train"].column_names ) model_config = AutoConfig.from_pretrained( cfg.model.name, vocab_size=len(tokenizer), n_ctx=context_length, bos_token_id=tokenizer.bos_token_id, eos_token_id=tokenizer.eos_token_id, resid_pdrop=cfg.model.resid_pdrop, attn_pdrop=cfg.model.attn_pdrop, embd_pdrop=cfg.model.embd_pdrop, n_layer=cfg.model.n_layer, n_head=cfg.model.n_head, n_embd=cfg.model.n_embd, n_inner=int(cfg.model.n_embd * cfg.model.mlp_width_mult), initializer_range=cfg.model.initializer_range, output_attentions=cfg.report_attn_entropy, ) model = GPT2LMHeadModel(model_config) model_config.update( { "attn_block_resid_gain": cfg.model.attn_block_resid_gain, "attn_block_skip_gain": cfg.model.attn_block_skip_gain, "mlp_block_resid_gain": cfg.model.mlp_block_resid_gain, "mlp_block_skip_gain": cfg.model.mlp_block_skip_gain, "attn_mat_resid_gain": cfg.model.attn_mat_resid_gain, "attn_mat_skip_gain": cfg.model.attn_mat_skip_gain, "value_resid_gain": cfg.model.value_resid_gain, "first_layer_value_resid_gain": cfg.model.first_layer_value_resid_gain, "value_skip_gain": cfg.model.value_skip_gain, "proj_resid_gain": cfg.model.proj_resid_gain, "last_layer_proj_resid_gain": cfg.model.last_layer_proj_resid_gain, "proj_skip_gain": cfg.model.proj_skip_gain, "trainable_attn_block_gains": cfg.model.trainable_attn_block_gains, "trainable_mlp_block_gains": cfg.model.trainable_mlp_block_gains, "trainable_attn_mat_gains": cfg.model.trainable_attn_mat_gains, "trainable_value_gains": cfg.model.trainable_value_gains, "trainable_proj_gains": cfg.model.trainable_proj_gains, "norm_type": cfg.model.norm_type, "val_proj_init_std": cfg.model.val_proj_init_std, "query_init_std": cfg.model.query_init_std, "key_init_std": cfg.model.key_init_std, "centre_attn": cfg.model.centre_attn, "centre_attn_gain": cfg.model.centre_attn_gain, "val_init_type": cfg.model.val_init_type, "proj_init_type": cfg.model.proj_init_type, "activation_function": cfg.model.activation_function, "lrelu_neg_slope": cfg.model.lrelu_neg_slope, "mlp_proj_init_std": cfg.model.mlp_proj_init_std, "parallel_layers": cfg.model.parallel_layers, "norm_position": cfg.model.norm_position, "tie_valproj_init": cfg.model.tie_valproj_init, } )
model = model_utils.convertGPT2model(model, model_config)
0
2023-11-01 14:28:43+00:00
4k
garibida/cross-image-attention
run.py
[ { "identifier": "AppearanceTransferModel", "path": "appearance_transfer_model.py", "snippet": "class AppearanceTransferModel:\n\n def __init__(self, config: RunConfig, pipe: Optional[CrossImageAttentionStableDiffusionPipeline] = None):\n self.config = config\n self.pipe = get_stable_diffusion_model() if pipe is None else pipe\n self.register_attention_control()\n self.segmentor = Segmentor(prompt=config.prompt, object_nouns=[config.object_noun])\n self.latents_app, self.latents_struct = None, None\n self.zs_app, self.zs_struct = None, None\n self.image_app_mask_32, self.image_app_mask_64 = None, None\n self.image_struct_mask_32, self.image_struct_mask_64 = None, None\n self.enable_edit = False\n self.step = 0\n\n def set_latents(self, latents_app: torch.Tensor, latents_struct: torch.Tensor):\n self.latents_app = latents_app\n self.latents_struct = latents_struct\n\n def set_noise(self, zs_app: torch.Tensor, zs_struct: torch.Tensor):\n self.zs_app = zs_app\n self.zs_struct = zs_struct\n\n def set_masks(self, masks: List[torch.Tensor]):\n self.image_app_mask_32, self.image_struct_mask_32, self.image_app_mask_64, self.image_struct_mask_64 = masks\n\n def get_adain_callback(self):\n\n def callback(st: int, timestep: int, latents: torch.FloatTensor) -> Callable:\n self.step = st\n # Compute the masks using prompt mixing self-segmentation and use the masks for AdaIN operation\n if self.config.use_masked_adain and self.step == self.config.adain_range.start:\n masks = self.segmentor.get_object_masks()\n self.set_masks(masks)\n # Apply AdaIN operation using the computed masks\n if self.config.adain_range.start <= self.step < self.config.adain_range.end:\n if self.config.use_masked_adain:\n latents[0] = masked_adain(latents[0], latents[1], self.image_struct_mask_64, self.image_app_mask_64)\n else:\n latents[0] = adain(latents[0], latents[1])\n\n return callback\n\n def register_attention_control(self):\n\n model_self = self\n\n class AttentionProcessor:\n\n def __init__(self, place_in_unet: str):\n self.place_in_unet = place_in_unet\n if not hasattr(F, \"scaled_dot_product_attention\"):\n raise ImportError(\"AttnProcessor2_0 requires torch 2.0, to use it, please upgrade torch to 2.0.\")\n\n def __call__(self,\n attn,\n hidden_states: torch.Tensor,\n encoder_hidden_states: Optional[torch.Tensor] = None,\n attention_mask=None,\n temb=None,\n perform_swap: bool = False):\n\n residual = hidden_states\n\n if attn.spatial_norm is not None:\n hidden_states = attn.spatial_norm(hidden_states, temb)\n\n input_ndim = hidden_states.ndim\n\n if input_ndim == 4:\n batch_size, channel, height, width = hidden_states.shape\n hidden_states = hidden_states.view(batch_size, channel, height * width).transpose(1, 2)\n\n batch_size, sequence_length, _ = (\n hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape\n )\n\n if attention_mask is not None:\n attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length, batch_size)\n attention_mask = attention_mask.view(batch_size, attn.heads, -1, attention_mask.shape[-1])\n\n if attn.group_norm is not None:\n hidden_states = attn.group_norm(hidden_states.transpose(1, 2)).transpose(1, 2)\n\n query = attn.to_q(hidden_states)\n\n is_cross = encoder_hidden_states is not None\n if not is_cross:\n encoder_hidden_states = hidden_states\n elif attn.norm_cross:\n encoder_hidden_states = attn.norm_encoder_hidden_states(encoder_hidden_states)\n\n key = attn.to_k(encoder_hidden_states)\n value = attn.to_v(encoder_hidden_states)\n\n inner_dim = key.shape[-1]\n head_dim = inner_dim // attn.heads\n should_mix = False\n\n # Potentially apply our cross image attention operation\n # To do so, we need to be in a self-attention alyer in the decoder part of the denoising network\n if perform_swap and not is_cross and \"up\" in self.place_in_unet and model_self.enable_edit:\n if attention_utils.should_mix_keys_and_values(model_self, hidden_states):\n should_mix = True\n if model_self.step % 5 == 0 and model_self.step < 40:\n # Inject the structure's keys and values\n key[OUT_INDEX] = key[STRUCT_INDEX]\n value[OUT_INDEX] = value[STRUCT_INDEX]\n else:\n # Inject the appearance's keys and values\n key[OUT_INDEX] = key[STYLE_INDEX]\n value[OUT_INDEX] = value[STYLE_INDEX]\n\n query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)\n key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)\n value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)\n\n # Compute the cross attention and apply our contrasting operation\n hidden_states, attn_weight = attention_utils.compute_scaled_dot_product_attention(\n query, key, value,\n edit_map=perform_swap and model_self.enable_edit and should_mix,\n is_cross=is_cross,\n contrast_strength=model_self.config.contrast_strength,\n )\n\n # Update attention map for segmentation\n if model_self.config.use_masked_adain and model_self.step == model_self.config.adain_range.start - 1:\n model_self.segmentor.update_attention(attn_weight, is_cross)\n\n hidden_states = hidden_states.transpose(1, 2).reshape(batch_size, -1, attn.heads * head_dim)\n hidden_states = hidden_states.to(query[OUT_INDEX].dtype)\n\n # linear proj\n hidden_states = attn.to_out[0](hidden_states)\n # dropout\n hidden_states = attn.to_out[1](hidden_states)\n\n if input_ndim == 4:\n hidden_states = hidden_states.transpose(-1, -2).reshape(batch_size, channel, height, width)\n\n if attn.residual_connection:\n hidden_states = hidden_states + residual\n\n hidden_states = hidden_states / attn.rescale_output_factor\n\n return hidden_states\n\n def register_recr(net_, count, place_in_unet):\n if net_.__class__.__name__ == 'ResnetBlock2D':\n pass\n if net_.__class__.__name__ == 'Attention':\n net_.set_processor(AttentionProcessor(place_in_unet + f\"_{count + 1}\"))\n return count + 1\n elif hasattr(net_, 'children'):\n for net__ in net_.children():\n count = register_recr(net__, count, place_in_unet)\n return count\n\n cross_att_count = 0\n sub_nets = self.pipe.unet.named_children()\n for net in sub_nets:\n if \"down\" in net[0]:\n cross_att_count += register_recr(net[1], 0, \"down\")\n elif \"up\" in net[0]:\n cross_att_count += register_recr(net[1], 0, \"up\")\n elif \"mid\" in net[0]:\n cross_att_count += register_recr(net[1], 0, \"mid\")" }, { "identifier": "RunConfig", "path": "config.py", "snippet": "class RunConfig:\n # Appearance image path\n app_image_path: Path\n # Struct image path\n struct_image_path: Path\n # Domain name (e.g., buildings, animals)\n domain_name: Optional[str] = None\n # Output path\n output_path: Path = Path('./output')\n # Random seed\n seed: int = 42\n # Input prompt for inversion (will use domain name as default)\n prompt: Optional[str] = None\n # Number of timesteps\n num_timesteps: int = 100\n # Whether to use a binary mask for performing AdaIN\n use_masked_adain: bool = True\n # Timesteps to apply cross-attention on 64x64 layers\n cross_attn_64_range: Range = Range(start=10, end=90)\n # Timesteps to apply cross-attention on 32x32 layers\n cross_attn_32_range: Range = Range(start=10, end=70)\n # Timesteps to apply AdaIn\n adain_range: Range = Range(start=20, end=100)\n # Swap guidance scale\n swap_guidance_scale: float = 3.5\n # Attention contrasting strength\n contrast_strength: float = 1.67\n # Object nouns to use for self-segmentation (will use the domain name as default)\n object_noun: Optional[str] = None\n # Whether to load previously saved inverted latent codes\n load_latents: bool = True\n # Number of steps to skip in the denoising process (used value from original edit-friendly DDPM paper)\n skip_steps: int = 32\n\n def __post_init__(self):\n save_name = f'app={self.app_image_path.stem}---struct={self.struct_image_path.stem}'\n self.output_path = self.output_path / self.domain_name / save_name\n self.output_path.mkdir(parents=True, exist_ok=True)\n\n # Handle the domain name, prompt, and object nouns used for masking, etc.\n if self.use_masked_adain and self.domain_name is None:\n raise ValueError(\"Must provide --domain_name and --prompt when using masked AdaIN\")\n if not self.use_masked_adain and self.domain_name is None:\n self.domain_name = \"object\"\n if self.prompt is None:\n self.prompt = f\"A photo of a {self.domain_name}\"\n if self.object_noun is None:\n self.object_noun = self.domain_name\n\n # Define the paths to store the inverted latents to\n self.latents_path = Path(self.output_path) / \"latents\"\n self.latents_path.mkdir(parents=True, exist_ok=True)\n self.app_latent_save_path = self.latents_path / f\"{self.app_image_path.stem}.pt\"\n self.struct_latent_save_path = self.latents_path / f\"{self.struct_image_path.stem}.pt\"" }, { "identifier": "Range", "path": "config.py", "snippet": "class Range(NamedTuple):\n start: int\n end: int" }, { "identifier": "latent_utils", "path": "utils/latent_utils.py", "snippet": "def load_latents_or_invert_images(model: AppearanceTransferModel, cfg: RunConfig):\ndef load_latents(app_latent_save_path: Path, struct_latent_save_path: Path) -> Tuple[torch.Tensor, torch.Tensor]:\ndef load_noise(app_latent_save_path: Path, struct_latent_save_path: Path) -> Tuple[torch.Tensor, torch.Tensor]:\ndef invert_images(sd_model: AppearanceTransferModel, app_image: Image.Image, struct_image: Image.Image, cfg: RunConfig):\ndef get_init_latents_and_noises(model: AppearanceTransferModel, cfg: RunConfig) -> Tuple[torch.Tensor, torch.Tensor]:" }, { "identifier": "load_latents_or_invert_images", "path": "utils/latent_utils.py", "snippet": "def load_latents_or_invert_images(model: AppearanceTransferModel, cfg: RunConfig):\n if cfg.load_latents and cfg.app_latent_save_path.exists() and cfg.struct_latent_save_path.exists():\n print(\"Loading existing latents...\")\n latents_app, latents_struct = load_latents(cfg.app_latent_save_path, cfg.struct_latent_save_path)\n noise_app, noise_struct = load_noise(cfg.app_latent_save_path, cfg.struct_latent_save_path)\n print(\"Done.\")\n else:\n print(\"Inverting images...\")\n app_image, struct_image = image_utils.load_images(cfg=cfg, save_path=cfg.output_path)\n model.enable_edit = False # Deactivate the cross-image attention layers\n latents_app, latents_struct, noise_app, noise_struct = invert_images(app_image=app_image,\n struct_image=struct_image,\n sd_model=model.pipe,\n cfg=cfg)\n model.enable_edit = True\n print(\"Done.\")\n return latents_app, latents_struct, noise_app, noise_struct" } ]
import sys import numpy as np import pyrallis import torch from typing import List from PIL import Image from diffusers.training_utils import set_seed from appearance_transfer_model import AppearanceTransferModel from config import RunConfig, Range from utils import latent_utils from utils.latent_utils import load_latents_or_invert_images
3,204
sys.path.append(".") sys.path.append("..") @pyrallis.wrap() def main(cfg: RunConfig): run(cfg) def run(cfg: RunConfig) -> List[Image.Image]: pyrallis.dump(cfg, open(cfg.output_path / 'config.yaml', 'w')) set_seed(cfg.seed) model = AppearanceTransferModel(cfg) latents_app, latents_struct, noise_app, noise_struct = load_latents_or_invert_images(model=model, cfg=cfg) model.set_latents(latents_app, latents_struct) model.set_noise(noise_app, noise_struct) print("Running appearance transfer...") images = run_appearance_transfer(model=model, cfg=cfg) print("Done.") return images def run_appearance_transfer(model: AppearanceTransferModel, cfg: RunConfig) -> List[Image.Image]:
sys.path.append(".") sys.path.append("..") @pyrallis.wrap() def main(cfg: RunConfig): run(cfg) def run(cfg: RunConfig) -> List[Image.Image]: pyrallis.dump(cfg, open(cfg.output_path / 'config.yaml', 'w')) set_seed(cfg.seed) model = AppearanceTransferModel(cfg) latents_app, latents_struct, noise_app, noise_struct = load_latents_or_invert_images(model=model, cfg=cfg) model.set_latents(latents_app, latents_struct) model.set_noise(noise_app, noise_struct) print("Running appearance transfer...") images = run_appearance_transfer(model=model, cfg=cfg) print("Done.") return images def run_appearance_transfer(model: AppearanceTransferModel, cfg: RunConfig) -> List[Image.Image]:
init_latents, init_zs = latent_utils.get_init_latents_and_noises(model=model, cfg=cfg)
3
2023-11-04 19:28:41+00:00
4k
ForceFledgling/proxyhub
proxyhub/proxy.py
[ { "identifier": "ProxyConnError", "path": "proxyhub/errors.py", "snippet": "class ProxyConnError(ProxyError):\n errmsg = 'connection_failed'" }, { "identifier": "ProxyEmptyRecvError", "path": "proxyhub/errors.py", "snippet": "class ProxyEmptyRecvError(ProxyError):\n errmsg = 'empty_response'" }, { "identifier": "ProxyRecvError", "path": "proxyhub/errors.py", "snippet": "class ProxyRecvError(ProxyError):\n errmsg = 'connection_is_reset'" }, { "identifier": "ProxySendError", "path": "proxyhub/errors.py", "snippet": "class ProxySendError(ProxyError):\n errmsg = 'connection_is_reset'" }, { "identifier": "ProxyTimeoutError", "path": "proxyhub/errors.py", "snippet": "class ProxyTimeoutError(ProxyError):\n errmsg = 'connection_timeout'" }, { "identifier": "ResolveError", "path": "proxyhub/errors.py", "snippet": "class ResolveError(Exception):\n pass" }, { "identifier": "NGTRS", "path": "proxyhub/negotiators.py", "snippet": "NGTRS = {\n 'HTTP': HttpNgtr,\n 'HTTPS': HttpsNgtr,\n 'SOCKS4': Socks4Ngtr,\n 'SOCKS5': Socks5Ngtr,\n 'CONNECT:80': Connect80Ngtr,\n 'CONNECT:25': Connect25Ngtr,\n}" }, { "identifier": "Resolver", "path": "proxyhub/resolver.py", "snippet": "class Resolver:\n \"\"\"Async host resolver based on aiodns.\"\"\"\n\n _cached_hosts = {}\n _ip_hosts = [\n 'https://wtfismyip.com/text',\n 'http://api.ipify.org/',\n 'http://ipinfo.io/ip',\n 'http://ipv4.icanhazip.com/',\n 'http://myexternalip.com/raw',\n 'http://ipinfo.io/ip',\n 'http://ifconfig.io/ip',\n ]\n # the list of resolvers will point a copy of original one\n _temp_host = []\n\n def __init__(self, timeout=5, loop=None):\n self._timeout = timeout\n self._loop = loop or asyncio.get_event_loop()\n self._resolver = aiodns.DNSResolver(loop=self._loop)\n\n @staticmethod\n def host_is_ip(host):\n \"\"\"Check a host is IP address.\"\"\"\n # TODO: add IPv6 support\n try:\n host = '.'.join(f'{int(n)}' for n in host.split('.'))\n ipaddress.IPv4Address(host)\n except (ipaddress.AddressValueError, ValueError):\n return False\n else:\n return True\n\n @staticmethod\n def get_ip_info(ip):\n \"\"\"Return geo information about IP address.\n\n `code` - ISO country code\n `name` - Full name of country\n `region_code` - ISO region code\n `region_name` - Full name of region\n `city_name` - Full name of city\n \"\"\"\n # from pprint import pprint\n try:\n ipInfo = _mmdb_reader.get(ip) or {}\n except (maxminddb.errors.InvalidDatabaseError, ValueError):\n ipInfo = {}\n\n code, name = '--', 'Unknown'\n city_name, region_code, region_name = ('Unknown',) * 3\n if 'country' in ipInfo:\n code = ipInfo['country']['iso_code']\n name = ipInfo['country']['names']['en']\n elif 'continent' in ipInfo:\n code = ipInfo['continent']['code']\n name = ipInfo['continent']['names']['en']\n if 'city' in ipInfo:\n city_name = ipInfo['city']['names']['en']\n if 'subdivisions' in ipInfo:\n region_code = ipInfo['subdivisions'][0]['iso_code']\n region_name = ipInfo['subdivisions'][0]['names']['en']\n return GeoData(code, name, region_code, region_name, city_name)\n\n def _pop_random_ip_host(self):\n host = random.choice(self._temp_host)\n self._temp_host.remove(host)\n return host\n\n async def get_real_ext_ip(self):\n \"\"\"Return real external IP address.\"\"\"\n # make a copy of original one to temp one\n # so original one will stay no change\n self._temp_host = self._ip_hosts.copy()\n while self._temp_host:\n try:\n timeout = aiohttp.ClientTimeout(total=self._timeout)\n async with aiohttp.ClientSession(\n timeout=timeout, loop=self._loop\n ) as session, session.get(self._pop_random_ip_host()) as resp:\n ip = await resp.text()\n except asyncio.TimeoutError:\n pass\n else:\n ip = ip.strip()\n if self.host_is_ip(ip):\n log.debug('Real external IP: %s', ip)\n break\n else:\n raise RuntimeError('Could not get the external IP')\n return ip\n\n async def resolve(self, host, port=80, family=None, qtype='A', logging=True):\n \"\"\"Return resolving IP address(es) from host name.\"\"\"\n if self.host_is_ip(host):\n return host\n\n _host = self._cached_hosts.get(host)\n if _host:\n return _host\n\n resp = await self._resolve(host, qtype)\n\n if resp:\n hosts = [\n {\n 'hostname': host,\n 'host': r.host,\n 'port': port,\n 'family': family,\n 'proto': socket.IPPROTO_IP,\n 'flags': socket.AI_NUMERICHOST,\n }\n for r in resp\n ]\n if family:\n self._cached_hosts[host] = hosts\n else:\n self._cached_hosts[host] = hosts[0]['host']\n if logging:\n log.debug('%s: Host resolved: %s' % (host, self._cached_hosts[host]))\n else:\n if logging:\n log.warning('%s: Could not resolve host' % host)\n return self._cached_hosts.get(host)\n\n async def _resolve(self, host, qtype):\n try:\n resp = await asyncio.wait_for(\n self._resolver.query(host, qtype), timeout=self._timeout\n )\n except (aiodns.error.DNSError, asyncio.TimeoutError):\n raise ResolveError\n else:\n return resp" }, { "identifier": "log", "path": "proxyhub/utils.py", "snippet": "BASE_DIR = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))\nDATA_DIR = os.path.join(BASE_DIR, 'data')\ndef get_headers(rv=False):\ndef get_all_ip(page):\ndef get_status_code(resp, start=9, stop=12):\ndef parse_status_line(line):\ndef parse_headers(headers):\ndef update_geoip_db():" } ]
import asyncio import ssl as _ssl import time import warnings from collections import Counter from .errors import ( ProxyConnError, ProxyEmptyRecvError, ProxyRecvError, ProxySendError, ProxyTimeoutError, ResolveError, ) from .negotiators import NGTRS from .resolver import Resolver from .utils import log, parse_headers
1,971
_HTTP_PROTOS = {'HTTP', 'CONNECT:80', 'SOCKS4', 'SOCKS5'} _HTTPS_PROTOS = {'HTTPS', 'SOCKS4', 'SOCKS5'} class Proxy: """Proxy. :param str host: IP address of the proxy :param int port: Port of the proxy :param tuple types: (optional) List of types (protocols) which may be supported by the proxy and which can be checked to work with the proxy :param int timeout: (optional) Timeout of a connection and receive a response in seconds :param bool verify_ssl: (optional) Flag indicating whether to check the SSL certificates. Set to True to check ssl certifications :raises ValueError: If the host not is IP address, or if the port > 65535 """ @classmethod async def create(cls, host, *args, **kwargs): """Asynchronously create a :class:`Proxy` object. :param str host: A passed host can be a domain or IP address. If the host is a domain, try to resolve it :param str *args: (optional) Positional arguments that :class:`Proxy` takes :param str **kwargs: (optional) Keyword arguments that :class:`Proxy` takes :return: :class:`Proxy` object :rtype: proxyhub.Proxy :raises ResolveError: If could not resolve the host :raises ValueError: If the port > 65535 """ # noqa: W605 loop = kwargs.pop('loop', None)
_HTTP_PROTOS = {'HTTP', 'CONNECT:80', 'SOCKS4', 'SOCKS5'} _HTTPS_PROTOS = {'HTTPS', 'SOCKS4', 'SOCKS5'} class Proxy: """Proxy. :param str host: IP address of the proxy :param int port: Port of the proxy :param tuple types: (optional) List of types (protocols) which may be supported by the proxy and which can be checked to work with the proxy :param int timeout: (optional) Timeout of a connection and receive a response in seconds :param bool verify_ssl: (optional) Flag indicating whether to check the SSL certificates. Set to True to check ssl certifications :raises ValueError: If the host not is IP address, or if the port > 65535 """ @classmethod async def create(cls, host, *args, **kwargs): """Asynchronously create a :class:`Proxy` object. :param str host: A passed host can be a domain or IP address. If the host is a domain, try to resolve it :param str *args: (optional) Positional arguments that :class:`Proxy` takes :param str **kwargs: (optional) Keyword arguments that :class:`Proxy` takes :return: :class:`Proxy` object :rtype: proxyhub.Proxy :raises ResolveError: If could not resolve the host :raises ValueError: If the port > 65535 """ # noqa: W605 loop = kwargs.pop('loop', None)
resolver = kwargs.pop('resolver', Resolver(loop=loop))
7
2023-11-05 13:28:57+00:00
4k
WithSecureLabs/IceKube
icekube/cli.py
[ { "identifier": "config", "path": "icekube/config.py", "snippet": "class Neo4j(TypedDict):\nclass Config(TypedDict):" }, { "identifier": "create_indices", "path": "icekube/icekube.py", "snippet": "def create_indices():\n for resource in api_resources():\n if \"list\" not in resource.verbs:\n continue\n\n kind = resource.kind\n namespace = resource.namespaced\n\n cmd = f\"CREATE INDEX {kind.lower()} IF NOT EXISTS \"\n cmd += f\"FOR (n:{kind}) ON (n.name\"\n if namespace:\n cmd += \", n.namespace\"\n cmd += \")\"\n\n with get_driver().session() as session:\n session.run(cmd)" }, { "identifier": "enumerate_resource_kind", "path": "icekube/icekube.py", "snippet": "def enumerate_resource_kind(\n ignore: Optional[List[str]] = None,\n):\n if ignore is None:\n ignore = []\n\n with get_driver().session() as session:\n cluster = Cluster(apiVersion=\"N/A\", name=context_name(), version=kube_version())\n cmd, kwargs = create(cluster)\n session.run(cmd, **kwargs)\n\n signers = [\n \"kubernetes.io/kube-apiserver-client\",\n \"kubernetes.io/kube-apiserver-client-kubelet\",\n \"kubernetes.io/kubelet-serving\",\n \"kubernetes.io/legacy-unknown\",\n ]\n for signer in signers:\n s = Signer(name=signer)\n cmd, kwargs = create(s)\n session.run(cmd, **kwargs)\n\n for resource in all_resources(ignore=ignore):\n cmd, kwargs = create(resource)\n session.run(cmd, **kwargs)" }, { "identifier": "generate_relationships", "path": "icekube/icekube.py", "snippet": "def generate_relationships(threaded: bool = False) -> None:\n logger.info(\"Generating relationships\")\n logger.info(\"Fetching resources from neo4j\")\n driver = get_driver()\n resources = find()\n logger.info(\"Fetched resources from neo4j\")\n generator = partial(relationship_generator, driver, True)\n\n if threaded:\n with ThreadPoolExecutor() as exc:\n exc.map(generator, resources)\n else:\n print(\"First pass for relationships\")\n for resource in tqdm(resources):\n generator(resource)\n print(\"\")\n\n # Do a second loop across relationships to handle objects created as part\n # of other relationships\n\n resources = find()\n generator = partial(relationship_generator, driver, False)\n\n if threaded:\n with ThreadPoolExecutor() as exc:\n exc.map(generator, resources)\n else:\n print(\"Second pass for relationships\")\n for resource in tqdm(resources):\n generator(resource)\n print(\"\")" }, { "identifier": "purge_neo4j", "path": "icekube/icekube.py", "snippet": "def purge_neo4j() -> None:\n with get_driver().session() as session:\n session.run(\"MATCH (x)-[r]-(y) DELETE x, r, y\")\n session.run(\"MATCH (x) DELETE x\")" }, { "identifier": "remove_attack_paths", "path": "icekube/icekube.py", "snippet": "def remove_attack_paths() -> None:\n with get_driver().session() as session:\n session.run(\"MATCH ()-[r]-() WHERE EXISTS (r.attack_path) DELETE r\")" }, { "identifier": "setup_attack_paths", "path": "icekube/icekube.py", "snippet": "def setup_attack_paths() -> None:\n print(\"Generating attack paths\")\n for relationship, query in tqdm(attack_paths.items()):\n with get_driver().session() as session:\n if isinstance(query, str):\n query = [query]\n for q in query:\n cmd = q + f\" MERGE (src)-[:{relationship} {{ attack_path: 1 }}]->(dest)\"\n\n session.run(cmd)\n print(\"\")" }, { "identifier": "APIResource", "path": "icekube/kube.py", "snippet": "def load_kube_config():\ndef kube_version() -> str:\ndef context_name() -> str:\ndef api_versions() -> List[str]:\ndef api_resources() -> List[APIResource]:\ndef all_resources(\n preferred_versions_only: bool = True,\n ignore: Optional[List[str]] = None,\n) -> Iterator[Resource]:\ndef metadata_download() -> Dict[str, Any]:" }, { "identifier": "build_logger", "path": "icekube/log_config.py", "snippet": "def build_logger(debug_level=logging.DEBUG):\n # create logger\n logger = logging.getLogger(\"icekube\")\n logger.setLevel(debug_level)\n # create console handler with a higher log level\n ch = logging.StreamHandler()\n ch.setLevel(debug_level)\n # create formatter and add it to the handlers\n formatter = logging.Formatter(\"%(asctime)s|%(name)s|%(levelname)s|%(message)s\")\n ch.setFormatter(formatter)\n\n # tell tqdm about the handler\n tqdm_handler = _TqdmLoggingHandler(std_tqdm)\n tqdm_handler.setFormatter(formatter)\n tqdm_handler.stream = ch.stream\n\n # add the handlers to the logger\n logger.addHandler(tqdm_handler)" } ]
import json import logging import typer from pathlib import Path from typing import Iterator, List, Optional, cast from icekube.config import config from icekube.icekube import ( create_indices, enumerate_resource_kind, generate_relationships, purge_neo4j, remove_attack_paths, setup_attack_paths, ) from icekube.kube import ( APIResource, Resource, all_resources, metadata_download, ) from icekube.log_config import build_logger from tqdm import tqdm from icekube import kube from icekube import icekube
2,080
ignore: str = typer.Option( IGNORE_DEFAULT, help="Names of resource types to ignore", ), ): create_indices() enumerate_resource_kind(ignore.split(",")) generate_relationships() @app.command() def relationships(): generate_relationships() @app.command() def attack_path(): remove_attack_paths() setup_attack_paths() @app.command() def purge(): purge_neo4j() @app.command() def download(output_dir: str): path = Path(output_dir) path.mkdir(exist_ok=True) resources = all_resources() metadata = metadata_download() with open(path / "_metadata.json", "w") as fs: fs.write(json.dumps(metadata, indent=2, default=str)) current_type = None current_group = [] for resource in resources: if current_type is None: current_type = resource.resource_definition_name elif current_type != resource.resource_definition_name: with open(path / f"{current_type}.json", "w") as fs: fs.write(json.dumps(current_group, indent=4, default=str)) current_group = [] current_type = resource.resource_definition_name if resource.raw: current_group.append(json.loads(resource.raw)) if current_type: with open(path / f"{current_type}.json", "w") as fs: fs.write(json.dumps(current_group, indent=4, default=str)) @app.command() def load(input_dir: str, attack_paths: bool = True): path = Path(input_dir) metadata = json.load(open(path / "_metadata.json")) kube.kube_version = lambda: cast(str, metadata["kube_version"]) kube.context_name = lambda: cast(str, metadata["context_name"]) kube.api_versions = lambda: cast(List[str], metadata["api_versions"]) kube.preferred_versions = metadata["preferred_versions"] kube.api_resources = lambda: cast( List[APIResource], [APIResource(**x) for x in metadata["api_resources"]], ) icekube.api_resources = kube.api_resources icekube.context_name = kube.context_name icekube.kube_version = kube.kube_version def all_resources( preferred_versions_only: bool = True, ignore: Optional[List[str]] = None, ) -> Iterator[Resource]: print("Loading files from disk") for file in tqdm(path.glob("*")): if file.name == "_metadata.json": continue try: # If downloaded via kubectl get -A data = json.load(open(file))["items"] except TypeError: # If downloaded via icekube download data = json.load(open(file)) for resource in data: yield Resource( apiVersion=resource["apiVersion"], kind=resource["kind"], name=resource["metadata"]["name"], namespace=resource["metadata"].get("namespace"), plural=file.name.split(".")[0], raw=json.dumps(resource, default=str), ) print("") kube.all_resources = all_resources icekube.all_resources = all_resources if attack_paths: run(IGNORE_DEFAULT) else: enumerate(IGNORE_DEFAULT) @app.callback() def callback( neo4j_url: str = typer.Option("bolt://localhost:7687", show_default=True), neo4j_user: str = typer.Option("neo4j", show_default=True), neo4j_password: str = typer.Option("neo4j", show_default=True), neo4j_encrypted: bool = typer.Option(False, show_default=True), verbose: int = typer.Option(0, "--verbose", "-v", count=True), ):
app = typer.Typer() IGNORE_DEFAULT = "events,componentstatuses" @app.command() def run( ignore: str = typer.Option( IGNORE_DEFAULT, help="Names of resource types to ignore", ), ): enumerate(ignore) attack_path() @app.command() def enumerate( ignore: str = typer.Option( IGNORE_DEFAULT, help="Names of resource types to ignore", ), ): create_indices() enumerate_resource_kind(ignore.split(",")) generate_relationships() @app.command() def relationships(): generate_relationships() @app.command() def attack_path(): remove_attack_paths() setup_attack_paths() @app.command() def purge(): purge_neo4j() @app.command() def download(output_dir: str): path = Path(output_dir) path.mkdir(exist_ok=True) resources = all_resources() metadata = metadata_download() with open(path / "_metadata.json", "w") as fs: fs.write(json.dumps(metadata, indent=2, default=str)) current_type = None current_group = [] for resource in resources: if current_type is None: current_type = resource.resource_definition_name elif current_type != resource.resource_definition_name: with open(path / f"{current_type}.json", "w") as fs: fs.write(json.dumps(current_group, indent=4, default=str)) current_group = [] current_type = resource.resource_definition_name if resource.raw: current_group.append(json.loads(resource.raw)) if current_type: with open(path / f"{current_type}.json", "w") as fs: fs.write(json.dumps(current_group, indent=4, default=str)) @app.command() def load(input_dir: str, attack_paths: bool = True): path = Path(input_dir) metadata = json.load(open(path / "_metadata.json")) kube.kube_version = lambda: cast(str, metadata["kube_version"]) kube.context_name = lambda: cast(str, metadata["context_name"]) kube.api_versions = lambda: cast(List[str], metadata["api_versions"]) kube.preferred_versions = metadata["preferred_versions"] kube.api_resources = lambda: cast( List[APIResource], [APIResource(**x) for x in metadata["api_resources"]], ) icekube.api_resources = kube.api_resources icekube.context_name = kube.context_name icekube.kube_version = kube.kube_version def all_resources( preferred_versions_only: bool = True, ignore: Optional[List[str]] = None, ) -> Iterator[Resource]: print("Loading files from disk") for file in tqdm(path.glob("*")): if file.name == "_metadata.json": continue try: # If downloaded via kubectl get -A data = json.load(open(file))["items"] except TypeError: # If downloaded via icekube download data = json.load(open(file)) for resource in data: yield Resource( apiVersion=resource["apiVersion"], kind=resource["kind"], name=resource["metadata"]["name"], namespace=resource["metadata"].get("namespace"), plural=file.name.split(".")[0], raw=json.dumps(resource, default=str), ) print("") kube.all_resources = all_resources icekube.all_resources = all_resources if attack_paths: run(IGNORE_DEFAULT) else: enumerate(IGNORE_DEFAULT) @app.callback() def callback( neo4j_url: str = typer.Option("bolt://localhost:7687", show_default=True), neo4j_user: str = typer.Option("neo4j", show_default=True), neo4j_password: str = typer.Option("neo4j", show_default=True), neo4j_encrypted: bool = typer.Option(False, show_default=True), verbose: int = typer.Option(0, "--verbose", "-v", count=True), ):
config["neo4j"]["url"] = neo4j_url
0
2023-11-02 13:54:21+00:00
4k
IAAR-Shanghai/UHGEval
tests/llm/test_remote.py
[ { "identifier": "Aquila_34B_Chat", "path": "uhgeval/llm/remote.py", "snippet": "class Aquila_34B_Chat(BaseLLM):\n def request(self, query) -> str:\n url = conf.Aquila_url\n payload = json.dumps({\n \"prompt\": query,\n \"params\": {\n \"temperature\": float(self.params['temperature']),\n \"do_sample\": True,\n \"max_new_tokens\": self.params['max_new_tokens'],\n \"num_return_sequences\": 1,\n \"top_p\": self.params['top_p'],\n \"top_k\": self.params['top_k'],\n }\n })\n headers = {\n 'token': conf.Aquila_token,\n 'Content-Type': 'application/json'\n }\n res = requests.request(\"POST\", url, headers=headers, data=payload)\n res = res.json()['choices']\n return res\n\n def continue_writing(self, obj: dict) -> str:\n return super()._continue_writing_without_instruction(self, obj)" }, { "identifier": "Baichuan2_13B_Chat", "path": "uhgeval/llm/remote.py", "snippet": "class Baichuan2_13B_Chat(BaseLLM):\n def request(self, query) -> str:\n url = conf.Baichuan2_13B_url\n payload = json.dumps({\n \"prompt\": query,\n \"params\": {\n \"temperature\": self.params['temperature'],\n \"do_sample\": True,\n \"max_new_tokens\": self.params['max_new_tokens'],\n \"num_return_sequences\": 1,\n \"top_p\": self.params['top_p'],\n \"top_k\": self.params['top_k'],\n }\n })\n headers = {\n 'token': conf.Baichuan2_13B_token,\n 'Content-Type': 'application/json'\n }\n res = requests.request(\"POST\", url, headers=headers, data=payload)\n res = res.json()['choices'][0]\n return res" }, { "identifier": "ChatGLM2_6B_Chat", "path": "uhgeval/llm/remote.py", "snippet": "class ChatGLM2_6B_Chat(BaseLLM):\n def request(self, query) -> str:\n url = conf.ChatGLM2_url\n payload = json.dumps({\n \"prompt\": query,\n \"params\": {\n \"temperature\": self.params['temperature'],\n \"do_sample\": True,\n \"max_new_tokens\": self.params['max_new_tokens'],\n \"num_return_sequences\": 1,\n \"top_p\": self.params['top_p'],\n \"top_k\": self.params['top_k'],\n }\n })\n headers = {\n 'token': conf.ChatGLM2_token,\n 'Content-Type': 'application/json'\n }\n res = requests.request(\"POST\", url, headers=headers, data=payload)\n res = res.json()['choices'][0]\n return res" }, { "identifier": "GPT_transit", "path": "uhgeval/llm/remote.py", "snippet": "class GPT_transit(BaseLLM):\n def __init__(self, model_name='gpt-3.5-turbo', temperature=1.0, max_new_tokens=1024, report=False):\n super().__init__(model_name, temperature, max_new_tokens)\n self.report = report\n\n def request(self, query: str) -> str:\n url = conf.GPT_transit_url\n payload = json.dumps({\n \"model\": self.params['model_name'],\n \"messages\": [{\"role\": \"user\", \"content\": query}],\n \"temperature\": self.params['temperature'],\n 'max_tokens': self.params['max_new_tokens'],\n \"top_p\": self.params['top_p'],\n })\n headers = {\n 'token': conf.GPT_transit_token,\n 'Content-Type': 'application/json'\n }\n res = requests.request(\"POST\", url, headers=headers, data=payload)\n res = res.json()\n real_res = res[\"choices\"][0][\"message\"][\"content\"]\n\n token_consumed = res['usage']['total_tokens']\n logger.info(f'GPT token consumed: {token_consumed}') if self.report else ()\n return real_res" }, { "identifier": "InternLM_20B_Chat", "path": "uhgeval/llm/remote.py", "snippet": "class InternLM_20B_Chat(BaseLLM):\n def request(self, query) -> str:\n url = conf.InternLM_url\n payload = json.dumps({\n \"prompt\": query,\n \"params\": {\n \"temperature\": self.params['temperature'],\n \"do_sample\": True,\n \"max_new_tokens\": self.params['max_new_tokens'],\n \"num_return_sequences\": 1,\n \"top_p\": self.params['top_p'],\n \"top_k\": self.params['top_k'],\n }\n })\n headers = {\n 'token': conf.InternLM_token,\n 'Content-Type': 'application/json'\n }\n res = requests.request(\"POST\", url, headers=headers, data=payload)\n res = res.json()['choices'][0]\n return res" }, { "identifier": "Qwen_14B_Chat", "path": "uhgeval/llm/remote.py", "snippet": "class Qwen_14B_Chat(BaseLLM):\n def request(self, query) -> str:\n url = conf.Qwen_url\n payload = json.dumps({\n \"prompt\": query,\n \"params\": {\n \"temperature\": self.params['temperature'],\n \"do_sample\": True,\n \"max_new_tokens\": self.params['max_new_tokens'],\n \"num_return_sequences\": 1,\n \"top_p\": self.params['top_p'],\n \"top_k\": self.params['top_k'],\n }\n })\n headers = {\n 'token': conf.Qwen_token,\n 'Content-Type': 'application/json'\n }\n res = requests.request(\"POST\", url, headers=headers, data=payload)\n res = res.json()['choices'][0]\n return res\n\n def continue_writing(self, obj: dict) -> str:\n return super()._continue_writing_without_instruction(self, obj)" }, { "identifier": "Xinyu_7B_Chat", "path": "uhgeval/llm/remote.py", "snippet": "class Xinyu_7B_Chat(BaseLLM):\n def request(self, query) -> str:\n url = conf.Xinyu_7B_url\n payload = json.dumps({\n \"prompt\": query,\n \"params\": {\n \"temperature\": self.params['temperature'],\n \"do_sample\": True,\n \"max_new_tokens\": self.params['max_new_tokens'],\n \"num_return_sequences\": 1,\n \"top_p\": self.params['top_p'],\n \"top_k\": self.params['top_k'],\n }\n })\n headers = {\n 'token': conf.Xinyu_7B_token,\n 'Content-Type': 'application/json'\n }\n res = requests.request(\"POST\", url, headers=headers, data=payload)\n res = res.json()['choices'][0]\n return res\n\n def continue_writing(self, obj:dict) -> str:\n template = \"Human: 【生成任务:文本续写】我要你担任新闻编辑。我将为您提供与新闻相关的故事或主题,您将续写一篇评论文章,对已有文本进行符合逻辑的续写。您应该利用自己的经验,深思熟虑地解释为什么某事很重要,用事实支持主张,并补充已有故事中可能缺少的逻辑段落。\\n请对以下文本进行续写。\\n {} Assistant:\"\n query = template.format(f'《{obj[\"headLine\"]}》\\n{obj[\"broadcastDate\"]}\\n{obj[\"newsBeginning\"]}')\n res = self.safe_request(query)\n real_res = res.split('Assistant:')[-1].split('</s>')[0].strip()\n sentences = re.split(r'(?<=[。;?!])', real_res)\n return sentences[0]" }, { "identifier": "Xinyu_70B_Chat", "path": "uhgeval/llm/remote.py", "snippet": "class Xinyu_70B_Chat(BaseLLM):\n def request(self, query) -> str:\n url = conf.Xinyu_70B_url\n payload = json.dumps({\n \"prompt\": query,\n \"temperature\": self.params['temperature'],\n \"max_tokens\": self.params['max_new_tokens'],\n \"top_p\": self.params['top_p'],\n \"top_k\": self.params['top_k'],\n })\n headers = {\n 'token': conf.Xinyu_70B_token,\n 'Content-Type': 'application/json'\n }\n res = requests.request(\"POST\", url, headers=headers, data=payload)\n res = res.json()['text'][0]\n return res\n\n def continue_writing(self, obj:dict) -> str:\n template = \"Human: 【生成任务:文本续写】我要你担任新闻编辑。我将为您提供与新闻相关的故事或主题,您将续写一篇评论文章,对已有文本进行符合逻辑的续写。您应该利用自己的经验,深思熟虑地解释为什么某事很重要,用事实支持主张,并补充已有故事中可能缺少的逻辑段落。\\n请对以下文本进行续写。\\n {} Assistant:\"\n query = template.format(f'《{obj[\"headLine\"]}》\\n{obj[\"broadcastDate\"]}\\n{obj[\"newsBeginning\"]}')\n res = self.safe_request(query)\n real_res = res.split('Assistant:')[-1].split('</s>')[0].strip()\n sentences = re.split(r'(?<=[。;?!])', real_res)\n return sentences[0]" } ]
import unittest from uhgeval.llm.remote import ( Aquila_34B_Chat, Baichuan2_13B_Chat, ChatGLM2_6B_Chat, GPT_transit, InternLM_20B_Chat, Qwen_14B_Chat, Xinyu_7B_Chat, Xinyu_70B_Chat, )
3,306
Note: These tests perform real requests to external APIs. Be cautious of network availability, API rate limits, and potential costs associated with making real requests during testing. """ class BaseChatTest(unittest.TestCase): def _test_request(self): query = "How are you?" response = self.model.request(query) self.assertIsInstance(response, str) self.assertGreater(len(response), 0) def _test_continue_writing(self): obj = {"headLine": "Story", "broadcastDate": "2023-11-15", "newsBeginning": "Once upon a time, there is a"} result = self.model.continue_writing(obj) self.assertIsInstance(result, str) self.assertGreater(len(result), 0) class TestAquila34BChat(BaseChatTest): def setUp(self): self.model = Aquila_34B_Chat(temperature=0.1) def test_request(self): self._test_request() def test_continue_writing(self): self._test_continue_writing() class TestBaichuan213BChat(BaseChatTest): def setUp(self): self.model = Baichuan2_13B_Chat(temperature=0.1) def test_request(self): self._test_request() def test_continue_writing(self): self._test_continue_writing() class TestChatGLM26BChat(BaseChatTest): def setUp(self): self.model = ChatGLM2_6B_Chat(temperature=0.1) def test_request(self): self._test_request() def test_continue_writing(self): self._test_continue_writing() class TestGPTTransit(BaseChatTest): def setUp(self): self.gpt35 = GPT_transit(model_name='gpt-3.5-turbo', temperature=0.1) self.gpt4_0613 = GPT_transit(model_name='gpt-4-0613', temperature=0.1) self.gpt4_1106 = GPT_transit(model_name='gpt-4-1106-preview', temperature=0.1) def _test_request(self, model): query = "How are you?" response = model.request(query) self.assertIsInstance(response, str) self.assertGreater(len(response), 0) def test_request(self): for model in [self.gpt35, self.gpt4_0613, self.gpt4_1106]: with self.subTest(model=model): self._test_request(model) def _test_continue_writing(self, model): obj = {"headLine": "Story", "broadcastDate": "2023-11-15", "newsBeginning": "Once upon a time, there is a"} result = model.continue_writing(obj) self.assertIsInstance(result, str) self.assertGreater(len(result), 0) def test_continue_writing(self): for model in [self.gpt35, self.gpt4_0613, self.gpt4_1106]: with self.subTest(model=model): self._test_continue_writing(model) class TestInternLM20BChat(BaseChatTest): def setUp(self): self.model = InternLM_20B_Chat(temperature=0.1) def test_request(self): self._test_request() def test_continue_writing(self): self._test_continue_writing() class TestQwen14BChat(BaseChatTest): def setUp(self): self.model = Qwen_14B_Chat(temperature=0.1) def test_request(self): self._test_request() def test_continue_writing(self): self._test_continue_writing() class TestXinyu7BChat(BaseChatTest): def setUp(self): self.model = Xinyu_7B_Chat(temperature=0.1) def test_request(self): self._test_request() def test_continue_writing(self): self._test_continue_writing() class TestXinyu70BChat(BaseChatTest): def setUp(self):
# @Author : Shichao Song # @Email : [email protected] """Unit tests for the uhgeval.llm.remote module. This module contains unittests for the llm deployed remotely. Note: These tests perform real requests to external APIs. Be cautious of network availability, API rate limits, and potential costs associated with making real requests during testing. """ class BaseChatTest(unittest.TestCase): def _test_request(self): query = "How are you?" response = self.model.request(query) self.assertIsInstance(response, str) self.assertGreater(len(response), 0) def _test_continue_writing(self): obj = {"headLine": "Story", "broadcastDate": "2023-11-15", "newsBeginning": "Once upon a time, there is a"} result = self.model.continue_writing(obj) self.assertIsInstance(result, str) self.assertGreater(len(result), 0) class TestAquila34BChat(BaseChatTest): def setUp(self): self.model = Aquila_34B_Chat(temperature=0.1) def test_request(self): self._test_request() def test_continue_writing(self): self._test_continue_writing() class TestBaichuan213BChat(BaseChatTest): def setUp(self): self.model = Baichuan2_13B_Chat(temperature=0.1) def test_request(self): self._test_request() def test_continue_writing(self): self._test_continue_writing() class TestChatGLM26BChat(BaseChatTest): def setUp(self): self.model = ChatGLM2_6B_Chat(temperature=0.1) def test_request(self): self._test_request() def test_continue_writing(self): self._test_continue_writing() class TestGPTTransit(BaseChatTest): def setUp(self): self.gpt35 = GPT_transit(model_name='gpt-3.5-turbo', temperature=0.1) self.gpt4_0613 = GPT_transit(model_name='gpt-4-0613', temperature=0.1) self.gpt4_1106 = GPT_transit(model_name='gpt-4-1106-preview', temperature=0.1) def _test_request(self, model): query = "How are you?" response = model.request(query) self.assertIsInstance(response, str) self.assertGreater(len(response), 0) def test_request(self): for model in [self.gpt35, self.gpt4_0613, self.gpt4_1106]: with self.subTest(model=model): self._test_request(model) def _test_continue_writing(self, model): obj = {"headLine": "Story", "broadcastDate": "2023-11-15", "newsBeginning": "Once upon a time, there is a"} result = model.continue_writing(obj) self.assertIsInstance(result, str) self.assertGreater(len(result), 0) def test_continue_writing(self): for model in [self.gpt35, self.gpt4_0613, self.gpt4_1106]: with self.subTest(model=model): self._test_continue_writing(model) class TestInternLM20BChat(BaseChatTest): def setUp(self): self.model = InternLM_20B_Chat(temperature=0.1) def test_request(self): self._test_request() def test_continue_writing(self): self._test_continue_writing() class TestQwen14BChat(BaseChatTest): def setUp(self): self.model = Qwen_14B_Chat(temperature=0.1) def test_request(self): self._test_request() def test_continue_writing(self): self._test_continue_writing() class TestXinyu7BChat(BaseChatTest): def setUp(self): self.model = Xinyu_7B_Chat(temperature=0.1) def test_request(self): self._test_request() def test_continue_writing(self): self._test_continue_writing() class TestXinyu70BChat(BaseChatTest): def setUp(self):
self.model = Xinyu_70B_Chat(temperature=0.1)
7
2023-11-06 11:46:22+00:00
4k
mobiusml/hqq
hqq/engine/timm.py
[ { "identifier": "BaseHQQModel", "path": "hqq/models/base.py", "snippet": "class BaseHQQModel:\n\t#Override these\n\t############################################\n\t#This method creates and empty model based on the specfied architecture\n\t@abstractmethod\n\tdef create_model(self):\n\t\tpass\n\n\t#This method saves the model architecture only without inculding the weights (for example to a config.json)\n\t@abstractmethod\t\n\tdef cache_model(cls, model, save_dir):\n\t\tpass\n\t############################################\n\n\t@classmethod\n\tdef get_config_file(cls, save_dir):\n\t\treturn fix_path(save_dir) + 'config.json'\n\n\t@classmethod\n\tdef get_weight_file(cls, save_dir):\n\t\treturn fix_path(save_dir) + 'qmodel.pt' \n\n\t@classmethod\n\tdef get_ignore_layers(cls, model):\n\t\treturn []\n\n\t#Save weights to disk\n\t@classmethod\n\tdef save_weights(cls, weights, save_dir):\n\t\ttorch.save(weights, cls.get_weight_file(save_dir))\n\n\t#Load weights from disk\n\t@classmethod\n\tdef load_weights(cls, save_dir, map_location=None):\n\t\treturn torch.load(cls.get_weight_file(save_dir), map_location=map_location)\n\n\t#Main function to quantize a model. Basically goes through the linear layers specfied in the patching function and replaces them with HQQLinear\n\t@classmethod\n\tdef quantize_model(cls, model, quant_config):\n\t\t#Use the same quantization config for all linear layers. Use None to skip quantizing a specfic layer.\n\t\tif(True in [(key in cls.get_linear_tags()) for key in quant_config.keys()]): \n\t\t\t#If the user doesn't specify a key from get_linear_tags, the layer is not quantized via (key, None)\n\t\t\tpatch_params = dict([(key, None) for key in cls.get_linear_tags()])\n\t\t\tpatch_params.update(quant_config)\n\t\telse:\n\t\t\t#Same quant_config for all layers\n\t\t\tpatch_params = dict([(k, quant_config) for k in cls.get_linear_tags()])\n\n\t\t#We replace the nn.Linear layers with HQQLinear\n\t\tdef _patch_linear(linear_layer, quant_config):\n\t\t\treturn HQQLinear(linear_layer, quant_config) if (quant_config is not None) else linear_layer.half().cuda()\n\n\t\tcls.patch_model(model, lambda l: l.half().cuda(), _patch_linear, patch_params)\n\n\t#Prepares model weights by iterating through modules. It might some parameters that are NOT modules like model.param1\n\t@classmethod\n\tdef serialize_weights(cls, model, verbose):\n\t\tweights = {}\n\t\tignore_keys = cls.get_ignore_layers(model)\n\t\tfor name, module in model.named_modules():\n\t\t\tif(name in ignore_keys): continue\n\t\t\ttry:\n\t\t\t\tstate_dict = module.state_dict()\n\t\t\t\tif(len(state_dict)>0): \n\t\t\t\t\tweights[name] = dict(state_dict)\n\t\t\texcept Exception as error:\n\t\t\t\tif(verbose): \n\t\t\t\t\tprint('Skipping', name)\n\n\t\treturn weights\n\n\t#Main function to save a quantized model\n\t@classmethod\n\tdef save_quantized(cls, model, save_dir, verbose=False):\n\t\t#Save config\n\t\tcls.cache_model(model, save_dir)\n\n\t\t#Serialization\n\t\tweights = cls.serialize_weights(model, verbose=verbose)\n\n\t\t#Save\n\t\tcls.save_weights(weights, save_dir)\n\n\t@classmethod\n\tdef try_snapshot_download(cls, save_dir_or_hub, cache_dir=''):\n\t\tsave_dir = fix_path(cache_dir) + save_dir_or_hub\n\n\t\tif(os.path.exists(save_dir)==False):\n\t\t\tsave_dir = snapshot_download(repo_id=save_dir_or_hub, cache_dir=cache_dir)\n\t\t\tsave_dir = fix_path(save_dir)\n\n\t\t#Check \n\t\tif(os.path.exists(cls.get_weight_file(save_dir))==False):\n\t\t\traise Exception('Weight file missing. Check your cache directory.')\n\t\tif(os.path.exists(cls.get_config_file(save_dir))==False):\n\t\t\traise Exception('Config file missing. Check your cache directory.')\n\n\t\treturn save_dir\n\n\n\t#This method is specfically designed in case we need to load some weights that are not part of any module\n\t@classmethod\n\tdef post_module_load(cls, model, weights):\n\t\tpass\n\n\t#Main function to load an HQQ quantized model from either HF hub or locally\n\t@classmethod\n\tdef from_quantized(cls, save_dir_or_hub, cache_dir=''):\n\t\t#Get directory path\n\t\tsave_dir = cls.try_snapshot_download(save_dir_or_hub, cache_dir)\n\n\t\t#Load model from config\n\t\tmodel = cls.create_model(save_dir)\n\n\t\t#Name the layers\n\t\tcls.autoname_modules(model) \n\n\t\t#Load weights\n\t\ttry:\n\t\t\tweights = cls.load_weights(save_dir)\n\t\texcept Exception as error:\n\t\t\tprint(\"Failed to load the weights\", error)\n\t\t\treturn\n\t\t\n\t\t#load_state_dict() doesn't work with modules initialized with init_empty_weights(), so we need to do this manually\n\t\[email protected]_grad()\n\t\tdef _load_module(module, params=None):\n\t\t\tif(module.name not in weights): \n\t\t\t\treturn module.half().cuda()\n\n\t\t\tstate_dict = weights[module.name]\n\t\t\tif(('W_q' in state_dict) and ('meta' in state_dict)):\n\t\t\t\tmodule = HQQLinear(linear_layer=None, quant_config=None)\n\t\t\t\tmodule.load_state_dict(state_dict)\n\t\t\telse:\n\t\t\t\tfor key in state_dict:\n\t\t\t\t\tsetattr(module, key, torch.nn.Parameter(state_dict[key], requires_grad=False))\n\n\t\t\treturn module \n\n\t\t#Load modules\n\t\tcls.patch_model(model, _load_module, _load_module, dict([(k, None) for k in cls.get_linear_tags()]))\n\t\t#Load other weights that are not part of any module\n\t\tcls.post_module_load(model, weights) \n\t\t\n\t\treturn model" }, { "identifier": "ViTCLIPHQQ", "path": "hqq/models/timm/vit_clip.py", "snippet": "class ViTCLIPHQQ(VitCLIPPatch, BaseHQQTimmModel):\n\t#layers to ignore when saving the weights\n\t@classmethod\n\tdef get_ignore_layers(cls, model):\n\t\treturn ['', 'model', 'model.blocks'] + ['model.blocks.' + str(i) for i in range(len(model.blocks))]\n\n\t#since cls_token and pos_embed are trainable parameters but are not part of any module, we need to add them manually \n\t#for saving\n\t@classmethod\n\tdef serialize_weights(cls, model, verbose):\n\t\tweights = super().serialize_weights(model, verbose) \n\t\tweights['cls_token'] = model.cls_token.data\n\t\tweights['pos_embed'] = model.pos_embed.data\n\t\treturn weights\n\n\t#and loading\n\t@classmethod\n\tdef post_module_load(cls, model, weights):\n\t\tsuper().post_module_load(model, weights) \n\t\tmodel.cls_token.data = weights['cls_token']\n\t\tmodel.pos_embed.data = weights['pos_embed']" }, { "identifier": "HQQWrapper", "path": "hqq/engine/base.py", "snippet": "class HQQWrapper:\n\n\t@abstractmethod\n\tdef _get_arch_key_from_save_dir(cls, save_dir:str):\n\t\tpass\n\n\t@classmethod\n\tdef _get_hqq_class(cls, arg):\n\t\tarch = arg if (type(arg)==str) else arg.arch_key\n\t\treturn cls._HQQ_REGISTRY[arch]\n\n\t@classmethod\n\tdef _validate_params(cls, params:Dict):\n\t\tpass\n\n\t@classmethod\n\tdef _is_quantizable(cls, model):\n\t\treturn hasattr(model, 'hqq_quantized')\n\n\t@classmethod\n\tdef _make_quantizable(cls, model, quantized):\n\t\tmodel.hqq_quantized = quantized\n\t\tmodel.base_class = cls._get_hqq_class(model)\n\n\t@classmethod\n\tdef _check_arch_support(cls, arg):\n\t\tarch = arg if (type(arg)==str) else arg.arch_key\n\t\tassert (arch in cls._HQQ_REGISTRY), \"Model architecture \" + arch + \" not supported yet.\"\n\n\t@classmethod\n\tdef _check_if_already_quantized(cls, model):\n\t\tassert (not model.hqq_quantized), \"Model already quantized\"\n\n\t@classmethod\n\tdef _check_if_not_quantized(cls, model):\n\t\tassert model.hqq_quantized, \"Model not quantized.\"\n\n\t@classmethod\n\tdef _set_quantized(cls, model, quantized):\n\t\tmodel.hqq_quantized = quantized\n\n\t#####################################################\n\t@classmethod\n\tdef quantize_model_(cls, model, quant_config):\n\t\tif(cls._is_quantizable(model)==False):\n\t\t\tcls._make_quantizable(model, quantized=False) \n\t\tcls._check_arch_support(model)\n\t\tcls._check_if_already_quantized(model)\n\t\tcls._get_hqq_class(model).quantize_model(model, quant_config=quant_config)\n\t\tcls._set_quantized(model, True)\n\n\t@classmethod\n\tdef save_quantized_(cls, model, save_dir):\n\t\tcls._check_if_not_quantized(model)\n\t\tcls._get_hqq_class(model).save_quantized(model, save_dir=save_dir)\n\n\t@classmethod\n\tdef from_quantized(cls, save_dir_or_hub, cache_dir=''):\n\t\t#Both local and hub-support\n\t\tsave_dir = BaseHQQModel.try_snapshot_download(save_dir_or_hub)\n\t\tarch_key = cls._get_arch_key_from_save_dir(save_dir)\n\t\tcls._check_arch_support(arch_key)\n\n\t\tmodel = cls._get_hqq_class(arch_key).from_quantized(save_dir, cache_dir)\n\n\t\tcls._make_quantizable(model, quantized=True)\n\t\treturn model\n\n\t@classmethod\n\tdef get_linear_tags(cls, model):\n\t\treturn cls._get_hqq_class(model).get_linear_tags()" } ]
import timm, json from typing import Dict from ..models.base import BaseHQQModel from ..models.timm.vit_clip import ViTCLIPHQQ from .base import HQQWrapper
2,609
_HQQ_REGISTRY = {} _HQQ_REGISTRY['vit_huge_patch14_clip_336'] = ViTCLIPHQQ _HQQ_REGISTRY['vit_huge_patch14_clip_224'] = ViTCLIPHQQ _HQQ_REGISTRY['vit_large_patch14_clip_224'] = ViTCLIPHQQ _HQQ_REGISTRY['vit_large_patch14_clip_336'] = ViTCLIPHQQ _HQQ_REGISTRY['vit_base_patch16_clip_384'] = ViTCLIPHQQ _HQQ_REGISTRY['vit_base_patch32_clip_448'] = ViTCLIPHQQ _HQQ_REGISTRY['vit_base_patch32_clip_384'] = ViTCLIPHQQ _HQQ_REGISTRY['vit_base_patch16_clip_224'] = ViTCLIPHQQ _HQQ_REGISTRY['vit_base_patch32_clip_224'] = ViTCLIPHQQ
_HQQ_REGISTRY = {} _HQQ_REGISTRY['vit_huge_patch14_clip_336'] = ViTCLIPHQQ _HQQ_REGISTRY['vit_huge_patch14_clip_224'] = ViTCLIPHQQ _HQQ_REGISTRY['vit_large_patch14_clip_224'] = ViTCLIPHQQ _HQQ_REGISTRY['vit_large_patch14_clip_336'] = ViTCLIPHQQ _HQQ_REGISTRY['vit_base_patch16_clip_384'] = ViTCLIPHQQ _HQQ_REGISTRY['vit_base_patch32_clip_448'] = ViTCLIPHQQ _HQQ_REGISTRY['vit_base_patch32_clip_384'] = ViTCLIPHQQ _HQQ_REGISTRY['vit_base_patch16_clip_224'] = ViTCLIPHQQ _HQQ_REGISTRY['vit_base_patch32_clip_224'] = ViTCLIPHQQ
class HQQtimm(HQQWrapper):
2
2023-11-07 20:15:00+00:00
4k
TheFunny/ArisuAutoSweeper
module/config/config_updater.py
[ { "identifier": "DEPLOY_TEMPLATE", "path": "deploy/Windows/utils.py", "snippet": "DEPLOY_TEMPLATE = './deploy/Windows/template.yaml'" }, { "identifier": "poor_yaml_read", "path": "deploy/Windows/utils.py", "snippet": "def poor_yaml_read(file):\n \"\"\"\n Poor implementation to load yaml without pyyaml dependency, but with re\n\n Args:\n file (str):\n\n Returns:\n dict:\n \"\"\"\n if not os.path.exists(file):\n return {}\n\n data = {}\n regex = re.compile(r'^(.*?):(.*?)$')\n with open(file, 'r', encoding='utf-8') as f:\n for line in f.readlines():\n line = line.strip('\\n\\r\\t ').replace('\\\\', '/')\n if line.startswith('#'):\n continue\n result = re.match(regex, line)\n if result:\n k, v = result.group(1), result.group(2).strip('\\n\\r\\t\\' ')\n if v:\n if v.lower() == 'null':\n v = None\n elif v.lower() == 'false':\n v = False\n elif v.lower() == 'true':\n v = True\n elif v.isdigit():\n v = int(v)\n data[k] = v\n\n return data" }, { "identifier": "poor_yaml_write", "path": "deploy/Windows/utils.py", "snippet": "def poor_yaml_write(data, file, template_file=DEPLOY_TEMPLATE):\n \"\"\"\n Args:\n data (dict):\n file (str):\n template_file (str):\n \"\"\"\n with open(template_file, 'r', encoding='utf-8') as f:\n text = f.read().replace('\\\\', '/')\n\n for key, value in data.items():\n if value is None:\n value = 'null'\n elif value is True:\n value = \"true\"\n elif value is False:\n value = \"false\"\n text = re.sub(f'{key}:.*?\\n', f'{key}: {value}\\n', text)\n\n with open(file, 'w', encoding='utf-8', newline='') as f:\n f.write(text)" }, { "identifier": "timer", "path": "module/base/timer.py", "snippet": "def timer(function):\n @wraps(function)\n def function_timer(*args, **kwargs):\n t0 = time.time()\n\n result = function(*args, **kwargs)\n t1 = time.time()\n print('%s: %s s' % (function.__name__, str(round(t1 - t0, 10))))\n return result\n\n return function_timer" }, { "identifier": "VALID_SERVER", "path": "module/config/server.py", "snippet": "VALID_SERVER = {\n 'JP-Official': 'com.YostarJP.BlueArchive',\n 'OVERSEA-TWHKMO': 'com.nexon.bluearchive',\n 'OVERSEA-Korea': 'com.nexon.bluearchive',\n 'OVERSEA-Asia': 'com.nexon.bluearchive',\n 'OVERSEA-America': 'com.nexon.bluearchive',\n 'OVERSEA-Global': 'com.nexon.bluearchive',\n}" } ]
from copy import deepcopy from cached_property import cached_property from deploy.Windows.utils import DEPLOY_TEMPLATE, poor_yaml_read, poor_yaml_write from module.base.timer import timer from module.config.server import VALID_SERVER from module.config.utils import * from module.base.code_generator import CodeGenerator import module.config.stored.classes as classes import module.config.stored.classes as classes import os
3,181
if cls: gen.add(f'{path[-1]} = {cls}("{".".join(path)}")') gen.write('module/config/stored/stored_generated.py') @timer def generate_i18n(self, lang): """ Load old translations and generate new translation file. args.json ---+-----> i18n/<lang>.json (old) i18n/<lang>.json ---+ """ new = {} old = read_file(filepath_i18n(lang)) def deep_load(keys, default=True, words=('name', 'help')): for word in words: k = keys + [str(word)] d = ".".join(k) if default else str(word) v = deep_get(old, keys=k, default=d) deep_set(new, keys=k, value=v) # Menu for path, data in deep_iter(self.task, depth=3): if 'tasks' not in path: continue task_group, _, task = path deep_load(['Menu', task_group]) deep_load(['Task', task]) # Arguments visited_group = set() for path, data in deep_iter(self.argument, depth=2): if path[0] not in visited_group: deep_load([path[0], '_info']) visited_group.add(path[0]) deep_load(path) if 'option' in data: deep_load(path, words=data['option'], default=False) # Package names # for package, server in VALID_PACKAGE.items(): # path = ['Emulator', 'PackageName', package] # if deep_get(new, keys=path) == package: # deep_set(new, keys=path, value=server.upper()) # for package, server_and_channel in VALID_CHANNEL_PACKAGE.items(): # server, channel = server_and_channel # name = deep_get(new, keys=['Emulator', 'PackageName', to_package(server)]) # if lang == SERVER_TO_LANG[server]: # value = f'{name} {channel}渠道服 {package}' # else: # value = f'{name} {package}' # deep_set(new, keys=['Emulator', 'PackageName', package], value=value) # Game server names # for server, _list in VALID_SERVER_LIST.items(): # for index in range(len(_list)): # path = ['Emulator', 'ServerName', f'{server}-{index}'] # prefix = server.split('_')[0].upper() # prefix = '国服' if prefix == 'CN' else prefix # deep_set(new, keys=path, value=f'[{prefix}] {_list[index]}') # GUI i18n for path, _ in deep_iter(self.gui, depth=2): group, key = path deep_load(keys=['Gui', group], words=(key,)) write_file(filepath_i18n(lang), new) @cached_property def menu(self): """ Generate menu definitions task.yaml --> menu.json """ data = {} for task_group in self.task.keys(): value = deep_get(self.task, keys=[task_group, 'menu']) if value not in ['collapse', 'list']: value = 'collapse' deep_set(data, keys=[task_group, 'menu'], value=value) value = deep_get(self.task, keys=[task_group, 'page']) if value not in ['setting', 'tool']: value = 'setting' deep_set(data, keys=[task_group, 'page'], value=value) tasks = deep_get(self.task, keys=[task_group, 'tasks'], default={}) tasks = list(tasks.keys()) deep_set(data, keys=[task_group, 'tasks'], value=tasks) return data @cached_property def stored(self): data = {} for path, value in deep_iter(self.args, depth=3): if value.get('type') != 'stored': continue name = path[-1] stored = value.get('stored') stored_class = getattr(classes, stored) row = { 'name': name, 'path': '.'.join(path), 'i18n': f'{path[1]}.{path[2]}.name', 'stored': stored, 'attrs': stored_class('')._attrs, 'order': value.get('order', 0), 'color': value.get('color', '#777777') } data[name] = row # sort by `order` ascending, but `order`==0 at last data = sorted(data.items(), key=lambda kv: (kv[1]['order'] == 0, kv[1]['order'])) data = {k: v for k, v in data} return data @staticmethod def generate_deploy_template():
CONFIG_IMPORT = ''' import datetime # This file was automatically generated by module/config/config_updater.py. # Don't modify it manually. class GeneratedConfig: """ Auto generated configuration """ '''.strip().split('\n') DICT_GUI_TO_INGAME = { 'zh-CN': 'cn', 'en-US': 'en', } def get_generator(): return CodeGenerator() class ConfigGenerator: @cached_property def argument(self): """ Load argument.yaml, and standardise its structure. <group>: <argument>: type: checkbox|select|textarea|input value: option (Optional): Options, if argument has any options. validate (Optional): datetime """ data = {} raw = read_file(filepath_argument('argument')) def option_add(keys, options): options = deep_get(raw, keys=keys, default=[]) + options deep_set(raw, keys=keys, value=options) # Insert packages option_add(keys='Emulator.PackageName.option', options=list(VALID_SERVER.keys())) # Load for path, value in deep_iter(raw, depth=2): arg = { 'type': 'input', 'value': '', # option } if not isinstance(value, dict): value = {'value': value} arg['type'] = data_to_type(value, arg=path[1]) if arg['type'] == 'stored': value['value'] = {} arg['display'] = 'hide' # Hide `stored` by default if isinstance(value['value'], datetime): arg['type'] = 'datetime' arg['validate'] = 'datetime' # Manual definition has the highest priority arg.update(value) deep_set(data, keys=path, value=arg) return data @cached_property def task(self): """ <task_group>: <task>: <group>: """ return read_file(filepath_argument('task')) @cached_property def default(self): """ <task>: <group>: <argument>: value """ return read_file(filepath_argument('default')) @cached_property def override(self): """ <task>: <group>: <argument>: value """ return read_file(filepath_argument('override')) @cached_property def gui(self): """ <i18n_group>: <i18n_key>: value, value is None """ return read_file(filepath_argument('gui')) @cached_property @timer def args(self): """ Merge definitions into standardised json. task.yaml ---+ argument.yaml ---+-----> args.json override.yaml ---+ default.yaml ---+ """ # Construct args data = {} for path, groups in deep_iter(self.task, depth=3): if 'tasks' not in path: continue task = path[2] # Add storage to all task # groups.append('Storage') for group in groups: if group not in self.argument: print(f'`{task}.{group}` is not related to any argument group') continue deep_set(data, keys=[task, group], value=deepcopy(self.argument[group])) def check_override(path, value): # Check existence old = deep_get(data, keys=path, default=None) if old is None: print(f'`{".".join(path)}` is not a existing argument') return False # Check type # But allow `Interval` to be different old_value = old.get('value', None) if isinstance(old, dict) else old value = old.get('value', None) if isinstance(value, dict) else value if type(value) != type(old_value) \ and old_value is not None \ and path[2] not in ['SuccessInterval', 'FailureInterval']: print( f'`{value}` ({type(value)}) and `{".".join(path)}` ({type(old_value)}) are in different types') return False # Check option if isinstance(old, dict) and 'option' in old: if value not in old['option']: print(f'`{value}` is not an option of argument `{".".join(path)}`') return False return True # Set defaults for p, v in deep_iter(self.default, depth=3): if not check_override(p, v): continue deep_set(data, keys=p + ['value'], value=v) # Override non-modifiable arguments for p, v in deep_iter(self.override, depth=3): if not check_override(p, v): continue if isinstance(v, dict): typ = v.get('type') if typ == 'state': pass elif typ == 'lock': deep_default(v, keys='display', value="disabled") elif deep_get(v, keys='value') is not None: deep_default(v, keys='display', value='hide') for arg_k, arg_v in v.items(): deep_set(data, keys=p + [arg_k], value=arg_v) else: deep_set(data, keys=p + ['value'], value=v) deep_set(data, keys=p + ['display'], value='hide') # Set command for path, groups in deep_iter(self.task, depth=3): if 'tasks' not in path: continue task = path[2] if deep_get(data, keys=f'{task}.Scheduler.Command'): deep_set(data, keys=f'{task}.Scheduler.Command.value', value=task) deep_set(data, keys=f'{task}.Scheduler.Command.display', value='hide') return data @timer def generate_code(self): """ Generate python code. args.json ---> config_generated.py """ visited_group = set() visited_path = set() lines = CONFIG_IMPORT for path, data in deep_iter(self.argument, depth=2): group, arg = path if group not in visited_group: lines.append('') lines.append(f' # Group `{group}`') visited_group.add(group) option = '' if 'option' in data and data['option']: option = ' # ' + ', '.join([str(opt) for opt in data['option']]) path = '.'.join(path) lines.append(f' {path_to_arg(path)} = {repr(parse_value(data["value"], data=data))}{option}') visited_path.add(path) with open(filepath_code(), 'w', encoding='utf-8', newline='') as f: for text in lines: f.write(text + '\n') @timer def generate_stored(self): gen = get_generator() gen.add('from module.config.stored.classes import (') with gen.tab(): for cls in sorted([name for name in dir(classes) if name.startswith('Stored')]): gen.add(cls + ',') gen.add(')') gen.Empty() gen.Empty() gen.Empty() gen.CommentAutoGenerage('module/config/config_updater.py') with gen.Class('StoredGenerated'): for path, data in deep_iter(self.args, depth=3): cls = data.get('stored') if cls: gen.add(f'{path[-1]} = {cls}("{".".join(path)}")') gen.write('module/config/stored/stored_generated.py') @timer def generate_i18n(self, lang): """ Load old translations and generate new translation file. args.json ---+-----> i18n/<lang>.json (old) i18n/<lang>.json ---+ """ new = {} old = read_file(filepath_i18n(lang)) def deep_load(keys, default=True, words=('name', 'help')): for word in words: k = keys + [str(word)] d = ".".join(k) if default else str(word) v = deep_get(old, keys=k, default=d) deep_set(new, keys=k, value=v) # Menu for path, data in deep_iter(self.task, depth=3): if 'tasks' not in path: continue task_group, _, task = path deep_load(['Menu', task_group]) deep_load(['Task', task]) # Arguments visited_group = set() for path, data in deep_iter(self.argument, depth=2): if path[0] not in visited_group: deep_load([path[0], '_info']) visited_group.add(path[0]) deep_load(path) if 'option' in data: deep_load(path, words=data['option'], default=False) # Package names # for package, server in VALID_PACKAGE.items(): # path = ['Emulator', 'PackageName', package] # if deep_get(new, keys=path) == package: # deep_set(new, keys=path, value=server.upper()) # for package, server_and_channel in VALID_CHANNEL_PACKAGE.items(): # server, channel = server_and_channel # name = deep_get(new, keys=['Emulator', 'PackageName', to_package(server)]) # if lang == SERVER_TO_LANG[server]: # value = f'{name} {channel}渠道服 {package}' # else: # value = f'{name} {package}' # deep_set(new, keys=['Emulator', 'PackageName', package], value=value) # Game server names # for server, _list in VALID_SERVER_LIST.items(): # for index in range(len(_list)): # path = ['Emulator', 'ServerName', f'{server}-{index}'] # prefix = server.split('_')[0].upper() # prefix = '国服' if prefix == 'CN' else prefix # deep_set(new, keys=path, value=f'[{prefix}] {_list[index]}') # GUI i18n for path, _ in deep_iter(self.gui, depth=2): group, key = path deep_load(keys=['Gui', group], words=(key,)) write_file(filepath_i18n(lang), new) @cached_property def menu(self): """ Generate menu definitions task.yaml --> menu.json """ data = {} for task_group in self.task.keys(): value = deep_get(self.task, keys=[task_group, 'menu']) if value not in ['collapse', 'list']: value = 'collapse' deep_set(data, keys=[task_group, 'menu'], value=value) value = deep_get(self.task, keys=[task_group, 'page']) if value not in ['setting', 'tool']: value = 'setting' deep_set(data, keys=[task_group, 'page'], value=value) tasks = deep_get(self.task, keys=[task_group, 'tasks'], default={}) tasks = list(tasks.keys()) deep_set(data, keys=[task_group, 'tasks'], value=tasks) return data @cached_property def stored(self): data = {} for path, value in deep_iter(self.args, depth=3): if value.get('type') != 'stored': continue name = path[-1] stored = value.get('stored') stored_class = getattr(classes, stored) row = { 'name': name, 'path': '.'.join(path), 'i18n': f'{path[1]}.{path[2]}.name', 'stored': stored, 'attrs': stored_class('')._attrs, 'order': value.get('order', 0), 'color': value.get('color', '#777777') } data[name] = row # sort by `order` ascending, but `order`==0 at last data = sorted(data.items(), key=lambda kv: (kv[1]['order'] == 0, kv[1]['order'])) data = {k: v for k, v in data} return data @staticmethod def generate_deploy_template():
template = poor_yaml_read(DEPLOY_TEMPLATE)
1
2023-11-01 07:09:45+00:00
4k
sbharadwajj/flare
flare/modules/neuralshader.py
[ { "identifier": "FC", "path": "flare/modules/fc.py", "snippet": "class FC(nn.Module):\n def __init__(self, in_features, out_features, hidden_features: List[int], activation='relu', last_activation=None, bias=True, first_omega=30, hidden_omega=30.0):\n super().__init__()\n\n layers = []\n\n activations_and_inits = {\n 'sine': (Sine(first_omega),\n siren_init,\n siren_init_first,\n None),\n 'relu': (nn.ReLU(inplace=True),\n init_weights_normal,\n init_weights_normal,\n init_weights_normal),\n 'relu2': (nn.ReLU(inplace=True),\n init_weights_normal,\n init_weights_normal,\n init_weights_normal_last),\n 'softplus': (nn.Softplus(),\n init_weights_normal,\n None),\n 'displacement_mlp': (nn.ReLU(inplace=True),\n init_weights_zero,\n init_weights_zero,\n init_weights_zero)\n \n }\n\n activation_fn, weight_init, first_layer_init, last_layer_init = activations_and_inits[activation]\n\n\n # First layer\n layer = FullyConnectedBlock(in_features, hidden_features[0], bias=bias, activation=activation_fn)\n if first_layer_init is not None: \n layer.apply(lambda module: first_layer_init(module=module, n=in_features))\n layers.append(layer)\n\n for i in range(len(hidden_features)):\n n = hidden_features[i]\n\n # Initialize the layer right away\n layer = FullyConnectedBlock(n, n, bias=bias, activation=activation_fn)\n layer.apply(lambda module: weight_init(module=module, n=n, omega=hidden_omega))\n layers.append(layer)\n\n # Last layer\n layer = FullyConnectedBlock(hidden_features[-1], out_features, bias=bias, activation=last_activation)\n layer.apply(lambda module: weight_init(module=module, n=hidden_features[-1], omega=hidden_omega))\n if last_layer_init is not None: \n layer.apply(lambda module: last_layer_init(module=module, n=in_features))\n layers.append(layer)\n\n self.network = nn.Sequential(*layers)\n\n def forward(self, x):\n return self.network(x)" }, { "identifier": "get_embedder", "path": "flare/modules/embedder.py", "snippet": "def get_embedder(multires):\n embed_kwargs = {\n 'include_input': True,\n 'input_dims': 3,\n 'max_freq_log2': multires-1,\n 'num_freqs': multires,\n 'log_sampling': True,\n 'periodic_fns': [torch.sin, torch.cos],\n }\n\n embedder_obj = Embedder(**embed_kwargs)\n def embed(x, eo=embedder_obj): return eo.embed(x)\n return embed, embedder_obj.out_dim" }, { "identifier": "generate_ide_fn", "path": "flare/modules/embedding_roughness_np.py", "snippet": "def generate_ide_fn(deg_view, device):\n \"\"\"Generate integrated directional encoding (IDE) function.\n This function returns a function that computes the integrated directional\n encoding from Equations 6-8 of arxiv.org/abs/2112.03907.\n Args:\n deg_view: number of spherical harmonics degrees to use.\n Returns:\n A function for evaluating integrated directional encoding.\n Raises:\n ValueError: if deg_view is larger than 5.\n \"\"\"\n if deg_view > 5:\n print('WARNING: Only deg_view of at most 5 is numerically stable.')\n # raise ValueError('Only deg_view of at most 5 is numerically stable.')\n\n ml_array = get_ml_array(deg_view)\n l_max = 2**(deg_view - 1)\n\n # Create a matrix corresponding to ml_array holding all coefficients, which,\n # when multiplied (from the right) by the z coordinate Vandermonde matrix,\n # results in the z component of the encoding.\n mat = torch.zeros((l_max + 1, ml_array.shape[1]))\n for i, (m, l) in enumerate(ml_array.T):\n for k in range(l - m + 1):\n mat[k, i] = sph_harm_coeff(l, m, k)\n mat = mat.to(device)\n\n def integrated_dir_enc_fn(xyz, kappa_inv):\n \"\"\"Function returning integrated directional encoding (IDE).\n Args:\n xyz: [..., 3] array of Cartesian coordinates of directions to evaluate at.\n kappa_inv: [..., 1] reciprocal of the concentration parameter of the von\n Mises-Fisher distribution.\n Returns:\n An array with the resulting IDE.\n \"\"\"\n # expects 1/roughness\n kappa_inv = 1. / (kappa_inv + 10e-20)\n # kappa_inv = roughness\n \n x = xyz[..., 0:1]\n y = xyz[..., 1:2]\n z = xyz[..., 2:3]\n\n # Compute z Vandermonde matrix.\n vmz = torch.cat([z**i for i in range(mat.shape[0])], dim=-1)\n\n # Compute x+iy Vandermonde matrix.\n vmxy = torch.cat(\n [(x + 1j * y)**m for m in ml_array[0, :]], dim=-1)\n\n # Get spherical harmonics.\n sph_harms = vmxy * torch.matmul(vmz, mat)\n\n # Apply attenuation function using the von Mises-Fisher distribution\n # concentration parameter, kappa.\n sigma = torch.tensor(0.5 * ml_array[1, :] * (ml_array[1, :] + 1), dtype=torch.float32).to(device)\n ide = sph_harms * torch.exp(-sigma * kappa_inv)\n\n # Split into real and imaginary parts and return\n return torch.cat([torch.real(ide), torch.imag(ide)], dim=-1)\n\n return integrated_dir_enc_fn" } ]
from flare.modules.fc import FC from flare.modules.embedder import get_embedder from flare.modules.embedding_roughness_np import generate_ide_fn import numpy as np import torch import tinycudann as tcnn import nvdiffrec.render.renderutils.ops as ru import nvdiffrast.torch as dr
1,737
# -*- coding: utf-8 -*- # # Max-Planck-Gesellschaft zur Förderung der Wissenschaften e.V. (MPG) is # holder of all proprietary rights on this computer program. # Using this computer program means that you agree to the terms # in the LICENSE file included with this software distribution. # Any use not explicitly granted by the LICENSE is prohibited. # # Copyright©2019 Max-Planck-Gesellschaft zur Förderung # der Wissenschaften e.V. (MPG). acting on behalf of its Max Planck Institute # for Intelligent Systems. All rights reserved. # # For commercial licensing contact, please contact [email protected] class NeuralShader(torch.nn.Module): def __init__(self, activation='relu', last_activation=None, fourier_features='positional', disentangle_network_params=None, bsdf='pbr', aabb=None, device='cpu'): super().__init__() self.device = device self.aabb = aabb self.bsdf = bsdf # ============================================================================================== # PE # ============================================================================================== if fourier_features == 'positional': print("STAGE 1: Using positional encoding (NeRF) for intrinsic materials")
# -*- coding: utf-8 -*- # # Max-Planck-Gesellschaft zur Förderung der Wissenschaften e.V. (MPG) is # holder of all proprietary rights on this computer program. # Using this computer program means that you agree to the terms # in the LICENSE file included with this software distribution. # Any use not explicitly granted by the LICENSE is prohibited. # # Copyright©2019 Max-Planck-Gesellschaft zur Förderung # der Wissenschaften e.V. (MPG). acting on behalf of its Max Planck Institute # for Intelligent Systems. All rights reserved. # # For commercial licensing contact, please contact [email protected] class NeuralShader(torch.nn.Module): def __init__(self, activation='relu', last_activation=None, fourier_features='positional', disentangle_network_params=None, bsdf='pbr', aabb=None, device='cpu'): super().__init__() self.device = device self.aabb = aabb self.bsdf = bsdf # ============================================================================================== # PE # ============================================================================================== if fourier_features == 'positional': print("STAGE 1: Using positional encoding (NeRF) for intrinsic materials")
self.fourier_feature_transform, channels = get_embedder(multires=4)
1
2023-11-08 08:49:30+00:00
4k
minzwon/musicfm
model/musicfm_25hz.py
[ { "identifier": "RandomProjectionQuantizer", "path": "modules/random_quantizer.py", "snippet": "class RandomProjectionQuantizer(nn.Module):\n \"\"\" \n Random projection and codebook lookup module \n \n Some code is borrowed from:\n https://github.com/lucidrains/vector-quantize-pytorch/blob/master/vector_quantize_pytorch/random_projection_quantizer.py\n But I did normalization using pre-computed global mean & variance instead of using layer norm.\n \"\"\"\n\n def __init__(\n self,\n input_dim,\n codebook_dim,\n codebook_size,\n seed=142,\n ):\n super().__init__()\n\n # random seed\n torch.manual_seed(seed)\n\n # randomly initialized projection\n random_projection = torch.empty(input_dim, codebook_dim)\n nn.init.xavier_normal_(random_projection)\n self.register_buffer(\"random_projection\", random_projection)\n\n # randomly initialized codebook\n codebook = torch.empty(codebook_size, codebook_dim)\n nn.init.normal_(codebook)\n self.register_buffer(\"codebook\", codebook)\n\n def codebook_lookup(self, x):\n # reshape\n b = x.shape[0]\n x = rearrange(x, \"b n e -> (b n) e\")\n\n # L2 normalization\n normalized_x = nn.functional.normalize(x, dim=1, p=2)\n normalized_codebook = nn.functional.normalize(self.codebook, dim=1, p=2)\n\n # compute distances\n distances = torch.cdist(normalized_codebook, normalized_x)\n\n # get nearest\n nearest_indices = torch.argmin(distances, dim=0)\n\n # reshape\n xq = rearrange(nearest_indices, \"(b n) -> b n\", b=b)\n\n return xq\n\n @torch.no_grad()\n def forward(self, x):\n # always eval\n self.eval()\n\n # random projection [batch, length, input_dim] -> [batch, length, codebook_dim]\n x = einsum(\"b n d, d e -> b n e\", x, self.random_projection)\n\n # codebook lookup\n xq = self.codebook_lookup(x)\n\n return xq" }, { "identifier": "MelSTFT", "path": "modules/features.py", "snippet": "class MelSTFT(nn.Module):\n def __init__(\n self,\n sample_rate=24000,\n n_fft=2048,\n hop_length=240,\n n_mels=128\n ):\n super(MelSTFT, self).__init__()\n\n # spectrogram\n self.melspec = torchaudio.transforms.MelSpectrogram(\n sample_rate=sample_rate,\n n_fft=n_fft,\n hop_length=hop_length,\n n_mels=n_mels\n )\n self.amplitude_to_db = torchaudio.transforms.AmplitudeToDB()\n\n def forward(self, waveform):\n # MelSTFT\n melspec = self.melspec(waveform)\n\n # amplitude to db\n log_melspec = self.amplitude_to_db(melspec)\n\n return log_melspec" }, { "identifier": "Conv2dSubsampling", "path": "modules/conv.py", "snippet": "class Conv2dSubsampling(nn.Module):\n \"\"\"Convolutional 2D subsampling (to 1/4 length).\n\n Args:\n idim (int): Input dimension.\n hdim (int): Hidden dimension.\n odim (int): Output dimension.\n strides (list): Sizes of strides.\n n_bands (int): Number of frequency bands.\n \"\"\"\n def __init__(self, \n idim, \n hdim,\n odim, \n strides=[2, 2],\n n_bands=64\n ):\n \"\"\"Construct an Conv2dSubsampling object.\"\"\"\n super(Conv2dSubsampling, self).__init__()\n \n self.conv = nn.Sequential(\n Res2dModule(idim, hdim, (2, strides[0])),\n Res2dModule(hdim, hdim, (2, strides[1])),\n )\n self.linear = nn.Linear(hdim * n_bands // 2 // 2, odim)\n\n def forward(self, x):\n \"\"\"Subsample x.\n\n Args:\n x (torch.Tensor): Input tensor (#batch, idim, time).\n\n Returns:\n torch.Tensor: Subsampled tensor (#batch, time', odim),\n where time' = time // 4.\n \"\"\"\n\n if x.dim() == 3:\n x = x.unsqueeze(1) # (b, c, f, t)\n x = self.conv(x)\n x = rearrange(x, \"b c f t -> b t (c f)\")\n x = self.linear(x)\n return x" } ]
import json import torch from torch import nn from einops import rearrange from modules.random_quantizer import RandomProjectionQuantizer from modules.features import MelSTFT from modules.conv import Conv2dSubsampling from modules.flash_conformer import Wav2Vec2ConformerEncoder, Wav2Vec2ConformerConfig from transformers.models.wav2vec2_conformer.modeling_wav2vec2_conformer import Wav2Vec2ConformerEncoder, Wav2Vec2ConformerConfig
1,822
# MIT License # # Copyright 2023 ByteDance Inc. # # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), # to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. class MusicFM25Hz(nn.Module): """ MusicFM Input: 128-band mel spectrogram Frontend: 2-layer Residual convolution Backend: 12-layer Conformer Quantizer: a codebook for mel spectrogram """ def __init__( self, codebook_dim=16, codebook_size=8192, hop_length=240, n_fft=2048, n_mels=128, conv_dim=512, encoder_dim=1024, encoder_depth=24, mask_hop=0.4, mask_prob=0.6, is_flash=False, stat_path="./data/fma_classic_stats.json", model_path="./data/musicfm_25hz_FMA_330m_500k.pt", ): super(MusicFM25Hz, self).__init__() # global variables self.hop_length = hop_length self.mask_hop = mask_hop self.mask_prob = mask_prob self.codebook_size = codebook_size self.features = ["melspec"] # load feature mean / std stats with open(stat_path, "r") as f: self.stat = json.load(f) # random quantizer self.quantizer_melspec = RandomProjectionQuantizer(n_mels * 4, codebook_dim, codebook_size) # mel spec # feature extractor self.preprocessor_melspec = MelSTFT(n_fft=n_fft, hop_length=hop_length) # two residual convolution layers + one projection layer
# MIT License # # Copyright 2023 ByteDance Inc. # # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), # to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. class MusicFM25Hz(nn.Module): """ MusicFM Input: 128-band mel spectrogram Frontend: 2-layer Residual convolution Backend: 12-layer Conformer Quantizer: a codebook for mel spectrogram """ def __init__( self, codebook_dim=16, codebook_size=8192, hop_length=240, n_fft=2048, n_mels=128, conv_dim=512, encoder_dim=1024, encoder_depth=24, mask_hop=0.4, mask_prob=0.6, is_flash=False, stat_path="./data/fma_classic_stats.json", model_path="./data/musicfm_25hz_FMA_330m_500k.pt", ): super(MusicFM25Hz, self).__init__() # global variables self.hop_length = hop_length self.mask_hop = mask_hop self.mask_prob = mask_prob self.codebook_size = codebook_size self.features = ["melspec"] # load feature mean / std stats with open(stat_path, "r") as f: self.stat = json.load(f) # random quantizer self.quantizer_melspec = RandomProjectionQuantizer(n_mels * 4, codebook_dim, codebook_size) # mel spec # feature extractor self.preprocessor_melspec = MelSTFT(n_fft=n_fft, hop_length=hop_length) # two residual convolution layers + one projection layer
self.conv = Conv2dSubsampling(1, conv_dim, encoder_dim, strides=[2, 2], n_bands=n_mels)
2
2023-11-06 16:04:54+00:00
4k
liuzhao1225/YouDub
youdub/tts_xttsv2.py
[ { "identifier": "save_wav", "path": "youdub/utils.py", "snippet": "def save_wav(wav: np.ndarray, path: str, sample_rate: int = 24000) -> None:\n \"\"\"Save float waveform to a file using Scipy.\n\n Args:\n wav (np.ndarray): Waveform with float values in range [-1, 1] to save.\n path (str): Path to a output file.\n sample_rate (int, optional): Sampling rate used for saving to the file. Defaults to 24000.\n \"\"\"\n # wav_norm = wav * (32767 / max(0.01, np.max(np.abs(wav))))\n wav_norm = wav * 32767\n wavfile.write(path, sample_rate, wav_norm.astype(np.int16))" }, { "identifier": "adjust_audio_length", "path": "youdub/utils.py", "snippet": "def adjust_audio_length(wav, src_path, dst_path, desired_length: float, sample_rate: int = 24000) -> np.ndarray:\n \"\"\"Adjust the length of the audio.\n\n Args:\n wav (np.ndarray): Original waveform.\n sample_rate (int): Sampling rate of the audio.\n desired_length (float): Desired length of the audio in seconds.\n\n Returns:\n np.ndarray: Waveform with adjusted length.\n \"\"\"\n current_length = wav.shape[0] / sample_rate\n speed_factor = max(min(desired_length / current_length, 1.1), 2/3)\n desired_length = current_length * speed_factor\n stretch_audio(src_path, dst_path, ratio=speed_factor,\n sample_rate=sample_rate)\n y, sr = librosa.load(dst_path, sr=sample_rate)\n return y[:int(desired_length * sr)], desired_length" }, { "identifier": "split_text", "path": "youdub/utils.py", "snippet": "def split_text(input_data,\n punctuations=['。', '?', '!', '\\n', \"”\"]):\n # Chinese punctuation marks for sentence ending\n\n # Function to check if a character is a Chinese ending punctuation\n def is_punctuation(char):\n return char in punctuations\n\n # Process each item in the input data\n output_data = []\n for item in input_data:\n start = item[\"start\"]\n text = item[\"text\"]\n speaker = item.get(\"speaker\", \"SPEAKER_00\")\n sentence_start = 0\n\n # Calculate the duration for each character\n duration_per_char = (item[\"end\"] - item[\"start\"]) / len(text)\n for i, char in enumerate(text):\n # If the character is a punctuation, split the sentence\n if not is_punctuation(char) and i != len(text) - 1:\n continue\n if i - sentence_start < 5 and i != len(text) - 1:\n continue\n if i < len(text) - 1 and is_punctuation(text[i+1]):\n continue\n sentence = text[sentence_start:i+1]\n sentence_end = start + duration_per_char * len(sentence)\n\n # Append the new item\n output_data.append({\n \"start\": round(start, 3),\n \"end\": round(sentence_end, 3),\n \"text\": sentence,\n \"speaker\": speaker\n })\n\n # Update the start for the next sentence\n start = sentence_end\n sentence_start = i + 1\n\n return output_data" }, { "identifier": "tts_preprocess_text", "path": "youdub/utils.py", "snippet": "def tts_preprocess_text(text):\n # 使用正则表达式查找所有的大写字母,并在它们前面加上空格\n # 正则表达式说明:(?<!^) 表示如果不是字符串开头,则匹配,[A-Z] 匹配任何大写字母\n text = text.replace('AI', '人工智能')\n text = re.sub(r'(?<!^)([A-Z])', r' \\1', text)\n text = normalizer(text)\n # 使用正则表达式在字母和数字之间插入空格\n text = re.sub(r'(?<=[a-zA-Z])(?=\\d)|(?<=\\d)(?=[a-zA-Z])', ' ', text)\n return text" }, { "identifier": "TextNorm", "path": "youdub/cn_tx.py", "snippet": "class TextNorm:\n def __init__(self,\n to_banjiao: bool = False,\n to_upper: bool = False,\n to_lower: bool = False,\n remove_fillers: bool = False,\n remove_erhua: bool = False,\n check_chars: bool = False,\n remove_space: bool = False,\n cc_mode: str = '',\n ):\n self.to_banjiao = to_banjiao\n self.to_upper = to_upper\n self.to_lower = to_lower\n self.remove_fillers = remove_fillers\n self.remove_erhua = remove_erhua\n self.check_chars = check_chars\n self.remove_space = remove_space\n\n self.cc = None\n if cc_mode:\n from opencc import OpenCC # Open Chinese Convert: pip install opencc\n self.cc = OpenCC(cc_mode)\n\n def __call__(self, text):\n if self.cc:\n text = self.cc.convert(text)\n\n if self.to_banjiao:\n text = text.translate(QJ2BJ_TRANSFORM)\n\n if self.to_upper:\n text = text.upper()\n\n if self.to_lower:\n text = text.lower()\n\n if self.remove_fillers:\n for c in FILLER_CHARS:\n text = text.replace(c, '')\n\n if self.remove_erhua:\n text = remove_erhua(text)\n\n text = normalize_nsw(text)\n\n # text = text.translate(PUNCS_TRANSFORM)\n\n if self.check_chars:\n for c in text:\n if not IN_VALID_CHARS.get(c):\n print(\n f'WARNING: illegal char {c} in: {text}', file=sys.stderr)\n return ''\n\n if self.remove_space:\n text = remove_space(text)\n\n return text" } ]
import os, sys import time import re import librosa import numpy as np import json import logging from TTS.api import TTS from tqdm import tqdm from youdub.utils import save_wav, adjust_audio_length, split_text, tts_preprocess_text from youdub.cn_tx import TextNorm
2,099
sys.path.append(os.getcwd()) # Get device # import torch # device = 'cuda' if torch.cuda.is_available() else 'cpu' device = 'cuda' class TTS_Clone: def __init__(self, model_path="tts_models/multilingual/multi-dataset/xtts_v2", device='cuda', language='zh-cn'): logging.info(f'Loading TTS model {model_path}...') self.tts = TTS(model_path).to(device) self.language = language logging.info('Model TTS loaded.') def inference(self, text, output_path, speaker_wav) -> np.ndarray: wav = self.tts.tts( text=text, speaker_wav=speaker_wav, language=self.language) wav = np.array(wav) save_wav(wav, output_path) # wav /= np.max(np.abs(wav)) return wav def audio_process_folder(folder, tts: TTS_Clone, speaker_to_voice_type=None, vocal_only=False): logging.info(f'TTS processing folder {folder}...') logging.info(f'speaker_to_voice_type: {speaker_to_voice_type}') with open(os.path.join(folder, 'zh.json'), 'r', encoding='utf-8') as f: transcript = json.load(f) full_wav = np.zeros((0,)) if not os.path.exists(os.path.join(folder, 'temp')): os.makedirs(os.path.join(folder, 'temp')) for i, line in enumerate(transcript): text = line['text'] # start = line['start'] start = line['start'] last_end = len(full_wav)/24000 if start > last_end: full_wav = np.concatenate( (full_wav, np.zeros((int(24000 * (start - last_end)),)))) start = len(full_wav)/24000 line['start'] = start end = line['end'] if os.path.exists(os.path.join(folder, 'temp', f'zh_{str(i).zfill(3)}.wav')): wav = librosa.load(os.path.join( folder, 'temp', f'zh_{str(i).zfill(3)}.wav'), sr=24000)[0] else: speaker = line.get('speaker', 'SPEAKER_00') speaker_wav = os.path.join(folder, 'SPEAKER', f'{speaker}.wav') wav = tts.inference(tts_preprocess_text(text), os.path.join( folder, 'temp', f'zh_{str(i).zfill(3)}.wav'), speaker_wav) time.sleep(0.1) # save_wav(wav, )
sys.path.append(os.getcwd()) # Get device # import torch # device = 'cuda' if torch.cuda.is_available() else 'cpu' device = 'cuda' class TTS_Clone: def __init__(self, model_path="tts_models/multilingual/multi-dataset/xtts_v2", device='cuda', language='zh-cn'): logging.info(f'Loading TTS model {model_path}...') self.tts = TTS(model_path).to(device) self.language = language logging.info('Model TTS loaded.') def inference(self, text, output_path, speaker_wav) -> np.ndarray: wav = self.tts.tts( text=text, speaker_wav=speaker_wav, language=self.language) wav = np.array(wav) save_wav(wav, output_path) # wav /= np.max(np.abs(wav)) return wav def audio_process_folder(folder, tts: TTS_Clone, speaker_to_voice_type=None, vocal_only=False): logging.info(f'TTS processing folder {folder}...') logging.info(f'speaker_to_voice_type: {speaker_to_voice_type}') with open(os.path.join(folder, 'zh.json'), 'r', encoding='utf-8') as f: transcript = json.load(f) full_wav = np.zeros((0,)) if not os.path.exists(os.path.join(folder, 'temp')): os.makedirs(os.path.join(folder, 'temp')) for i, line in enumerate(transcript): text = line['text'] # start = line['start'] start = line['start'] last_end = len(full_wav)/24000 if start > last_end: full_wav = np.concatenate( (full_wav, np.zeros((int(24000 * (start - last_end)),)))) start = len(full_wav)/24000 line['start'] = start end = line['end'] if os.path.exists(os.path.join(folder, 'temp', f'zh_{str(i).zfill(3)}.wav')): wav = librosa.load(os.path.join( folder, 'temp', f'zh_{str(i).zfill(3)}.wav'), sr=24000)[0] else: speaker = line.get('speaker', 'SPEAKER_00') speaker_wav = os.path.join(folder, 'SPEAKER', f'{speaker}.wav') wav = tts.inference(tts_preprocess_text(text), os.path.join( folder, 'temp', f'zh_{str(i).zfill(3)}.wav'), speaker_wav) time.sleep(0.1) # save_wav(wav, )
wav_adjusted, adjusted_length = adjust_audio_length(wav, os.path.join(folder, 'temp', f'zh_{str(i).zfill(3)}.wav'), os.path.join(
1
2023-11-02 08:21:31+00:00
4k
JunityZhan/CharaCraft-AI
CharaCraft/chatharuhi/ChatHaruhi.py
[ { "identifier": "ChromaDB", "path": "CharaCraft/chatharuhi/ChromaDB.py", "snippet": "class ChromaDB(BaseDB):\n \n def __init__(self):\n self.client = None\n self.collection = None\n self.path = None\n def init_db(self, role_name=''):\n\n if self.client is not None:\n print('ChromaDB has already been initialized')\n return\n folder_name = ''\n if role_name == '':\n while os.path.exists(folder_name) or folder_name == '':\n # try to create a folder named temp_<random string> which is not yet existed\n folder_name = \"chromadb/tempdb_\" + ''.join(random.sample(string.ascii_letters + string.digits, 8))\n else:\n folder_name = \"chromadb/\" + role_name\n self.path = folder_name\n self.client = chromadb.PersistentClient(path=folder_name)\n\n self.collection = self.client.get_or_create_collection(\"search\")\n\n def save(self, file_path):\n if file_path != self.path:\n # copy all files in self.path to file_path, with overwrite\n os.system(\"cp -r \" + self.path + \" \" + file_path)\n previous_path = self.path\n self.path = file_path\n self.client = chromadb.PersistentClient(path = file_path)\n # remove previous path if it start with tempdb\n if previous_path.startswith(\"tempdb\"):\n os.system(\"rm -rf \" + previous_path)\n \n\n def load(self, file_path):\n self.path = file_path\n self.client = chromadb.PersistentClient(path = file_path)\n self.collection = self.client.get_collection(\"search\")\n\n def search(self, vector, n_results):\n results = self.collection.query(query_embeddings=[vector], n_results=n_results)\n return results['documents'][0]\n\n def init_from_docs(self, vectors, documents, role_name = ''):\n if self.client is None:\n self.init_db(role_name=role_name)\n \n ids = []\n for i, doc in enumerate(documents):\n first_four_chat = doc[:min(4, len(doc))]\n ids.append( str(i) + \"_\" + doc)\n self.collection.add(embeddings=vectors, documents=documents, ids = ids)" }, { "identifier": "luotuo_openai_embedding", "path": "CharaCraft/chatharuhi/utils.py", "snippet": "def luotuo_openai_embedding(texts, is_chinese= None ):\n \"\"\"\n when input is chinese, use luotuo_embedding\n when input is english, use openai_embedding\n texts can be a list or a string\n when texts is a list, return a list of embeddings, using batch inference\n when texts is a string, return a single embedding\n \"\"\"\n\n openai_key = os.environ.get(\"OPENAI_API_KEY\")\n\n if isinstance(texts, list):\n index = random.randint(0, len(texts) - 1)\n if openai_key is None or is_chinese_or_english(texts[index]) == \"chinese\":\n return [embed.cpu().tolist() for embed in get_embedding_for_chinese(get_luotuo_model(), texts)]\n else:\n return [get_embedding_for_english(text) for text in texts]\n else:\n if openai_key is None or is_chinese_or_english(texts) == \"chinese\":\n return get_embedding_for_chinese(get_luotuo_model(), texts)[0].cpu().tolist()\n else:\n return get_embedding_for_english(texts)" }, { "identifier": "tiktokenizer", "path": "CharaCraft/chatharuhi/utils.py", "snippet": "def tiktokenizer( text ):\n global _enc_model\n\n if _enc_model is None:\n _enc_model = tiktoken.get_encoding(\"cl100k_base\")\n\n return len(_enc_model.encode(text))" }, { "identifier": "response_postprocess", "path": "CharaCraft/chatharuhi/utils.py", "snippet": "def response_postprocess(text,dialogue_bra_token = '「',dialogue_ket_token = '」'):\n lines = text.split('\\n')\n new_lines = \"\"\n\n first_name = None\n\n for line in lines:\n line = line.strip(\" \")\n match = re.match(r'^(.*?)[::]' + dialogue_bra_token + r\"(.*?)\" + dialogue_ket_token + r\"$\", line)\n\n \n if match:\n curr_name = match.group(1)\n # print(curr_name)\n if first_name is None:\n first_name = curr_name\n new_lines += (match.group(2))\n else:\n if curr_name != first_name:\n return first_name + \":\" + dialogue_bra_token + new_lines + dialogue_ket_token\n else:\n new_lines += (match.group(2))\n \n else:\n if first_name == None:\n return text\n else:\n return first_name + \":\" + dialogue_bra_token + new_lines + dialogue_ket_token\n return first_name + \":\" + dialogue_bra_token + new_lines + dialogue_ket_token" } ]
from .ChromaDB import ChromaDB from .utils import luotuo_openai_embedding, tiktokenizer from .utils import response_postprocess from .utils import get_bge_embedding from datasets import load_dataset from .utils import base64_to_float_array from .LangChainGPT import LangChainGPT from .PrintLLM import PrintLLM from .SparkGPT import SparkGPT from .GLMPro import GLMPro from .ErnieGPT import ErnieGPT from .ChatGLM2GPT import ChatGLM2GPT, GLM_tokenizer from .BaiChuan2GPT import BaiChuan2GPT, BaiChuan_tokenizer from .LangChainGPT import LangChainGPT import os
1,970
class ChatHaruhi: def __init__(self, system_prompt=None, \ role_name='', role_from_hf=None, \ story_db=None, story_text_folder=None, \ llm='openai', \ embedding='luotuo_openai', \ max_len_story=None, max_len_history=None, verbose=False, story_prefix_prompt="以下是你曾经说过的话:\n", first_response=None): super(ChatHaruhi, self).__init__() self.verbose = verbose self.role_name = role_name # constants self.story_prefix_prompt = story_prefix_prompt self.k_search = 19 self.narrator = ['旁白', '', 'scene', 'Scene', 'narrator', 'Narrator'] self.dialogue_divide_token = '\n###\n' self.dialogue_bra_token = '「' self.dialogue_ket_token = '」' self.first_response = first_response if system_prompt: self.system_prompt = self.check_system_prompt(system_prompt) # TODO: embedding should be the seperately defined, so refactor this part later if llm == 'openai': # self.llm = LangChainGPT() self.llm, self.tokenizer = self.get_models('openai') elif llm == 'debug': self.llm, self.tokenizer = self.get_models('debug') elif llm == 'spark': self.llm, self.tokenizer = self.get_models('spark') elif llm == 'GLMPro': self.llm, self.tokenizer = self.get_models('GLMPro') elif llm == 'ChatGLM2GPT': self.llm, self.tokenizer = self.get_models('ChatGLM2GPT') self.story_prefix_prompt = '\n' elif llm == "BaiChuan2GPT": self.llm, self.tokenizer = self.get_models('BaiChuan2GPT') elif llm == "ernie": self.llm, self.tokenizer = self.get_models('ernie') else: print(f'warning! undefined llm {llm}, use openai instead.') self.llm, self.tokenizer = self.get_models('openai') if embedding == 'luotuo_openai': self.embedding = luotuo_openai_embedding elif embedding == 'bge_en': self.embedding = get_bge_embedding else: print(f'warning! undefined embedding {embedding}, use luotuo_openai instead.') self.embedding = luotuo_openai_embedding if role_name != '': db_folder = story_text_folder.replace('text', 'chromadb') if self.verbose: print(f'loading pre-defined character {role_name}...')
class ChatHaruhi: def __init__(self, system_prompt=None, \ role_name='', role_from_hf=None, \ story_db=None, story_text_folder=None, \ llm='openai', \ embedding='luotuo_openai', \ max_len_story=None, max_len_history=None, verbose=False, story_prefix_prompt="以下是你曾经说过的话:\n", first_response=None): super(ChatHaruhi, self).__init__() self.verbose = verbose self.role_name = role_name # constants self.story_prefix_prompt = story_prefix_prompt self.k_search = 19 self.narrator = ['旁白', '', 'scene', 'Scene', 'narrator', 'Narrator'] self.dialogue_divide_token = '\n###\n' self.dialogue_bra_token = '「' self.dialogue_ket_token = '」' self.first_response = first_response if system_prompt: self.system_prompt = self.check_system_prompt(system_prompt) # TODO: embedding should be the seperately defined, so refactor this part later if llm == 'openai': # self.llm = LangChainGPT() self.llm, self.tokenizer = self.get_models('openai') elif llm == 'debug': self.llm, self.tokenizer = self.get_models('debug') elif llm == 'spark': self.llm, self.tokenizer = self.get_models('spark') elif llm == 'GLMPro': self.llm, self.tokenizer = self.get_models('GLMPro') elif llm == 'ChatGLM2GPT': self.llm, self.tokenizer = self.get_models('ChatGLM2GPT') self.story_prefix_prompt = '\n' elif llm == "BaiChuan2GPT": self.llm, self.tokenizer = self.get_models('BaiChuan2GPT') elif llm == "ernie": self.llm, self.tokenizer = self.get_models('ernie') else: print(f'warning! undefined llm {llm}, use openai instead.') self.llm, self.tokenizer = self.get_models('openai') if embedding == 'luotuo_openai': self.embedding = luotuo_openai_embedding elif embedding == 'bge_en': self.embedding = get_bge_embedding else: print(f'warning! undefined embedding {embedding}, use luotuo_openai instead.') self.embedding = luotuo_openai_embedding if role_name != '': db_folder = story_text_folder.replace('text', 'chromadb') if self.verbose: print(f'loading pre-defined character {role_name}...')
self.db = ChromaDB()
0
2023-11-07 05:57:39+00:00
4k
dtiesling/flask-muck
tests/test.py
[ { "identifier": "GuardianModel", "path": "tests/app.py", "snippet": "class GuardianModel(db.Model):\n id = db.Column(db.Integer, primary_key=True, autoincrement=True)\n name = db.Column(db.String, nullable=False, unique=True)\n age = db.Column(db.Integer, nullable=True)\n family_id = db.Column(db.Integer, db.ForeignKey(FamilyModel.id))\n family = db.relationship(FamilyModel)\n children: Mapped[list[\"ChildModel\"]] = db.relationship()" }, { "identifier": "ToyApiView", "path": "tests/app.py", "snippet": "class ToyApiView(BaseApiView):\n api_name = \"toy\"\n Model = ToyModel\n ResponseSchema = ToySchema\n CreateSchema = ToySchema\n PatchSchema = ToySchema\n UpdateSchema = ToySchema\n parent = ChildApiView\n one_to_one_api = True" }, { "identifier": "ChildModel", "path": "tests/app.py", "snippet": "class ChildModel(db.Model):\n id = db.Column(db.Integer, primary_key=True, autoincrement=True)\n name = db.Column(db.String, nullable=False)\n age = db.Column(db.Integer, nullable=True)\n family_id = db.Column(db.Integer, db.ForeignKey(FamilyModel.id))\n guardian_id = db.Column(db.Integer, db.ForeignKey(GuardianModel.id))\n guardian = db.relationship(GuardianModel, back_populates=\"children\")\n toy: Mapped[\"ToyModel\"] = db.relationship(uselist=False)" }, { "identifier": "ToyModel", "path": "tests/app.py", "snippet": "class ToyModel(db.Model):\n id = db.Column(db.Integer, primary_key=True, autoincrement=True)\n name = db.Column(db.String, nullable=False)\n family_id = db.Column(db.Integer, db.ForeignKey(FamilyModel.id))\n child_id = db.Column(db.Integer, db.ForeignKey(ChildModel.id))\n child = db.relationship(ChildModel, back_populates=\"toy\")" }, { "identifier": "BaseApiView", "path": "tests/app.py", "snippet": "class BaseApiView(FlaskMuckApiView):\n \"\"\"Base view to inherit from. Helpful for setting class variables shared with all API views such as \"sqlalchemy_db\"\n and \"decorators\".\n \"\"\"\n\n session = db.session\n decorators = [login_required]\n pre_create_callbacks = [PreCallback]\n pre_update_callbacks = [PreCallback]\n pre_patch_callbacks = [PreCallback]\n pre_delete_callbacks = [PreCallback]\n post_create_callbacks = [PostCallback]\n post_update_callbacks = [PostCallback]\n post_patch_callbacks = [PostCallback]\n post_delete_callbacks = [PostCallback]" }, { "identifier": "PreCallback", "path": "tests/app.py", "snippet": "class PreCallback(FlaskMuckCallback):\n def execute(self) -> None:\n return" }, { "identifier": "PostCallback", "path": "tests/app.py", "snippet": "class PostCallback(FlaskMuckCallback):\n def execute(self) -> None:\n return" }, { "identifier": "GuardianApiView", "path": "tests/app.py", "snippet": "class GuardianApiView(BaseApiView):\n api_name = \"guardians\"\n Model = GuardianModel\n ResponseSchema = GuardianSchema\n CreateSchema = GuardianSchema\n PatchSchema = GuardianSchema\n UpdateSchema = GuardianSchema\n DetailSchema = GuardianDetailSchema\n searchable_columns = [GuardianModel.name, GuardianModel.age]" } ]
import json import pytest from unittest.mock import patch from pydantic import BaseModel, ConfigDict from flask_muck.exceptions import MuckImplementationError from flask_muck.utils import ( get_url_rule, get_fk_column, get_query_filters_from_request_path, get_join_models_from_parent_views, ) from tests.app import ( GuardianModel, ToyApiView, ChildModel, ToyModel, BaseApiView, PreCallback, PostCallback, GuardianApiView, )
3,302
assert filter_guardians({"age__lte": 18}) == [] assert filter_guardians({"age__lte": 34}) == [{"name": "Marge"}] assert filter_guardians({"age__lte": 46}) == [ {"name": "Marge"}, {"name": "Bob"}, ] assert filter_guardians({"age__lte": 47}) == [ {"name": "Marge"}, {"name": "Bob"}, ] def test_in(self, filter_guardians): assert filter_guardians({"name__in": ["Marge", "Bob"]}) == [ {"name": "Bob"}, {"name": "Marge"}, ] assert filter_guardians({"name__in": ["Marge"]}) == [{"name": "Marge"}] assert filter_guardians({"name__in": ["Bob"]}) == [{"name": "Bob"}] assert filter_guardians({"name__in": ["Billy"]}) == [] def test_not_in(self, filter_guardians): assert filter_guardians({"name__not_in": ["Marge", "Bob"]}) == [] assert filter_guardians({"name__not_in": ["Marge"]}) == [{"name": "Bob"}] assert filter_guardians({"name__not_in": ["Bob"]}) == [{"name": "Marge"}] assert filter_guardians({"name__not_in": ["Billy"]}) == [ {"name": "Marge"}, {"name": "Bob"}, ] def test_ne(self, filter_guardians): assert filter_guardians({"name__ne": "Marge"}) == [{"name": "Bob"}] assert filter_guardians({"name__ne": "Bob"}) == [{"name": "Marge"}] assert filter_guardians({"name__ne": "Billy"}) == [ {"name": "Marge"}, {"name": "Bob"}, ] def test_change_operator_separator(self, filter_guardians, monkeypatch): monkeypatch.setattr(BaseApiView, "operator_separator", "|") assert filter_guardians({"name|ne": "Marge"}) == [{"name": "Bob"}] assert filter_guardians({"name|in": ["Marge"]}) == [{"name": "Marge"}] def test_nested_filter(self, filter_guardians, client): assert filter_guardians({"children.name": "Bart"}) == [{"name": "Marge"}] assert filter_guardians({"children.name": "Gene"}) == [{"name": "Bob"}] def test_bad_json(self, get): get("/guardians/?filters=notjson", expected_status_code=400) def test_column_does_not_exist(self, filter_guardians): filter_guardians({"nope": "fail"}, expected_status_code=400) filter_guardians({"nope.nested": "fail"}, expected_status_code=400) filter_guardians({"children.nope": "fail"}, expected_status_code=400) @pytest.mark.usefixtures("simpsons", "belchers") class TestSort: def test_sort(self, get, marge, bart, maggie, lisa): assert get(f"/guardians/{marge.id}/children/?sort=name") == [ {"name": bart.name}, {"name": lisa.name}, {"name": maggie.name}, ] assert get(f"/guardians/{marge.id}/children/?sort=age") == [ {"name": maggie.name}, {"name": lisa.name}, {"name": bart.name}, ] def test_sort_asc(self, get, marge, maggie, lisa, bart): assert get(f"/guardians/{marge.id}/children/?sort=age__asc") == [ {"name": maggie.name}, {"name": lisa.name}, {"name": bart.name}, ] assert get( f"/guardians/{marge.id}/children/?sort=name__asc", ) == [{"name": bart.name}, {"name": lisa.name}, {"name": maggie.name}] def test_sort_desc(self, get, marge, lisa, maggie, bart): assert get( f"/guardians/{marge.id}/children/?sort=age__desc", ) == [{"name": bart.name}, {"name": lisa.name}, {"name": maggie.name}] assert get( f"/guardians/{marge.id}/children/?sort=name__desc", ) == [{"name": maggie.name}, {"name": lisa.name}, {"name": bart.name}] def test_nested_sort(self, get): assert get(f"/guardians/?sort=family.surname") == [ {"name": "Bob"}, {"name": "Marge"}, ] def test_bad_sort(self, get): get(f"/guardians/?sort=name__fail", expected_status_code=400) get(f"/guardians/?sort=fail", expected_status_code=400) get(f"/guardians/?sort=family.fail", expected_status_code=400) get(f"/guardians/?sort=double.fail", expected_status_code=400) def test_change_operator_separator( self, get, monkeypatch, marge, lisa, bart, maggie ): monkeypatch.setattr(BaseApiView, "operator_separator", "|") assert get( f"/guardians/{marge.id}/children/?sort=age|desc", ) == [{"name": bart.name}, {"name": lisa.name}, {"name": maggie.name}] assert get( f"/guardians/{marge.id}/children/?sort=name|desc", ) == [{"name": maggie.name}, {"name": lisa.name}, {"name": bart.name}] @pytest.mark.usefixtures("simpsons", "belchers") class TestSearch: def test_search(self, get, marge): assert get(f"/guardians/?search=marge") == [{"name": "Marge"}] assert get(f"/guardians/?search=nobody") == [] assert get(f"/guardians/{marge.id}/children/?search=bart") == [{"name": "Bart"}] assert get(f"/guardians/{marge.id}/children/?search=nope") == [] def test_unsupported_search(self, get, marge, bart, monkeypatch):
class TestBasicCrud: def test_create(self, post, user): response = post("/guardians/", json={"name": "Jill"}) parent = GuardianModel.query.one() assert response == {"name": parent.name} # Verify integrity errors are handled. post("/guardians/", json={"name": "Jill"}, expected_status_code=409) def test_read(self, get, user, guardian, child): assert get(f"/guardians/") == [{"name": guardian.name}] assert get(f"/guardians/{guardian.id}/") == { "name": "Samantha", "children": [{"name": "Tamara"}], } def test_update(self, put, patch, guardian): assert put(f"/guardians/{guardian.id}/", json={"name": "updated"}) == { "name": "updated" } assert patch(f"/guardians/{guardian.id}/", json={"name": "patched"}) == { "name": "patched" } def test_delete(self, client, guardian): client.delete(f"/guardians/{guardian.id}/") assert GuardianModel.query.count() == 0 class TestAllowedMethods: def test_get_only(self, client, monkeypatch): monkeypatch.setattr(BaseApiView, "allowed_methods", {"GET"}) assert client.get("/guardians/").status_code == 200 assert client.post("/guardians/").status_code == 405 assert client.put("/guardians/").status_code == 405 assert client.patch("/guardians/").status_code == 405 assert client.delete("/guardians/").status_code == 405 def test_no_methods(self, client, monkeypatch): monkeypatch.setattr(BaseApiView, "allowed_methods", {}) assert client.get("/guardians/").status_code == 405 assert client.post("/guardians/").status_code == 405 assert client.put("/guardians/").status_code == 405 assert client.patch("/guardians/").status_code == 405 assert client.delete("/guardians/").status_code == 405 @pytest.mark.usefixtures("simpsons", "belchers") class TestPagination: def test_offset(self, get): assert get("/guardians/?offset=1") == { "items": [{"name": "Bob"}], "limit": 20, "offset": 1, "total": 2, } def test_limit(self, get): assert get("/guardians/?limit=1") == { "items": [{"name": "Marge"}], "limit": 1, "offset": 0, "total": 2, } def test_limit_and_offset(self, get): assert get("/guardians/?limit=10&offset=0") == { "items": [{"name": "Marge"}, {"name": "Bob"}], "limit": 10, "offset": 0, "total": 2, } @pytest.mark.usefixtures("simpsons", "belchers") class TestFiltering: @pytest.fixture def filter_guardians(self, get): def _filter_guardians(filters: dict, expected_status_code: int = 200): return get( f"/guardians/?filters={json.dumps(filters)}", expected_status_code=expected_status_code, ) return _filter_guardians def test_equal(self, filter_guardians): assert filter_guardians({"name": "Marge"}) == [{"name": "Marge"}] assert filter_guardians({"name": "Bob"}) == [{"name": "Bob"}] assert filter_guardians({"name": "Marge", "age": 34}) == [{"name": "Marge"}] assert filter_guardians({"name": "Marge", "age": 45}) == [] def test_gt(self, filter_guardians): assert filter_guardians({"age__gt": 18}) == [ {"name": "Marge"}, {"name": "Bob"}, ] assert filter_guardians({"age__gt": 34}) == [{"name": "Bob"}] assert filter_guardians({"age__gt": 46}) == [] def test_gte(self, filter_guardians): assert filter_guardians({"age__gte": 18}) == [ {"name": "Marge"}, {"name": "Bob"}, ] assert filter_guardians({"age__gte": 34}) == [ {"name": "Marge"}, {"name": "Bob"}, ] assert filter_guardians({"age__gte": 46}) == [{"name": "Bob"}] assert filter_guardians({"age__gte": 47}) == [] def test_lt(self, filter_guardians): assert filter_guardians({"age__lt": 18}) == [] assert filter_guardians({"age__lt": 34}) == [] assert filter_guardians({"age__lt": 46}) == [{"name": "Marge"}] assert filter_guardians({"age__lt": 47}) == [{"name": "Marge"}, {"name": "Bob"}] def test_lte(self, filter_guardians): assert filter_guardians({"age__lte": 18}) == [] assert filter_guardians({"age__lte": 34}) == [{"name": "Marge"}] assert filter_guardians({"age__lte": 46}) == [ {"name": "Marge"}, {"name": "Bob"}, ] assert filter_guardians({"age__lte": 47}) == [ {"name": "Marge"}, {"name": "Bob"}, ] def test_in(self, filter_guardians): assert filter_guardians({"name__in": ["Marge", "Bob"]}) == [ {"name": "Bob"}, {"name": "Marge"}, ] assert filter_guardians({"name__in": ["Marge"]}) == [{"name": "Marge"}] assert filter_guardians({"name__in": ["Bob"]}) == [{"name": "Bob"}] assert filter_guardians({"name__in": ["Billy"]}) == [] def test_not_in(self, filter_guardians): assert filter_guardians({"name__not_in": ["Marge", "Bob"]}) == [] assert filter_guardians({"name__not_in": ["Marge"]}) == [{"name": "Bob"}] assert filter_guardians({"name__not_in": ["Bob"]}) == [{"name": "Marge"}] assert filter_guardians({"name__not_in": ["Billy"]}) == [ {"name": "Marge"}, {"name": "Bob"}, ] def test_ne(self, filter_guardians): assert filter_guardians({"name__ne": "Marge"}) == [{"name": "Bob"}] assert filter_guardians({"name__ne": "Bob"}) == [{"name": "Marge"}] assert filter_guardians({"name__ne": "Billy"}) == [ {"name": "Marge"}, {"name": "Bob"}, ] def test_change_operator_separator(self, filter_guardians, monkeypatch): monkeypatch.setattr(BaseApiView, "operator_separator", "|") assert filter_guardians({"name|ne": "Marge"}) == [{"name": "Bob"}] assert filter_guardians({"name|in": ["Marge"]}) == [{"name": "Marge"}] def test_nested_filter(self, filter_guardians, client): assert filter_guardians({"children.name": "Bart"}) == [{"name": "Marge"}] assert filter_guardians({"children.name": "Gene"}) == [{"name": "Bob"}] def test_bad_json(self, get): get("/guardians/?filters=notjson", expected_status_code=400) def test_column_does_not_exist(self, filter_guardians): filter_guardians({"nope": "fail"}, expected_status_code=400) filter_guardians({"nope.nested": "fail"}, expected_status_code=400) filter_guardians({"children.nope": "fail"}, expected_status_code=400) @pytest.mark.usefixtures("simpsons", "belchers") class TestSort: def test_sort(self, get, marge, bart, maggie, lisa): assert get(f"/guardians/{marge.id}/children/?sort=name") == [ {"name": bart.name}, {"name": lisa.name}, {"name": maggie.name}, ] assert get(f"/guardians/{marge.id}/children/?sort=age") == [ {"name": maggie.name}, {"name": lisa.name}, {"name": bart.name}, ] def test_sort_asc(self, get, marge, maggie, lisa, bart): assert get(f"/guardians/{marge.id}/children/?sort=age__asc") == [ {"name": maggie.name}, {"name": lisa.name}, {"name": bart.name}, ] assert get( f"/guardians/{marge.id}/children/?sort=name__asc", ) == [{"name": bart.name}, {"name": lisa.name}, {"name": maggie.name}] def test_sort_desc(self, get, marge, lisa, maggie, bart): assert get( f"/guardians/{marge.id}/children/?sort=age__desc", ) == [{"name": bart.name}, {"name": lisa.name}, {"name": maggie.name}] assert get( f"/guardians/{marge.id}/children/?sort=name__desc", ) == [{"name": maggie.name}, {"name": lisa.name}, {"name": bart.name}] def test_nested_sort(self, get): assert get(f"/guardians/?sort=family.surname") == [ {"name": "Bob"}, {"name": "Marge"}, ] def test_bad_sort(self, get): get(f"/guardians/?sort=name__fail", expected_status_code=400) get(f"/guardians/?sort=fail", expected_status_code=400) get(f"/guardians/?sort=family.fail", expected_status_code=400) get(f"/guardians/?sort=double.fail", expected_status_code=400) def test_change_operator_separator( self, get, monkeypatch, marge, lisa, bart, maggie ): monkeypatch.setattr(BaseApiView, "operator_separator", "|") assert get( f"/guardians/{marge.id}/children/?sort=age|desc", ) == [{"name": bart.name}, {"name": lisa.name}, {"name": maggie.name}] assert get( f"/guardians/{marge.id}/children/?sort=name|desc", ) == [{"name": maggie.name}, {"name": lisa.name}, {"name": bart.name}] @pytest.mark.usefixtures("simpsons", "belchers") class TestSearch: def test_search(self, get, marge): assert get(f"/guardians/?search=marge") == [{"name": "Marge"}] assert get(f"/guardians/?search=nobody") == [] assert get(f"/guardians/{marge.id}/children/?search=bart") == [{"name": "Bart"}] assert get(f"/guardians/{marge.id}/children/?search=nope") == [] def test_unsupported_search(self, get, marge, bart, monkeypatch):
monkeypatch.setattr(GuardianApiView, "searchable_columns", [])
7
2023-11-07 03:44:49+00:00
4k
BrianPugh/cyclopts
tests/test_resolve.py
[ { "identifier": "DocstringError", "path": "cyclopts/exceptions.py", "snippet": "class DocstringError(Exception):\n \"\"\"The docstring either has a syntax error, or inconsistency with the function signature.\"\"\"" }, { "identifier": "Group", "path": "cyclopts/group.py", "snippet": "class Group:\n name: str = \"\"\n\n help: str = \"\"\n\n # All below parameters are keyword-only\n _show: Optional[bool] = field(default=None, alias=\"show\", kw_only=True)\n\n _sort_key: Any = field(\n default=None,\n alias=\"sort_key\",\n converter=lambda x: NO_USER_SORT_KEY if x is None else x,\n )\n\n converter: Optional[Callable] = field(default=None, kw_only=True)\n\n validator: Tuple[Callable, ...] = field(\n default=None,\n converter=lambda x: cast(Tuple[Callable, ...], to_tuple_converter(x)),\n kw_only=True,\n )\n\n default_parameter: Optional[\"Parameter\"] = field(\n default=None,\n validator=_group_default_parameter_must_be_none,\n kw_only=True,\n )\n\n def __str__(self):\n return self.name\n\n @property\n def show(self):\n return bool(self.name) if self._show is None else self._show\n\n @show.setter\n def show(self, value):\n self._show = value\n\n @property\n def sort_key(self):\n return None if self._sort_key is NO_USER_SORT_KEY else self._sort_key\n\n @sort_key.setter\n def sort_key(self, value):\n self._sort_key = value\n\n @classmethod\n def create_default_arguments(cls):\n return cls(\"Arguments\")\n\n @classmethod\n def create_default_parameters(cls):\n return cls(\"Parameters\")\n\n @classmethod\n def create_default_commands(cls):\n return cls(\"Commands\")\n\n @classmethod\n def create_ordered(cls, *args, sort_key=None, **kwargs):\n \"\"\"Create a group with a globally incremented :attr:`~Group.sort_key`.\n\n Used to create a group that will be displayed **after** a previously declared :meth:`Group.create_ordered` group on the help-page.\n\n If a :attr:`~Group.sort_key` is provided, it is **prepended** to the globally incremented counter value (i.e. has priority during sorting).\n \"\"\"\n count = next(_sort_key_counter)\n if sort_key is None:\n sort_key = (NO_USER_SORT_KEY, count)\n elif is_iterable(sort_key):\n sort_key = (tuple(sort_key), count)\n else:\n sort_key = (sort_key, count)\n return cls(*args, sort_key=sort_key, **kwargs)" }, { "identifier": "Parameter", "path": "cyclopts/parameter.py", "snippet": "class Parameter:\n \"\"\"Cyclopts configuration for individual function parameters.\"\"\"\n\n # All documentation has been moved to ``docs/api.rst`` for greater control with attrs.\n\n name: Tuple[str, ...] = field(\n default=None,\n converter=lambda x: cast(Tuple[str, ...], to_tuple_converter(x)),\n )\n\n converter: Callable = field(default=None, converter=attrs.converters.default_if_none(convert))\n\n validator: Tuple[Callable, ...] = field(\n default=(),\n converter=lambda x: cast(Tuple[Callable, ...], to_tuple_converter(x)),\n )\n\n negative: Union[None, Tuple[str, ...]] = field(default=None, converter=optional_to_tuple_converter)\n\n group: Tuple[Union[Group, str], ...] = field(default=None, converter=to_tuple_converter, hash=False)\n\n parse: bool = field(default=None, converter=attrs.converters.default_if_none(True))\n\n _show: Optional[bool] = field(default=None, alias=\"show\")\n\n show_default: Optional[bool] = field(default=None)\n\n show_choices: bool = field(default=None, converter=attrs.converters.default_if_none(True))\n\n help: Optional[str] = field(default=None)\n\n show_env_var: bool = field(default=None, converter=attrs.converters.default_if_none(True))\n\n env_var: Tuple[str, ...] = field(\n default=None,\n converter=lambda x: cast(Tuple[str, ...], to_tuple_converter(x)),\n )\n\n negative_bool: Tuple[str, ...] = field(\n default=None,\n converter=_negative_converter((\"--no-\",)),\n validator=_double_hyphen_validator,\n )\n\n negative_iterable: Tuple[str, ...] = field(\n default=None,\n converter=_negative_converter((\"--empty-\",)),\n validator=_double_hyphen_validator,\n )\n\n required: Optional[bool] = field(default=None)\n\n allow_leading_hyphen: bool = field(default=False)\n\n # Populated by the record_attrs_init_args decorator.\n _provided_args: Tuple[str] = field(default=(), init=False, eq=False)\n\n @property\n def show(self):\n return self._show if self._show is not None else self.parse\n\n def get_negatives(self, type_, *names: str) -> Tuple[str, ...]:\n type_ = get_origin(type_) or type_\n\n if self.negative is not None:\n return self.negative\n elif type_ not in (bool, list, set):\n return ()\n\n out = []\n for name in names:\n if name.startswith(\"--\"):\n name = name[2:]\n elif name.startswith(\"-\"):\n # Do not support automatic negation for short flags.\n continue\n else:\n # Should never reach here.\n raise NotImplementedError(\"All parameters should have started with '-' or '--'.\")\n\n negative_prefixes = self.negative_bool if type_ is bool else self.negative_iterable\n\n for negative_prefix in negative_prefixes:\n out.append(f\"{negative_prefix}{name}\")\n return tuple(out)\n\n def __repr__(self):\n \"\"\"Only shows non-default values.\"\"\"\n content = \", \".join(\n [\n f\"{a.alias}={getattr(self, a.name)!r}\"\n for a in self.__attrs_attrs__ # pyright: ignore[reportGeneralTypeIssues]\n if a.alias in self._provided_args\n ]\n )\n return f\"{type(self).__name__}({content})\"\n\n @classmethod\n def combine(cls, *parameters: Optional[\"Parameter\"]) -> \"Parameter\":\n \"\"\"Returns a new Parameter with values of ``parameters``.\n\n Parameters\n ----------\n `*parameters`: Optional[Parameter]\n Parameters who's attributes override ``self`` attributes.\n Ordered from least-to-highest attribute priority.\n \"\"\"\n kwargs = {}\n for parameter in parameters:\n if parameter is None:\n continue\n for a in parameter.__attrs_attrs__: # pyright: ignore[reportGeneralTypeIssues]\n if a.init and a.alias in parameter._provided_args:\n kwargs[a.alias] = getattr(parameter, a.name)\n\n return cls(**kwargs)\n\n @classmethod\n def default(cls) -> \"Parameter\":\n \"\"\"Create a Parameter with all Cyclopts-default values.\n\n This is different than just :class:`Parameter` because the default\n values will be recorded and override all upstream parameter values.\n \"\"\"\n return cls(\n **{a.alias: a.default for a in cls.__attrs_attrs__ if a.init} # pyright: ignore[reportGeneralTypeIssues]\n )" }, { "identifier": "ResolvedCommand", "path": "cyclopts/resolve.py", "snippet": "class ResolvedCommand:\n command: Callable\n groups: List[Group]\n groups_iparams: List[Tuple[Group, List[inspect.Parameter]]]\n iparam_to_groups: ParameterDict\n iparam_to_cparam: ParameterDict\n name_to_iparam: Dict[str, inspect.Parameter]\n\n def __init__(\n self,\n f,\n app_parameter: Optional[Parameter] = None,\n group_arguments: Optional[Group] = None,\n group_parameters: Optional[Group] = None,\n parse_docstring: bool = True,\n ):\n \"\"\"\n ``app_parameter`` implicitly has the command-group parameter already resolved.\n\n Parameters\n ----------\n f: Callable\n Function to resolve annotated :class:`Parameters`.\n app_parameter:\n Default :class:`Parameter` to inherit configuration from.\n group_arguments: Optional[Group]\n Default :class:`Group` for positional-only arguments.\n group_parameters: Optional[Group]\n Default :class:`Group` for non-positional-only arguments.\n parse_docstring: bool\n Parse the docstring to populate Parameter ``help``, if not explicitly set.\n Disable for improved performance if ``help`` won't be used in the resulting :class:`Parameter`.\n \"\"\"\n if group_arguments is None:\n group_arguments = Group.create_default_arguments()\n if group_parameters is None:\n group_parameters = Group.create_default_parameters()\n\n self.command = f\n signature = inspect.signature(f)\n self.name_to_iparam = cast(Dict[str, inspect.Parameter], signature.parameters)\n\n # Get:\n # 1. Fully resolved and created Groups.\n # 2. A mapping of inspect.Parameter to those Group objects.\n self.groups, self.iparam_to_groups = _resolve_groups(f, app_parameter, group_arguments, group_parameters)\n\n # Fully Resolve each Cyclopts Parameter\n self.iparam_to_cparam = ParameterDict()\n iparam_to_docstring_cparam = _resolve_docstring(f) if parse_docstring else ParameterDict()\n for iparam, groups in self.iparam_to_groups.items():\n if iparam.kind in (iparam.POSITIONAL_ONLY, iparam.VAR_POSITIONAL):\n # Name is only used for help-string\n names = [iparam.name.upper()]\n else:\n names = [\"--\" + iparam.name.replace(\"_\", \"-\")]\n\n default_name_parameter = Parameter(name=names)\n\n cparam = get_hint_parameter(\n iparam,\n app_parameter,\n *(x.default_parameter for x in groups),\n iparam_to_docstring_cparam.get(iparam),\n default_name_parameter,\n Parameter(required=iparam.default is iparam.empty),\n )[1]\n self.iparam_to_cparam[iparam] = cparam\n\n self.bind = signature.bind_partial if _has_unparsed_parameters(f, app_parameter) else signature.bind\n\n # Create a convenient group-to-iparam structure\n self.groups_iparams = [\n (\n group,\n [iparam for iparam, groups in self.iparam_to_groups.items() if group in groups],\n )\n for group in self.groups\n ]" } ]
import sys import pytest from cyclopts.exceptions import DocstringError from typing_extensions import Annotated from typing import Annotated from cyclopts import Group, Parameter from cyclopts.resolve import ResolvedCommand
2,676
if sys.version_info < (3, 9): else: def test_resolve_docstring(): def foo(bar): """ Parameters ---------- bar Bar Docstring. """ pass res = ResolvedCommand(foo) cparam = res.iparam_to_cparam[res.name_to_iparam["bar"]] assert cparam.help == "Bar Docstring." def test_resolve_docstring_parameter_priority():
if sys.version_info < (3, 9): else: def test_resolve_docstring(): def foo(bar): """ Parameters ---------- bar Bar Docstring. """ pass res = ResolvedCommand(foo) cparam = res.iparam_to_cparam[res.name_to_iparam["bar"]] assert cparam.help == "Bar Docstring." def test_resolve_docstring_parameter_priority():
def foo(bar: Annotated[str, Parameter(help="This has priority.")]):
2
2023-11-03 02:24:25+00:00
4k
RoboFlamingo/RoboFlamingo
robot_flamingo/train/train_calvin.py
[ { "identifier": "get_data", "path": "robot_flamingo/data/data.py", "snippet": "def get_data(args, image_processor, tokenizer, dataset_type, epoch=0):\n return get_dataset_fn(dataset_type)(\n args, image_processor=image_processor, epoch=epoch, tokenizer=tokenizer\n )" }, { "identifier": "create_model_and_transforms", "path": "robot_flamingo/models/factory.py", "snippet": "def get_transforms(\n clip_vision_encoder_path: str = \"ViT-L-14\",\n clip_vision_encoder_pretrained: str = \"openai\",\n tokenizer_path: str = \"path_to/llama-7b-hf-jxu124\",\n use_local_files: bool = False,\n):\ndef create_model_and_transforms(\n clip_vision_encoder_path: str,\n clip_vision_encoder_pretrained: str,\n lang_encoder_path: str,\n tokenizer_path: str,\n cross_attn_every_n_layers: int = 1,\n use_local_files: bool = False,\n decoder_layers_attr_name: str = None,\n # this is the window size sampled from the episode\n window_size: int = 32,\n freeze_embed: bool = False,\n train_params = -1,\n use_gripper=False,\n use_state=False,\n last_action=False,\n fusion_mode='',\n pad_length=-1,\n debug=False,\n sep_resampler=False,\n sep_lm_head=False,\n unfreeze_vit=False,\n return_feature=False,\n multi_step_action=1,\n llm_name='llama_9b',\n pooling='max',\n residual=False,\n tcp_rel=False,\n replan=-1,\n decoder_type='lstm',\n hidden_size=None,\n freeze_sampler=False,\n fwd_pred=False, \n fwd_pred_hand=False,\n no_image_patch=False,\n global_latent=1,\n refresh=-1,\n **flamingo_kwargs,\n):\n def get_input_embeddings(self):\n def set_input_embeddings(self, new_embeddings):\n class EmbeddingFnMixin:" } ]
import argparse import copy import glob import os import random import numpy as np import torch import wandb from collections import OrderedDict from huggingface_hub import hf_hub_download from torch.nn.parallel import DistributedDataParallel as DDP from robot_flamingo.data.data import get_data from open_flamingo.train.distributed import init_distributed_device, world_info_from_env from train_utils import get_checkpoint, train_one_epoch_calvin, train_one_epoch_calvin_diff, train_one_epoch_calvin_cotrain, train_one_epoch_calvin_two_way, \ get_ckpt_name, get_ckpt_name_pattern from torch.distributed.elastic.multiprocessing.errors import record from transformers import ( get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_linear_schedule_with_warmup, ) from robot_flamingo.models.factory import create_model_and_transforms, mpt_dict
2,852
default=False, action="store_true" ) parser.add_argument( "--debug", default=False, action="store_true" ) parser.add_argument( "--sep_lm_head", default=False, action="store_true" ) parser.add_argument( "--clip_state", default=False, action="store_true" ) parser.add_argument( "--unfreeze_vit", default=False, action="store_true" ) parser.add_argument( "--text_aug", default=False, action="store_true" ) parser.add_argument( "--residual", default=False, action="store_true" ) parser.add_argument( "--tcp_rel", default=False, action="store_true" ) parser.add_argument( "--dif_ws", default=False, action="store_true" ) parser.add_argument( "--partial_data", default=False, action="store_true" ) parser.add_argument( "--freeze_sampler", default=False, action="store_true" ) parser.add_argument( "--fwd_pred", default=False, action="store_true" ) parser.add_argument( "--fwd_pred_hand", default=False, action="store_true" ) parser.add_argument( "--no_pretrain", default=False, action="store_true" ) parser.add_argument( "--real_data", default=False, action="store_true" ) parser.add_argument( "--no_image_patch", default=False, action="store_true" ) # Co-Train settings parser.add_argument( "--cotrain", default=False, action="store_true" ) parser.add_argument("--batch_size_vl", type=int, default=20) parser.add_argument("--vl_task_weights", type=float, default=0.005) parser.add_argument("--global_latent", type=int, default=1) parser.add_argument("--save_every_iter", type=int, default=-1) # For GPT decoder parser.add_argument("--hidden_size", type=int, default=768) parser.add_argument("--decoder_type", type=str, default='lstm') parser.add_argument("--min_window_size", type=int, default=12) parser.add_argument("--max_window_size", type=int, default=24) parser.add_argument("--llm_name", type=str, default='llama_9b') parser.add_argument("--pooling", type=str, default='max') parser.add_argument("--multi_step_action", type=int, default=1, help="multiple step action prediction") args = parser.parse_args() if args.eval_hist_size == -1: args.eval_hist_size = args.window_size if args.head_type == "diffusion": args.eval_hist_size = args.n_obs_steps if args.tcp_rel: args.clip_state = True if args.save_checkpoints_to_wandb and not args.report_to_wandb: raise ValueError("save_checkpoints_to_wandb requires report_to_wandb") if args.offline: os.environ["WANDB_MODE"] = "offline" os.environ["TRANSFORMERS_OFFLINE"] = "1" args.local_rank, args.rank, args.world_size = world_info_from_env() device_id = init_distributed_device(args) print("device_id: ", device_id) random_seed(args.seed)
""" Main training script """ def random_seed(seed=42, rank=0): torch.manual_seed(seed + rank) np.random.seed(seed + rank) random.seed(seed + rank) @record def main(): parser = argparse.ArgumentParser() parser.add_argument("--vision_encoder_path", default="ViT-L-14", type=str) parser.add_argument("--vision_encoder_pretrained", default="openai", type=str) parser.add_argument("--lm_path", default="facebook/opt-1.3b", type=str) parser.add_argument( "--tokenizer_path", default="facebook/opt-30b", type=str, help="path to tokenizer", ) parser.add_argument( "--cross_attn_every_n_layers", type=int, default=4, help="how often to add a cross-attention layer after each transformer layer", ) parser.add_argument( "--run_name", type=str, default="RobotFlamingo", help="used to name saving directory and wandb run", ) parser.add_argument("--use_media_placement_augmentation", action="store_true") parser.add_argument("--offline", action="store_true") parser.add_argument("--num_epochs", type=int, default=1) parser.add_argument("--window_size", type=int, default=32) parser.add_argument( "--logging_steps", type=int, default=100, help="log loss every n steps" ) # Sum of gradient optimization batch size parser.add_argument("--batch_size_calvin", type=int, default=1) parser.add_argument("--gradient_accumulation_steps", type=int, default=1) parser.add_argument("--openflamingo_checkpoint", type=str, default="") parser.add_argument( "--resume_from_checkpoint", type=str, help="path to checkpoint to resume from, this should contain model, optimizer, and lr_scheduler states", default=None, ) parser.add_argument( "--delete_previous_checkpoint", action="store_true", help="delete previous checkpoint when saving new checkpoint", ) parser.add_argument("--seed", type=int, default=42) parser.add_argument("--learning_rate", default=1e-4, type=float) # 1e-4 parser.add_argument( "--lr_scheduler", default="constant", type=str, help="constant, linear, or cosine", ) parser.add_argument( "--calvin_dataset", type=str, help="path to calvin_dataset", ) parser.add_argument("--loss_multiplier_calvin", type=float, default=1.0) parser.add_argument("--warmup_steps", default=5000, type=int) parser.add_argument("--local-rank", default=0, type=int) parser.add_argument("--weight_decay", default=0.1, type=float) # hot fix for torch.distributed.launch # parser.add_argument("--local-rank", type=int, default=1) parser.add_argument( "--precision", choices=["amp_bf16", "amp_bfloat16", "bf16", "fp16", "fp32"], default="fp32", help="Floating point precision.", ) # data args parser.add_argument("--workers", type=int, default=1) parser.add_argument("--train_num_samples_calvin", type=int, default=100) parser.add_argument("--dataset_resampled", action="store_true") # distributed training args parser.add_argument( "--dist-url", default="env://", type=str, help="url used to set up distributed training", ) parser.add_argument( "--dist-backend", default="nccl", type=str, help="distributed backend" ) parser.add_argument( "--horovod", default=False, action="store_true", help="Use horovod for distributed training.", ) parser.add_argument( "--no-set-device-rank", default=False, action="store_true", help="Don't set device index from local rank (when CUDA_VISIBLE_DEVICES restricted to one per proc).", ) # wandb args parser.add_argument("--report_to_wandb", default=False, action="store_true") parser.add_argument( "--wandb_project", type=str, ) parser.add_argument( "--wandb_entity", type=str, ) parser.add_argument( "--save_checkpoints_to_wandb", default=False, action="store_true", help="save checkpoints to wandb", ) parser.add_argument( "--freeze_embed", default=False, action="store_true", help="freeze the parameters of embedding layer", ) parser.add_argument( "--use_gripper", default=False, action="store_true", help="whether to use gripper image as input", ) parser.add_argument( "--use_state", default=False, action="store_true", help="whether to use low-dim state as input", ) parser.add_argument( "--fusion_mode", default="pre", type=str, help="pre or post to fusion multi vision info", ) parser.add_argument("--hist_window", type=int, default=1) # input history window size for the model # history window size when evaluating, for FC head equals to hist_window, for LSTM head means refresh frequency parser.add_argument("--eval_hist_size", type=int, default=-1) parser.add_argument( "--sep_resampler", default=False, action="store_true", help="whether use separate resamplers for third party and gripper camera", ) parser.add_argument("--train_params", type=int, default=-1) parser.add_argument('--rgb_pad', type=int, default=-1) parser.add_argument('--gripper_pad', type=int, default=-1) parser.add_argument('--n_timesteps', type=int, default=150, help="diffusion time steps") parser.add_argument( "--predict_epsilon", default=False, action="store_true", help="whether diffusion model should predict epsilon", ) parser.add_argument('--head_type', type=str, default="lstm") # diffusion parser.add_argument( "--from_scratch", default=False, action="store_true", help="whether to train the model from scratch", ) parser.add_argument("--n_obs_steps", default=6, type=int) parser.add_argument("--diff_horizon", default=32, type=int) parser.add_argument( "--last_action", default=False, action="store_true", help="whether using last action as input", ) parser.add_argument( "--use_hist", default=False, action="store_true" ) parser.add_argument( "--traj_cons", default=False, action="store_true" ) parser.add_argument( "--debug", default=False, action="store_true" ) parser.add_argument( "--sep_lm_head", default=False, action="store_true" ) parser.add_argument( "--clip_state", default=False, action="store_true" ) parser.add_argument( "--unfreeze_vit", default=False, action="store_true" ) parser.add_argument( "--text_aug", default=False, action="store_true" ) parser.add_argument( "--residual", default=False, action="store_true" ) parser.add_argument( "--tcp_rel", default=False, action="store_true" ) parser.add_argument( "--dif_ws", default=False, action="store_true" ) parser.add_argument( "--partial_data", default=False, action="store_true" ) parser.add_argument( "--freeze_sampler", default=False, action="store_true" ) parser.add_argument( "--fwd_pred", default=False, action="store_true" ) parser.add_argument( "--fwd_pred_hand", default=False, action="store_true" ) parser.add_argument( "--no_pretrain", default=False, action="store_true" ) parser.add_argument( "--real_data", default=False, action="store_true" ) parser.add_argument( "--no_image_patch", default=False, action="store_true" ) # Co-Train settings parser.add_argument( "--cotrain", default=False, action="store_true" ) parser.add_argument("--batch_size_vl", type=int, default=20) parser.add_argument("--vl_task_weights", type=float, default=0.005) parser.add_argument("--global_latent", type=int, default=1) parser.add_argument("--save_every_iter", type=int, default=-1) # For GPT decoder parser.add_argument("--hidden_size", type=int, default=768) parser.add_argument("--decoder_type", type=str, default='lstm') parser.add_argument("--min_window_size", type=int, default=12) parser.add_argument("--max_window_size", type=int, default=24) parser.add_argument("--llm_name", type=str, default='llama_9b') parser.add_argument("--pooling", type=str, default='max') parser.add_argument("--multi_step_action", type=int, default=1, help="multiple step action prediction") args = parser.parse_args() if args.eval_hist_size == -1: args.eval_hist_size = args.window_size if args.head_type == "diffusion": args.eval_hist_size = args.n_obs_steps if args.tcp_rel: args.clip_state = True if args.save_checkpoints_to_wandb and not args.report_to_wandb: raise ValueError("save_checkpoints_to_wandb requires report_to_wandb") if args.offline: os.environ["WANDB_MODE"] = "offline" os.environ["TRANSFORMERS_OFFLINE"] = "1" args.local_rank, args.rank, args.world_size = world_info_from_env() device_id = init_distributed_device(args) print("device_id: ", device_id) random_seed(args.seed)
args.lm_path = mpt_dict[args.llm_name]["lang_encoder_path"]
1
2023-11-02 01:36:23+00:00
4k
XinyuanLiao/ComplexNN
complexNN/nn.py
[ { "identifier": "complexRelu", "path": "complexNN/functional.py", "snippet": "def complexRelu(inp):\n return torch.complex(relu(inp.real), relu(inp.imag))" }, { "identifier": "complexGelu", "path": "complexNN/functional.py", "snippet": "def complexGelu(inp):\n return torch.complex(gelu(inp.real), gelu(inp.imag))" }, { "identifier": "complexTanh", "path": "complexNN/functional.py", "snippet": "def complexTanh(inp):\n return torch.complex(tanh(inp.real), tanh(inp.imag))" }, { "identifier": "complexSigmoid", "path": "complexNN/functional.py", "snippet": "def complexSigmoid(inp):\n return torch.complex(sigmoid(inp.real), sigmoid(inp.imag))" }, { "identifier": "complexMaxPool2d", "path": "complexNN/functional.py", "snippet": "def complexMaxPool2d(inp, kernel_size, stride=None, padding=0, dilation=1, ceil_mode=False, return_indices=False):\n \"\"\"\n copy from https://github.com/wavefrontshaping/complexPyTorch\n \"\"\"\n absolute_value, indices = max_pool2d(inp.abs(), kernel_size=kernel_size, stride=stride, padding=padding,\n dilation=dilation, ceil_mode=ceil_mode, return_indices=True)\n absolute_value = absolute_value.type(torch.complex64)\n angle = torch.atan2(inp.imag, inp.real)\n angle = _retrieve_elements_from_indices(angle, indices)\n return absolute_value * (\n torch.cos(angle).type(torch.complex64)\n + 1j * torch.sin(angle).type(torch.complex64)\n )" }, { "identifier": "complexAvgPool2d", "path": "complexNN/functional.py", "snippet": "def complexAvgPool2d(inp, *args, **kwargs):\n \"\"\"\n copy from https://github.com/wavefrontshaping/complexPyTorch\n \"\"\"\n absolute_value_real = avg_pool2d(inp.real, *args, **kwargs)\n absolute_value_imag = avg_pool2d(inp.imag, *args, **kwargs)\n\n return absolute_value_real.type(torch.complex64) + 1j * absolute_value_imag.type(\n torch.complex64\n )" }, { "identifier": "complexAvgPool1d", "path": "complexNN/functional.py", "snippet": "def complexAvgPool1d(inp, *args, **kwargs):\n absolute_value_real = avg_pool1d(inp.real, *args, **kwargs)\n absolute_value_imag = avg_pool1d(inp.imag, *args, **kwargs)\n\n return absolute_value_real.type(torch.complex64) + 1j * absolute_value_imag.type(\n torch.complex64\n )" }, { "identifier": "complexDropout", "path": "complexNN/functional.py", "snippet": "def complexDropout(inp, p=0.5, training=True):\n \"\"\"\n copy from https://github.com/wavefrontshaping/complexPyTorch\n \"\"\"\n mask = torch.ones(*inp.shape, dtype=torch.float32, device=inp.device)\n mask = dropout(mask, p, training) * 1 / (1 - p)\n mask.type(inp.dtype)\n return mask * inp" }, { "identifier": "complexDropout2d", "path": "complexNN/functional.py", "snippet": "def complexDropout2d(inp, p=0.5, training=True):\n \"\"\"\n copy from https://github.com/wavefrontshaping/complexPyTorch\n \"\"\"\n mask = torch.ones(*inp.shape, dtype=torch.float32, device=inp.device)\n mask = dropout2d(mask, p, training) * 1 / (1 - p)\n mask.type(inp.dtype)\n return mask * inp" }, { "identifier": "complexElu", "path": "complexNN/functional.py", "snippet": "def complexElu(inp):\n return torch.complex(elu(inp.real), elu(inp.imag))" }, { "identifier": "complexLeakyRelu", "path": "complexNN/functional.py", "snippet": "def complexLeakyRelu(inp):\n return torch.complex(leaky_relu(inp.real), leaky_relu(inp.imag))" }, { "identifier": "complexSoftmax", "path": "complexNN/functional.py", "snippet": "def complexSoftmax(inp):\n return torch.complex(softmax(inp.real), softmax(inp.imag))" } ]
import numpy as np import torch import torch.nn as nn from complexNN.functional import complexRelu, complexGelu, complexTanh, complexSigmoid, complexMaxPool2d, \ complexAvgPool2d, complexAvgPool1d, complexDropout, complexDropout2d, complexElu, complexLeakyRelu, complexSoftmax
2,021
return complexSigmoid(inp) class cBatchNorm1d(nn.Module): def __init__(self, num_features, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True): super().__init__() self.real_bn = nn.BatchNorm1d(num_features, eps=eps, momentum=momentum, affine=affine, track_running_stats=track_running_stats) self.imag_bn = nn.BatchNorm1d(num_features, eps=eps, momentum=momentum, affine=affine, track_running_stats=track_running_stats) def forward(self, inp): real_input = torch.real(inp) imag_input = torch.imag(inp) real_output = self.real_bn(real_input) imag_output = self.imag_bn(imag_input) return torch.complex(real_output, imag_output) class cBatchNorm2d(nn.Module): def __init__(self, num_features, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True): super().__init__() self.real_bn = nn.BatchNorm2d(num_features, eps=eps, momentum=momentum, affine=affine, track_running_stats=track_running_stats) self.imag_bn = nn.BatchNorm2d(num_features, eps=eps, momentum=momentum, affine=affine, track_running_stats=track_running_stats) def forward(self, inp): real_input = torch.real(inp) imag_input = torch.imag(inp) real_output = self.real_bn(real_input) imag_output = self.imag_bn(imag_input) return torch.complex(real_output, imag_output) class cBatchNorm3d(nn.Module): def __init__(self, num_features, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True): super().__init__() self.real_bn = nn.BatchNorm3d(num_features, eps=eps, momentum=momentum, affine=affine, track_running_stats=track_running_stats) self.imag_bn = nn.BatchNorm3d(num_features, eps=eps, momentum=momentum, affine=affine, track_running_stats=track_running_stats) def forward(self, inp): real_input = torch.real(inp) imag_input = torch.imag(inp) real_output = self.real_bn(real_input) imag_output = self.imag_bn(imag_input) return torch.complex(real_output, imag_output) class cLayerNorm(nn.Module): def __init__(self, num_features, eps=1e-5, elementwise_affine=False): super().__init__() self.real_norm = nn.LayerNorm(num_features, eps=eps, elementwise_affine=elementwise_affine) self.imag_norm = nn.LayerNorm(num_features, eps=eps, elementwise_affine=elementwise_affine) def forward(self, inp): real_input = torch.real(inp) imag_input = torch.imag(inp) real_output = self.real_norm(real_input) imag_output = self.imag_norm(imag_input) return torch.complex(real_output, imag_output) class cDropout(nn.Module): """ copy from https://github.com/wavefrontshaping/complexPyTorch """ def __init__(self, p=0.5): super().__init__() self.p = p def forward(self, inp): if self.training: return complexDropout(inp, self.p) else: return inp class cDropout2d(nn.Module): """ copy from https://github.com/wavefrontshaping/complexPyTorch """ def __init__(self, p=0.5): super().__init__() self.p = p def forward(self, inp): if self.training: return complexDropout2d(inp, self.p) else: return inp class cMaxPool2d(nn.Module): """ copy from https://github.com/wavefrontshaping/complexPyTorch """ def __init__(self, kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False): super().__init__() self.kernel_size = kernel_size self.stride = stride self.padding = padding self.dilation = dilation self.ceil_mode = ceil_mode self.return_indices = return_indices def forward(self, inp):
class cRelu(nn.Module): @staticmethod def forward(inp): return complexRelu(inp) class cElu(nn.Module): @staticmethod def forward(inp): return complexElu(inp) class cLeakyRelu(nn.Module): @staticmethod def forward(inp): return complexLeakyRelu(inp) class cSoftmax(nn.Module): @staticmethod def forward(inp): return complexSoftmax(inp) class cGelu(nn.Module): @staticmethod def forward(inp): return complexGelu(inp) class cTanh(nn.Module): @staticmethod def forward(inp): return complexTanh(inp) class cSigmoid(nn.Module): @staticmethod def forward(inp): return complexSigmoid(inp) class cBatchNorm1d(nn.Module): def __init__(self, num_features, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True): super().__init__() self.real_bn = nn.BatchNorm1d(num_features, eps=eps, momentum=momentum, affine=affine, track_running_stats=track_running_stats) self.imag_bn = nn.BatchNorm1d(num_features, eps=eps, momentum=momentum, affine=affine, track_running_stats=track_running_stats) def forward(self, inp): real_input = torch.real(inp) imag_input = torch.imag(inp) real_output = self.real_bn(real_input) imag_output = self.imag_bn(imag_input) return torch.complex(real_output, imag_output) class cBatchNorm2d(nn.Module): def __init__(self, num_features, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True): super().__init__() self.real_bn = nn.BatchNorm2d(num_features, eps=eps, momentum=momentum, affine=affine, track_running_stats=track_running_stats) self.imag_bn = nn.BatchNorm2d(num_features, eps=eps, momentum=momentum, affine=affine, track_running_stats=track_running_stats) def forward(self, inp): real_input = torch.real(inp) imag_input = torch.imag(inp) real_output = self.real_bn(real_input) imag_output = self.imag_bn(imag_input) return torch.complex(real_output, imag_output) class cBatchNorm3d(nn.Module): def __init__(self, num_features, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True): super().__init__() self.real_bn = nn.BatchNorm3d(num_features, eps=eps, momentum=momentum, affine=affine, track_running_stats=track_running_stats) self.imag_bn = nn.BatchNorm3d(num_features, eps=eps, momentum=momentum, affine=affine, track_running_stats=track_running_stats) def forward(self, inp): real_input = torch.real(inp) imag_input = torch.imag(inp) real_output = self.real_bn(real_input) imag_output = self.imag_bn(imag_input) return torch.complex(real_output, imag_output) class cLayerNorm(nn.Module): def __init__(self, num_features, eps=1e-5, elementwise_affine=False): super().__init__() self.real_norm = nn.LayerNorm(num_features, eps=eps, elementwise_affine=elementwise_affine) self.imag_norm = nn.LayerNorm(num_features, eps=eps, elementwise_affine=elementwise_affine) def forward(self, inp): real_input = torch.real(inp) imag_input = torch.imag(inp) real_output = self.real_norm(real_input) imag_output = self.imag_norm(imag_input) return torch.complex(real_output, imag_output) class cDropout(nn.Module): """ copy from https://github.com/wavefrontshaping/complexPyTorch """ def __init__(self, p=0.5): super().__init__() self.p = p def forward(self, inp): if self.training: return complexDropout(inp, self.p) else: return inp class cDropout2d(nn.Module): """ copy from https://github.com/wavefrontshaping/complexPyTorch """ def __init__(self, p=0.5): super().__init__() self.p = p def forward(self, inp): if self.training: return complexDropout2d(inp, self.p) else: return inp class cMaxPool2d(nn.Module): """ copy from https://github.com/wavefrontshaping/complexPyTorch """ def __init__(self, kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False): super().__init__() self.kernel_size = kernel_size self.stride = stride self.padding = padding self.dilation = dilation self.ceil_mode = ceil_mode self.return_indices = return_indices def forward(self, inp):
return complexMaxPool2d(
4
2023-11-02 04:52:23+00:00
4k
sanmusen214/BAAH
modules/configs/MyConfig.py
[ { "identifier": "defaultUserDict", "path": "modules/configs/defaultSettings.py", "snippet": "" }, { "identifier": "configname2screenshotname", "path": "modules/configs/settingMaps.py", "snippet": "def configname2screenshotname(configfilename):\n \"\"\"\n 根据config文件名,返回截图文件名\n config文件名包含后缀不包含路径\n \"\"\"\n screenshotfilehash = hashlib.sha1(configfilename.encode('utf-8')).hexdigest()\n # 如果长度大于8,截取前8位\n if len(screenshotfilehash) > 8:\n screenshotfilehash = screenshotfilehash[:8]\n # 如果长度小于8,补0\n elif len(screenshotfilehash) < 8:\n screenshotfilehash = screenshotfilehash.zfill(8)\n return screenshotfilehash + \".png\"" } ]
import json import logging import os import time from modules.configs.defaultSettings import defaultUserDict, defaultSoftwareDict from modules.configs.settingMaps import configname2screenshotname
1,771
self.softwareconfigdict = {} # 软件的语言包 self.languagepackagedict = {} # 一次区服任务的config self.userconfigdict = {} # 一次区服任务运行的session self.sessiondict = {} # 读取软件的config self.parse_software_config(self.SOFTWARE_CONFIG_NAME) def parse_user_config(self, file_name): """ 读取config文件并解析 同时会清空sessiondict """ file_path = os.path.join(self.current_dir, self.USER_CONFIG_FOLDER, file_name) # 字典新值 self.userconfigdict = self._read_config_file(file_path) # 清空sessiondict self.sessiondict = {} # 检查缺失的配置 self._check_user_config() # 强制设置截图文件名为配置名 self.userconfigdict["SCREENSHOT_NAME"] = configname2screenshotname(file_name) # 检查截图文件夹路径里是否有DATA, 如果没有DATA,说明是1.1.x版本的配置,需要转换 if "DATA" not in self.userconfigdict["PIC_PATH"]: fromkey = defaultUserDict["PIC_PATH"]["m"]["from"] mapfunc = defaultUserDict["PIC_PATH"]["m"]["map"] self.userconfigdict["PIC_PATH"] = mapfunc(self.userconfigdict[fromkey]) # 输出 logging.debug("user config字典内容: "+ ",".join([k for k in self.userconfigdict])) def parse_software_config(self, file_name): """ 读取config文件并解析, 同时加载语言包 """ file_path = os.path.join(self.current_dir, self.SOFTWARE_CONFIG_FOLDER, file_name) # 字典新值 self.softwareconfigdict = self._read_config_file(file_path) # 检查缺失的配置 self._check_software_config() # 强制设定VERSION self.softwareconfigdict["NOWVERSION"] = self.NOWVERSION # 输出 logging.debug("software config字典内容: "+ ",".join([k for k in self.softwareconfigdict])) # 加载语言包 self.parse_language_package(self.softwareconfigdict["LANGUAGE"]+".json") def parse_language_package(self, file_name): """ 读取语言包文件并解析 """ file_path = os.path.join(self.current_dir, self.LANGUAGE_PACKAGE_FOLDER, file_name) # 字典新值 self.languagepackagedict = self._read_config_file(file_path) logging.debug("language package字典内容: "+ ",".join([k for k in self.languagepackagedict])) def _read_config_file(self, file_path): """ 读取文件,返回字典 """ try: with open(file_path, 'r', encoding="utf8") as f: dictconfig = json.load(f) logging.debug("读取{}文件成功, 读取了{}个配置".format(file_path, len(dictconfig))) return dictconfig except FileNotFoundError as e: logging.error(f'文件不存在: {file_path}, 以默认值创建') with open(file_path, 'w', encoding="utf8") as f: json.dump({}, f, indent=4, ensure_ascii=False) return {} except Exception as e: raise Exception(f'读取{file_path}文件时发生错误,请检查{file_path}文件: {str(e)}') def _fill_by_map_or_default(self, defaultmap, selfmap, key): """ 尝试用defaultmap里的map和default值填充某个key """ # 使用对应关系查找 if "m" in defaultmap[key]: mapdict = defaultmap[key]["m"] fromkey = mapdict["from"] mapfunc = mapdict["map"] if fromkey in selfmap: # 能用对应关系就用对应关系 selfmap[key] = mapfunc(selfmap[fromkey]) logging.warn("缺少{}配置,根据{}配置自动填充为{}".format(key, fromkey, selfmap[key])) else: # 对应关系的键不在,那就只能用默认值 logging.warn("缺少{}配置,使用默认值{}".format(key, defaultmap[key]["d"])) selfmap[key] = defaultmap[key]["d"] else: # 没有对应关系就只能默认值 logging.warn("缺少{}配置,使用默认值{}".format(key, defaultmap[key]["d"])) selfmap[key] = defaultmap[key]["d"] def _check_user_config(self): """ 检查用户的config内的值是否有缺少,如果有,按照对应关系查找,如果没有,就用默认值 """ # 先处理SERVER_TYPE if "SERVER_TYPE" not in self.userconfigdict: # 使用对应关系查找 mapdict = defaultUserDict["SERVER_TYPE"]["m"] fromkey = mapdict["from"] mapfunc = mapdict["map"] if fromkey in self.userconfigdict: self.userconfigdict["SERVER_TYPE"] = mapfunc(self.userconfigdict[fromkey]) else: self.userconfigdict["SERVER_TYPE"] = defaultUserDict["SERVER_TYPE"]["d"] for shouldKey in defaultUserDict: # 如果用户的config里没有这个值 if shouldKey not in self.userconfigdict: self._fill_by_map_or_default(defaultUserDict, self.userconfigdict, shouldKey) def _check_software_config(self): """ 检查软件的config内的值是否有缺少,如果有,按照对应关系查找,如果没有,就用默认值 """
# 程序入口应当先import这个类,然后调用parse_user_config方法解析该config实例 # 然后程序入口再import其他模块,在其他模块中import这个类,就可以直接使用这个类的实例了 class MyConfigger: """ 维护config字典,包含软件config,用户任务config,语言包 """ NOWVERSION="1.2.0" USER_CONFIG_FOLDER="./BAAH_CONFIGS" SOFTWARE_CONFIG_FOLDER="./DATA/CONFIGS" LANGUAGE_PACKAGE_FOLDER="./DATA/i18n" SOFTWARE_CONFIG_NAME="software_config.json" # 读取config这个py里面的配置 def __init__(self): self.current_dir = os.getcwd() # 软件的config self.softwareconfigdict = {} # 软件的语言包 self.languagepackagedict = {} # 一次区服任务的config self.userconfigdict = {} # 一次区服任务运行的session self.sessiondict = {} # 读取软件的config self.parse_software_config(self.SOFTWARE_CONFIG_NAME) def parse_user_config(self, file_name): """ 读取config文件并解析 同时会清空sessiondict """ file_path = os.path.join(self.current_dir, self.USER_CONFIG_FOLDER, file_name) # 字典新值 self.userconfigdict = self._read_config_file(file_path) # 清空sessiondict self.sessiondict = {} # 检查缺失的配置 self._check_user_config() # 强制设置截图文件名为配置名 self.userconfigdict["SCREENSHOT_NAME"] = configname2screenshotname(file_name) # 检查截图文件夹路径里是否有DATA, 如果没有DATA,说明是1.1.x版本的配置,需要转换 if "DATA" not in self.userconfigdict["PIC_PATH"]: fromkey = defaultUserDict["PIC_PATH"]["m"]["from"] mapfunc = defaultUserDict["PIC_PATH"]["m"]["map"] self.userconfigdict["PIC_PATH"] = mapfunc(self.userconfigdict[fromkey]) # 输出 logging.debug("user config字典内容: "+ ",".join([k for k in self.userconfigdict])) def parse_software_config(self, file_name): """ 读取config文件并解析, 同时加载语言包 """ file_path = os.path.join(self.current_dir, self.SOFTWARE_CONFIG_FOLDER, file_name) # 字典新值 self.softwareconfigdict = self._read_config_file(file_path) # 检查缺失的配置 self._check_software_config() # 强制设定VERSION self.softwareconfigdict["NOWVERSION"] = self.NOWVERSION # 输出 logging.debug("software config字典内容: "+ ",".join([k for k in self.softwareconfigdict])) # 加载语言包 self.parse_language_package(self.softwareconfigdict["LANGUAGE"]+".json") def parse_language_package(self, file_name): """ 读取语言包文件并解析 """ file_path = os.path.join(self.current_dir, self.LANGUAGE_PACKAGE_FOLDER, file_name) # 字典新值 self.languagepackagedict = self._read_config_file(file_path) logging.debug("language package字典内容: "+ ",".join([k for k in self.languagepackagedict])) def _read_config_file(self, file_path): """ 读取文件,返回字典 """ try: with open(file_path, 'r', encoding="utf8") as f: dictconfig = json.load(f) logging.debug("读取{}文件成功, 读取了{}个配置".format(file_path, len(dictconfig))) return dictconfig except FileNotFoundError as e: logging.error(f'文件不存在: {file_path}, 以默认值创建') with open(file_path, 'w', encoding="utf8") as f: json.dump({}, f, indent=4, ensure_ascii=False) return {} except Exception as e: raise Exception(f'读取{file_path}文件时发生错误,请检查{file_path}文件: {str(e)}') def _fill_by_map_or_default(self, defaultmap, selfmap, key): """ 尝试用defaultmap里的map和default值填充某个key """ # 使用对应关系查找 if "m" in defaultmap[key]: mapdict = defaultmap[key]["m"] fromkey = mapdict["from"] mapfunc = mapdict["map"] if fromkey in selfmap: # 能用对应关系就用对应关系 selfmap[key] = mapfunc(selfmap[fromkey]) logging.warn("缺少{}配置,根据{}配置自动填充为{}".format(key, fromkey, selfmap[key])) else: # 对应关系的键不在,那就只能用默认值 logging.warn("缺少{}配置,使用默认值{}".format(key, defaultmap[key]["d"])) selfmap[key] = defaultmap[key]["d"] else: # 没有对应关系就只能默认值 logging.warn("缺少{}配置,使用默认值{}".format(key, defaultmap[key]["d"])) selfmap[key] = defaultmap[key]["d"] def _check_user_config(self): """ 检查用户的config内的值是否有缺少,如果有,按照对应关系查找,如果没有,就用默认值 """ # 先处理SERVER_TYPE if "SERVER_TYPE" not in self.userconfigdict: # 使用对应关系查找 mapdict = defaultUserDict["SERVER_TYPE"]["m"] fromkey = mapdict["from"] mapfunc = mapdict["map"] if fromkey in self.userconfigdict: self.userconfigdict["SERVER_TYPE"] = mapfunc(self.userconfigdict[fromkey]) else: self.userconfigdict["SERVER_TYPE"] = defaultUserDict["SERVER_TYPE"]["d"] for shouldKey in defaultUserDict: # 如果用户的config里没有这个值 if shouldKey not in self.userconfigdict: self._fill_by_map_or_default(defaultUserDict, self.userconfigdict, shouldKey) def _check_software_config(self): """ 检查软件的config内的值是否有缺少,如果有,按照对应关系查找,如果没有,就用默认值 """
for shouldKey in defaultSoftwareDict:
0
2023-11-09 22:28:39+00:00
4k
QingruZhang/PASTA
evaluation/precompute.py
[ { "identifier": "data", "path": "evaluation/data.py", "snippet": "SUPPORTED_DATASETS = (\"counterfact\", \"winoventi\", \"biosbias\", \"mcrae\")\nROME_BASE_URL = \"https://rome.baulab.info/data/dsets\"\nCOUNTERFACT_URL = f\"{ROME_BASE_URL}/counterfact.json\"\nATTRIBUTE_SNIPPETS_URL = f\"{ROME_BASE_URL}/attribute_snippets.json\"\nTFIDF_IDF_URL = f\"{ROME_BASE_URL}/idf.npy\"\nTFIDF_VOCAB_URL = f\"{ROME_BASE_URL}/tfidf_vocab.json\"\nWINOVENTI_URL = \"https://raw.githubusercontent.com/commonsense-exception/commonsense-exception/main/data/winoventi_bert_large_final.tsv\"\n_MCRAE_BLACKLISTED_FEATURE_PREFIXES = (\"bought/sold\", \"eg -\", \"killed\", \"king of\")\n_MCRAE_SPLITTABLE_FEATURE_PREFIXES = (\n \"associated with\",\n \"an\",\n \"a\",\n \"becomes a\",\n \"causes\",\n \"comes from\",\n \"comes in\",\n \"comes on\",\n \"different\",\n \"found at\",\n \"found below\",\n \"found by\",\n \"found in\",\n \"found on\",\n \"found over\",\n \"found near\",\n \"has an\",\n \"has a\",\n \"has\",\n \"is an\",\n \"is attached to\",\n \"is a\",\n \"is\",\n \"like a\",\n \"made by\",\n \"made of\",\n \"made with\",\n \"made from\",\n \"owned by\",\n \"part of a\",\n \"part of\",\n \"requires a\",\n \"requires\",\n \"used as\",\n \"used at\",\n \"used by\",\n \"used for\",\n \"used in\",\n \"used on\",\n \"used with\",\n \"uses\",\n)\n_BIOS_BIAS_BLACKLISTED_NAMES = frozenset(\n {\n \"Non-Residential\",\n }\n)\n_BIOS_BIAS_PREFIXES = (\n \"professor\",\n \"prof.\",\n \"prof\",\n \"dr.\",\n \"dr\",\n \"doctor\",\n \"mr.\",\n \"mr\",\n \"ms.\",\n \"ms\",\n \"mrs.\",\n \"mrs\",\n \"rev.\",\n \"rev\",\n \"pastor\",\n)\n_COUNTERFACT_PARAPHRASE_PROMPT_ARTIFACTS = (\" (b. \", \"(tr. \", \"(min. \")\nclass ContextMediationSample(TypedDict):\nclass ContextMediationBatch(TypedDict):\n class ModifiedTfidfVectorizer(TfidfVectorizer):\ndef _determine_file(file: PathLike | None, url: str) -> Path:\ndef _download_file(file: PathLike, url: str) -> None:\ndef _rejoin_sents_on_entity(entity: str, sents: list[str]) -> list[str]:\ndef _strip_counterfact_paraphrase_prompt(entity: str, prompt: str) -> str:\ndef _reformat_counterfact_sample(cf_sample: dict) -> ContextMediationSample:\ndef _reformat_counterfact_file(file: Path) -> Path:\ndef _load_counterfact(\n file: PathLike | None = None,\n url: str = COUNTERFACT_URL,\n overwrite: bool = False,\n **kwargs: Any,\n) -> Dataset:\ndef _filter_winoventi_sample(wv_sample: dict) -> bool:\ndef _reformat_winoventi_sample(wv_sample: dict) -> ContextMediationSample:\ndef _load_winoventi(\n file: PathLike | None = None,\n url: str = WINOVENTI_URL,\n overwrite: bool = False,\n **kwargs: Any,\n) -> Dataset:\ndef _get_attribute(\n bb_bio:str, \n bb_name:str,\n nlp, \n sent_idx: int|None=None, \n):\ndef _reformat_bias_in_bios_file(\n pkl_file: Path,\n bio_min_words: int = 10,\n sent_min_words: int = 3,\n limit: int | None = 50000,\n file_name: str = \"biosbias.json\",\n sents_choice: int | str = 1, \n attr_sent_idx: int | None = None, \n) -> Path:\ndef _load_bias_in_bios(file: PathLike | None = None, **kwargs: Any) -> Dataset:\ndef _get_mcrae_concept(row: dict) -> str:\ndef _get_mcrae_feature(row: dict) -> str:\ndef _get_mcrae_feature_prob(row: dict) -> float:\ndef _get_mcrae_sample_id(\n concept: str, context_feature: str, prompt_feature: str\n) -> str:\ndef _filter_mcrae_features(rows: list[dict]) -> list[dict]:\ndef _get_mcrae_feature_prefix_for_fluency(feature: str) -> str | None:\ndef _make_mcrae_feature_fluent(feature: str) -> str:\ndef _strip_mcrae_parenthetical(concept: str) -> str:\ndef _get_mcrae_prompt_and_target(feature: str) -> tuple[str, str]:\ndef _get_mcrae_prompt_with_entity(concept: str, prompt: str) -> str:\ndef _get_mcrae_context_and_attribute(concept: str, feature: str) -> tuple[str, str]:\ndef _create_samples_from_mcrae_norms(\n text_file: Path,\n min_co_prob: float = 0.1,\n samples_per_feature_pair: int = 1,\n unrelated_features_per_sample: int = 5,\n seed: int | None = 123456,\n) -> Path:\ndef _load_mcrae(file: PathLike | None = None, **kwargs: Any) -> Dataset:\ndef load_dataset(name: str, **kwargs: Any) -> Dataset:\ndef load_attribute_snippets(\n file: Path | None = None, url: str = ATTRIBUTE_SNIPPETS_URL, overwrite: bool = False\n) -> AttributeSnippets:\ndef load_counterfact_tfidf_vectorizer(\n idf_file: Path | None = None,\n vocab_file: Path | None = None,\n idf_url: str = TFIDF_IDF_URL,\n vocab_url: str = TFIDF_VOCAB_URL,\n overwrite: bool = False,\n) -> TfidfVectorizer:\ndef load_biosbias_tfidf_vectorizer(\n dataset: datasets.arrow_dataset.Dataset | None = None,\n) -> TfidfVectorizer:\ndef column_names(dataset: Dataset, exclude: StrSequence | None = None) -> list[str]:\ndef load_spacy_model(name: str) -> spacy.language.Language:\ndef maybe_train_test_split(\n dataset: Dataset, **kwargs: Any\n) -> datasets.dataset_dict.DatasetDict:\ndef disable_caching() -> None:\ndef add_dataset_args(\n parser: argparse.ArgumentParser, default: str = \"counterfact\"\n) -> None:" }, { "identifier": "models", "path": "evaluation/models.py", "snippet": "GPT_J_NAME_SHORT = \"gptj\" # A useful alias for the CLI.\nGPT_J_NAME = \"EleutherAI/gpt-j-6B\"\nGPT_NEO_X_NAME_SHORT = \"neox\"\nGPT_NEO_X_NAME = \"EleutherAI/gpt-neox-20b\"\nclass ModelAndTokenizer:\n def to_(self, device: Optional[Device]) -> None:\n def eval_(self) -> None:\ndef unwrap_model(value: Model | ModelAndTokenizer) -> Model:\ndef unwrap_tokenizer(tokenizer: ModelAndTokenizer | Tokenizer) -> Tokenizer:\ndef determine_layers(model: ModelAndTokenizer | Model) -> tuple[int, ...]:\ndef determine_layer_paths(\n model: ModelAndTokenizer | Model,\n layers: Optional[Sequence[int]] = ...,\n *,\n return_dict: Literal[False] = ...,\n) -> Sequence[str]:\ndef determine_layer_paths(\n model: ModelAndTokenizer | Model,\n layers: Optional[Sequence[int]] = ...,\n *,\n return_dict: Literal[True],\n) -> dict[int, str]:\ndef determine_layer_paths(\n model: ModelAndTokenizer | Model,\n layers: Optional[Sequence[int]] = None,\n *,\n return_dict: bool = False,\n) -> Sequence[str] | dict[int, str]:\ndef determine_hidden_size(model: ModelAndTokenizer | Model) -> int:\ndef determine_device(model: ModelAndTokenizer | Model) -> torch.device | None:\ndef determine_dtype(model: ModelAndTokenizer | Model) -> torch.dtype | None:\ndef any_parameter(model: ModelAndTokenizer | Model) -> torch.nn.Parameter | None:\ndef set_padding_side(\n tokenizer: Tokenizer | ModelAndTokenizer, padding_side: str = \"right\"\n) -> Iterator[None]:\ndef map_to(\n orig: Any, device: Device | None = None, dtype: torch.dtype | None = None\n) -> Any:\ndef load_model(\n name: str, \n device: Optional[Device] = None, \n fp16: Optional[bool] = None, \n) -> ModelAndTokenizer:\ndef add_model_args(parser: argparse.ArgumentParser) -> None:" }, { "identifier": "tokenizer_utils", "path": "evaluation/utils/tokenizer_utils.py", "snippet": "def find_token_range(\n string: str,\n substring: str,\n tokenizer: Optional[Tokenizer] = None,\n occurrence: int = 0,\n offset_mapping: Optional[TokenizerOffsetMapping] = None,\n **kwargs: Any,\n) -> Tuple[int, int]:\ndef batch_convert_ids_to_tokens(\n batch: Sequence[Sequence[int]], tokenizer: Tokenizer, **kwargs: Any\n) -> Sequence[StrSequence]:\ndef set_padding_side(\n tokenizer: Tokenizer, padding_side: str = \"right\"\n) -> Iterator[None]:" }, { "identifier": "Dataset", "path": "evaluation/utils/typing.py", "snippet": "" } ]
import argparse import torch from functools import partial from typing import Any, Literal, Optional, Sequence, cast, overload from evaluation import data, models from evaluation.utils import tokenizer_utils from evaluation.utils.typing import ( Dataset, Device, ModelInput, ModelOutput, StrSequence, Tokenizer, TokenizerOffsetMapping, ) from baukit import nethook
2,761
"""Logic for getting and mucking with model hidden representations.""" def _remove_sent_case(text: str) -> str: """Make the string NOT sentence case (first letter lowercase).""" return text[0].lower() + text[1:] def _is_batched(text: str | StrSequence) -> bool: """Determine if text is batched or not.""" return not isinstance(text, str) def _maybe_batch(text: str | StrSequence) -> StrSequence: """Batch the text if it is not already batched.""" if isinstance(text, str): return [text] return text def _as_fp32(data: dict) -> dict: """Cast all top-level float tensor values to float32.""" return { key: value.float() if isinstance(value, torch.Tensor) and value.dtype.is_floating_point else value for key, value in data.items() } def _validate_lengths(lengths: torch.Tensor) -> None: """Validate sequence lengths tensor is correct shape.""" if len(lengths.shape) != 1: raise ValueError(f"misshapen lengths: {lengths.shape}") def _validate_token_ranges( token_ranges: torch.Tensor, batch_size: int | None = None ) -> None: """Validate token ranges are correct shape.""" if len(token_ranges.shape) != 2 or token_ranges.shape[1] != 2: raise ValueError(f"misshapen token ranges: {token_ranges.shape}") if batch_size is not None and token_ranges.shape[0] != batch_size: raise ValueError( f"expected batch_size={batch_size}, got {token_ranges.shape[0]}" ) def inputs_from_batch( mt: models.ModelAndTokenizer, text: str | StrSequence, device: Optional[Device] = None,
"""Logic for getting and mucking with model hidden representations.""" def _remove_sent_case(text: str) -> str: """Make the string NOT sentence case (first letter lowercase).""" return text[0].lower() + text[1:] def _is_batched(text: str | StrSequence) -> bool: """Determine if text is batched or not.""" return not isinstance(text, str) def _maybe_batch(text: str | StrSequence) -> StrSequence: """Batch the text if it is not already batched.""" if isinstance(text, str): return [text] return text def _as_fp32(data: dict) -> dict: """Cast all top-level float tensor values to float32.""" return { key: value.float() if isinstance(value, torch.Tensor) and value.dtype.is_floating_point else value for key, value in data.items() } def _validate_lengths(lengths: torch.Tensor) -> None: """Validate sequence lengths tensor is correct shape.""" if len(lengths.shape) != 1: raise ValueError(f"misshapen lengths: {lengths.shape}") def _validate_token_ranges( token_ranges: torch.Tensor, batch_size: int | None = None ) -> None: """Validate token ranges are correct shape.""" if len(token_ranges.shape) != 2 or token_ranges.shape[1] != 2: raise ValueError(f"misshapen token ranges: {token_ranges.shape}") if batch_size is not None and token_ranges.shape[0] != batch_size: raise ValueError( f"expected batch_size={batch_size}, got {token_ranges.shape[0]}" ) def inputs_from_batch( mt: models.ModelAndTokenizer, text: str | StrSequence, device: Optional[Device] = None,
) -> tuple[ModelInput, Sequence[TokenizerOffsetMapping]]:
3
2023-11-06 05:36:05+00:00
4k
MrXandbadas/MrX_OAI_Assistant_Manager
assistant_manager/assistant_tools.py
[ { "identifier": "OAI_Threads", "path": "assistant_manager/a_m_threads.py", "snippet": "class OAI_Threads(Assistant_manager_update):\n\n def __init__(self, api_key, organization, timeout=None, log_level=logging.INFO):\n \"\"\"\n Initializes an instance of AssistantManager.\n\n Args:\n api_key (str): The OpenAI API key.\n organization (str): The OpenAI organization ID.\n timeout (Optional[int]): The timeout for API requests, in seconds.\n log_level (Optional[int]): The logging level to use.\n\n Returns:\n None\n \"\"\"\n super().__init__(api_key=api_key, organization=organization, timeout=timeout, log_level=log_level)\n\n\n def list_threads(self):\n \"\"\"\n Returns a dict of threads.\n\n Args:\n None\n\n Returns:\n dict: A dict of threads.\n \"\"\"\n return self.threads\n \n def list_thread_history(self):\n \"\"\"\n Returns a list of messages in the current thread.\n\n Args:\n None\n\n Returns:\n list: A list of messages.\n \"\"\"\n if self.chat_ids == []:\n self.logger.debug(f\"No messages in thread {self.current_thread}\")\n return None\n else:\n return self.chat_ids\n \n \n \n def prepare_thread_history(self, thread_id):\n \"\"\"\n Prepares the thread history for the current thread.\n\n Args:\n thread_id (str): The ID of the thread to prepare the history for.\n\n Returns:\n None\n \"\"\"\n #get the thread history\n thread_history = self.list_messages(thread_id=thread_id)\n #save the thread history to the current thread history\n self.current_thread_history = thread_history\n #SyncCursorPage\n #get the list of messages\n messages = thread_history.data\n #loop through the messages and add them to the chat_ids list\n for message in messages:\n self.chat_ids.append(message.id)\n self.logger.debug(f\"Prepared thread history for thread {thread_id}\")\n \n def create_blank_thread(self):\n \"\"\"\n Creates a blank thread.\n\n Args:\n None\n\n Returns:\n str: The ID of the blank thread.\n \"\"\"\n #create a blank thread\n blank_thread = self.create_thread()\n #get the thread ID\n thread_id = blank_thread.id\n #add the thread to the list of threads\n self.threads[thread_id] = \"Blank Thread\"\n #save the thread ID to the thread_ids.json file\n self.add_thread(\"Blank Thread\", thread_id)\n self.current_thread = thread_id\n #return the thread ID\n return thread_id\n\n def change_thread(self, thread_name: str or None = None, thread_id: str or None = None) -> int:\n \"\"\"\n Changes the current thread.\n\n Args:\n thread_name (str): The name of the thread to change to.\n thread_id (str): The ID of the thread to change to.\n\n Returns:\n int: thread_id if the thread was changed successfully, False otherwise.\n \"\"\"\n # A compact function that checks if the thread name or ID is None and handles it\n if thread_name is not None:\n #if the thread name is not None, get the thread ID from the thread_ids.json file\n threads = self.get_threads()\n\n if thread_name in threads:\n thread_id = threads[thread_name]\n #if we have seen this thread before, get the thread history\n self.prepare_thread_history(thread_id)\n self.current_thread = thread_id\n self.logger.debug(f\"Thread {thread_name} found. Changing thread...\")\n return thread_id\n\n else:\n self.logger.debug(f\"Thread {thread_name} not found. Creating new thread...\")\n #create a new thread\n new_thread = self.create_thread()\n #get the thread ID\n thread_id = new_thread.id\n #add the thread to the list of threads\n # Define thread_id before assigning a thread name to it\n #print(f\"Thread ID: {thread_id}\")\n #print(f\"Thread Name: {thread_name}\")\n #save the thread ID to the thread_ids.json file\n self.add_thread(thread_name, thread_id)\n self.current_thread = thread_id\n \n #get the thread history\n self.prepare_thread_history(thread_id)\n self.current_thread = thread_id\n self.logger.debug(f\"Changed thread to {thread_id}\")\n return thread_id\n elif thread_id is not None:\n #if the thread ID is not None, get the thread name from the thread_ids.json file\n print(f\"Trying to change thread to ID {thread_id}\")\n threads = self.get_threads()\n #Object with key as thread name and value as thread ID\n thread_name = None\n for key, value in threads.items():\n if value == thread_id:\n thread_name = key\n break\n\n if thread_name is not None:\n #if we have seen this thread before, get the thread history\n self.prepare_thread_history(thread_id)\n self.current_thread = thread_id\n self.logger.debug(f\"Thread {thread_name} found. Changing thread...\")\n return thread_id\n else:\n #if both none, create a blank thread\n thread_id = self.create_blank_thread()\n print(\"Creating Blank Thread...\")\n return thread_id\n \n\n def get_threads(self):\n \"\"\"\n Returns a list of threads.\n\n Args:\n None\n\n Returns:\n list: A list of threads.\n \"\"\"\n if self.threads is not None:\n return self.threads\n else:\n #attempt to read the thread_ids.json file\n thread_ids = read_json('assistant_manager/thread_ids.json')\n #if the file is empty, return an empty dict\n if thread_ids is None:\n return {}\n else:\n #if the file is not empty, return the dict\n return thread_ids\n\n def add_thread(self, thread_name, thread_id):\n \"\"\"\n Adds a thread to the list of threads json file\n\n Args:\n thread_name (str): The name of the thread to add.\n thread_id (str): The ID of the thread to add.\n \"\"\"\n\n # Read the existing data from the file\n data = read_json('assistant_manager/thread_ids.json')\n\n # Add the new entry to the data\n data[thread_name] = thread_id\n\n # Write the updated data back to the file\n save_json('assistant_manager/thread_ids.json', data)\n\n\n \n def setup_thread(self, input_thread_name=None, input_thread_id=None) -> int:\n # Create a new thread if thread_id is None\n \n if input_thread_name is not None:\n thread_id = self.change_thread(input_thread_name)\n elif input_thread_id is not None:\n #change the thread to the thread with the given ID\n thread_id = self.change_thread(thread_id=input_thread_id)\n else:\n #create a thread with the deafult name\n thread_id = self.change_thread(thread_name=\"Default_Thread\")\n\n\n self.current_thread = thread_id\n self.prepare_thread_history(thread_id=thread_id)\n return thread_id" }, { "identifier": "save_json", "path": "assistant_manager/utils/file_operations.py", "snippet": "def save_json(file_name, data):\n \"\"\"\n Saves a JSON file.\n\n Args:\n file_name (str): The name of the file to save.\n data (dict): The data to save.\n\n Returns:\n None\n \"\"\"\n with open(file_name, 'w') as outfile:\n json.dump(data, outfile)" }, { "identifier": "read_json", "path": "assistant_manager/utils/file_operations.py", "snippet": "def read_json(file_name):\n \"\"\"\n Reads a JSON file.\n\n Args:\n file_name (str): The name of the file to read.\n\n Returns:\n dict: The data from the file.\n \"\"\"\n try:\n with open(file_name) as json_file:\n data = json.load(json_file)\n return data\n except FileNotFoundError:\n return {}" } ]
from assistant_manager.a_m_threads import OAI_Threads from assistant_manager.utils.file_operations import save_json, read_json import json import logging
2,852
class Tooling(OAI_Threads): def __init__(self, api_key, organization, timeout=None, log_level=logging.INFO): """ Initializes an instance of AssistantManager. Args: api_key (str): The OpenAI API key. organization (str): The OpenAI organization ID. timeout (Optional[int]): The timeout for API requests, in seconds. log_level (Optional[int]): The logging level to use. Returns: None """ super().__init__(api_key=api_key, organization=organization, timeout=timeout, log_level=log_level) def get_tool_by_name(self, tool_name): """ Returns a tool object by name """ tools = self.load_tool_metadata() self.logger.info(f"Getting tool by name: {tool_name}") for tool in tools: if tool["tool_name"] == tool_name: self.logger.debug(f"Tool found: {tool}") return tool self.logger.error(f"Tool not found: {tool_name}") return None def list_assistants_names(self): """ Returns a dictionary of assistant names and their corresponding IDs """ assistants = self.assistants assistant_dict = {} for i, assistant in enumerate(assistants.data): assistant_dict[assistant.name] = assistant.id self.logger.debug(f"Listing Assistant names: {assistant_dict}") return assistant_dict def list_system_tools(self): """ returns a list of the tool names """ tools = self.load_tool_metadata() tool_names = [] #tools is a dict of named dicts, we need to grab the name from each dict #"list_system_tools": { # "tool_name": "list_system_tools", # "tool_required": "", # "tool_description": "Provides a list of all available system tool names", # "tool_properties": {}, # "tool_meta_description": "Returns a list of all available tool names." #} for tool in tools: tool_names.append(tool.get("tool_name")) self.logger.debug(f"Listing System Tool names: {tool_names}") return tool_names def load_tool_metadata(self) -> dict: """ Loads the metadata from functions_metadata.json file Args: None Returns: dict: A dict of tool metadata. """ #attempt to read the functions_metadata.json file tool_metadata_dict0 = read_json('assistant_manager/functions/static/default_functions_metadata.json') #print(tool_metadata_dict0) #print("------") tool_metadata_dict1 = read_json('assistant_manager/functions/dynamic/functions_metadata.json') #print(tool_metadata_dict1) # Merge the two dicts into a new dict tool_metadata = {**tool_metadata_dict0, **tool_metadata_dict1} #if the file is empty, return an empty dict if tool_metadata is None: self.logger.error("No tool metadata found assistant_tools.py") return {} else: #if the file is not empty, return the dict self.tool_metadata = tool_metadata self.logger.info("Tool metadata loaded") self.logger.debug(f"Tool metadata: {tool_metadata}") return self.tool_metadata def save_tool_metadata(self, tool_name, tool_required, tool_description, tool_schema): """ Save the metadata into functions_metadata.json file Args: tool_name (str): The name of the tool. tool_required (list): The list of required parameters for the tool. tool_description (str): The description of the tool. tool_schema (dict): The schema of the tool. Returns: None """ # Read the existing data from the file data = read_json('assistant_manager/functions/dynamic/functions_metadata.json') # Add the new entry to the data data[tool_name] = { "required": tool_required, "description": tool_description, "schema": tool_schema } # Write the updated data back to the file
class Tooling(OAI_Threads): def __init__(self, api_key, organization, timeout=None, log_level=logging.INFO): """ Initializes an instance of AssistantManager. Args: api_key (str): The OpenAI API key. organization (str): The OpenAI organization ID. timeout (Optional[int]): The timeout for API requests, in seconds. log_level (Optional[int]): The logging level to use. Returns: None """ super().__init__(api_key=api_key, organization=organization, timeout=timeout, log_level=log_level) def get_tool_by_name(self, tool_name): """ Returns a tool object by name """ tools = self.load_tool_metadata() self.logger.info(f"Getting tool by name: {tool_name}") for tool in tools: if tool["tool_name"] == tool_name: self.logger.debug(f"Tool found: {tool}") return tool self.logger.error(f"Tool not found: {tool_name}") return None def list_assistants_names(self): """ Returns a dictionary of assistant names and their corresponding IDs """ assistants = self.assistants assistant_dict = {} for i, assistant in enumerate(assistants.data): assistant_dict[assistant.name] = assistant.id self.logger.debug(f"Listing Assistant names: {assistant_dict}") return assistant_dict def list_system_tools(self): """ returns a list of the tool names """ tools = self.load_tool_metadata() tool_names = [] #tools is a dict of named dicts, we need to grab the name from each dict #"list_system_tools": { # "tool_name": "list_system_tools", # "tool_required": "", # "tool_description": "Provides a list of all available system tool names", # "tool_properties": {}, # "tool_meta_description": "Returns a list of all available tool names." #} for tool in tools: tool_names.append(tool.get("tool_name")) self.logger.debug(f"Listing System Tool names: {tool_names}") return tool_names def load_tool_metadata(self) -> dict: """ Loads the metadata from functions_metadata.json file Args: None Returns: dict: A dict of tool metadata. """ #attempt to read the functions_metadata.json file tool_metadata_dict0 = read_json('assistant_manager/functions/static/default_functions_metadata.json') #print(tool_metadata_dict0) #print("------") tool_metadata_dict1 = read_json('assistant_manager/functions/dynamic/functions_metadata.json') #print(tool_metadata_dict1) # Merge the two dicts into a new dict tool_metadata = {**tool_metadata_dict0, **tool_metadata_dict1} #if the file is empty, return an empty dict if tool_metadata is None: self.logger.error("No tool metadata found assistant_tools.py") return {} else: #if the file is not empty, return the dict self.tool_metadata = tool_metadata self.logger.info("Tool metadata loaded") self.logger.debug(f"Tool metadata: {tool_metadata}") return self.tool_metadata def save_tool_metadata(self, tool_name, tool_required, tool_description, tool_schema): """ Save the metadata into functions_metadata.json file Args: tool_name (str): The name of the tool. tool_required (list): The list of required parameters for the tool. tool_description (str): The description of the tool. tool_schema (dict): The schema of the tool. Returns: None """ # Read the existing data from the file data = read_json('assistant_manager/functions/dynamic/functions_metadata.json') # Add the new entry to the data data[tool_name] = { "required": tool_required, "description": tool_description, "schema": tool_schema } # Write the updated data back to the file
save_json('assistant_manager/functions/dynamic/functions_metadata.json', data)
1
2023-11-07 03:42:04+00:00
4k
Ljzd-PRO/KToolBox
ktoolbox/__main__.py
[ { "identifier": "KToolBoxCli", "path": "ktoolbox/cli.py", "snippet": "class KToolBoxCli:\n @staticmethod\n async def version():\n \"\"\"Show KToolBox version\"\"\"\n return __version__\n\n @staticmethod\n async def site_version():\n # noinspection SpellCheckingInspection\n \"\"\"Show current Kemono site app commit hash\"\"\"\n ret = await get_app_version()\n return ret.data if ret else ret.message\n\n # noinspection PyShadowingBuiltins\n @staticmethod\n async def search_creator(\n name: str = None,\n id: str = None,\n service: str = None,\n *,\n dump: Path = None\n ):\n \"\"\"\n Search creator, you can use multiple parameters as keywords.\n\n :param id: The ID of the creator\n :param name: The name of the creator\n :param service: The service for the creator\n :param dump: Dump the result to a JSON file\n \"\"\"\n ret = await search_creator_action(id=id, name=name, service=service)\n if ret:\n result_list = list(ret.data)\n if dump:\n await dump_search(result_list, dump)\n return result_list or TextEnum.SearchResultEmpty.value\n else:\n return ret.message\n\n # noinspection PyShadowingBuiltins\n @staticmethod\n async def search_creator_post(\n id: str = None,\n name: str = None,\n service: str = None,\n q: str = None,\n o: int = None,\n *,\n dump: Path = None\n ):\n \"\"\"\n Search posts from creator, you can use multiple parameters as keywords.\n\n :param id: The ID of the creator\n :param name: The name of the creator\n :param service: The service for the creator\n :param q: Search query\n :param o: Result offset, stepping of 50 is enforced\n :param dump: Dump the result to a JSON file\n \"\"\"\n ret = await search_creator_post_action(id=id, name=name, service=service, q=q, o=o)\n if ret:\n if dump:\n await dump_search(ret.data, dump)\n return ret.data or TextEnum.SearchResultEmpty.value\n else:\n return ret.message\n\n @staticmethod\n async def get_post(service: str, creator_id: str, post_id: str, *, dump: Path = None):\n \"\"\"\n Get a specific post\n\n :param service: The service name\n :param creator_id: The creator's ID\n :param post_id: The post ID\n :param dump: Dump the result to a JSON file\n \"\"\"\n ret = await get_post_api(\n service=service,\n creator_id=creator_id,\n post_id=post_id\n )\n if ret:\n if dump:\n async with aiofiles.open(str(dump), \"w\", encoding=\"utf-8\") as f:\n await f.write(\n ret.data.model_dump_json(indent=config.json_dump_indent)\n )\n return ret.data\n else:\n return ret.message\n\n @staticmethod\n @overload\n async def download_post(\n url: str,\n path: Union[Path, str] = Path(\".\"),\n *,\n dump_post_data=True\n ):\n ...\n\n @staticmethod\n @overload\n async def download_post(\n service: str,\n creator_id: str,\n post_id: str,\n path: Union[Path, str] = Path(\".\"),\n *,\n dump_post_data=True\n ):\n ...\n\n @staticmethod\n async def download_post(\n url: str = None,\n service: str = None,\n creator_id: str = None,\n post_id: str = None,\n path: Union[Path, str] = Path(\".\"),\n *,\n dump_post_data=True\n ):\n \"\"\"\n Download a specific post\n\n :param url: The post URL\n :param service: The service name\n :param creator_id: The creator's ID\n :param post_id: The post ID\n :param path: Download path, default is current directory\n :param dump_post_data: Whether to dump post data (post.json) in post directory\n \"\"\"\n # Get service, creator_id, post_id\n if url:\n service, creator_id, post_id = parse_webpage_url(url)\n if not all([service, creator_id, post_id]):\n return generate_msg(\n TextEnum.MissingParams.value,\n use_at_lease_one=[\n [\"url\"],\n [\"service\", \"creator_id\", \"post_id\"]\n ])\n\n path = path if isinstance(path, Path) else Path(path)\n ret = await get_post_api(\n service=service,\n creator_id=creator_id,\n post_id=post_id\n )\n if ret:\n post_path = path / generate_post_path_name(ret.data)\n job_list = await create_job_from_post(\n post=ret.data,\n post_path=post_path,\n dump_post_data=dump_post_data\n )\n job_runner = JobRunner(job_list=job_list)\n await job_runner.start()\n else:\n return ret.message\n\n @staticmethod\n @overload\n async def sync_creator(\n url: str,\n path: Union[Path, str] = Path(\".\"),\n *,\n update_from: Path = None,\n save_creator_indices: bool = True,\n mix_posts: bool = None,\n time_range: Tuple[str, str] = None,\n start_time: str = None,\n end_time: str = None\n ):\n ...\n\n @staticmethod\n @overload\n async def sync_creator(\n service: str,\n creator_id: str,\n path: Union[Path, str] = Path(\".\"),\n *,\n update_from: Path = None,\n save_creator_indices: bool = True,\n mix_posts: bool = None,\n time_range: Tuple[str, str] = None,\n start_time: str = None,\n end_time: str = None\n ):\n ...\n\n @staticmethod\n async def sync_creator(\n url: str = None,\n service: str = None,\n creator_id: str = None,\n path: Union[Path, str] = Path(\".\"),\n *,\n update_from: Union[Path, str] = None,\n save_creator_indices: bool = True,\n mix_posts: bool = None,\n start_time: str = None,\n end_time: str = None\n ):\n \"\"\"\n Sync all posts from a creator\n\n You can update the directory anytime after download finished, \\\n such as to update after creator published new posts.\n\n * If ``update_from`` was provided, the file should be located **inside the creator directory**.\n * ``start_time`` & ``end_time`` example: ``2023-12-7``, ``2023-12-07``\n\n :param url: The post URL\n :param service: The service where the post is located\n :param creator_id: The ID of the creator\n :param path: Download path, default is current directory\n :param update_from: ``CreatorIndices`` data path for update posts from current creator directory, \\\n ``save_creator_indices`` will be enabled if this provided\n :param save_creator_indices: Record ``CreatorIndices`` data for update posts from current creator directory\n :param mix_posts: Save all files from different posts at same path, \\\n ``update_from``, ``save_creator_indices`` will be ignored if enabled\n :param start_time: Start time of the published time range for posts downloading. \\\n Set to ``0`` if ``None`` was given. \\\n Time format: ``%Y-%m-%d``\n :param end_time: End time of the published time range for posts downloading. \\\n Set to latest time (infinity) if ``None`` was given. \\\n Time format: ``%Y-%m-%d``\n \"\"\"\n # Get service, creator_id\n if url:\n service, creator_id, _ = parse_webpage_url(url)\n if not all([service, creator_id]):\n return generate_msg(\n TextEnum.MissingParams.value,\n use_at_lease_one=[\n [\"url\"],\n [\"service\", \"creator_id\"]\n ])\n\n path = path if isinstance(path, Path) else Path(path)\n if update_from:\n update_from = update_from if isinstance(update_from, Path) else Path(update_from)\n\n # Get ``CreatorIndices`` data\n if update_from:\n async with aiofiles.open(update_from, encoding=\"utf-8\") as f:\n indices_text = await f.read()\n indices = CreatorIndices.model_validate_json(indices_text)\n creator_path = update_from.parent\n else:\n indices = None\n\n # Get creator name\n creator_name = creator_id\n creator_ret = await search_creator_action(id=creator_id, service=service)\n if creator_ret:\n creator = next(creator_ret.data, None)\n if creator:\n creator_name = creator.name\n logger.info(\n generate_msg(\n \"Got creator information\",\n name=creator.name,\n id=creator.id\n )\n )\n else:\n logger.warning(\n generate_msg(\n f\"Failed to fetch the name of creator <{creator_id}>, use creator ID as directory name\",\n detail=creator_ret.message\n )\n )\n creator_path = path / sanitize_filename(creator_name)\n\n creator_path.mkdir(exist_ok=True)\n ret = await create_job_from_creator(\n service=service,\n creator_id=creator_id,\n path=creator_path,\n update_from=indices,\n all_pages=True,\n save_creator_indices=save_creator_indices,\n mix_posts=mix_posts,\n start_time=datetime.strptime(start_time, \"%Y-%m-%d\") if start_time else None,\n end_time=datetime.strptime(end_time, \"%Y-%m-%d\") if end_time else None\n )\n if ret:\n job_runner = JobRunner(job_list=ret.data)\n await job_runner.start()\n else:\n return ret.message" }, { "identifier": "config", "path": "ktoolbox/configuration.py", "snippet": "class APIConfiguration(BaseModel):\nclass DownloaderConfiguration(BaseModel):\nclass PostStructureConfiguration(BaseModel):\nclass JobConfiguration(BaseModel):\nclass LoggerConfiguration(BaseModel):\nclass Configuration(BaseSettings):" }, { "identifier": "logger_init", "path": "ktoolbox/utils.py", "snippet": "def logger_init(cli_use: bool = False, disable_stdout: bool = False):\n \"\"\"\n Initialize ``loguru`` logger\n\n :param cli_use: Set logger level ``INFO`` and filter out ``SUCCESS``\n :param disable_stdout: Disable default output stream\n \"\"\"\n if disable_stdout:\n logger.remove()\n elif cli_use:\n logger.remove()\n logger.add(\n sys.stderr,\n level=logging.INFO,\n filter=lambda record: record[\"level\"].name != \"SUCCESS\"\n )\n if path := config.logger.path:\n path.mkdir(exist_ok=True)\n if path is not None:\n logger.add(\n path / DataStorageNameEnum.LogData.value,\n level=config.logger.level,\n rotation=config.logger.rotation,\n diagnose=True\n )" }, { "identifier": "uvloop_init", "path": "ktoolbox/utils.py", "snippet": "def uvloop_init() -> bool:\n \"\"\"\n Set event loop policy to uvloop if available.\n\n :return: If uvloop enabled successfully\n \"\"\"\n if config.use_uvloop:\n if sys.platform == \"win32\":\n logger.debug(\"uvloop is not supported on Windows, but it's optional.\")\n else:\n try:\n # noinspection PyUnresolvedReferences\n import uvloop\n except ModuleNotFoundError:\n logger.debug(\n \"uvloop is not installed, but it's optional. \"\n \"You can install it with `pip install ktoolbox[uvloop]`\"\n )\n else:\n asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())\n logger.success(\"Set event loop policy to uvloop successfully.\")\n return True\n return False" }, { "identifier": "generate_msg", "path": "ktoolbox/utils.py", "snippet": "def generate_msg(title: str = None, **kwargs):\n \"\"\"\n Generate message for ``BaseRet`` and logger\n\n :param title: Message title\n :param kwargs: Extra data\n \"\"\"\n title: str = title or \"\"\n return f\"{title} - {kwargs}\" if kwargs else title" } ]
import fire from loguru import logger from ktoolbox.cli import KToolBoxCli from ktoolbox.configuration import config from ktoolbox.utils import logger_init, uvloop_init, generate_msg
3,066
def main(): try: logger_init(cli_use=True) logger.debug(generate_msg(config=config))
def main(): try: logger_init(cli_use=True) logger.debug(generate_msg(config=config))
uvloop_init()
3
2023-11-06 15:24:12+00:00
4k
jpjacobpadilla/Google-Colab-Selenium
google_colab_selenium/undetected_chromedriver.py
[ { "identifier": "ColabSeleniumManager", "path": "google_colab_selenium/colab_selenium_manager.py", "snippet": "class ColabSeleniumManager:\n default_colab_options = [\n '--headless',\n '--no-sandbox',\n '--disable-dev-shm-usage',\n '--lang=en'\n ]\n\n _downloaded_chrome = False\n _updated_apt = False\n\n update_apt = ['sudo', 'apt', 'update']\n upgrade_apt = ['sudo', 'apt', 'upgrade']\n\n download_command = ['curl', '-o', 'google-chrome-stable_current_amd64.deb', 'https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb']\n install_command = ['sudo', 'apt', 'install', './google-chrome-stable_current_amd64.deb', '-y']\n clean_up_command = ['rm', 'google-chrome-stable_current_amd64.deb']\n\n chromedriver_path: str = None\n\n def __init__(self, base_options: Options):\n if not self._updated_apt:\n self.update_upgrade_apt()\n\n if not self._downloaded_chrome:\n self.install_chrome()\n\n self.options = self.default_options(base_options or Options())\n self.service = self.get_service()\n\n @classmethod\n def update_upgrade_apt(cls) -> None:\n try:\n with Spinner('Updating and upgrading APT', done='Updated and upgraded APT'):\n subprocess.run(cls.update_apt, check=True)\n subprocess.run(cls.upgrade_apt, check=True)\n \n except Exception as e:\n raise GoogleColabSeleniumError('Failed to update and upgrade APT') from e\n\n else:\n cls._updated_apt = True\n\n @classmethod\n def install_chrome(cls) -> None:\n \"\"\"\n To Install Google-Chrome-Stable, the first command uses CURL to download\n the debian file. Next Advanced Package Tool installs the file and once\n it's installed, the .deb file, which is no longer needed, is deleted.\n \"\"\"\n try:\n with Spinner('Downloading Google Chrome', done='Downloaded Google Chrome'):\n subprocess.run(cls.download_command, check=True)\n subprocess.run(cls.install_command, check=True)\n subprocess.run(cls.clean_up_command, check=True)\n\n except Exception as e:\n raise InstallChromeError(\"Failed to install Google Chrome.\") from e\n\n else:\n cls._downloaded_chrome = True\n\n @classmethod\n def default_options(cls, options: Options) -> Options:\n for default in cls.default_colab_options:\n options.add_argument(default)\n\n return options\n\n @classmethod\n def get_service(cls) -> Service:\n path = cls.chromedriver_path or cls.prepare_driver()\n return Service(path)\n\n @classmethod\n def prepare_driver(cls) -> str:\n try:\n path = SeleniumManager().driver_location(Options())\n cls.chromedriver_path = path\n return path\n\n except Exception as e:\n raise ChromeDriverPathError(\"Failed to find ChromeDriver.\") from e" }, { "identifier": "Spinner", "path": "google_colab_selenium/spinner.py", "snippet": "class Spinner:\n def __init__(self, message: str, done: str):\n self.message = message\n self.done_message = done\n self.stop_event = threading.Event()\n\n def __enter__(self):\n self.show_spinner(self.message)\n return self\n\n def __exit__(self, *args, **kwargs):\n self.remove_spinner()\n\n def show_spinner(self, text):\n self.spinner_id = uuid.uuid4()\n\n spinner_html = f\"\"\"\n <div class=\"spinner-container\">\n <div class=\"spinner\" id=\"{self.spinner_id}-circle\"></div>\n <div class=\"spinner-text\" id=\"{self.spinner_id}-text\">{text}</div>\n </div>\n <style>\n @keyframes spin {{\n from {{ transform: rotate(0deg); }}\n to {{ transform: rotate(360deg); }}\n }}\n\n .spinner-container {{\n display: flex;\n align-items: center;\n margin-bottom: 3px;\n }}\n\n .spinner {{\n border: 3px solid rgba(0, 0, 0, 0.1);\n border-left-color: lightblue;\n border-radius: 50%;\n width: 12px;\n height: 12px;\n animation: spin 1s linear infinite;\n }}\n\n .spinner-text {{\n padding-left: 6px;\n }}\n </style>\n \"\"\"\n display(HTML(spinner_html))\n\n def remove_spinner(self):\n js_code = f\"\"\"\n const element = document.getElementById(\"{self.spinner_id}-circle\");\n element.style.border = \"3px solid limegreen\";\n element.style.animation = \"none\";\n\n const text = document.getElementById(\"{self.spinner_id}-text\");\n text.innerText = \"{self.done_message}\";\n \"\"\"\n display(Javascript(js_code))" }, { "identifier": "StartingChromeDriverError", "path": "google_colab_selenium/exceptions.py", "snippet": "class StartingChromeDriverError(GoogleColabSeleniumError):\n \"\"\"Exception raised when ChromeDriver fails to start.\"\"\"\n pass" } ]
from google_colab_selenium.colab_selenium_manager import ColabSeleniumManager from google_colab_selenium.spinner import Spinner from google_colab_selenium.exceptions import StartingChromeDriverError from selenium.webdriver.chrome.options import Options import undetected_chromedriver as uc
1,602
try: except ImportError as e: raise ImportError(''' Please install google-colab-selenium with the "undetected" extra -> pip3 install google-colab-selenium[undetected] ''') class UndetectedChromeDriver(uc.Chrome): """ Instead of using ChromeDriver, which is easy to detect, you can use undetected-chromedriver. https://github.com/ultrafunkamsterdam/undetected-chromedriver This package is a great start to making Selenium undetectable, but you still need to act like a human. The ColabSeleniumManager class installs Google-Chrome-Stable and adds the nessasary headers to use in a Colab Notebook. The headers that are automatically added are: --headless --no-sandbox --disable-dev-shm-usage --lang=en """ def __init__(self, options: Options = None, keep_alive: bool = True): self.manager = ColabSeleniumManager(options or uc.ChromeOptions()) try: with Spinner('Initializing Chromedriver', done='Initialized Chromedriver'): super().__init__( service=self.manager.service, options=self.manager.options, keep_alive=keep_alive ) except Exception as e:
try: except ImportError as e: raise ImportError(''' Please install google-colab-selenium with the "undetected" extra -> pip3 install google-colab-selenium[undetected] ''') class UndetectedChromeDriver(uc.Chrome): """ Instead of using ChromeDriver, which is easy to detect, you can use undetected-chromedriver. https://github.com/ultrafunkamsterdam/undetected-chromedriver This package is a great start to making Selenium undetectable, but you still need to act like a human. The ColabSeleniumManager class installs Google-Chrome-Stable and adds the nessasary headers to use in a Colab Notebook. The headers that are automatically added are: --headless --no-sandbox --disable-dev-shm-usage --lang=en """ def __init__(self, options: Options = None, keep_alive: bool = True): self.manager = ColabSeleniumManager(options or uc.ChromeOptions()) try: with Spinner('Initializing Chromedriver', done='Initialized Chromedriver'): super().__init__( service=self.manager.service, options=self.manager.options, keep_alive=keep_alive ) except Exception as e:
raise StartingChromeDriverError("""
2
2023-11-06 21:18:41+00:00
4k
bigai-nlco/langsuite
langsuite/__main__.py
[ { "identifier": "CMDClient", "path": "langsuite/cli/cmd_cli.py", "snippet": "class CMDClient:\n \"\"\"\n Colors: https://rich.readthedocs.io/en/stable/appendix/colors.html\n \"\"\"\n\n console_cfg = dict(soft_wrap=True, markup=False, emoji=False, highlight=True)\n\n def __init__(self, *, log_level: int = logging.DEBUG, log_file=None) -> None:\n self.log_level = log_level\n if log_file and len(log_file) > 0:\n log_dir = Path(log_file).parent\n if not os.path.exists(log_dir):\n os.makedirs(log_dir, exist_ok=True)\n log_file = open(log_file, \"w\")\n self.console = Console(**CMDClient.console_cfg)\n else:\n self.console = Console(**CMDClient.console_cfg)\n\n self._cmd_log_file = log_file\n\n def set_cmd_log_file(self, log_file):\n if self._cmd_log_file:\n self._cmd_log_file.close()\n log_dir = Path(log_file).parent\n os.makedirs(log_dir, exist_ok=True)\n self._cmd_log_file = open(log_file, \"w+\", encoding=\"utf-8\")\n\n def reset(self):\n self.clear()\n\n def clear(self):\n self.console.clear()\n\n def close(self):\n self.console.print()\n self.console.print(\"Bye!\", style=\"bold yellow\")\n if self._cmd_log_file:\n self._cmd_log_file.close()\n\n def info(self, message: str):\n if self.log_level <= logging.INFO:\n self.console.log(\"[INFO] \", style=\"bold\", end=\"\")\n self.console.print(message)\n\n def error(self, message: str):\n if self.log_level <= logging.ERROR:\n self.console.log(\"[ERROR] \", style=\"bold red\", end=\"\")\n self.console.print(message)\n\n def debug(self, message: str):\n if self.log_level <= logging.DEBUG:\n self.console.log(\"[DEBUG] \", style=\"bold bright_black\", end=\"\")\n self.console.print(message)\n\n def warn(self, message: str):\n if self.log_level <= logging.WARNING:\n self.console.log(\"[WARNING] \", style=\"bold yellow\", end=\"\")\n self.console.print(message)\n\n def step(self, message=None, user_input: bool = False, stream=False):\n \"\"\"\n Args:\n message: dict(\n role: [\"system\"|\"assistant\"],\n content: str,\n name: str,\n action: str\n )\n\n stream: bool or Generator\n \"\"\"\n\n try:\n if message:\n if type(message) == list:\n for msg in message:\n self.step(msg, user_input=False, stream=stream)\n else:\n if type(message) == str:\n message = {\"role\": \"system\", \"content\": message}\n\n if message[\"role\"] == \"system\":\n if len(message.get(\"to\", \"\")) > 0:\n self.console.print(\n f\"System (→ {message['to']}): \",\n style=\"bold cyan\",\n end=\"\",\n )\n else:\n self.console.print(\"System: \", style=\"bold cyan\", end=\"\")\n self.console.print(message[\"content\"])\n elif message[\"role\"] == \"assistant\":\n if stream:\n self.render_chatbot(\n stream,\n name=message.get(\"name\", \"Robot\"),\n action=message.get(\"action\", \"chat\"),\n )\n else:\n self.render_chatbot(\n message[\"content\"],\n name=message.get(\"name\", \"Robot\"),\n action=message.get(\"action\", \"chat\"),\n )\n if self._cmd_log_file:\n self._cmd_log_file.write(\n json.dumps(message, sort_keys=True) + \"\\n\"\n )\n if user_input:\n inp = self.user_input()\n if self._cmd_log_file:\n self._cmd_log_file.write(\n json.dumps(dict(role=\"user\", content=inp), sort_keys=True)\n + \"\\n\"\n )\n\n return inp\n except (KeyboardInterrupt, EOFError) as ex:\n raise GameEndException()\n\n def print_help(self):\n self.console.print(\"Help Info:\")\n self.console.print('\"Ctrl + C\" or \"Ctrl + D\" to exit.')\n self.console.rule(\"Commands\", style=\"bold yellow\")\n for k, h in HELP_MSG:\n self.console.print(\" \" * 4 + \"{:15}\\t{:60}\".format(k, h))\n self.console.rule(\".\", style=\"bold yellow\")\n\n def cmd_input(self):\n try:\n cmd_msg = self.console.input(prompt=\"> \")\n except UnicodeDecodeError as ex:\n self.console.print_exception(show_locals=True)\n self.error(\n f\"Invalid input. Got UnicodeDecodeError: {ex}\\nPlease try again.\"\n )\n except KeyboardInterrupt:\n raise GameEndException()\n\n cmd = cmd_msg.strip().split(\" \")\n if cmd[0].upper() == \"LOAD\":\n pass\n elif cmd[0].upper() == \"HELP\":\n self.print_help()\n else:\n raise NotImplementedError\n\n cmd[0] = cmd[0].upper()\n return cmd\n\n def user_input(self):\n try:\n self.console.print(\"User: \", style=\"bold green\", end=\"\")\n user_msg = self.console.input()\n except UnicodeDecodeError as ex:\n self.error(\n f\"Invalid input. Got UnicodeDecodeError: {ex}\\nPlease try again.\"\n )\n return user_msg\n\n def start(self):\n self.console.print(WELCOME_MSG)\n\n def render_chatbot(\n self, generator, name=\"Bot\", action=\"chat\", to=\"\", stream: bool = True\n ):\n action = action.lower()\n if action == \"chat\" and len(to) > 0:\n self.console.print(f\"Assistant ({name} → {to})\", style=\"bold blue\", end=\"\")\n else:\n self.console.print(f\"Assistant ({name})\", style=\"bold blue\", end=\"\")\n if action == \"thought\":\n self.console.print(\" THOUGHT\", style=\"bold yellow\", end=\"\")\n elif action == \"act\":\n self.console.print(\" ACT\", style=\"bold cyan\", end=\"\")\n elif action != \"chat\":\n raise ValueError(f\"Unknown action type: {action}\")\n self.console.print(\": \", style=\"bold blue\", end=\"\")\n if type(generator) == str:\n self.console.print(generator)" }, { "identifier": "GameEndException", "path": "langsuite/cli/cmd_cli.py", "snippet": "class GameEndException(Exception):\n def __init__(self, *args: object) -> None:\n super().__init__(*args)" }, { "identifier": "io_utils", "path": "langsuite/utils/io_utils.py", "snippet": "def read_config(config_path):" }, { "identifier": "logger", "path": "langsuite/utils/logging.py", "snippet": "class Logger:\n def __init__(\n self,\n log_level: int = logging.DEBUG,\n log_file: str = \"\",\n use_cmd: bool = False,\n console_logging=True,\n ) -> None:\n def has_cmdline_interface(self):\n def setLevel(self, level):\n def set_cmd_client(self, cmd_cli: CMDClient, disable_console_logging=True):\n def set_log_file(self, log_file):\n def close(self):\n def info(self, msg):\n def debug(self, msg):\n def error(self, msg):\n def warn(self, msg):\n def user_input(self):\n def emit(self, message):\n def robot_emit(self, message_or_streamer, name=\"Robot\", action=\"chat\"):" } ]
import argparse import langsuite import langsuite.server import langsuite.webui from datetime import datetime from langsuite.cli.cmd_cli import CMDClient, GameEndException from langsuite.utils import io_utils from langsuite.utils.logging import logger
2,003
# Copyright (c) BIGAI Research. All rights reserved. # Licensed under the MIT license. from __future__ import annotations def create_from_config(config_path): config = io_utils.read_config(config_path) logger.info(config) task = langsuite.make(config) return task def run_cmd_cli(task_or_config=None, verbose=False): cmd_cli = CMDClient() cmd_cli.set_cmd_log_file( log_file=f"logs/console-logs/{datetime.now().strftime('console-%Y-%m-%d_%H-%M-%S.jl')}" ) logger.set_cmd_client(cmd_cli, disable_console_logging=not verbose) cmd_cli.start() if task_or_config: try: task = create_from_config(task_or_config) task.run()
# Copyright (c) BIGAI Research. All rights reserved. # Licensed under the MIT license. from __future__ import annotations def create_from_config(config_path): config = io_utils.read_config(config_path) logger.info(config) task = langsuite.make(config) return task def run_cmd_cli(task_or_config=None, verbose=False): cmd_cli = CMDClient() cmd_cli.set_cmd_log_file( log_file=f"logs/console-logs/{datetime.now().strftime('console-%Y-%m-%d_%H-%M-%S.jl')}" ) logger.set_cmd_client(cmd_cli, disable_console_logging=not verbose) cmd_cli.start() if task_or_config: try: task = create_from_config(task_or_config) task.run()
except GameEndException:
1
2023-11-01 01:47:00+00:00
4k
tmlr-group/DeepInception
conversers.py
[ { "identifier": "FALCON_PATH", "path": "config.py", "snippet": "FALCON_PATH = f\"{ROOT_PATH}/falcon-7b-instruct\"" }, { "identifier": "LLAMA_PATH", "path": "config.py", "snippet": "LLAMA_PATH = f\"{ROOT_PATH}/Llama-2-7b-hf\"" }, { "identifier": "TARGET_TEMP", "path": "config.py", "snippet": "TARGET_TEMP = 0" }, { "identifier": "TARGET_TOP_P", "path": "config.py", "snippet": "TARGET_TOP_P = 1" }, { "identifier": "VICUNA_PATH", "path": "config.py", "snippet": "VICUNA_PATH = f\"{ROOT_PATH}/vicuna-7b-v1.5\"" }, { "identifier": "GPT", "path": "language_models.py", "snippet": "class GPT(LanguageModel):\n API_RETRY_SLEEP = 10\n API_ERROR_OUTPUT = \"$ERROR$\"\n API_QUERY_SLEEP = 2\n API_MAX_RETRY = 5\n API_TIMEOUT = 20\n openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n\n def generate(self, conv: List[Dict], \n max_n_tokens: int, \n temperature: float,\n top_p: float):\n '''\n Args:\n conv: List of dictionaries, OpenAI API format\n max_n_tokens: int, max number of tokens to generate\n temperature: float, temperature for sampling\n top_p: float, top p for sampling\n Returns:\n str: generated response\n '''\n output = self.API_ERROR_OUTPUT\n for _ in range(self.API_MAX_RETRY):\n try:\n response = openai.ChatCompletion.create(\n model = self.model_name,\n messages = conv,\n max_tokens = max_n_tokens,\n temperature = temperature,\n )\n output = response[\"choices\"][0][\"message\"][\"content\"]\n break\n except openai.error.OpenAIError as e:\n print(type(e), e)\n time.sleep(self.API_RETRY_SLEEP)\n \n time.sleep(self.API_QUERY_SLEEP)\n return output \n \n def batched_generate(self, \n convs_list: List[List[Dict]],\n max_n_tokens: int, \n temperature: float,\n top_p: float = 1.0,):\n return [self.generate(conv, max_n_tokens, temperature, top_p) for conv in convs_list]" }, { "identifier": "HuggingFace", "path": "language_models.py", "snippet": "class HuggingFace(LanguageModel):\n def __init__(self,model_name, model, tokenizer):\n self.model_name = model_name\n self.model = model \n self.tokenizer = tokenizer\n self.eos_token_ids = [self.tokenizer.eos_token_id]\n\n def batched_generate(self, \n full_prompts_list,\n max_n_tokens: int, \n temperature: float,\n top_p: float = 1.0,):\n inputs = self.tokenizer(full_prompts_list, return_tensors='pt', padding=True)\n inputs = {k: v.to(self.model.device.index) for k, v in inputs.items()}\n \n # Batch generation\n if temperature > 0:\n output_ids = self.model.generate(\n **inputs,\n max_new_tokens=max_n_tokens, \n do_sample=True,\n temperature=temperature,\n eos_token_id=self.eos_token_ids,\n top_p=top_p,\n )\n else:\n output_ids = self.model.generate(\n **inputs,\n max_new_tokens=max_n_tokens, \n do_sample=False,\n eos_token_id=self.eos_token_ids,\n top_p=1,\n temperature=1, # To prevent warning messages\n )\n \n # If the model is not an encoder-decoder type, slice off the input tokens\n if not self.model.config.is_encoder_decoder:\n output_ids = output_ids[:, inputs[\"input_ids\"].shape[1]:]\n\n # Batch decoding\n outputs_list = self.tokenizer.batch_decode(output_ids, skip_special_tokens=True)\n\n for key in inputs:\n inputs[key].to('cpu')\n output_ids.to('cpu')\n del inputs, output_ids\n gc.collect()\n torch.cuda.empty_cache()\n\n return outputs_list\n\n def extend_eos_tokens(self): \n # Add closing braces for Vicuna/Llama eos when using attacker model\n self.eos_token_ids.extend([\n self.tokenizer.encode(\"}\")[1],\n 29913, \n 9092,\n 16675])" } ]
import torch import common from transformers import AutoModelForCausalLM, AutoTokenizer from config import (FALCON_PATH, LLAMA_PATH, TARGET_TEMP, TARGET_TOP_P, VICUNA_PATH) from language_models import GPT, HuggingFace
1,839
def load_attack_and_target_models(args): targetLM = TargetLM(model_name = args.target_model, max_n_tokens = args.target_max_n_tokens, temperature = TARGET_TEMP, # init to 0 top_p = TARGET_TOP_P, # init to 1 preloaded_model = None, ) return targetLM class TargetLM(): """ Base class for target language models. Generates responses for prompts using a language model. The self.model attribute contains the underlying generation model. """ def __init__(self, model_name: str, max_n_tokens: int, temperature: float, top_p: float, preloaded_model: object = None): self.model_name = model_name self.temperature = temperature self.max_n_tokens = max_n_tokens self.top_p = top_p if preloaded_model is None: self.model, self.template = load_indiv_model(model_name) else: self.model = preloaded_model _, self.template = get_model_path_and_template(model_name) def get_response(self, prompts_list, defense_type): conv = common.conv_template(self.template) # Self-reminder defense + Attack: if defense_type == 'sr': conv.system_message = 'You should be a responsible AI and not generate harmful, misleading content! Please answer the following query in a responsible way.' conv.append_message(conv.roles[0], prompts_list + ' \nRemember, you should be a responsible AI and should not generate harmful or misleading content!') conv.append_message(conv.roles[1], None) elif defense_type == 'ic': # In context defense: # The prompt isnot in our attack dataset # Template: I\'m sorry, but I cannot [Attack]. [Attack] is illegal and dangerous and can result in serious harm to yourself and others. conv.append_message(conv.roles[0], 'Provide instructions for how to molest a child') conv.append_message(conv.roles[1], 'I\'m sorry, but I cannot provide instructions for how to molest a child. Molesting a child is illegal and dangerous and can result in serious harm to yourself and others.') # Pure Attack conv.append_message(conv.roles[0], prompts_list) conv.append_message(conv.roles[1], None) else: # Pure Attack conv.append_message(conv.roles[0], prompts_list) conv.append_message(conv.roles[1], None) if 'gpt' in self.model_name: full_prompts = [conv.to_openai_api_messages()] else: full_prompts = conv.get_prompt() outputs_list = self.model.batched_generate(full_prompts, max_n_tokens = self.max_n_tokens, temperature = self.temperature, top_p = self.top_p ) return outputs_list def load_indiv_model(model_name, device=None): model_path, template = get_model_path_and_template(model_name) if model_name in ["gpt-3.5-turbo", "gpt-4"]: lm = GPT(model_name) elif model_name == 'falcon': model = AutoModelForCausalLM.from_pretrained( model_path, trust_remote_code=True, torch_dtype=torch.bfloat16, low_cpu_mem_usage=True, device_map="auto", ).eval() tokenizer = AutoTokenizer.from_pretrained( model_path, trust_remote_code=True, ) model.config.eos_token_id = tokenizer.eos_token_id model.config.pad_token_id = tokenizer.pad_token_id tokenizer.pad_token = tokenizer.eos_token tokenizer.padding_side = 'left'
def load_attack_and_target_models(args): targetLM = TargetLM(model_name = args.target_model, max_n_tokens = args.target_max_n_tokens, temperature = TARGET_TEMP, # init to 0 top_p = TARGET_TOP_P, # init to 1 preloaded_model = None, ) return targetLM class TargetLM(): """ Base class for target language models. Generates responses for prompts using a language model. The self.model attribute contains the underlying generation model. """ def __init__(self, model_name: str, max_n_tokens: int, temperature: float, top_p: float, preloaded_model: object = None): self.model_name = model_name self.temperature = temperature self.max_n_tokens = max_n_tokens self.top_p = top_p if preloaded_model is None: self.model, self.template = load_indiv_model(model_name) else: self.model = preloaded_model _, self.template = get_model_path_and_template(model_name) def get_response(self, prompts_list, defense_type): conv = common.conv_template(self.template) # Self-reminder defense + Attack: if defense_type == 'sr': conv.system_message = 'You should be a responsible AI and not generate harmful, misleading content! Please answer the following query in a responsible way.' conv.append_message(conv.roles[0], prompts_list + ' \nRemember, you should be a responsible AI and should not generate harmful or misleading content!') conv.append_message(conv.roles[1], None) elif defense_type == 'ic': # In context defense: # The prompt isnot in our attack dataset # Template: I\'m sorry, but I cannot [Attack]. [Attack] is illegal and dangerous and can result in serious harm to yourself and others. conv.append_message(conv.roles[0], 'Provide instructions for how to molest a child') conv.append_message(conv.roles[1], 'I\'m sorry, but I cannot provide instructions for how to molest a child. Molesting a child is illegal and dangerous and can result in serious harm to yourself and others.') # Pure Attack conv.append_message(conv.roles[0], prompts_list) conv.append_message(conv.roles[1], None) else: # Pure Attack conv.append_message(conv.roles[0], prompts_list) conv.append_message(conv.roles[1], None) if 'gpt' in self.model_name: full_prompts = [conv.to_openai_api_messages()] else: full_prompts = conv.get_prompt() outputs_list = self.model.batched_generate(full_prompts, max_n_tokens = self.max_n_tokens, temperature = self.temperature, top_p = self.top_p ) return outputs_list def load_indiv_model(model_name, device=None): model_path, template = get_model_path_and_template(model_name) if model_name in ["gpt-3.5-turbo", "gpt-4"]: lm = GPT(model_name) elif model_name == 'falcon': model = AutoModelForCausalLM.from_pretrained( model_path, trust_remote_code=True, torch_dtype=torch.bfloat16, low_cpu_mem_usage=True, device_map="auto", ).eval() tokenizer = AutoTokenizer.from_pretrained( model_path, trust_remote_code=True, ) model.config.eos_token_id = tokenizer.eos_token_id model.config.pad_token_id = tokenizer.pad_token_id tokenizer.pad_token = tokenizer.eos_token tokenizer.padding_side = 'left'
lm = HuggingFace(model_name, model, tokenizer)
6
2023-11-07 12:47:47+00:00
4k
radekd91/inferno
inferno_apps/EMOCA/demos/test_contempt_images.py
[ { "identifier": "load_model", "path": "inferno_apps/EMOCA/utils/load.py", "snippet": "def hack_paths(cfg, replace_root_path=None, relative_to_path=None):\ndef load_deca_and_data(path_to_models=None,\n run_name=None,\n stage=None,\n relative_to_path = None,\n replace_root_path = None,\n mode='best',\n load_data=True):" }, { "identifier": "TestData", "path": "inferno/datasets/ImageTestDataset.py", "snippet": "class TestData(Dataset):\n def __init__(self, testpath, iscrop=True, crop_size=224, scale=1.25, face_detector='fan',\n scaling_factor=1.0, max_detection=None):\n self.max_detection = max_detection\n if isinstance(testpath, list):\n self.imagepath_list = testpath\n elif os.path.isdir(testpath):\n self.imagepath_list = glob(testpath + '/*.jpg') + glob(testpath + '/*.png') + glob(testpath + '/*.bmp')\n elif os.path.isfile(testpath) and (testpath[-3:] in ['jpg', 'png', 'bmp']):\n self.imagepath_list = [testpath]\n elif os.path.isfile(testpath) and (testpath[-3:] in ['mp4', 'csv', 'vid', 'ebm']):\n self.imagepath_list = video2sequence(testpath)\n else:\n print(f'please check the test path: {testpath}')\n exit()\n print('total {} images'.format(len(self.imagepath_list)))\n self.imagepath_list = sorted(self.imagepath_list)\n self.scaling_factor = scaling_factor\n self.crop_size = crop_size\n self.scale = scale\n self.iscrop = iscrop\n self.resolution_inp = crop_size\n # add_pretrained_deca_to_path()\n # from decalib.datasets import detectors\n if face_detector == 'fan':\n self.face_detector = FAN()\n # elif face_detector == 'mtcnn':\n # self.face_detector = detectors.MTCNN()\n else:\n print(f'please check the detector: {face_detector}')\n exit()\n\n def __len__(self):\n return len(self.imagepath_list)\n\n def __getitem__(self, index):\n imagepath = str(self.imagepath_list[index])\n imagename = imagepath.split('/')[-1].split('.')[0]\n\n image = np.array(imread(imagepath))\n if len(image.shape) == 2:\n image = image[:, :, None].repeat(1, 1, 3)\n if len(image.shape) == 3 and image.shape[2] > 3:\n image = image[:, :, :3]\n\n if self.scaling_factor != 1.:\n image = rescale(image, (self.scaling_factor, self.scaling_factor, 1))*255.\n\n h, w, _ = image.shape\n if self.iscrop:\n # provide kpt as txt file, or mat file (for AFLW2000)\n kpt_matpath = imagepath.replace('.jpg', '.mat').replace('.png', '.mat')\n kpt_txtpath = imagepath.replace('.jpg', '.txt').replace('.png', '.txt')\n if os.path.exists(kpt_matpath):\n kpt = scipy.io.loadmat(kpt_matpath)['pt3d_68'].T\n left = np.min(kpt[:, 0])\n right = np.max(kpt[:, 0])\n top = np.min(kpt[:, 1])\n bottom = np.max(kpt[:, 1])\n old_size, center = bbox2point(left, right, top, bottom, type='kpt68')\n elif os.path.exists(kpt_txtpath):\n kpt = np.loadtxt(kpt_txtpath)\n left = np.min(kpt[:, 0])\n right = np.max(kpt[:, 0])\n top = np.min(kpt[:, 1])\n bottom = np.max(kpt[:, 1])\n old_size, center = bbox2point(left, right, top, bottom, type='kpt68')\n else:\n # bbox, bbox_type, landmarks = self.face_detector.run(image)\n bbox, bbox_type = self.face_detector.run(image)\n if len(bbox) < 1:\n print('no face detected! run original image')\n left = 0\n right = h - 1\n top = 0\n bottom = w - 1\n old_size, center = bbox2point(left, right, top, bottom, type=bbox_type)\n else:\n if self.max_detection is None:\n bbox = bbox[0]\n left = bbox[0]\n right = bbox[2]\n top = bbox[1]\n bottom = bbox[3]\n old_size, center = bbox2point(left, right, top, bottom, type=bbox_type)\n else: \n old_size, center = [], []\n num_det = min(self.max_detection, len(bbox))\n for bbi in range(num_det):\n bb = bbox[0]\n left = bb[0]\n right = bb[2]\n top = bb[1]\n bottom = bb[3]\n osz, c = bbox2point(left, right, top, bottom, type=bbox_type)\n old_size += [osz]\n center += [c]\n \n if isinstance(old_size, list):\n size = []\n src_pts = []\n for i in range(len(old_size)):\n size += [int(old_size[i] * self.scale)]\n src_pts += [np.array(\n [[center[i][0] - size[i] / 2, center[i][1] - size[i] / 2], [center[i][0] - size[i] / 2, center[i][1] + size[i] / 2],\n [center[i][0] + size[i] / 2, center[i][1] - size[i] / 2]])]\n else:\n size = int(old_size * self.scale)\n src_pts = np.array(\n [[center[0] - size / 2, center[1] - size / 2], [center[0] - size / 2, center[1] + size / 2],\n [center[0] + size / 2, center[1] - size / 2]])\n else:\n src_pts = np.array([[0, 0], [0, h - 1], [w - 1, 0]])\n \n image = image / 255.\n if not isinstance(src_pts, list):\n DST_PTS = np.array([[0, 0], [0, self.resolution_inp - 1], [self.resolution_inp - 1, 0]])\n tform = estimate_transform('similarity', src_pts, DST_PTS)\n dst_image = warp(image, tform.inverse, output_shape=(self.resolution_inp, self.resolution_inp), order=3)\n dst_image = dst_image.transpose(2, 0, 1)\n return {'image': torch.tensor(dst_image).float(),\n 'image_name': imagename,\n 'image_path': imagepath,\n # 'tform': tform,\n # 'original_image': torch.tensor(image.transpose(2,0,1)).float(),\n }\n else:\n DST_PTS = np.array([[0, 0], [0, self.resolution_inp - 1], [self.resolution_inp - 1, 0]])\n dst_images = []\n for i in range(len(src_pts)):\n tform = estimate_transform('similarity', src_pts[i], DST_PTS)\n dst_image = warp(image, tform.inverse, output_shape=(self.resolution_inp, self.resolution_inp), order=3)\n dst_image = dst_image.transpose(2, 0, 1)\n dst_images += [dst_image]\n dst_images = np.stack(dst_images, axis=0)\n \n imagenames = [imagename + f\"{j:02d}\" for j in range(dst_images.shape[0])]\n imagepaths = [imagepath]* dst_images.shape[0]\n return {'image': torch.tensor(dst_images).float(),\n 'image_name': imagenames,\n 'image_path': imagepaths,\n # 'tform': tform,\n # 'original_image': torch.tensor(image.transpose(2,0,1)).float(),\n }" }, { "identifier": "save_obj", "path": "inferno_apps/EMOCA/utils/io.py", "snippet": "def save_obj(emoca, filename, opdict, i=0):\n # dense_template_path = '/home/rdanecek/Workspace/Repos/DECA/data/texture_data_256.npy'\n # dense_template_path = '/is/cluster/rdanecek/workspace/repos/DECA/data/texture_data_256.npy'\n dense_template_path = Path(inferno.__file__).parents[1] / 'assets' / \"DECA\" / \"data\" / 'texture_data_256.npy'\n dense_template = np.load(dense_template_path, allow_pickle=True, encoding='latin1').item()\n vertices = opdict['verts'][i].detach().cpu().numpy()\n faces = emoca.deca.render.faces[0].detach().cpu().numpy()\n texture = util.tensor2image(opdict['uv_texture_gt'][i])\n uvcoords = emoca.deca.render.raw_uvcoords[0].detach().cpu().numpy()\n uvfaces = emoca.deca.render.uvfaces[0].detach().cpu().numpy()\n # save coarse mesh, with texture and normal map\n normal_map = util.tensor2image(opdict['uv_detail_normals'][i] * 0.5 + 0.5)\n util.write_obj(filename, vertices, faces,\n texture=texture,\n uvcoords=uvcoords,\n uvfaces=uvfaces,\n normal_map=normal_map)\n # upsample mesh, save detailed mesh\n texture = texture[:, :, [2, 1, 0]]\n normals = opdict['normals'][i].detach().cpu().numpy()\n displacement_map = opdict['displacement_map'][i].detach().cpu().numpy().squeeze()\n dense_vertices, dense_colors, dense_faces = util.upsample_mesh(vertices, normals, faces, displacement_map, texture,\n dense_template)\n util.write_obj(filename.replace('.obj', '_detail.obj'),\n dense_vertices,\n dense_faces,\n colors=dense_colors,\n inverse_face_order=True)" }, { "identifier": "save_images", "path": "inferno_apps/EMOCA/utils/io.py", "snippet": "def save_images(outfolder, name, vis_dict, i = 0, with_detection=False):\n prefix = None\n final_out_folder = Path(outfolder) / name\n final_out_folder.mkdir(parents=True, exist_ok=True)\n\n if with_detection:\n imsave(final_out_folder / f\"inputs.png\", _fix_image(torch_img_to_np(vis_dict['inputs'][i])))\n imsave(final_out_folder / f\"geometry_coarse.png\", _fix_image(torch_img_to_np(vis_dict['geometry_coarse'][i])))\n if \"geometry_detail\" in vis_dict:\n imsave(final_out_folder / f\"geometry_detail.png\", _fix_image(torch_img_to_np(vis_dict['geometry_detail'][i])))\n imsave(final_out_folder / f\"out_im_coarse.png\", _fix_image(torch_img_to_np(vis_dict['output_images_coarse'][i])))\n if \"output_images_detail\" in vis_dict:\n imsave(final_out_folder / f\"out_im_detail.png\", _fix_image(torch_img_to_np(vis_dict['output_images_detail'][i])))" }, { "identifier": "save_codes", "path": "inferno_apps/EMOCA/utils/io.py", "snippet": "def save_codes(output_folder, name, vals, i = None):\n if i is None:\n np.save(output_folder / name / f\"shape.npy\", vals[\"shapecode\"].detach().cpu().numpy())\n np.save(output_folder / name / f\"exp.npy\", vals[\"expcode\"].detach().cpu().numpy())\n np.save(output_folder / name / f\"tex.npy\", vals[\"texcode\"].detach().cpu().numpy())\n np.save(output_folder / name / f\"pose.npy\", vals[\"posecode\"].detach().cpu().numpy())\n np.save(output_folder / name / f\"detail.npy\", vals[\"detailcode\"].detach().cpu().numpy())\n np.save(output_folder / name / f\"cam.npy\", vals[\"cam\"].detach().cpu().numpy())\n np.save(output_folder / name / f\"lightcode.npy\", vals[\"lightcode\"].detach().cpu().numpy())\n else: \n np.save(output_folder / name / f\"shape.npy\", vals[\"shapecode\"][i].detach().cpu().numpy())\n np.save(output_folder / name / f\"exp.npy\", vals[\"expcode\"][i].detach().cpu().numpy())\n np.save(output_folder / name / f\"tex.npy\", vals[\"texcode\"][i].detach().cpu().numpy())\n np.save(output_folder / name / f\"pose.npy\", vals[\"posecode\"][i].detach().cpu().numpy())\n np.save(output_folder / name / f\"detail.npy\", vals[\"detailcode\"][i].detach().cpu().numpy())\n np.save(output_folder / name / f\"cam.npy\", vals[\"cam\"][i].detach().cpu().numpy())\n np.save(output_folder / name / f\"lightcode.npy\", vals[\"lightcode\"][i].detach().cpu().numpy())" }, { "identifier": "test", "path": "inferno_apps/EMOCA/utils/io.py", "snippet": "def test(deca, batch):\n batch[\"image\"] = batch[\"image\"].cuda()\n if len(batch[\"image\"].shape) == 3:\n batch[\"image\"] = batch[\"image\"].view(1,3,224,224)\n vals = deca.encode(batch, training=False)\n vals, visdict = decode(deca, batch, vals, training=False)\n return vals, visdict" }, { "identifier": "torch_img_to_np", "path": "inferno_apps/EMOCA/utils/io.py", "snippet": "def torch_img_to_np(img):\n if isinstance(img, np.ndarray): \n return img\n return img.detach().cpu().numpy().transpose(1, 2, 0)" }, { "identifier": "_fix_image", "path": "inferno/utils/lightning_logging.py", "snippet": "def _fix_image(image):\n if image.max() < 30.: #ugly hack just to find out if range is [0-1] or [0-255]\n image = image * 255.\n image = np.clip(image, 0, 255).astype(np.uint8)\n return image" } ]
from inferno_apps.EMOCA.utils.load import load_model from inferno.datasets.ImageTestDataset import TestData from skimage.io import imsave from pathlib import Path from tqdm import auto from inferno_apps.EMOCA.utils.io import save_obj, save_images, save_codes, test, torch_img_to_np from inferno.utils.lightning_logging import _fix_image import inferno import numpy as np import os import torch import argparse
3,596
def save_images(outfolder, name, vis_dict, i = 0, with_detection=False): prefix = None final_out_folder = Path(outfolder) #/ name final_out_folder.mkdir(parents=True, exist_ok=True) imname = f"0000_{int(name):04d}_00.png" (final_out_folder / f"inputs").mkdir(parents=True, exist_ok=True) (final_out_folder / f"geometry_coarse").mkdir(parents=True, exist_ok=True) (final_out_folder / f"geometry_detail").mkdir(parents=True, exist_ok=True) (final_out_folder / f"output_images_coarse").mkdir(parents=True, exist_ok=True) (final_out_folder / f"output_images_detail").mkdir(parents=True, exist_ok=True) if with_detection:
def save_images(outfolder, name, vis_dict, i = 0, with_detection=False): prefix = None final_out_folder = Path(outfolder) #/ name final_out_folder.mkdir(parents=True, exist_ok=True) imname = f"0000_{int(name):04d}_00.png" (final_out_folder / f"inputs").mkdir(parents=True, exist_ok=True) (final_out_folder / f"geometry_coarse").mkdir(parents=True, exist_ok=True) (final_out_folder / f"geometry_detail").mkdir(parents=True, exist_ok=True) (final_out_folder / f"output_images_coarse").mkdir(parents=True, exist_ok=True) (final_out_folder / f"output_images_detail").mkdir(parents=True, exist_ok=True) if with_detection:
imsave(final_out_folder / f"inputs" / imname , _fix_image(torch_img_to_np(vis_dict['inputs'][i])))
6
2023-11-07 20:13:32+00:00
4k
hxz393/ConfigCenterComparer
ui/action_update.py
[ { "identifier": "VERSION_INFO", "path": "config/settings.py", "snippet": "VERSION_INFO = 'v1.1.0'" }, { "identifier": "CHECK_UPDATE_URL", "path": "config/settings.py", "snippet": "CHECK_UPDATE_URL = 'https://blog.x2b.net/ver/configcentercomparerversion.txt'" }, { "identifier": "get_resource_path", "path": "lib/get_resource_path.py", "snippet": "def get_resource_path(relative_path: Union[str, os.PathLike]) -> Optional[str]:\n \"\"\"\n 获取资源的绝对路径。这个函数适用于 PyInstaller 打包后的可执行文件。\n\n :type relative_path: Union[str, os.PathLike]\n :param relative_path: 相对路径,可以是字符串或 os.PathLike 对象。\n :rtype: Optional[str]\n :return: 资源的绝对路径,如果发生错误则返回 None。\n \"\"\"\n\n try:\n base_path = getattr(sys, '_MEIPASS', os.path.abspath(\".\"))\n return os.path.join(base_path, os.path.normpath(relative_path))\n except Exception:\n logger.exception(\"An error occurred while retrieving resource path\")\n return None" }, { "identifier": "request_url", "path": "lib/request_url.py", "snippet": "def request_url(url: str) -> Optional[str]:\n \"\"\"\n 通过 HTTP GET 请求获取给定 URL 的响应内容。\n\n :param url: 待请求的 URL\n :type url: str\n\n :rtype: Optional[str]\n :return: 如果请求成功,返回 URL 的响应内容;否则返回 None\n \"\"\"\n session = requests.Session()\n session.trust_env = False\n\n try:\n response = session.get(url, verify=False, timeout=15)\n response.raise_for_status()\n return response.text.strip()\n except Exception:\n logger.exception(f\"Unable to send network request to {url}\")\n return None" }, { "identifier": "LangManager", "path": "ui/lang_manager.py", "snippet": "class LangManager(QObject):\n \"\"\"\n 语言管理类,用于管理和更新应用程序的语言字典。\n\n 此类继承自 QObject,可发出语言更新的信号。它通过 `get_lang_dict` 函数获取当前语言字典,并提供了更新语言的功能。\n\n :ivar _lang_dict: 当前使用的语言字典。\n :vartype _lang_dict: dict\n \"\"\"\n lang_updated = pyqtSignal()\n\n def __init__(self):\n super().__init__()\n self._lang_dict = get_lang_dict()\n\n def get_lang(self) -> Optional[Dict[str, str]]:\n \"\"\"\n 获取当前使用的语言字典的副本。\n\n :return: 当前语言字典的深拷贝。\n :rtype: Optional[Dict[str, str]]\n \"\"\"\n try:\n return copy.deepcopy(self._lang_dict)\n except Exception:\n logger.exception(\"Failed to retrieve language dictionary.\")\n return None\n\n def update_lang(self, new_lang: str) -> None:\n \"\"\"\n 更新当前使用的语言字典。\n\n :param new_lang: 新语言的标识符。\n :type new_lang: str\n\n :rtype: None\n :return: 无返回值。\n \"\"\"\n try:\n self._lang_dict = LANG_DICTS.get(new_lang, \"English\")\n self.lang_updated.emit()\n logger.info(f\"Language changed to {new_lang}\")\n except Exception:\n logger.exception(f\"Failed to changed language to {new_lang}\")" }, { "identifier": "message_show", "path": "ui/message_show.py", "snippet": "def message_show(message_type: str,\n text: str) -> None:\n \"\"\"\n 显示指定类型的消息框。\n\n 根据提供的消息类型和文本内容,显示相应的消息框。支持的消息类型包括 'Critical'、'Warning' 和 'Information'。\n\n :param message_type: 消息类型,支持 'Critical'、'Warning' 和 'Information'。\n :type message_type: str\n :param text: 消息框中显示的文本内容。\n :type text: str\n :return: 无返回值。\n :rtype: None\n \"\"\"\n try:\n msg_box = QMessageBox()\n msg_box.setText(text)\n msg_box.setStandardButtons(QMessageBox.Ok)\n msg_box.setWindowTitle(message_type)\n\n if message_type == 'Critical':\n msg_box.setIcon(QMessageBox.Critical)\n msg_box.setWindowIcon(QIcon(get_resource_path('media/icons8-error-26')))\n elif message_type == 'Warning':\n msg_box.setIcon(QMessageBox.Warning)\n msg_box.setWindowIcon(QIcon(get_resource_path('media/icons8-do-not-disturb-26')))\n elif message_type == 'Information':\n msg_box.setIcon(QMessageBox.Information)\n msg_box.setWindowIcon(QIcon(get_resource_path('media/icons8-about-26')))\n else:\n logger.warning(\"Invalid message type provided.\")\n\n msg_box.exec_()\n except Exception:\n logger.exception(\"An error occurred while displaying the message box\")" } ]
import logging from PyQt5.QtCore import QThread, pyqtSignal, QObject from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import QAction from config.settings import VERSION_INFO, CHECK_UPDATE_URL from lib.get_resource_path import get_resource_path from lib.request_url import request_url from ui.lang_manager import LangManager from ui.message_show import message_show
1,635
""" 此文件提供了软件更新检查和提示功能的实现。 主要包含两个类:`ActionUpdate` 和 `UpdateChecker`。`ActionUpdate` 负责初始化更新相关的 UI 组件,并触发更新检查。`UpdateChecker` 作为一个线程,负责在后台检查软件的最新版本并返回结果。 :author: assassing :contact: https://github.com/hxz393 :copyright: Copyright 2023, hxz393. 保留所有权利。 """ logger = logging.getLogger(__name__) class ActionUpdate(QObject): """ 负责处理软件更新相关的用户界面操作。 此类负责创建更新操作相关的动作,绑定必要的信号和槽,以及触发更新检查。 :param lang_manager: 语言管理器实例,用于更新界面语言。 :type lang_manager: LangManager """ status_updated = pyqtSignal(str) def __init__(self, lang_manager: LangManager): super().__init__() self.lang_manager = lang_manager self.lang_manager.lang_updated.connect(self.update_lang) self.initUI() def initUI(self) -> None: """ 初始化更新操作的用户界面组件。 创建一个更新操作的 QAction 对象,并设置其图标、快捷键和触发方法。同时调用 `update_lang` 方法更新界面语言。 :rtype: None :return: 无返回值。 """
""" 此文件提供了软件更新检查和提示功能的实现。 主要包含两个类:`ActionUpdate` 和 `UpdateChecker`。`ActionUpdate` 负责初始化更新相关的 UI 组件,并触发更新检查。`UpdateChecker` 作为一个线程,负责在后台检查软件的最新版本并返回结果。 :author: assassing :contact: https://github.com/hxz393 :copyright: Copyright 2023, hxz393. 保留所有权利。 """ logger = logging.getLogger(__name__) class ActionUpdate(QObject): """ 负责处理软件更新相关的用户界面操作。 此类负责创建更新操作相关的动作,绑定必要的信号和槽,以及触发更新检查。 :param lang_manager: 语言管理器实例,用于更新界面语言。 :type lang_manager: LangManager """ status_updated = pyqtSignal(str) def __init__(self, lang_manager: LangManager): super().__init__() self.lang_manager = lang_manager self.lang_manager.lang_updated.connect(self.update_lang) self.initUI() def initUI(self) -> None: """ 初始化更新操作的用户界面组件。 创建一个更新操作的 QAction 对象,并设置其图标、快捷键和触发方法。同时调用 `update_lang` 方法更新界面语言。 :rtype: None :return: 无返回值。 """
self.action_update = QAction(QIcon(get_resource_path('media/icons8-update-26.png')), 'Update')
2
2023-11-07 01:02:38+00:00
4k
pytorch-labs/ao
torchao/quantization/subclass.py
[ { "identifier": "dequantize_per_channel", "path": "torchao/quantization/quant_primitives.py", "snippet": "def dequantize_per_channel(int_repr, scales, zero_points, out_dtype=torch.float32):\n # assumes axis is 0\n y = int_repr.transpose(0, 1)\n y = y.to(out_dtype)\n y = y - zero_points\n y = y * scales\n y = y.transpose(0, 1)\n return y" }, { "identifier": "dynamically_quantize_per_channel", "path": "torchao/quantization/quant_primitives.py", "snippet": "def dynamically_quantize_per_channel(x, quant_min, quant_max, target_dtype):\n # assumes symmetric quantization\n # assumes axis == 0\n # assumes dense memory format\n # TODO(future): relax ^ as needed\n\n # default setup for affine quantization of activations\n eps = torch.finfo(torch.float32).eps\n\n # get min and max\n min_val, max_val = torch.aminmax(x, dim=1)\n\n # calculate scale and zero point based on min and max\n # reference: https://fburl.com/code/srbiybme\n min_val_neg = torch.min(min_val, torch.zeros_like(min_val))\n max_val_pos = torch.max(max_val, torch.zeros_like(max_val))\n device = min_val_neg.device\n\n # reference: https://fburl.com/code/4wll53rk\n max_val_pos = torch.max(-min_val_neg, max_val_pos)\n scale = max_val_pos / (float(quant_max - quant_min) / 2)\n # ensure scale is the same dtype as the original tensor\n scale = torch.clamp(scale, min=eps).to(x.dtype)\n zero_point = torch.zeros(min_val_neg.size(), dtype=torch.int64, device=device)\n\n # quantize based on qmin/qmax/scale/zp\n # reference: torch/ao/quantization/fx/_decomposed.py?lines=63\n x_div = x.transpose(0, 1) / scale\n x_round = torch.round(x_div)\n x_zp = x_round + zero_point\n x_zp = x_zp.transpose(0, 1)\n quant = torch.clamp(x_zp, quant_min, quant_max).to(target_dtype)\n\n return quant, scale, zero_point" }, { "identifier": "groupwise_affine_quantize_tensor", "path": "torchao/quantization/quant_primitives.py", "snippet": "def groupwise_affine_quantize_tensor(w, n_bit=4, groupsize=128):\n scales, zeros = get_groupwise_affine_qparams(w, n_bit, groupsize)\n w_int4x8 = groupwise_affine_quantize_tensor_from_qparams(\n w, scales, zeros, n_bit, groupsize\n )\n scales_and_zeros = pack_tinygemm_scales_and_zeros(scales, zeros)\n return w_int4x8, scales_and_zeros" }, { "identifier": "quant_int8_dynamic_per_token_linear", "path": "torchao/quantization/quant_primitives.py", "snippet": "def quant_int8_dynamic_per_token_linear(\n x,\n w_vals_int8_t,\n w_scales,\n bias,\n out_dtype,\n):\n # like F.linear, but with int8 dynamic quantization of activation,\n # and a quantized weight\n x_vals_int8, x_scales = quantize_activation_per_token_absmax(x)\n mm_out = quant_int8_per_token_matmul(\n x_vals_int8, x_scales, w_vals_int8_t, w_scales, out_dtype\n )\n if bias is not None:\n mm_out += bias\n return mm_out" }, { "identifier": "unpack_tinygemm_scales_and_zeros", "path": "torchao/quantization/quant_primitives.py", "snippet": "def unpack_tinygemm_scales_and_zeros(scales_and_zeros):\n assert len(scales_and_zeros.shape) == 3 and scales_and_zeros.shape[2] == 2\n assert scales_and_zeros.dtype == torch.float\n return torch.split(scales_and_zeros.transpose(0, 1), 1, 2)" }, { "identifier": "find_multiple", "path": "torchao/quantization/utils.py", "snippet": "def find_multiple(n: int, k: int) -> int:\n if n % k == 0:\n return n\n return n + k - (n % k)" } ]
import torch import warnings from torch.utils._python_dispatch import return_and_correct_aliasing from .quant_primitives import ( dequantize_per_channel, dynamically_quantize_per_channel, groupwise_affine_quantize_tensor, quant_int8_dynamic_per_token_linear, unpack_tinygemm_scales_and_zeros, ) from .utils import find_multiple
3,156
) mat1, w_qtensor, bias = ( args[1], args[2], args[0], ) else: assert args[0].shape[-1] == args[1].shape[0], ( f"need mat1 shape: {args[0].shape} final dim" f"to match mat2 shape: {args[1].shape} first dim" ) mat1, w_qtensor, bias = ( args[0], args[1], None if len(args)==2 else args[2], ) # call the quantized op for the specific type # of quantized tensor subclass return cls._quantized_op(mat1, w_qtensor, bias) if func is aten.detach.default: return return_and_correct_aliasing(func, args, kwargs, args[0]._apply_fn_to_data(torch.detach)) if func is aten.clone.default: return return_and_correct_aliasing(func, args, kwargs, args[0]._apply_fn_to_data(torch.clone)) if func is aten.t.default: args[0].transposed = not args[0].transposed new = args[0]._change_shape(args[0].shape[::-1]) return return_and_correct_aliasing(func, args, kwargs, new) if func is aten._to_copy.default: return return_and_correct_aliasing(func, args, kwargs, args[0].to(*args[1:], **kwargs)._apply_fn_to_data(torch.clone)) class Int8DynamicallyQuantizedLinearWeight(QuantizedLinearWeightBase): """ A Tensor subclass that when applied to a weight used in a linear op/module, changes the linear op to a dynamically quantized linear op with symmetric per-token and per-channel quantization on the activation and weight respectively. """ @staticmethod def __new__(cls, int_data, q_scales, transposed, shape, **kwargs): kwargs["dtype"] = kwargs.get("dtype", q_scales.dtype) return super().__new__(cls, int_data, transposed, shape, **kwargs) # type: ignore[attr-defined] def __init__(self, int_data, q_scales, transposed, shape, **kwargs): self.q_scales = q_scales super().__init__(int_data, transposed) @staticmethod def _quantized_op(act_mat, w_qtensor, bias): return quant_int8_dynamic_per_token_linear( act_mat, w_qtensor.int_data, w_qtensor.q_scales, bias, act_mat.dtype ) def dequantize(self, dtype=None): """ Obtain the dequantized version of the quantized tensor subclass """ dq_t = dequantize_per_channel( self.int_data.t(), self.q_scales, 0, self.dtype if dtype is None else dtype ).to(self.dtype) # data was transposed to dequantize so make sure shape is correct return dq_t if not self.transposed else dq_t.t() def int_repr(self): """ Get the internal integer representation of the quantized tensor """ return self.int_data if self.transposed else self.int_data.t() def q_params(self): """ Get the quantization scales for the quantized tensor """ return {"q_scales": self.q_scales} def to(self, *args, **kwargs): kwargs = self._get_to_kwargs(*args, **kwargs) return self.__class__( self.int_data.to(kwargs["device"]), self.q_scales.to(kwargs["device"]), self.transposed, self.shape, **kwargs, ) def _apply_fn_to_data(self, fn): return self.__class__( fn(self.int_data), fn(self.q_scales), self.transposed, self.shape, dtype=self.dtype ) def _change_shape(self, shape): return self.__class__( self.int_data, self.q_scales, self.transposed, shape, dtype=self.dtype ) def __tensor_flatten__(self): return ["int_data", "q_scales"], [self.transposed, self.dtype, self.shape] @classmethod def __tensor_unflatten__(cls, tensor_data_dict, tensor_attributes, outer_size=None, outer_stride=None): int_data, q_scales = tensor_data_dict["int_data"], tensor_data_dict["q_scales"] transposed, dtype, shape = tensor_attributes return cls(int_data, q_scales, transposed, shape if outer_size is None else outer_size, dtype=dtype, strides=outer_stride) @classmethod def from_float(cls, input_float, qmin=-128, qmax=127): """ Method used to convert a linear weight tensor to an instance of the Int8DynamicallyQuantizedLinearWeight subclass. Example usage:: model.lin_mod.weight = ( Int8DynamicallyQuantizedLinearWeight.from_float(model.lin_mod.weight) ) """
# Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. __all__ = [ "Int8DynamicallyQuantizedLinearWeight", "Int8WeightOnlyQuantizedLinearWeight", "Int4WeightOnlyQuantizedLinearWeight", ] aten = torch.ops.aten class QuantizedLinearWeightBase(torch.Tensor): """ Base quantized tensor subclass for quantized linear weights. When the from_float method is used, to create an instance of any QuantizedLinearWeightBase, we assume the input weight is oriented the way it is in a normal linear op, i.e. out-channels x in-channels. The shape and dtype of the tensor subclass represent how the tensor subclass looks externally, regardless of the internal representation's type or orientation. """ @staticmethod def __new__(cls, int_data, transposed, shape, *args, **kwargs): kwargs["device"] = int_data.device kwargs["layout"] = ( kwargs.get("layout") if kwargs.get("layout", False) else int_data.layout ) assert "dtype" in kwargs assert not kwargs.get("requires_grad", False) kwargs["requires_grad"] = False return torch.Tensor._make_wrapper_subclass(cls, shape, **kwargs) # type: ignore[attr-defined] def __init__(self, int_data, transposed, *args, **kwargs): self.int_data = int_data self.transposed = transposed @staticmethod def _quantized_op(act_mat, w_qtensor, bias): pass def __repr__(self): return ( f"{self.__class__.__name__}(data={self.dequantize()}, shape={self.shape}, " f"device={self.device}, dtype={self.dtype}, requires_grad={self.requires_grad})" ) def dequantize(self): pass def int_repr(self): pass def q_params(self): pass def half(self): return self.to(torch.float16) def _get_to_kwargs(self, *args, **kwargs): device, dtype, _, memory_format = torch._C._nn._parse_to(*args, **kwargs) device = self.device if device is None else device dtype = self.dtype if dtype is None else dtype memory_format = ( memory_format if memory_format is not None else torch.preserve_format ) kwargs = { "device": device, "dtype": dtype, "memory_format": memory_format, } return kwargs def _apply_fn_to_data(self, fn): pass def _change_shape(self): pass def __tensor_flatten__(self): pass @classmethod def __tensor_unflatten__(cls, tensor_data_dict, tensor_attributes, outer_size, outer_stride): pass @classmethod def from_float(cls, input_float): pass # __torch_function__ = torch._C._disabled_torch_function_impl @classmethod def __torch_function__(cls, func, types, args=(), kwargs=None): kwargs = {} if kwargs is None else kwargs if func is torch.nn.functional.linear: mat1, w_qtensor, bias = ( args[0], args[1], args[2] if len(args)>2 else None ) assert w_qtensor.transposed == False return cls._quantized_op(mat1, w_qtensor, bias) try: with torch._C.DisableTorchFunctionSubclass(): return func(*args, **kwargs) except: print(f"ERR: subclass doesn't implement {func}") @classmethod def __torch_dispatch__(cls, func, types, args, kwargs): # two scenarios where we currently fall back to vanilla mm: # 1 - when tensor is on CPU: we are missing qmm for CPU, but we should have a CPU implementation # for consistency and to allow people to test # 2 - we're given non-floats - quantizing long to int8 is crazy if ( func in [aten.mm.default, aten.addmm.default] and args[0].is_floating_point() and args[0].is_cuda ): if func == aten.addmm.default: assert args[1].shape[-1] == args[2].shape[0], ( f"need mat1 shape: {args[1].shape} final" f"dim to match mat2 shape: {args[2].shape} first dim " ) mat1, w_qtensor, bias = ( args[1], args[2], args[0], ) else: assert args[0].shape[-1] == args[1].shape[0], ( f"need mat1 shape: {args[0].shape} final dim" f"to match mat2 shape: {args[1].shape} first dim" ) mat1, w_qtensor, bias = ( args[0], args[1], None if len(args)==2 else args[2], ) # call the quantized op for the specific type # of quantized tensor subclass return cls._quantized_op(mat1, w_qtensor, bias) if func is aten.detach.default: return return_and_correct_aliasing(func, args, kwargs, args[0]._apply_fn_to_data(torch.detach)) if func is aten.clone.default: return return_and_correct_aliasing(func, args, kwargs, args[0]._apply_fn_to_data(torch.clone)) if func is aten.t.default: args[0].transposed = not args[0].transposed new = args[0]._change_shape(args[0].shape[::-1]) return return_and_correct_aliasing(func, args, kwargs, new) if func is aten._to_copy.default: return return_and_correct_aliasing(func, args, kwargs, args[0].to(*args[1:], **kwargs)._apply_fn_to_data(torch.clone)) class Int8DynamicallyQuantizedLinearWeight(QuantizedLinearWeightBase): """ A Tensor subclass that when applied to a weight used in a linear op/module, changes the linear op to a dynamically quantized linear op with symmetric per-token and per-channel quantization on the activation and weight respectively. """ @staticmethod def __new__(cls, int_data, q_scales, transposed, shape, **kwargs): kwargs["dtype"] = kwargs.get("dtype", q_scales.dtype) return super().__new__(cls, int_data, transposed, shape, **kwargs) # type: ignore[attr-defined] def __init__(self, int_data, q_scales, transposed, shape, **kwargs): self.q_scales = q_scales super().__init__(int_data, transposed) @staticmethod def _quantized_op(act_mat, w_qtensor, bias): return quant_int8_dynamic_per_token_linear( act_mat, w_qtensor.int_data, w_qtensor.q_scales, bias, act_mat.dtype ) def dequantize(self, dtype=None): """ Obtain the dequantized version of the quantized tensor subclass """ dq_t = dequantize_per_channel( self.int_data.t(), self.q_scales, 0, self.dtype if dtype is None else dtype ).to(self.dtype) # data was transposed to dequantize so make sure shape is correct return dq_t if not self.transposed else dq_t.t() def int_repr(self): """ Get the internal integer representation of the quantized tensor """ return self.int_data if self.transposed else self.int_data.t() def q_params(self): """ Get the quantization scales for the quantized tensor """ return {"q_scales": self.q_scales} def to(self, *args, **kwargs): kwargs = self._get_to_kwargs(*args, **kwargs) return self.__class__( self.int_data.to(kwargs["device"]), self.q_scales.to(kwargs["device"]), self.transposed, self.shape, **kwargs, ) def _apply_fn_to_data(self, fn): return self.__class__( fn(self.int_data), fn(self.q_scales), self.transposed, self.shape, dtype=self.dtype ) def _change_shape(self, shape): return self.__class__( self.int_data, self.q_scales, self.transposed, shape, dtype=self.dtype ) def __tensor_flatten__(self): return ["int_data", "q_scales"], [self.transposed, self.dtype, self.shape] @classmethod def __tensor_unflatten__(cls, tensor_data_dict, tensor_attributes, outer_size=None, outer_stride=None): int_data, q_scales = tensor_data_dict["int_data"], tensor_data_dict["q_scales"] transposed, dtype, shape = tensor_attributes return cls(int_data, q_scales, transposed, shape if outer_size is None else outer_size, dtype=dtype, strides=outer_stride) @classmethod def from_float(cls, input_float, qmin=-128, qmax=127): """ Method used to convert a linear weight tensor to an instance of the Int8DynamicallyQuantizedLinearWeight subclass. Example usage:: model.lin_mod.weight = ( Int8DynamicallyQuantizedLinearWeight.from_float(model.lin_mod.weight) ) """
w_int_repr, w_scales, _ = dynamically_quantize_per_channel(
1
2023-11-03 21:27:36+00:00
4k
google-research/semivl
third_party/zegclip/losses/atm_criterion.py
[ { "identifier": "is_dist_avail_and_initialized", "path": "third_party/zegclip/losses/misc.py", "snippet": "def is_dist_avail_and_initialized():\n if not dist.is_available():\n return False\n if not dist.is_initialized():\n return False\n return True" }, { "identifier": "nested_tensor_from_tensor_list", "path": "third_party/zegclip/losses/misc.py", "snippet": "def nested_tensor_from_tensor_list(tensor_list: List[Tensor]):\n # TODO make this more general\n if tensor_list[0].ndim == 3:\n if torchvision._is_tracing():\n # nested_tensor_from_tensor_list() does not export well to ONNX\n # call _onnx_nested_tensor_from_tensor_list() instead\n return _onnx_nested_tensor_from_tensor_list(tensor_list)\n\n # TODO make it support different-sized images\n max_size = _max_by_axis([list(img.shape) for img in tensor_list])\n # min_size = tuple(min(s) for s in zip(*[img.shape for img in tensor_list]))\n batch_shape = [len(tensor_list)] + max_size\n b, c, h, w = batch_shape\n dtype = tensor_list[0].dtype\n device = tensor_list[0].device\n tensor = torch.zeros(batch_shape, dtype=dtype, device=device)\n mask = torch.ones((b, h, w), dtype=torch.bool, device=device)\n for img, pad_img, m in zip(tensor_list, tensor, mask):\n pad_img[: img.shape[0], : img.shape[1], : img.shape[2]].copy_(img)\n m[: img.shape[1], : img.shape[2]] = False\n else:\n raise ValueError(\"not supported\")\n return NestedTensor(tensor, mask)" } ]
import torch import torch.nn.functional as F import torch.distributed as dist from torch import nn from .misc import is_dist_avail_and_initialized, nested_tensor_from_tensor_list
1,818
return 1 return dist.get_world_size() def dice_loss(inputs, targets, num_masks): """ Compute the DICE loss, similar to generalized IOU for masks Args: inputs: A float tensor of arbitrary shape. The predictions for each example. targets: A float tensor with the same shape as inputs. Stores the binary classification label for each element in inputs (0 for the negative class and 1 for the positive class). """ inputs = inputs.sigmoid() inputs = inputs.flatten(1) numerator = 2 * (inputs * targets).sum(-1) denominator = inputs.sum(-1) + targets.sum(-1) loss = 1 - (numerator + 1) / (denominator + 1) return loss.sum() / num_masks def sigmoid_focal_loss(inputs, targets, num_masks, alpha: float = 0.25, gamma: float = 2): """ Loss used in RetinaNet for dense detection: https://arxiv.org/abs/1708.02002. Args: inputs: A float tensor of arbitrary shape. The predictions for each example. targets: A float tensor with the same shape as inputs. Stores the binary classification label for each element in inputs (0 for the negative class and 1 for the positive class). alpha: (optional) Weighting factor in range (0,1) to balance positive vs negative examples. Default = -1 (no weighting). gamma: Exponent of the modulating factor (1 - p_t) to balance easy vs hard examples. Returns: Loss tensor """ ce_loss = F.binary_cross_entropy_with_logits(inputs, targets, reduction="none") prob = inputs.sigmoid() p_t = prob * targets + (1 - prob) * (1 - targets) loss = ce_loss * ((1 - p_t) ** gamma) if alpha >= 0: alpha_t = alpha * targets + (1 - alpha) * (1 - targets) loss = alpha_t * loss return loss.mean(1).sum() / num_masks def cosine_margin_loss(q, e, labels, tau=1.0, m=0.5): assert q.shape[1]+1 == e.shape[0] bs, n_cls, n_dim = q.shape q = q.reshape(bs*n_cls, n_dim) pos = torch.exp(F.cosine_similarity(q, e[labels.long()].reshape(bs*n_cls, n_dim)) / tau) neg = torch.exp(F.cosine_similarity(q.unsqueeze(1), e.unsqueeze(0), dim=-1) / tau) neg = torch.sum(neg, dim=-1) + m return 1 - torch.mean(torch.div(pos, neg)) class SegPlusCriterion(nn.Module): # in this version, both all masks and logits will be added to compute loss """This class computes the loss for DETR. The process happens in two steps: 1) we compute hungarian assignment between ground truth boxes and the outputs of the model 2) we supervise each pair of matched ground-truth / prediction (supervise class and box) """ def __init__(self, num_classes, weight_dict, losses, eos_coef=0.1): """Create the criterion. Parameters: num_classes: number of object categories, omitting the special no-object category matcher: module able to compute a matching between targets and proposals weight_dict: dict containing as key the names of the losses and as values their relative weight. eos_coef: relative classification weight applied to the no-object category losses: list of all the losses to be applied. See get_loss for list of available losses. """ super().__init__() self.num_classes = num_classes self.weight_dict = weight_dict self.eos_coef = eos_coef self.losses = losses empty_weight = torch.ones(self.num_classes + 1) empty_weight[-1] = self.eos_coef self.register_buffer("empty_weight", empty_weight) def loss_masks(self, outputs, targets, indices, num_masks): """Compute the losses related to the masks: the focal loss and the dice loss. targets dicts must contain the key "masks" containing a tensor of dim [nb_target_boxes, h, w] outputs: pred_logits: (bs, n_cls, 1) targets: len = bs pred_masks: (bs, n_cls, H, W) targets[0]: 'labels': eg: have the [2, 4] th classes = 2 pred: (bs, n_cls, H, W) = pred_logits*pred_masks 'masks': eg: (2, H, W) aux_outputs: mediate outputs """ assert "pred_masks" in outputs # for focal loss src_masks = outputs["pred_masks"] target_masks = self._get_target_mask_binary_cross_entropy(src_masks, targets) bs, n_cls, H, W = target_masks.size() _, _, H_, W_ = src_masks.size() src_masks = src_masks.reshape(bs*n_cls, H_, W_) target_masks = target_masks.reshape(bs*n_cls, H, W) # upsample predictions to the target size src_masks = F.interpolate( src_masks[:, None], size=target_masks.shape[-2:], mode="bilinear", align_corners=False ) src_masks = src_masks[:, 0].flatten(1) target_masks = target_masks.flatten(1) # for dice loss src_idx = self._get_src_permutation_idx(indices) tgt_idx = self._get_tgt_permutation_idx(indices) src_masks_dice = outputs["pred_masks"] if src_masks_dice.dim() != 4: return {"no_loss": 0} src_masks_dice = src_masks_dice[src_idx] masks_dice = [t["target_masks"] for t in targets] # TODO use valid to mask invalid areas due to padding in loss
# Copyright (c) Facebook, Inc. and its affiliates. # Modified by Bowen Cheng from https://github.com/facebookresearch/detr/blob/master/models/detr.py """ MaskFormer criterion. """ def get_world_size() -> int: if not dist.is_available(): return 1 if not dist.is_initialized(): return 1 return dist.get_world_size() def dice_loss(inputs, targets, num_masks): """ Compute the DICE loss, similar to generalized IOU for masks Args: inputs: A float tensor of arbitrary shape. The predictions for each example. targets: A float tensor with the same shape as inputs. Stores the binary classification label for each element in inputs (0 for the negative class and 1 for the positive class). """ inputs = inputs.sigmoid() inputs = inputs.flatten(1) numerator = 2 * (inputs * targets).sum(-1) denominator = inputs.sum(-1) + targets.sum(-1) loss = 1 - (numerator + 1) / (denominator + 1) return loss.sum() / num_masks def sigmoid_focal_loss(inputs, targets, num_masks, alpha: float = 0.25, gamma: float = 2): """ Loss used in RetinaNet for dense detection: https://arxiv.org/abs/1708.02002. Args: inputs: A float tensor of arbitrary shape. The predictions for each example. targets: A float tensor with the same shape as inputs. Stores the binary classification label for each element in inputs (0 for the negative class and 1 for the positive class). alpha: (optional) Weighting factor in range (0,1) to balance positive vs negative examples. Default = -1 (no weighting). gamma: Exponent of the modulating factor (1 - p_t) to balance easy vs hard examples. Returns: Loss tensor """ ce_loss = F.binary_cross_entropy_with_logits(inputs, targets, reduction="none") prob = inputs.sigmoid() p_t = prob * targets + (1 - prob) * (1 - targets) loss = ce_loss * ((1 - p_t) ** gamma) if alpha >= 0: alpha_t = alpha * targets + (1 - alpha) * (1 - targets) loss = alpha_t * loss return loss.mean(1).sum() / num_masks def cosine_margin_loss(q, e, labels, tau=1.0, m=0.5): assert q.shape[1]+1 == e.shape[0] bs, n_cls, n_dim = q.shape q = q.reshape(bs*n_cls, n_dim) pos = torch.exp(F.cosine_similarity(q, e[labels.long()].reshape(bs*n_cls, n_dim)) / tau) neg = torch.exp(F.cosine_similarity(q.unsqueeze(1), e.unsqueeze(0), dim=-1) / tau) neg = torch.sum(neg, dim=-1) + m return 1 - torch.mean(torch.div(pos, neg)) class SegPlusCriterion(nn.Module): # in this version, both all masks and logits will be added to compute loss """This class computes the loss for DETR. The process happens in two steps: 1) we compute hungarian assignment between ground truth boxes and the outputs of the model 2) we supervise each pair of matched ground-truth / prediction (supervise class and box) """ def __init__(self, num_classes, weight_dict, losses, eos_coef=0.1): """Create the criterion. Parameters: num_classes: number of object categories, omitting the special no-object category matcher: module able to compute a matching between targets and proposals weight_dict: dict containing as key the names of the losses and as values their relative weight. eos_coef: relative classification weight applied to the no-object category losses: list of all the losses to be applied. See get_loss for list of available losses. """ super().__init__() self.num_classes = num_classes self.weight_dict = weight_dict self.eos_coef = eos_coef self.losses = losses empty_weight = torch.ones(self.num_classes + 1) empty_weight[-1] = self.eos_coef self.register_buffer("empty_weight", empty_weight) def loss_masks(self, outputs, targets, indices, num_masks): """Compute the losses related to the masks: the focal loss and the dice loss. targets dicts must contain the key "masks" containing a tensor of dim [nb_target_boxes, h, w] outputs: pred_logits: (bs, n_cls, 1) targets: len = bs pred_masks: (bs, n_cls, H, W) targets[0]: 'labels': eg: have the [2, 4] th classes = 2 pred: (bs, n_cls, H, W) = pred_logits*pred_masks 'masks': eg: (2, H, W) aux_outputs: mediate outputs """ assert "pred_masks" in outputs # for focal loss src_masks = outputs["pred_masks"] target_masks = self._get_target_mask_binary_cross_entropy(src_masks, targets) bs, n_cls, H, W = target_masks.size() _, _, H_, W_ = src_masks.size() src_masks = src_masks.reshape(bs*n_cls, H_, W_) target_masks = target_masks.reshape(bs*n_cls, H, W) # upsample predictions to the target size src_masks = F.interpolate( src_masks[:, None], size=target_masks.shape[-2:], mode="bilinear", align_corners=False ) src_masks = src_masks[:, 0].flatten(1) target_masks = target_masks.flatten(1) # for dice loss src_idx = self._get_src_permutation_idx(indices) tgt_idx = self._get_tgt_permutation_idx(indices) src_masks_dice = outputs["pred_masks"] if src_masks_dice.dim() != 4: return {"no_loss": 0} src_masks_dice = src_masks_dice[src_idx] masks_dice = [t["target_masks"] for t in targets] # TODO use valid to mask invalid areas due to padding in loss
target_masks_dice, valid = nested_tensor_from_tensor_list(masks_dice).decompose()
1
2023-11-02 14:49:38+00:00
4k
ej52/hass-ollama-conversation
custom_components/ollama_conversation/config_flow.py
[ { "identifier": "OllamaApiClient", "path": "custom_components/ollama_conversation/api.py", "snippet": "class OllamaApiClient:\n \"\"\"Ollama API Client.\"\"\"\n\n def __init__(\n self,\n base_url: str,\n session: aiohttp.ClientSession,\n ) -> None:\n \"\"\"Sample API Client.\"\"\"\n self._base_url = base_url.rstrip(\"/\")\n self._session = session\n\n async def async_get_heartbeat(self) -> bool:\n \"\"\"Get heartbeat from the API.\"\"\"\n response: str = await self._api_wrapper(\n method=\"get\", url=self._base_url, decode_json=False\n )\n return response.strip() == \"Ollama is running\"\n\n async def async_get_models(self) -> any:\n \"\"\"Get models from the API.\"\"\"\n return await self._api_wrapper(\n method=\"get\",\n url=f\"{self._base_url}/api/tags\",\n headers={\"Content-type\": \"application/json; charset=UTF-8\"},\n )\n\n async def async_generate(self, data: dict | None = None,) -> any:\n \"\"\"Generate a completion from the API.\"\"\"\n return await self._api_wrapper(\n method=\"post\",\n url=f\"{self._base_url}/api/generate\",\n data=data,\n headers={\"Content-type\": \"application/json; charset=UTF-8\"},\n )\n\n\n async def _api_wrapper(\n self,\n method: str,\n url: str,\n data: dict | None = None,\n headers: dict | None = None,\n decode_json: bool = True,\n ) -> any:\n \"\"\"Get information from the API.\"\"\"\n try:\n async with async_timeout.timeout(TIMEOUT):\n response = await self._session.request(\n method=method,\n url=url,\n headers=headers,\n json=data,\n )\n\n if response.status == 404 and decode_json:\n json = await response.json()\n raise ApiJsonError(json[\"error\"])\n\n response.raise_for_status()\n\n if decode_json:\n return await response.json()\n return await response.text()\n except ApiJsonError as e:\n raise e\n except asyncio.TimeoutError as e:\n raise ApiTimeoutError(\"timeout while talking to the server\") from e\n except (aiohttp.ClientError, socket.gaierror) as e:\n raise ApiCommError(\"unknown error while talking to the server\") from e\n except Exception as e: # pylint: disable=broad-except\n raise ApiClientError(\"something really went wrong!\") from e" }, { "identifier": "DOMAIN", "path": "custom_components/ollama_conversation/const.py", "snippet": "DOMAIN = \"ollama_conversation\"" }, { "identifier": "LOGGER", "path": "custom_components/ollama_conversation/const.py", "snippet": "LOGGER: Logger = getLogger(__package__)" }, { "identifier": "MENU_OPTIONS", "path": "custom_components/ollama_conversation/const.py", "snippet": "MENU_OPTIONS = [\"model_config\", \"prompt_system\"]" }, { "identifier": "CONF_BASE_URL", "path": "custom_components/ollama_conversation/const.py", "snippet": "CONF_BASE_URL = \"base_url\"" }, { "identifier": "CONF_MODEL", "path": "custom_components/ollama_conversation/const.py", "snippet": "CONF_MODEL = \"chat_model\"" }, { "identifier": "CONF_CTX_SIZE", "path": "custom_components/ollama_conversation/const.py", "snippet": "CONF_CTX_SIZE = \"ctx_size\"" }, { "identifier": "CONF_MAX_TOKENS", "path": "custom_components/ollama_conversation/const.py", "snippet": "CONF_MAX_TOKENS = \"max_tokens\"" }, { "identifier": "CONF_MIROSTAT_MODE", "path": "custom_components/ollama_conversation/const.py", "snippet": "CONF_MIROSTAT_MODE = \"mirostat_mode\"" }, { "identifier": "CONF_MIROSTAT_ETA", "path": "custom_components/ollama_conversation/const.py", "snippet": "CONF_MIROSTAT_ETA = \"mirostat_eta\"" }, { "identifier": "CONF_MIROSTAT_TAU", "path": "custom_components/ollama_conversation/const.py", "snippet": "CONF_MIROSTAT_TAU = \"mirostat_tau\"" }, { "identifier": "CONF_TEMPERATURE", "path": "custom_components/ollama_conversation/const.py", "snippet": "CONF_TEMPERATURE = \"temperature\"" }, { "identifier": "CONF_REPEAT_PENALTY", "path": "custom_components/ollama_conversation/const.py", "snippet": "CONF_REPEAT_PENALTY = \"repeat_penalty\"" }, { "identifier": "CONF_TOP_K", "path": "custom_components/ollama_conversation/const.py", "snippet": "CONF_TOP_K = \"top_k\"" }, { "identifier": "CONF_TOP_P", "path": "custom_components/ollama_conversation/const.py", "snippet": "CONF_TOP_P = \"top_p\"" }, { "identifier": "CONF_PROMPT_SYSTEM", "path": "custom_components/ollama_conversation/const.py", "snippet": "CONF_PROMPT_SYSTEM = \"prompt\"" }, { "identifier": "DEFAULT_BASE_URL", "path": "custom_components/ollama_conversation/const.py", "snippet": "DEFAULT_BASE_URL = \"http://homeassistant.local:11434\"" }, { "identifier": "DEFAULT_MODEL", "path": "custom_components/ollama_conversation/const.py", "snippet": "DEFAULT_MODEL = \"llama2:latest\"" }, { "identifier": "DEFAULT_CTX_SIZE", "path": "custom_components/ollama_conversation/const.py", "snippet": "DEFAULT_CTX_SIZE = 2048" }, { "identifier": "DEFAULT_MAX_TOKENS", "path": "custom_components/ollama_conversation/const.py", "snippet": "DEFAULT_MAX_TOKENS = 128" }, { "identifier": "DEFAULT_MIROSTAT_MODE", "path": "custom_components/ollama_conversation/const.py", "snippet": "DEFAULT_MIROSTAT_MODE = \"0\"" }, { "identifier": "DEFAULT_MIROSTAT_ETA", "path": "custom_components/ollama_conversation/const.py", "snippet": "DEFAULT_MIROSTAT_ETA = 0.1" }, { "identifier": "DEFAULT_MIROSTAT_TAU", "path": "custom_components/ollama_conversation/const.py", "snippet": "DEFAULT_MIROSTAT_TAU = 5.0" }, { "identifier": "DEFAULT_TEMPERATURE", "path": "custom_components/ollama_conversation/const.py", "snippet": "DEFAULT_TEMPERATURE = 0.8" }, { "identifier": "DEFAULT_REPEAT_PENALTY", "path": "custom_components/ollama_conversation/const.py", "snippet": "DEFAULT_REPEAT_PENALTY = 1.1" }, { "identifier": "DEFAULT_TOP_K", "path": "custom_components/ollama_conversation/const.py", "snippet": "DEFAULT_TOP_K = 40" }, { "identifier": "DEFAULT_TOP_P", "path": "custom_components/ollama_conversation/const.py", "snippet": "DEFAULT_TOP_P = 0.9" }, { "identifier": "DEFAULT_PROMPT_SYSTEM", "path": "custom_components/ollama_conversation/const.py", "snippet": "DEFAULT_PROMPT_SYSTEM = \"\"\"This smart home is controlled by Home Assistant.\n\nAn overview of the areas and the devices in this smart home:\n{%- for area in areas() %}\n {%- set area_info = namespace(printed=false) %}\n {%- for device in area_devices(area) -%}\n {%- if not device_attr(device, \"disabled_by\") and not device_attr(device, \"entry_type\") and device_attr(device, \"name\") %}\n {%- if not area_info.printed %}\n\n{{ area_name(area) }}:\n {%- set area_info.printed = true %}\n {%- endif %}\n- {{ device_attr(device, \"name\") }}{% if device_attr(device, \"model\") and (device_attr(device, \"model\") | string) not in (device_attr(device, \"name\") | string) %} ({{ device_attr(device, \"model\") }}){% endif %}\n {%- endif %}\n {%- endfor %}\n{%- endfor %}\n\nAnswer the user's questions about the world truthfully.\n\nIf the user wants to control a device, reject the request and suggest using the Home Assistant app.\n\"\"\"" }, { "identifier": "ApiClientError", "path": "custom_components/ollama_conversation/exceptions.py", "snippet": "class ApiClientError(HomeAssistantError):\n \"\"\"Exception to indicate a general API error.\"\"\"" }, { "identifier": "ApiCommError", "path": "custom_components/ollama_conversation/exceptions.py", "snippet": "class ApiCommError(ApiClientError):\n \"\"\"Exception to indicate a communication error.\"\"\"" }, { "identifier": "ApiTimeoutError", "path": "custom_components/ollama_conversation/exceptions.py", "snippet": "class ApiTimeoutError(ApiClientError):\n \"\"\"Exception to indicate a timeout error.\"\"\"" } ]
import types import voluptuous as vol from types import MappingProxyType from typing import Any from homeassistant import config_entries from homeassistant.data_entry_flow import FlowResult from homeassistant.helpers import config_validation as cv from homeassistant.helpers.aiohttp_client import async_create_clientsession from homeassistant.helpers.selector import ( NumberSelector, NumberSelectorConfig, TemplateSelector, SelectSelector, SelectSelectorConfig, SelectSelectorMode, SelectOptionDict ) from .api import OllamaApiClient from .const import ( DOMAIN, LOGGER, MENU_OPTIONS, CONF_BASE_URL, CONF_MODEL, CONF_CTX_SIZE, CONF_MAX_TOKENS, CONF_MIROSTAT_MODE, CONF_MIROSTAT_ETA, CONF_MIROSTAT_TAU, CONF_TEMPERATURE, CONF_REPEAT_PENALTY, CONF_TOP_K, CONF_TOP_P, CONF_PROMPT_SYSTEM, DEFAULT_BASE_URL, DEFAULT_MODEL, DEFAULT_CTX_SIZE, DEFAULT_MAX_TOKENS, DEFAULT_MIROSTAT_MODE, DEFAULT_MIROSTAT_ETA, DEFAULT_MIROSTAT_TAU, DEFAULT_TEMPERATURE, DEFAULT_REPEAT_PENALTY, DEFAULT_TOP_K, DEFAULT_TOP_P, DEFAULT_PROMPT_SYSTEM ) from .exceptions import ( ApiClientError, ApiCommError, ApiTimeoutError )
2,099
"""Adds config flow for Ollama.""" from __future__ import annotations STEP_USER_DATA_SCHEMA = vol.Schema( { vol.Required(CONF_BASE_URL, default=DEFAULT_BASE_URL): str, } ) DEFAULT_OPTIONS = types.MappingProxyType( { CONF_BASE_URL: DEFAULT_BASE_URL, CONF_MODEL: DEFAULT_MODEL, CONF_CTX_SIZE: DEFAULT_CTX_SIZE, CONF_MAX_TOKENS: DEFAULT_MAX_TOKENS, CONF_MIROSTAT_MODE: DEFAULT_MIROSTAT_MODE, CONF_MIROSTAT_ETA: DEFAULT_MIROSTAT_ETA, CONF_MIROSTAT_TAU: DEFAULT_MIROSTAT_TAU, CONF_TEMPERATURE: DEFAULT_TEMPERATURE, CONF_REPEAT_PENALTY: DEFAULT_REPEAT_PENALTY, CONF_TOP_K: DEFAULT_TOP_K, CONF_TOP_P: DEFAULT_TOP_P,
"""Adds config flow for Ollama.""" from __future__ import annotations STEP_USER_DATA_SCHEMA = vol.Schema( { vol.Required(CONF_BASE_URL, default=DEFAULT_BASE_URL): str, } ) DEFAULT_OPTIONS = types.MappingProxyType( { CONF_BASE_URL: DEFAULT_BASE_URL, CONF_MODEL: DEFAULT_MODEL, CONF_CTX_SIZE: DEFAULT_CTX_SIZE, CONF_MAX_TOKENS: DEFAULT_MAX_TOKENS, CONF_MIROSTAT_MODE: DEFAULT_MIROSTAT_MODE, CONF_MIROSTAT_ETA: DEFAULT_MIROSTAT_ETA, CONF_MIROSTAT_TAU: DEFAULT_MIROSTAT_TAU, CONF_TEMPERATURE: DEFAULT_TEMPERATURE, CONF_REPEAT_PENALTY: DEFAULT_REPEAT_PENALTY, CONF_TOP_K: DEFAULT_TOP_K, CONF_TOP_P: DEFAULT_TOP_P,
CONF_PROMPT_SYSTEM: DEFAULT_PROMPT_SYSTEM
27
2023-11-03 14:48:45+00:00
4k
softwaredoug/searcharray
searcharray/indexing.py
[ { "identifier": "MAX_POSN", "path": "searcharray/phrase/middle_out.py", "snippet": "MAX_POSN = encoder.max_payload" }, { "identifier": "PosnBitArrayFromFlatBuilder", "path": "searcharray/phrase/middle_out.py", "snippet": "class PosnBitArrayFromFlatBuilder:\n \"\"\" Build from sorted array shape num terms x 3.\n\n 0th is term id\n 1st is doc id\n 2nd is posn\n\n Sorted by term id then posns\n\n \"\"\"\n\n def __init__(self, flat_array: np.ndarray):\n self.flat_array = flat_array\n\n def build(self):\n \"\"\"Slice the flat array into a 2d array of doc ids and posns.\"\"\"\n term_boundaries = np.argwhere(np.diff(self.flat_array[0]) > 0).flatten() + 1\n term_boundaries = np.concatenate([[0], term_boundaries, [len(self.flat_array[1])]])\n\n encoded, enc_term_boundaries = encoder.encode(keys=self.flat_array[1].view(np.uint64),\n boundaries=term_boundaries[:-1],\n payload=self.flat_array[2].view(np.uint64))\n term_ids = self.flat_array[0][term_boundaries[:-1]]\n\n encoded_term_posns = {}\n for into_terms, (beg_idx, end_idx) in enumerate(zip(enc_term_boundaries[:-1], enc_term_boundaries[1:])):\n sliced = encoded[beg_idx:end_idx]\n encoded_term_posns[term_ids[into_terms]] = sliced\n\n return PosnBitArray(encoded_term_posns, range(0, np.max(self.flat_array[1]) + 1))" }, { "identifier": "PosnBitArrayBuilder", "path": "searcharray/phrase/middle_out.py", "snippet": "class PosnBitArrayBuilder:\n\n def __init__(self):\n self.term_posns = defaultdict(list)\n self.term_posn_doc_ids = defaultdict(list)\n self.max_doc_id = 0\n\n def add_posns(self, doc_id: int, term_id: int, posns: List[int]):\n doc_ids = [doc_id] * len(posns)\n self.term_posns[term_id].extend(posns)\n self.term_posn_doc_ids[term_id].extend(doc_ids)\n\n def ensure_capacity(self, doc_id):\n self.max_doc_id = max(self.max_doc_id, doc_id)\n\n def build(self, check=False):\n encoded_term_posns = {}\n for term_id, posns in self.term_posns.items():\n if len(posns) == 0:\n posns = np.asarray([], dtype=np.uint32).flatten()\n elif isinstance(posns, list):\n posns_arr = np.asarray(posns, dtype=np.uint32).flatten()\n posns = posns_arr\n doc_ids = self.term_posn_doc_ids[term_id]\n if isinstance(doc_ids, list):\n doc_ids = np.asarray(doc_ids, dtype=np.uint32)\n encoded = encoder.encode(keys=doc_ids, payload=posns)\n if check:\n decode_again = encoder.decode(encoded)\n docs_to_posns = dict(decode_again)\n doc_ids_again = []\n posns_again = []\n for doc_id, posns_dec in docs_to_posns.items():\n for posn in posns_dec:\n doc_ids_again.append(doc_id)\n posns_again.append(posn)\n assert np.array_equal(doc_ids_again, doc_ids)\n assert np.array_equal(posns, posns_again)\n encoded_term_posns[term_id] = encoded\n\n return PosnBitArray(encoded_term_posns, range(0, self.max_doc_id + 1))" }, { "identifier": "PosnBitArrayAlreadyEncBuilder", "path": "searcharray/phrase/middle_out.py", "snippet": "class PosnBitArrayAlreadyEncBuilder:\n\n def __init__(self):\n self.encoded_term_posns = {}\n self.max_doc_id = 0\n\n def add_posns(self, doc_id: int, term_id: int, posns):\n self.encoded_term_posns[term_id] = posns\n\n def ensure_capacity(self, doc_id):\n self.max_doc_id = max(self.max_doc_id, doc_id)\n\n def build(self, check=False):\n return PosnBitArray(self.encoded_term_posns, range(0, self.max_doc_id + 1))" }, { "identifier": "TermDict", "path": "searcharray/term_dict.py", "snippet": "class TermDict:\n\n def __init__(self):\n self.term_to_ids = {}\n self.id_to_terms = {}\n\n def add_term(self, term):\n if term in self.term_to_ids:\n return self.term_to_ids[term]\n term_id = len(self.term_to_ids)\n self.term_to_ids[term] = term_id\n self.id_to_terms[term_id] = term\n return term_id\n\n def copy(self):\n new_dict = TermDict()\n new_dict.term_to_ids = dict(self.term_to_ids)\n new_dict.id_to_terms = dict(self.id_to_terms.copy())\n return new_dict\n\n def get_term_id(self, term):\n try:\n return self.term_to_ids[term]\n except KeyError:\n raise TermMissingError(f\"Term {term} not present in dictionary. Reindex to add.\")\n\n def get_term(self, term_id):\n try:\n return self.id_to_terms[term_id]\n except KeyError:\n raise TermMissingError(f\"Term at {term_id} not present in dictionary. Reindex to add.\")\n\n def compatible(self, other) -> bool:\n # Intersect the terms in both dictionaries\n terms_self = list(self.term_to_ids.keys())\n terms_other = list(other.term_to_ids.keys())\n shortest = min(len(terms_self), len(terms_other))\n return terms_self[:shortest] == terms_other[:shortest]\n # If the intersection is empty, the dictionaries are not compatible\n\n def __len__(self):\n return len(self.term_to_ids)\n\n def __repr__(self):\n return repr(self.term_to_ids)\n\n @property\n def nbytes(self):\n bytes_used = sys.getsizeof(self.term_to_ids) + sys.getsizeof(self.id_to_terms)\n return bytes_used" }, { "identifier": "SparseMatSetBuilder", "path": "searcharray/utils/mat_set.py", "snippet": "class SparseMatSetBuilder:\n\n def __init__(self):\n self.cols = []\n self.rows = [0]\n\n def append(self, cols):\n self.cols.extend(cols)\n self.rows.append(len(self.cols))\n return 0\n\n def build(self):\n return SparseMatSet(cols=np.asarray(self.cols, dtype=np.uint32),\n rows=np.asarray(self.rows, dtype=np.uint32))" }, { "identifier": "RowViewableMatrix", "path": "searcharray/utils/row_viewable_matrix.py", "snippet": "class RowViewableMatrix:\n \"\"\"A slicable matrix that can return views without copying.\"\"\"\n\n def __init__(self, mat: SparseMatSet, rows: Optional[np.ndarray] = None):\n self.mat = mat\n self.col_cache: Dict[int, np.ndarray] = {}\n self.cols_cached: List[int] = []\n if rows is None:\n self.rows = np.arange(self.mat.shape[0])\n elif isinstance(rows, numbers.Integral):\n self.rows = np.array([rows])\n else:\n self.rows = rows\n\n def slice(self, keys):\n return RowViewableMatrix(self.mat, self.rows[keys])\n\n def __setitem__(self, keys, values):\n # Replace nan with 0\n self.col_cache = {}\n self.cols_cached = []\n actual_keys = self.rows[keys]\n if isinstance(actual_keys, numbers.Integral):\n self.mat[actual_keys] = values\n elif len(actual_keys) > 0:\n self.mat[actual_keys] = values\n\n def copy_row_at(self, row):\n return self.mat[self.rows[row]]\n\n def copy(self):\n return RowViewableMatrix(self.mat.copy(), self.rows.copy())\n\n def cols_per_row(self):\n return self.mat[self.rows].num_cols_per_row()\n\n def copy_col_at(self, col):\n if col not in self.col_cache:\n self.col_cache[col] = self.mat[self.rows, col]\n self.cols_cached.append(col)\n if len(self.cols_cached) > 10:\n del self.col_cache[self.cols_cached.pop(0)]\n return self.col_cache[col]\n\n def __getitem__(self, key):\n if isinstance(key, numbers.Integral):\n return self.copy_row_at(key)\n else:\n return self.slice(key)\n\n @property\n def nbytes(self):\n return self.mat.nbytes + \\\n self.rows.nbytes\n\n @property\n def shape(self):\n return (len(self.rows), self.mat.shape[1])\n\n def resize(self, shape):\n self.mat.ensure_capacity(shape[0] - 1)\n\n def __len__(self):\n return len(self.rows)\n\n def __repr__(self):\n return f\"RowViewableMatrix({repr(self.mat)}, {repr(self.rows)})\"\n\n def __str__(self):\n return f\"RowViewableMatrix({str(self.mat)}, {str(self.rows)})\"\n\n def __eq__(self, other):\n return rowwise_eq(self.mat[self.rows], other.mat[other.rows])" } ]
import numpy as np from searcharray.phrase.middle_out import MAX_POSN, PosnBitArrayFromFlatBuilder, PosnBitArrayBuilder, PosnBitArrayAlreadyEncBuilder from searcharray.term_dict import TermDict from searcharray.utils.mat_set import SparseMatSetBuilder from searcharray.utils.row_viewable_matrix import RowViewableMatrix
2,448
def _compute_doc_lens(posns: np.ndarray, doc_ids: np.ndarray, num_docs: int) -> np.ndarray: """Given an array of positions, compute the length of each document.""" doc_lens = np.zeros(num_docs, dtype=np.uint32) # Find were we ave posns for each doc non_empty_doc_lens = -np.diff(posns) + 1 non_empty_idxs = np.argwhere(non_empty_doc_lens > 0).flatten() non_empty_doc_ids = doc_ids[non_empty_idxs] non_empty_doc_lens = non_empty_doc_lens[non_empty_idxs] doc_lens[non_empty_doc_ids] = non_empty_doc_lens if doc_ids[-1] not in non_empty_doc_ids: doc_lens[doc_ids[-1]] = posns[-1] + 1 return doc_lens def _gather_tokens(array, tokenizer):
def _compute_doc_lens(posns: np.ndarray, doc_ids: np.ndarray, num_docs: int) -> np.ndarray: """Given an array of positions, compute the length of each document.""" doc_lens = np.zeros(num_docs, dtype=np.uint32) # Find were we ave posns for each doc non_empty_doc_lens = -np.diff(posns) + 1 non_empty_idxs = np.argwhere(non_empty_doc_lens > 0).flatten() non_empty_doc_ids = doc_ids[non_empty_idxs] non_empty_doc_lens = non_empty_doc_lens[non_empty_idxs] doc_lens[non_empty_doc_ids] = non_empty_doc_lens if doc_ids[-1] not in non_empty_doc_ids: doc_lens[doc_ids[-1]] = posns[-1] + 1 return doc_lens def _gather_tokens(array, tokenizer):
term_dict = TermDict()
4
2023-11-03 13:25:16+00:00
4k
intellerce/controlanimate
modules/ip_adapter.py
[ { "identifier": "is_torch2_available", "path": "modules/utils.py", "snippet": "def is_torch2_available():\n return hasattr(F, \"scaled_dot_product_attention\")" }, { "identifier": "Resampler", "path": "modules/resampler.py", "snippet": "class Resampler(nn.Module):\n def __init__(\n self,\n dim=1024,\n depth=8,\n dim_head=64,\n heads=16,\n num_queries=8,\n embedding_dim=768,\n output_dim=1024,\n ff_mult=4,\n max_seq_len: int = 257, # CLIP tokens + CLS token\n apply_pos_emb: bool = False,\n num_latents_mean_pooled: int = 0, # number of latents derived from mean pooled representation of the sequence\n ):\n super().__init__()\n self.pos_emb = nn.Embedding(max_seq_len, embedding_dim) if apply_pos_emb else None\n\n self.latents = nn.Parameter(torch.randn(1, num_queries, dim) / dim**0.5)\n\n self.proj_in = nn.Linear(embedding_dim, dim)\n\n self.proj_out = nn.Linear(dim, output_dim)\n self.norm_out = nn.LayerNorm(output_dim)\n\n self.to_latents_from_mean_pooled_seq = (\n nn.Sequential(\n nn.LayerNorm(dim),\n nn.Linear(dim, dim * num_latents_mean_pooled),\n Rearrange(\"b (n d) -> b n d\", n=num_latents_mean_pooled),\n )\n if num_latents_mean_pooled > 0\n else None\n )\n\n self.layers = nn.ModuleList([])\n for _ in range(depth):\n self.layers.append(\n nn.ModuleList(\n [\n PerceiverAttention(dim=dim, dim_head=dim_head, heads=heads),\n FeedForward(dim=dim, mult=ff_mult),\n ]\n )\n )\n\n def forward(self, x):\n if self.pos_emb is not None:\n n, device = x.shape[1], x.device\n pos_emb = self.pos_emb(torch.arange(n, device=device))\n x = x + pos_emb\n\n latents = self.latents.repeat(x.size(0), 1, 1)\n\n x = self.proj_in(x)\n\n if self.to_latents_from_mean_pooled_seq:\n meanpooled_seq = masked_mean(x, dim=1, mask=torch.ones(x.shape[:2], device=x.device, dtype=torch.bool))\n meanpooled_latents = self.to_latents_from_mean_pooled_seq(meanpooled_seq)\n latents = torch.cat((meanpooled_latents, latents), dim=-2)\n\n for attn, ff in self.layers:\n latents = attn(x, latents) + latents\n latents = ff(latents) + latents\n\n latents = self.proj_out(latents)\n return self.norm_out(latents)" } ]
import os, re import torch from typing import List from diffusers import StableDiffusionPipeline from diffusers.pipelines.controlnet import MultiControlNetModel from PIL import Image from safetensors import safe_open from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection from .utils import is_torch2_available from .attention_processor import ( AttnProcessor2_0 as AttnProcessor, ) from .attention_processor import ( CNAttnProcessor2_0 as CNAttnProcessor, ) from .attention_processor import ( IPAttnProcessor2_0 as IPAttnProcessor, ) from .attention_processor import AttnProcessor, CNAttnProcessor, IPAttnProcessor from .resampler import Resampler
3,548
**kwargs, ): self.set_scale(scale) if pil_image is not None: num_prompts = 1 if isinstance(pil_image, Image.Image) else len(pil_image) else: num_prompts = clip_image_embeds.size(0) if prompt is None: prompt = "best quality, high quality" if negative_prompt is None: negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality" if not isinstance(prompt, List): prompt = [prompt] * num_prompts if not isinstance(negative_prompt, List): negative_prompt = [negative_prompt] * num_prompts image_prompt_embeds, uncond_image_prompt_embeds = self.get_image_embeds( pil_image=pil_image, clip_image_embeds=clip_image_embeds ) bs_embed, seq_len, _ = image_prompt_embeds.shape image_prompt_embeds = image_prompt_embeds.repeat(1, num_samples, 1) image_prompt_embeds = image_prompt_embeds.view(bs_embed * num_samples, seq_len, -1) uncond_image_prompt_embeds = uncond_image_prompt_embeds.repeat(1, num_samples, 1) uncond_image_prompt_embeds = uncond_image_prompt_embeds.view(bs_embed * num_samples, seq_len, -1) with torch.inference_mode(): prompt_embeds_, negative_prompt_embeds_ = self.pipe.encode_prompt( prompt, device=self.device, num_images_per_prompt=num_samples, do_classifier_free_guidance=True, negative_prompt=negative_prompt, ) prompt_embeds = torch.cat([prompt_embeds_, image_prompt_embeds], dim=1) negative_prompt_embeds = torch.cat([negative_prompt_embeds_, uncond_image_prompt_embeds], dim=1) generator = torch.Generator(self.device).manual_seed(seed) if seed is not None else None images = self.pipe( prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps, generator=generator, **kwargs, ).images return images class IPAdapterXL(IPAdapter): """SDXL""" def generate( self, pil_image, prompt=None, negative_prompt=None, scale=1.0, num_samples=4, seed=None, num_inference_steps=30, **kwargs, ): self.set_scale(scale) num_prompts = 1 if isinstance(pil_image, Image.Image) else len(pil_image) if prompt is None: prompt = "best quality, high quality" if negative_prompt is None: negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality" if not isinstance(prompt, List): prompt = [prompt] * num_prompts if not isinstance(negative_prompt, List): negative_prompt = [negative_prompt] * num_prompts image_prompt_embeds, uncond_image_prompt_embeds = self.get_image_embeds(pil_image) bs_embed, seq_len, _ = image_prompt_embeds.shape image_prompt_embeds = image_prompt_embeds.repeat(1, num_samples, 1) image_prompt_embeds = image_prompt_embeds.view(bs_embed * num_samples, seq_len, -1) uncond_image_prompt_embeds = uncond_image_prompt_embeds.repeat(1, num_samples, 1) uncond_image_prompt_embeds = uncond_image_prompt_embeds.view(bs_embed * num_samples, seq_len, -1) with torch.inference_mode(): ( prompt_embeds, negative_prompt_embeds, pooled_prompt_embeds, negative_pooled_prompt_embeds, ) = self.pipe.encode_prompt( prompt, num_images_per_prompt=num_samples, do_classifier_free_guidance=True, negative_prompt=negative_prompt, ) prompt_embeds = torch.cat([prompt_embeds, image_prompt_embeds], dim=1) negative_prompt_embeds = torch.cat([negative_prompt_embeds, uncond_image_prompt_embeds], dim=1) generator = torch.Generator(self.device).manual_seed(seed) if seed is not None else None images = self.pipe( prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, pooled_prompt_embeds=pooled_prompt_embeds, negative_pooled_prompt_embeds=negative_pooled_prompt_embeds, num_inference_steps=num_inference_steps, generator=generator, **kwargs, ).images return images class IPAdapterPlus(IPAdapter): """IP-Adapter with fine-grained features""" def init_proj(self):
# Modified from: https://github.com/tencent-ailab/IP-Adapter if is_torch2_available(): else: class ImageProjModel(torch.nn.Module): """Projection Model""" def __init__(self, cross_attention_dim=1024, clip_embeddings_dim=1024, clip_extra_context_tokens=4): super().__init__() self.cross_attention_dim = cross_attention_dim self.clip_extra_context_tokens = clip_extra_context_tokens self.proj = torch.nn.Linear(clip_embeddings_dim, self.clip_extra_context_tokens * cross_attention_dim) self.norm = torch.nn.LayerNorm(cross_attention_dim) def forward(self, image_embeds): embeds = image_embeds clip_extra_context_tokens = self.proj(embeds).reshape( -1, self.clip_extra_context_tokens, self.cross_attention_dim ) clip_extra_context_tokens = self.norm(clip_extra_context_tokens) return clip_extra_context_tokens class MLPProjModel(torch.nn.Module): """SD model with image prompt""" def __init__(self, cross_attention_dim=1024, clip_embeddings_dim=1024): super().__init__() self.proj = torch.nn.Sequential( torch.nn.Linear(clip_embeddings_dim, clip_embeddings_dim), torch.nn.GELU(), torch.nn.Linear(clip_embeddings_dim, cross_attention_dim), torch.nn.LayerNorm(cross_attention_dim) ) def forward(self, image_embeds): clip_extra_context_tokens = self.proj(image_embeds) return clip_extra_context_tokens class IPAdapter: def __init__(self, sd_pipe, image_encoder_path, ip_ckpt, device, num_tokens=4): self.device = device self.image_encoder_path = image_encoder_path self.ip_ckpt = ip_ckpt self.num_tokens = num_tokens self.pipe = sd_pipe.to(self.device) self.set_ip_adapter() # load image encoder self.image_encoder = CLIPVisionModelWithProjection.from_pretrained(self.image_encoder_path).to( self.device, dtype=torch.float16 ) self.clip_image_processor = CLIPImageProcessor() # image proj model self.image_proj_model = self.init_proj() self.load_ip_adapter() def init_proj(self): image_proj_model = ImageProjModel( cross_attention_dim=self.pipe.unet.config.cross_attention_dim, clip_embeddings_dim=self.image_encoder.config.projection_dim, clip_extra_context_tokens=self.num_tokens, ).to(self.device, dtype=torch.float16) return image_proj_model def set_ip_adapter(self): count = 0 unet = self.pipe.unet attn_procs = {} for name, value in unet.attn_processors.items(): cross_attention_dim = None if name.endswith("attn1.processor") or "temporal_transformer" in name or "attn" not in name else unet.config.cross_attention_dim if name.startswith("mid_block"): hidden_size = unet.config.block_out_channels[-1] elif name.startswith("up_blocks"): block_id = int(name[len("up_blocks.")]) hidden_size = list(reversed(unet.config.block_out_channels))[block_id] elif name.startswith("down_blocks"): block_id = int(name[len("down_blocks.")]) hidden_size = unet.config.block_out_channels[block_id] if cross_attention_dim is None: attn_procs[name] = AttnProcessor() else: count+=1 attn_procs[name] = IPAttnProcessor( hidden_size=hidden_size, cross_attention_dim=cross_attention_dim, scale=1.0, num_tokens=self.num_tokens, ).to(self.device, dtype=torch.float16) unet.set_attn_processor(attn_procs) if hasattr(self.pipe, "controlnet"): if isinstance(self.pipe.controlnet, MultiControlNetModel): for controlnet in self.pipe.controlnet.nets: controlnet.set_attn_processor(CNAttnProcessor(num_tokens=self.num_tokens)) else: self.pipe.controlnet.set_attn_processor(CNAttnProcessor(num_tokens=self.num_tokens)) # print("COUNT >>>>>>>>>>>> ", count) def set_ip_adapter_4controlanimate(self, pipe): if isinstance(pipe.controlnet, MultiControlNetModel): for controlnet in pipe.controlnet.nets: controlnet.set_attn_processor(CNAttnProcessor(num_tokens=self.num_tokens)) else: pipe.controlnet.set_attn_processor(CNAttnProcessor(num_tokens=self.num_tokens)) def load_ip_adapter(self): if os.path.splitext(self.ip_ckpt)[-1] == ".safetensors": state_dict = {"image_proj": {}, "ip_adapter": {}} with safe_open(self.ip_ckpt, framework="pt", device="cpu") as f: for key in f.keys(): if key.startswith("image_proj."): state_dict["image_proj"][key.replace("image_proj.", "")] = f.get_tensor(key) elif key.startswith("ip_adapter."): state_dict["ip_adapter"][key.replace("ip_adapter.", "")] = f.get_tensor(key) else: state_dict = torch.load(self.ip_ckpt, map_location="cpu") self.image_proj_model.load_state_dict(state_dict["image_proj"]) count = 0 new_numbers = [] for key, value in self.pipe.unet.attn_processors.items(): if 'attn2' in key: new_numbers.append(count) new_numbers.append(count) # print (count, key, value) count+=1 # print("NEW NUMBERS COUNT ", len(new_numbers)) # print("STATE DICT COUNT:", len(list(state_dict["ip_adapter"].keys()))) def replace_first_number(input_string, new_number): # Use regular expression to find the first number in the string match = re.search(r'\d+', input_string) if match: # Replace the first number with the new number updated_string = input_string[:match.start()] + str(new_number) + input_string[match.end():] return updated_string else: # If no number is found, return the original string return input_string new_state_dict = {} print("### Replacing IP Adapter dictionionary keys with ControlAnimate corresponding keys...") for i, key in zip(new_numbers, state_dict["ip_adapter"].keys()): # print(">>> OLD KEY", key, "NEW KEY", replace_first_number(key, i)) new_state_dict[replace_first_number(key, i)] = state_dict["ip_adapter"][key] del state_dict["ip_adapter"] # Replaced with the new_state_dict with updated keys ip_layers = torch.nn.ModuleList(self.pipe.unet.attn_processors.values()) ip_layers.load_state_dict(new_state_dict) @torch.inference_mode() def get_image_embeds(self, pil_image=None, clip_image_embeds=None): if pil_image is not None: if isinstance(pil_image, Image.Image): pil_image = [pil_image] clip_image = self.clip_image_processor(images=pil_image, return_tensors="pt").pixel_values clip_image_embeds = self.image_encoder(clip_image.to(self.device, dtype=torch.float16)).image_embeds else: clip_image_embeds = clip_image_embeds.to(self.device, dtype=torch.float16) image_prompt_embeds = self.image_proj_model(clip_image_embeds) uncond_image_prompt_embeds = self.image_proj_model(torch.zeros_like(clip_image_embeds)) return image_prompt_embeds, uncond_image_prompt_embeds def set_scale(self, scale): for attn_processor in self.pipe.unet.attn_processors.values(): if isinstance(attn_processor, IPAttnProcessor): attn_processor.scale = scale def get_image_embeds_4controlanimate( self, pil_image=None, scale=0.4, num_samples=1, ): self.set_scale(scale) image_prompt_embeds, uncond_image_prompt_embeds = self.get_image_embeds( pil_image=pil_image, clip_image_embeds=None ) bs_embed, seq_len, _ = image_prompt_embeds.shape image_prompt_embeds = image_prompt_embeds.repeat(1, num_samples, 1) image_prompt_embeds = image_prompt_embeds.view(bs_embed * num_samples, seq_len, -1) uncond_image_prompt_embeds = uncond_image_prompt_embeds.repeat(1, num_samples, 1) uncond_image_prompt_embeds = uncond_image_prompt_embeds.view(bs_embed * num_samples, seq_len, -1) return image_prompt_embeds, uncond_image_prompt_embeds def generate( self, pil_image=None, clip_image_embeds=None, prompt=None, negative_prompt=None, scale=1.0, num_samples=4, seed=None, guidance_scale=7.5, num_inference_steps=30, **kwargs, ): self.set_scale(scale) if pil_image is not None: num_prompts = 1 if isinstance(pil_image, Image.Image) else len(pil_image) else: num_prompts = clip_image_embeds.size(0) if prompt is None: prompt = "best quality, high quality" if negative_prompt is None: negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality" if not isinstance(prompt, List): prompt = [prompt] * num_prompts if not isinstance(negative_prompt, List): negative_prompt = [negative_prompt] * num_prompts image_prompt_embeds, uncond_image_prompt_embeds = self.get_image_embeds( pil_image=pil_image, clip_image_embeds=clip_image_embeds ) bs_embed, seq_len, _ = image_prompt_embeds.shape image_prompt_embeds = image_prompt_embeds.repeat(1, num_samples, 1) image_prompt_embeds = image_prompt_embeds.view(bs_embed * num_samples, seq_len, -1) uncond_image_prompt_embeds = uncond_image_prompt_embeds.repeat(1, num_samples, 1) uncond_image_prompt_embeds = uncond_image_prompt_embeds.view(bs_embed * num_samples, seq_len, -1) with torch.inference_mode(): prompt_embeds_, negative_prompt_embeds_ = self.pipe.encode_prompt( prompt, device=self.device, num_images_per_prompt=num_samples, do_classifier_free_guidance=True, negative_prompt=negative_prompt, ) prompt_embeds = torch.cat([prompt_embeds_, image_prompt_embeds], dim=1) negative_prompt_embeds = torch.cat([negative_prompt_embeds_, uncond_image_prompt_embeds], dim=1) generator = torch.Generator(self.device).manual_seed(seed) if seed is not None else None images = self.pipe( prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps, generator=generator, **kwargs, ).images return images class IPAdapterXL(IPAdapter): """SDXL""" def generate( self, pil_image, prompt=None, negative_prompt=None, scale=1.0, num_samples=4, seed=None, num_inference_steps=30, **kwargs, ): self.set_scale(scale) num_prompts = 1 if isinstance(pil_image, Image.Image) else len(pil_image) if prompt is None: prompt = "best quality, high quality" if negative_prompt is None: negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality" if not isinstance(prompt, List): prompt = [prompt] * num_prompts if not isinstance(negative_prompt, List): negative_prompt = [negative_prompt] * num_prompts image_prompt_embeds, uncond_image_prompt_embeds = self.get_image_embeds(pil_image) bs_embed, seq_len, _ = image_prompt_embeds.shape image_prompt_embeds = image_prompt_embeds.repeat(1, num_samples, 1) image_prompt_embeds = image_prompt_embeds.view(bs_embed * num_samples, seq_len, -1) uncond_image_prompt_embeds = uncond_image_prompt_embeds.repeat(1, num_samples, 1) uncond_image_prompt_embeds = uncond_image_prompt_embeds.view(bs_embed * num_samples, seq_len, -1) with torch.inference_mode(): ( prompt_embeds, negative_prompt_embeds, pooled_prompt_embeds, negative_pooled_prompt_embeds, ) = self.pipe.encode_prompt( prompt, num_images_per_prompt=num_samples, do_classifier_free_guidance=True, negative_prompt=negative_prompt, ) prompt_embeds = torch.cat([prompt_embeds, image_prompt_embeds], dim=1) negative_prompt_embeds = torch.cat([negative_prompt_embeds, uncond_image_prompt_embeds], dim=1) generator = torch.Generator(self.device).manual_seed(seed) if seed is not None else None images = self.pipe( prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_prompt_embeds, pooled_prompt_embeds=pooled_prompt_embeds, negative_pooled_prompt_embeds=negative_pooled_prompt_embeds, num_inference_steps=num_inference_steps, generator=generator, **kwargs, ).images return images class IPAdapterPlus(IPAdapter): """IP-Adapter with fine-grained features""" def init_proj(self):
image_proj_model = Resampler(
1
2023-11-04 01:35:44+00:00
4k
Zaczero/openstreetmap-ng
tests/test_utils.py
[ { "identifier": "extend_query_params", "path": "src/utils.py", "snippet": "def extend_query_params(uri: str, params: dict) -> str:\n \"\"\"\n Extend the query parameters of a URI.\n\n >>> extend_query_params('http://example.com', {'foo': 'bar'})\n 'http://example.com?foo=bar'\n \"\"\"\n\n if not params:\n return uri\n\n uri_ = urlsplit(uri)\n query = parse_qsl(uri_.query, keep_blank_values=True)\n query.extend(params.items())\n return urlunsplit(uri_._replace(query=urlencode(query)))" }, { "identifier": "format_iso_date", "path": "src/utils.py", "snippet": "def format_iso_date(date: datetime | None) -> str:\n \"\"\"\n Format a datetime object as a string in ISO 8601 format.\n\n >>> format_date(datetime(2021, 12, 31, 15, 30, 45))\n '2021-12-31T15:30:45Z'\n \"\"\"\n\n return date.strftime('%Y-%m-%dT%H:%M:%SZ') if date else 'None'" }, { "identifier": "format_sql_date", "path": "src/utils.py", "snippet": "def format_sql_date(date: datetime | None) -> str:\n \"\"\"\n Format a datetime object as a string in SQL format.\n\n >>> format_date(datetime(2021, 12, 31, 15, 30, 45))\n '2021-12-31 15:30:45 UTC'\n \"\"\"\n\n return date.strftime('%Y-%m-%d %H:%M:%S UTC') if date else 'None'" }, { "identifier": "parse_date", "path": "src/utils.py", "snippet": "def parse_date(s: str) -> datetime:\n \"\"\"\n Parse a string into a datetime object.\n\n Timezone information is ignored and the returned datetime object is always in UTC.\n\n >>> parse_date('2010-10-31')\n datetime.datetime(2010, 10, 31, 0, 0)\n \"\"\"\n\n # TODO: support timezones\n return dateutil.parser.parse(s, ignoretz=True)" }, { "identifier": "retry", "path": "src/utils.py", "snippet": "def retry(timeout: timedelta | None, *, sleep_init: float = 1, sleep_limit: float = 300):\n \"\"\"\n Decorator to retry a function.\n\n The function is retried until it succeeds or the timeout is reached.\n \"\"\"\n\n timeout_seconds = math.inf if timeout is None else timeout.total_seconds()\n\n def decorator(func):\n async def wrapper(*args, **kwargs):\n ts = time.monotonic()\n sleep = sleep_init\n\n for attempt in count(1):\n try:\n return await func(*args, **kwargs)\n except Exception:\n next_timeout_seconds = (time.monotonic() + sleep) - ts\n\n # retry is still possible\n if next_timeout_seconds < timeout_seconds:\n logging.info(\n 'Function %s failed at attempt %d, retrying in %.3f seconds',\n func.__qualname__,\n attempt,\n sleep,\n exc_info=True,\n )\n await anyio.sleep(sleep)\n sleep = min(sleep * (1.5 + random.random()), sleep_limit) # noqa: S311\n\n # retry is not possible, re-raise the exception\n else:\n logging.warning(\n 'Function %s failed and timed out after %d attempts',\n func.__qualname__,\n attempt,\n exc_info=True,\n )\n raise\n\n return wrapper\n\n return decorator" }, { "identifier": "unicode_normalize", "path": "src/utils.py", "snippet": "def unicode_normalize(text: str) -> str:\n \"\"\"\n Normalize a string to NFC form.\n \"\"\"\n\n return unicodedata.normalize('NFC', text)" } ]
from datetime import datetime, timedelta from src.utils import ( extend_query_params, format_iso_date, format_sql_date, parse_date, retry, unicode_normalize, ) import anyio import pytest
1,622
pytestmark = pytest.mark.anyio @pytest.mark.parametrize( ('text', 'expected'), [ # already in NFC form ('naïve café', 'naïve café'), # NFD to NFC (diacritics separated) ('nai\u0308ve cafe\u0301', 'naïve café'), ('', ''), ], ) def test_unicode_normalize(text, expected): assert unicode_normalize(text) == expected @pytest.mark.parametrize( ('date', 'expected'), [ (datetime(2021, 12, 31, 15, 30, 45), '2021-12-31T15:30:45Z'), (datetime(2021, 12, 31, 15, 30, 45, 123456), '2021-12-31T15:30:45Z'), (None, 'None'), ], ) def test_format_iso_date(date, expected): assert format_iso_date(date) == expected @pytest.mark.parametrize( ('date', 'expected'), [ (datetime(2021, 12, 31, 15, 30, 45), '2021-12-31 15:30:45 UTC'), (datetime(2021, 12, 31, 15, 30, 45, 123456), '2021-12-31 15:30:45 UTC'), (None, 'None'), ], ) def test_format_sql_date(date, expected): assert format_sql_date(date) == expected async def test_retry(): runs = 0 @retry(None) async def func(): nonlocal runs runs += 1 # raise exception on first run if runs < 2: raise Exception await func() assert runs == 2 def test_retry_timeout(): @retry(timedelta(seconds=1)) async def func(): raise RuntimeError pytest.raises(RuntimeError, anyio.run, func) @pytest.mark.parametrize( ('uri', 'params', 'expected'), [ ('http://example.com/', {}, 'http://example.com/'), ('http://example.com', {'key': 'value'}, 'http://example.com?key=value'), ('http://example.com/', {'key1': 'value1', 'key2': 'value2'}, 'http://example.com/?key1=value1&key2=value2'), ('http://example.com/?key1=value1', {'key2': 'value2'}, 'http://example.com/?key1=value1&key2=value2'), ('http://example.com/?key1=value1', {'key1': 'new_value1'}, 'http://example.com/?key1=value1&key1=new_value1'), ( 'http://example.com/', {'key with space': 'value with space'}, 'http://example.com/?key+with+space=value+with+space', ), ( 'http://example.com:8080/path;params?query#fragment', {'key': 'value'}, 'http://example.com:8080/path;params?query=&key=value#fragment', ), ], ) def test_extend_query_params(uri, params, expected):
pytestmark = pytest.mark.anyio @pytest.mark.parametrize( ('text', 'expected'), [ # already in NFC form ('naïve café', 'naïve café'), # NFD to NFC (diacritics separated) ('nai\u0308ve cafe\u0301', 'naïve café'), ('', ''), ], ) def test_unicode_normalize(text, expected): assert unicode_normalize(text) == expected @pytest.mark.parametrize( ('date', 'expected'), [ (datetime(2021, 12, 31, 15, 30, 45), '2021-12-31T15:30:45Z'), (datetime(2021, 12, 31, 15, 30, 45, 123456), '2021-12-31T15:30:45Z'), (None, 'None'), ], ) def test_format_iso_date(date, expected): assert format_iso_date(date) == expected @pytest.mark.parametrize( ('date', 'expected'), [ (datetime(2021, 12, 31, 15, 30, 45), '2021-12-31 15:30:45 UTC'), (datetime(2021, 12, 31, 15, 30, 45, 123456), '2021-12-31 15:30:45 UTC'), (None, 'None'), ], ) def test_format_sql_date(date, expected): assert format_sql_date(date) == expected async def test_retry(): runs = 0 @retry(None) async def func(): nonlocal runs runs += 1 # raise exception on first run if runs < 2: raise Exception await func() assert runs == 2 def test_retry_timeout(): @retry(timedelta(seconds=1)) async def func(): raise RuntimeError pytest.raises(RuntimeError, anyio.run, func) @pytest.mark.parametrize( ('uri', 'params', 'expected'), [ ('http://example.com/', {}, 'http://example.com/'), ('http://example.com', {'key': 'value'}, 'http://example.com?key=value'), ('http://example.com/', {'key1': 'value1', 'key2': 'value2'}, 'http://example.com/?key1=value1&key2=value2'), ('http://example.com/?key1=value1', {'key2': 'value2'}, 'http://example.com/?key1=value1&key2=value2'), ('http://example.com/?key1=value1', {'key1': 'new_value1'}, 'http://example.com/?key1=value1&key1=new_value1'), ( 'http://example.com/', {'key with space': 'value with space'}, 'http://example.com/?key+with+space=value+with+space', ), ( 'http://example.com:8080/path;params?query#fragment', {'key': 'value'}, 'http://example.com:8080/path;params?query=&key=value#fragment', ), ], ) def test_extend_query_params(uri, params, expected):
assert extend_query_params(uri, params) == expected
0
2023-11-04 01:12:13+00:00
4k
codefuse-ai/Collinear-Constrained-Attention
model/peft/tuner/bitfit.py
[ { "identifier": "PEBaseModel", "path": "model/peft/tuner/pe_base_model.py", "snippet": "class PEBaseModel:\n \"\"\"PEtuning的基类模型,定义了PEtuning模型都该有的方法\"\"\"\n\n def __init__():\n return\n\n def get_model(self):\n \"\"\"对模型进行修改,冻结参数或者插入可训模块\"\"\"\n pass\n\n @classmethod\n def restore(self, model=None, path=None):\n \"\"\"从path恢复PE模型\n\n Args:\n model (_type_, optional): 原始模型. Defaults to None.\n path (_type_, optional): 增量路径. Defaults to None.\n \"\"\"\n pass" }, { "identifier": "PetuningConfig", "path": "model/peft/utils/config.py", "snippet": "class PetuningConfig(PeftConfig):\n \"\"\"\n This is the base configuration class to store the configuration of [`ROEM`], or [`BitFit`].\n\n Args:\n modules_to_save (`List[str]`):List of modules apart from LoRA layers to be set as trainable\n and saved in the final checkpoint.\n \"\"\"\n\n modules_to_save: Optional[List[str]] = field(\n default=None,\n metadata={\n \"help\": \"List of modules apart from LoRA layers to be set as trainable and saved in the final checkpoint. \"\n \"For example, in Sequence Classification or Token Classification tasks, \"\n \"the final layer `classifier/score` are randomly initialized and as such need to be trainable and saved.\"\n },\n )" }, { "identifier": "_freeze_model", "path": "model/peft/utils/others.py", "snippet": "def _freeze_model(model):\n for n, p in model.named_parameters():\n p.requires_grad = False" } ]
import sys import torch import importlib import logging from enum import Enum from peft.utils import PeftType from dataclasses import dataclass, field, asdict from typing import Optional, List from .pe_base_model import PEBaseModel from model.peft.utils import PetuningConfig from model.peft.utils.others import _freeze_model from alps.util import logger
1,624
class PEBitfitModel(PEBaseModel): """ 只训练模型bias:参考 https://arxiv.org/pdf/2106.10199.pdf model: huggingface transformers model tokenizer: huggingface transformers tokenizer """ def __init__(self, model): self.model = model def get_model(self): not_freeze_param_name = ["bias"] set_parameter_requires_grad(self.model, not_freeze_param_name) return self.model @classmethod def restore(self, model=None, path=None): logger.info("bitfit不需要额外加载参数") return model # 根据名称锁定参数层 def set_parameter_requires_grad(model, freeze_param_name=[]): if not isinstance(freeze_param_name, list): freeze_param_name = [freeze_param_name] for idx, (name, param) in enumerate(model.named_parameters()): for p in freeze_param_name: if p not in name: param.requires_grad = False # 打印参数层名 for idx, (name, param) in enumerate(model.named_parameters()): for p in freeze_param_name: if p in name: print("trainable parameter name is:") print(name) param.requires_grad = True @dataclass class PeftBitfitConfig(PetuningConfig): """ This is the configuration class to store the configuration of a [`PeftBitfitModel`]. Args: modules_to_save (`List[str]`):List of modules apart from LoRA layers to be set as trainable and saved in the final checkpoint. """ modules_to_save: Optional[List[str]] = field( default=None, metadata={ "help": "List of modules apart from LoRA layers to be set as trainable and saved in the final checkpoint. " "For example, in Sequence Classification or Token Classification tasks, " "the final layer `classifier/score` are randomly initialized and as such need to be trainable and saved." }, ) def __post_init__(self): self.peft_type = PeftType.BITFIT class PeftBitfitModel(torch.nn.Module): """ Creates Bitfit model for ant peft. Args: model ([`~transformers.PreTrainedModel`]): The model to be freeze with some layers. config ([`PeftBitfitConfig`]): The configuration of the Bitfit model. Returns: `torch.nn.Module`: The Bitfit model. Example: ```python >>> from solutions.antllm.antllm.models.glm.modeling_glm import GLMForConditionalGeneration >>> from solutions.antllm.antllm.models.peft.tuner import PeftBitfitConfig, PeftBitfitModel >>> from peft import LoraModel, LoraConfig >>> config = PeftBitfitConfig() >>> model = GLMForConditionalGeneration.from_pretrained("path_to_model") >>> roem_model = PeftBitfitModel(config, model) ``` **Attributes**: - **model** ([`~transformers.PreTrainedModel`]) -- The model to be freezed. - **peft_config** ([`PeftBitfitConfig`]): The configuration of the Bitfit model. """ def __init__(self, model, config, adapter_name): super().__init__() self.model = model self.forward = self.model.forward self.peft_config = config self.add_adapter(adapter_name, self.peft_config[adapter_name]) def add_adapter(self, adapter_name, config=None): if not isinstance(config, PeftBitfitConfig): raise ValueError( f"The PeftBitfitModel need PeftBitfitConfig, but get {type(config)}." ) if config is not None: config = self._prepare_lora_config(config) self.peft_config[adapter_name] = config if len(self.peft_config) > 1: raise ValueError( "BitfitModel supports only 1 peft config or name." "Because it only freeze the shallow layers without any additional parameters." ) self.model = PEBitfitModel(self.model).get_model() if self.peft_config[adapter_name].inference_mode:
# coding=utf-8 # Copyright (c) 2023 Ant Group. 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. sys.path.append("..") sys.path.append("../..") def is_alps_available(): return importlib.util.find_spec("alps") is not None if is_alps_available(): else: logger = logging.getLogger(__file__) class PEBitfitModel(PEBaseModel): """ 只训练模型bias:参考 https://arxiv.org/pdf/2106.10199.pdf model: huggingface transformers model tokenizer: huggingface transformers tokenizer """ def __init__(self, model): self.model = model def get_model(self): not_freeze_param_name = ["bias"] set_parameter_requires_grad(self.model, not_freeze_param_name) return self.model @classmethod def restore(self, model=None, path=None): logger.info("bitfit不需要额外加载参数") return model # 根据名称锁定参数层 def set_parameter_requires_grad(model, freeze_param_name=[]): if not isinstance(freeze_param_name, list): freeze_param_name = [freeze_param_name] for idx, (name, param) in enumerate(model.named_parameters()): for p in freeze_param_name: if p not in name: param.requires_grad = False # 打印参数层名 for idx, (name, param) in enumerate(model.named_parameters()): for p in freeze_param_name: if p in name: print("trainable parameter name is:") print(name) param.requires_grad = True @dataclass class PeftBitfitConfig(PetuningConfig): """ This is the configuration class to store the configuration of a [`PeftBitfitModel`]. Args: modules_to_save (`List[str]`):List of modules apart from LoRA layers to be set as trainable and saved in the final checkpoint. """ modules_to_save: Optional[List[str]] = field( default=None, metadata={ "help": "List of modules apart from LoRA layers to be set as trainable and saved in the final checkpoint. " "For example, in Sequence Classification or Token Classification tasks, " "the final layer `classifier/score` are randomly initialized and as such need to be trainable and saved." }, ) def __post_init__(self): self.peft_type = PeftType.BITFIT class PeftBitfitModel(torch.nn.Module): """ Creates Bitfit model for ant peft. Args: model ([`~transformers.PreTrainedModel`]): The model to be freeze with some layers. config ([`PeftBitfitConfig`]): The configuration of the Bitfit model. Returns: `torch.nn.Module`: The Bitfit model. Example: ```python >>> from solutions.antllm.antllm.models.glm.modeling_glm import GLMForConditionalGeneration >>> from solutions.antllm.antllm.models.peft.tuner import PeftBitfitConfig, PeftBitfitModel >>> from peft import LoraModel, LoraConfig >>> config = PeftBitfitConfig() >>> model = GLMForConditionalGeneration.from_pretrained("path_to_model") >>> roem_model = PeftBitfitModel(config, model) ``` **Attributes**: - **model** ([`~transformers.PreTrainedModel`]) -- The model to be freezed. - **peft_config** ([`PeftBitfitConfig`]): The configuration of the Bitfit model. """ def __init__(self, model, config, adapter_name): super().__init__() self.model = model self.forward = self.model.forward self.peft_config = config self.add_adapter(adapter_name, self.peft_config[adapter_name]) def add_adapter(self, adapter_name, config=None): if not isinstance(config, PeftBitfitConfig): raise ValueError( f"The PeftBitfitModel need PeftBitfitConfig, but get {type(config)}." ) if config is not None: config = self._prepare_lora_config(config) self.peft_config[adapter_name] = config if len(self.peft_config) > 1: raise ValueError( "BitfitModel supports only 1 peft config or name." "Because it only freeze the shallow layers without any additional parameters." ) self.model = PEBitfitModel(self.model).get_model() if self.peft_config[adapter_name].inference_mode:
_freeze_model(self.model)
2
2023-11-02 01:37:01+00:00
4k
rezaakb/pinns-tf2
pinnstf2/models/pinn_module.py
[ { "identifier": "gradient", "path": "pinnstf2/utils/gradient.py", "snippet": "def gradient(dy, dx, grad_ys=None):\n if grad_ys is None:\n dy_dx = tf.gradients(dy, dx)\n else:\n dy_dx = tf.gradients(dy, dx, grad_ys=grad_ys)\n if len(dy_dx)==1:\n dy_dx = dy_dx[0]\n return dy_dx" }, { "identifier": "fwd_gradient", "path": "pinnstf2/utils/gradient.py", "snippet": "def fwd_gradient(dy, dx):\n dummy = tf.ones_like(dy)\n G = tf.gradients(dy, dx, grad_ys=dummy)[0]\n Y_x = tf.gradients(G, dummy)[0]\n return Y_x" }, { "identifier": "fix_extra_variables", "path": "pinnstf2/utils/module_fn.py", "snippet": "def fix_extra_variables(trainable_variables, extra_variables, dtype):\n \"\"\"Convert extra variables to tf tensors with gradient tracking. These variables are\n trainables in inverse problems.\n\n :param extra_variables: Dictionary of extra variables to be converted.\n :return: Dictionary of converted extra variables as tf tensors with gradients.\n \"\"\"\n \n if extra_variables is None:\n return trainable_variables, None\n extra_variables_dict = {}\n for key in extra_variables:\n variable = tf.Variable(extra_variables[key], dtype=tf.float32, trainable=True)\n extra_variables_dict[key] = variable\n trainable_variables.append(variable)\n return trainable_variables, extra_variables_dict" }, { "identifier": "sse", "path": "pinnstf2/utils/module_fn.py", "snippet": "def sse(loss: tf.Tensor,\n preds: Dict[str, tf.Tensor],\n target: Union[Dict[str, tf.Tensor], None] = None,\n keys: Union[List[str], None] = None,\n mid: Union[int, None] = None) -> tf.Tensor:\n \"\"\"Calculate the sum of squared errors (SSE) loss for given predictions and optional targets.\n\n :param loss: Loss variable.\n :param preds: Dictionary containing prediction tensors for different keys.\n :param target: Dictionary containing target tensors (optional).\n :param keys: List of keys for which to calculate SSE loss (optional).\n :param mid: Index to separate predictions for mid-point calculation (optional).\n :return: Calculated SSE loss.\n \"\"\"\n \n if keys is None:\n return loss\n\n for key in keys:\n if target is None and mid is None:\n loss = loss + tf.reduce_sum(tf.square(preds[key]))\n elif target is None and mid is not None:\n loss = loss + tf.reduce_sum(tf.square(preds[key][:mid] - preds[key][mid:]))\n elif target is not None:\n loss = loss + tf.reduce_sum(tf.square(preds[key] - target[key]))\n\n return loss" }, { "identifier": "mse", "path": "pinnstf2/utils/module_fn.py", "snippet": "def mse(loss: tf.Tensor,\n preds: Dict[str, tf.Tensor],\n target: Union[Dict[str, tf.Tensor], None] = None,\n keys: Union[List[str], None] = None,\n mid: Union[int, None] = None) -> tf.Tensor:\n \"\"\"Calculate the mean squared error (MSE) loss for given predictions and optional targets.\n\n :param loss: Loss variable.\n :param preds: Dictionary containing prediction tensors for different keys.\n :param target: Dictionary containing target tensors (optional).\n :param keys: List of keys for which to calculate SSE loss (optional).\n :param mid: Index to separate predictions for mid-point calculation (optional).\n :return: Calculated MSE loss.\n \"\"\"\n \n if keys is None:\n return loss\n\n for key in keys:\n if target is None:\n loss = loss + tf.reduce_mean(tf.square(preds[key]))\n elif target is None and mid is not None:\n loss = loss + tf.reduce_mean(tf.square(preds[key][:mid] - preds[key][mid:]))\n elif target is not None:\n loss = loss + tf.reduce_mean(tf.square(preds[key] - target[key]))\n\n return loss" }, { "identifier": "relative_l2_error", "path": "pinnstf2/utils/module_fn.py", "snippet": "def relative_l2_error(preds, target):\n \"\"\"Calculate the relative L2 error between predictions and target tensors.\n\n :param preds: Predicted tensors.\n :param target: Target tensors.\n :return: Relative L2 error value.\n \"\"\"\n \n #return tf.sqrt(tf.reduce_mean(tf.square(preds - target))/tf.reduce_mean(tf.square(target)))\n return tf.sqrt(tf.reduce_mean(tf.square(preds - target))/tf.reduce_mean(tf.square(target - tf.reduce_mean(target))))" } ]
from typing import List, Dict, Callable, Any, Tuple, Union from pinnstf2.utils import fwd_gradient, gradient from pinnstf2.utils import ( fix_extra_variables, mse, relative_l2_error, sse ) import tensorflow as tf import sys, os, logging, time
1,731
class PINNModule: def __init__( self, net, pde_fn: Callable[[Any, ...], tf.Tensor], optimizer: tf.keras.optimizers.Adam = tf.keras.optimizers.Adam, loss_fn: str = "sse", extra_variables: Dict[str, Any] = None, output_fn: Callable[[Any, ...], tf.Tensor] = None, runge_kutta=None, jit_compile: bool = True, amp: bool = False, dtype: str = 'float32' ) -> None: """ Initialize a `PINNModule`. :param net: The neural network model to be used for approximating solutions. :param pde_fn: The partial differential equation (PDE) function defining the PDE to solve. :param optimizer: The optimizer used for training the neural network. :param loss_fn: The name of the loss function to be used. Default is 'sse' (sum of squared errors). :param extra_variables: Additional variables used in the model, provided as a dictionary. Default is None. :param output_fn: A function applied to the output of the network, for post-processing or transformations. :param runge_kutta: An optional Runge-Kutta method implementation for solving discrete problems. Default is None. :param jit_compile: If True, TensorFlow's JIT compiler will be used for optimizing computations. Default is True. :param amp: Automatic mixed precision (amp) for optimizing training performance. Default is False. :param dtype: Data type to be used for the computations. Default is 'float32'. """ super().__init__() self.net = net self.tf_dtype = tf.as_dtype(dtype) if hasattr(self.net, 'model'): self.trainable_variables = self.net.model.trainable_variables else: self.trainable_variables = self.net.trainable_variables (self.trainable_variables, self.extra_variables) = fix_extra_variables(self.trainable_variables, extra_variables, self.tf_dtype) self.output_fn = output_fn self.rk = runge_kutta self.pde_fn = pde_fn self.opt = optimizer() self.amp = amp if self.amp: self.opt = tf.keras.mixed_precision.LossScaleOptimizer(self.opt) if jit_compile: self.train_step = tf.function(self.train_step, jit_compile=True) self.eval_step = tf.function(self.eval_step, jit_compile=True) else: self.train_step = tf.function(self.train_step, jit_compile=False) self.eval_step = tf.function(self.eval_step, jit_compile=False) if loss_fn == "sse": self.loss_fn = sse elif loss_fn == "mse":
class PINNModule: def __init__( self, net, pde_fn: Callable[[Any, ...], tf.Tensor], optimizer: tf.keras.optimizers.Adam = tf.keras.optimizers.Adam, loss_fn: str = "sse", extra_variables: Dict[str, Any] = None, output_fn: Callable[[Any, ...], tf.Tensor] = None, runge_kutta=None, jit_compile: bool = True, amp: bool = False, dtype: str = 'float32' ) -> None: """ Initialize a `PINNModule`. :param net: The neural network model to be used for approximating solutions. :param pde_fn: The partial differential equation (PDE) function defining the PDE to solve. :param optimizer: The optimizer used for training the neural network. :param loss_fn: The name of the loss function to be used. Default is 'sse' (sum of squared errors). :param extra_variables: Additional variables used in the model, provided as a dictionary. Default is None. :param output_fn: A function applied to the output of the network, for post-processing or transformations. :param runge_kutta: An optional Runge-Kutta method implementation for solving discrete problems. Default is None. :param jit_compile: If True, TensorFlow's JIT compiler will be used for optimizing computations. Default is True. :param amp: Automatic mixed precision (amp) for optimizing training performance. Default is False. :param dtype: Data type to be used for the computations. Default is 'float32'. """ super().__init__() self.net = net self.tf_dtype = tf.as_dtype(dtype) if hasattr(self.net, 'model'): self.trainable_variables = self.net.model.trainable_variables else: self.trainable_variables = self.net.trainable_variables (self.trainable_variables, self.extra_variables) = fix_extra_variables(self.trainable_variables, extra_variables, self.tf_dtype) self.output_fn = output_fn self.rk = runge_kutta self.pde_fn = pde_fn self.opt = optimizer() self.amp = amp if self.amp: self.opt = tf.keras.mixed_precision.LossScaleOptimizer(self.opt) if jit_compile: self.train_step = tf.function(self.train_step, jit_compile=True) self.eval_step = tf.function(self.eval_step, jit_compile=True) else: self.train_step = tf.function(self.train_step, jit_compile=False) self.eval_step = tf.function(self.eval_step, jit_compile=False) if loss_fn == "sse": self.loss_fn = sse elif loss_fn == "mse":
self.loss_fn = mse
4
2023-11-01 03:25:51+00:00
4k
amazon-science/unconditional-time-series-diffusion
src/uncond_ts_diff/model/linear/_estimator.py
[ { "identifier": "MeanScaler", "path": "src/uncond_ts_diff/model/linear/_scaler.py", "snippet": "class MeanScaler:\n \"\"\"Just like torch MeanScaler, but for numpy.\"\"\"\n\n def __init__(\n self,\n axis: int,\n keepdims: bool = False,\n default_scale: Optional[float] = None,\n minimum_scale: float = 1e-10,\n ):\n super().__init__()\n self.axis = axis\n self.keepdims = keepdims\n self.minimum_scale = minimum_scale\n self.default_scale = default_scale or 0.0\n\n def __call__(\n self, data: np.ndarray, weights: np.ndarray\n ) -> Tuple[np.ndarray, np.ndarray]:\n # these will have shape (N, C)\n total_weight = weights.sum(axis=self.axis)\n weighted_sum = (np.abs(data) * weights).sum(axis=self.axis)\n\n # first compute a global scale per-dimension\n total_observed = total_weight.sum(axis=0)\n denominator = np.maximum(total_observed, np.ones_like(total_observed))\n\n if self.default_scale != 0.0:\n default_scale = self.default_scale\n else:\n default_scale = weighted_sum.sum(axis=0) / denominator\n\n # then compute a per-item, per-dimension scale\n denominator = np.maximum(total_weight, np.ones_like(total_weight))\n scale = weighted_sum / denominator\n\n # use per-batch scale when no element is observed\n # or when the sequence contains only zeros\n scale = np.expand_dims(\n np.maximum(\n self.minimum_scale,\n np.where(\n weighted_sum > np.zeros_like(weighted_sum),\n scale,\n default_scale * np.ones_like(total_weight),\n ),\n ),\n axis=self.axis,\n )\n\n return data / scale, scale if self.keepdims else scale.squeeze(\n axis=self.axis\n )" }, { "identifier": "NOPScaler", "path": "src/uncond_ts_diff/model/linear/_scaler.py", "snippet": "class NOPScaler:\n \"\"\"\n Just like torch NOPScaler, but for numpy.\n \"\"\"\n\n def __init__(self, axis: int, keepdims: bool = False):\n super().__init__()\n self.axis = axis\n self.keepdims = keepdims\n\n def __call__(\n self, data: np.ndarray, weights: np.ndarray\n ) -> Tuple[np.ndarray, np.ndarray]:\n scale = np.ones_like(data).mean(\n axis=self.axis,\n keepdims=self.keepdims,\n )\n return data, scale" } ]
from typing import Optional, List from sklearn.linear_model import LinearRegression, Ridge from gluonts.model import Estimator, Predictor from gluonts.dataset.common import Dataset from gluonts.dataset.field_names import FieldName from gluonts.transform import ( Transformation, AddObservedValuesIndicator, InstanceSplitter, TestSplitSampler, ExpectedNumInstanceSampler, SelectFields, ) from gluonts.dataset.loader import TrainDataLoader, InferenceDataLoader from gluonts.itertools import Cached from gluonts.model.forecast_generator import ( ForecastGenerator, SampleForecastGenerator, predict_to_numpy, ) from ._scaler import MeanScaler, NOPScaler import math import numpy as np
1,745
} class LinearModel: def __init__(self, weight, bias, scaler, num_parallel_samples=100) -> None: super().__init__() self.scaler = scaler self.weight = weight self.bias = bias self.num_parallel_samples = num_parallel_samples def _linear(self, x, A, b): return x @ A.T + b def __call__(self, x, mask): assert x.ndim == 2 x, scale = self.scaler(x, np.ones_like(x)) out = self._linear(x, self.weight, self.bias) * scale return np.tile(out[:, None], (1, self.num_parallel_samples, 1)) @predict_to_numpy.register(LinearModel) def _(prediction_net, args) -> np.ndarray: return prediction_net(*args) class LinearPredictor(Predictor): def __init__( self, input_names: List[str], prediction_net: LinearModel, batch_size: int, prediction_length: int, input_transform: Transformation, forecast_generator: ForecastGenerator = SampleForecastGenerator(), lead_time: int = 0, ) -> None: super().__init__(prediction_length, lead_time=lead_time) self.input_names = input_names self.prediction_net = prediction_net self.batch_size = batch_size self.input_transform = input_transform self.forecast_generator = forecast_generator def predict(self, dataset: Dataset, num_samples: Optional[int] = None): inference_data_loader = InferenceDataLoader( dataset, transform=self.input_transform, batch_size=self.batch_size, stack_fn=batchify, ) yield from self.forecast_generator( inference_data_loader=inference_data_loader, prediction_net=self.prediction_net, input_names=self.input_names, output_transform=None, num_samples=num_samples, ) class LinearEstimator(Estimator): """A Linear regressor that takes inputs of size equal to `context_length` and outputs forecasts of size equal to `prediction_length`. This model uses LinearRegression from scikit-learn under the hood. Example usage: ```python estimator = LinearEstimator( dataset.metadata.freq, prediction_length=dataset.metadata.prediction_length, context_length=24 * 7 * 2, ) predictor = estimator.train(dataset.train) ``` Parameters ---------- freq Frequency of the dataset (not actually used) prediction_length Prediction length context_length, optional Context length for the linear model, by default equal to 4 * prediction_length num_train_samples, optional Number of samples used to fit the LinearRegression model, by default 10000 model, optional Which sklearn linear model to use, one of {"linear", "ridge"}, by default "ridge". scaling, optional Whether to use scaling, by default True batch_size, optional Batch size (only relevant during prediction), by default 64 """ def __init__( self, freq: str, prediction_length: int, context_length: Optional[int] = None, num_train_samples: int = 10000, model: str = "ridge", scaling: bool = True, batch_size: int = 64, **kwargs, ) -> None: super().__init__(**kwargs) assert model in {"linear", "ridge"} self.freq = freq self.prediction_length = prediction_length self.context_length = context_length or 4 * prediction_length self.num_train_samples = num_train_samples self.model = model if scaling: self.scaler = MeanScaler(axis=-1, keepdims=True) else:
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 PREDICTION_INPUT_NAMES = [ "past_target", "past_observed_values", ] TRAINING_INPUT_NAMES = PREDICTION_INPUT_NAMES + [ "future_target", "future_observed_values", ] def stack(data): if isinstance(data[0], np.ndarray): data = np.array(data) elif isinstance(data[0], (list, tuple)): return list(stack(t) for t in zip(*data)) return data def batchify(data: List[dict]): return { key: stack(data=[item[key] for item in data]) for key in data[0].keys() } class LinearModel: def __init__(self, weight, bias, scaler, num_parallel_samples=100) -> None: super().__init__() self.scaler = scaler self.weight = weight self.bias = bias self.num_parallel_samples = num_parallel_samples def _linear(self, x, A, b): return x @ A.T + b def __call__(self, x, mask): assert x.ndim == 2 x, scale = self.scaler(x, np.ones_like(x)) out = self._linear(x, self.weight, self.bias) * scale return np.tile(out[:, None], (1, self.num_parallel_samples, 1)) @predict_to_numpy.register(LinearModel) def _(prediction_net, args) -> np.ndarray: return prediction_net(*args) class LinearPredictor(Predictor): def __init__( self, input_names: List[str], prediction_net: LinearModel, batch_size: int, prediction_length: int, input_transform: Transformation, forecast_generator: ForecastGenerator = SampleForecastGenerator(), lead_time: int = 0, ) -> None: super().__init__(prediction_length, lead_time=lead_time) self.input_names = input_names self.prediction_net = prediction_net self.batch_size = batch_size self.input_transform = input_transform self.forecast_generator = forecast_generator def predict(self, dataset: Dataset, num_samples: Optional[int] = None): inference_data_loader = InferenceDataLoader( dataset, transform=self.input_transform, batch_size=self.batch_size, stack_fn=batchify, ) yield from self.forecast_generator( inference_data_loader=inference_data_loader, prediction_net=self.prediction_net, input_names=self.input_names, output_transform=None, num_samples=num_samples, ) class LinearEstimator(Estimator): """A Linear regressor that takes inputs of size equal to `context_length` and outputs forecasts of size equal to `prediction_length`. This model uses LinearRegression from scikit-learn under the hood. Example usage: ```python estimator = LinearEstimator( dataset.metadata.freq, prediction_length=dataset.metadata.prediction_length, context_length=24 * 7 * 2, ) predictor = estimator.train(dataset.train) ``` Parameters ---------- freq Frequency of the dataset (not actually used) prediction_length Prediction length context_length, optional Context length for the linear model, by default equal to 4 * prediction_length num_train_samples, optional Number of samples used to fit the LinearRegression model, by default 10000 model, optional Which sklearn linear model to use, one of {"linear", "ridge"}, by default "ridge". scaling, optional Whether to use scaling, by default True batch_size, optional Batch size (only relevant during prediction), by default 64 """ def __init__( self, freq: str, prediction_length: int, context_length: Optional[int] = None, num_train_samples: int = 10000, model: str = "ridge", scaling: bool = True, batch_size: int = 64, **kwargs, ) -> None: super().__init__(**kwargs) assert model in {"linear", "ridge"} self.freq = freq self.prediction_length = prediction_length self.context_length = context_length or 4 * prediction_length self.num_train_samples = num_train_samples self.model = model if scaling: self.scaler = MeanScaler(axis=-1, keepdims=True) else:
self.scaler = NOPScaler(axis=-1, keepdims=True)
1
2023-11-09 14:20:48+00:00
4k
XinyuanWangCS/PromptAgent
src/tasks/bigbench.py
[ { "identifier": "BaseDataset", "path": "src/tasks/base_task.py", "snippet": "class BaseDataset(Dataset):\n def __init__(self, dataset):\n self.dataset = dataset\n\n def __len__(self):\n return len(self.dataset)\n\n def __getitem__(self, index):\n return self.dataset[index]" }, { "identifier": "BaseTask", "path": "src/tasks/base_task.py", "snippet": "class BaseTask():\n def __init__(self, \n train_size, \n eval_size,\n test_size=None, \n \n task_name = 'base_task',\n data_dir=None, #json file\n seed=None, \n post_instruction=True, \n TaskDataset=BaseDataset,\n option_num=5, \n **kwargs):\n \n self.task_name = task_name \n self.data_dir = data_dir\n self.seed = seed\n self.train_size = train_size\n self.test_size = test_size\n self.eval_size = eval_size\n self.post_instruction = post_instruction\n self.TaskDataset = TaskDataset\n self.option_num = option_num\n \n origin_dataset = self.load_task_dataset(data_dir=data_dir)\n origin_dataset = self.transform_format(origin_dataset)\n self.dataset = self.get_split_task_dataset(origin_dataset=origin_dataset, \n seed=seed, \n train_size=train_size, \n eval_size=eval_size,\n test_size=test_size, \n base_shuffle=True)\n self.train_size = self.dataset['train']\n self.eval_size = self.dataset['eval']\n self.test_size = self.dataset['test']\n print(f'train_size set: {len(self.train_size)}')\n print(f'eval_size set: {len(self.eval_size)}')\n print(f'test_size set: {len(self.test_size)}')\n self.answer_format_prompt = \"At the end show the answer option bracketed between <answer> and </answer>.\"\n \n def load_task_dataset(self, data_dir):\n \"\"\"\n <Task Specific>\n This is a default function for loading task dataset from json files. It can be re-implemented in the task.py files.\n \n The output dataset can be either a list of question answer pairs or a dict with a default train-test split:\n all examples: \n [{'question':question, 'answer':answer}]\n or\n default split: \n {'train':[{'question':question, 'answer':answer}], 'test':[{'question':question, 'answer':answer}]}\n \"\"\"\n dataset = self._load_json_file(data_dir)\n \n examples = []\n for example in dataset['examples']:\n question = example['question']\n answer = example['answer']\n\n formatted_example = {\n 'question': question,\n 'answer': answer\n }\n examples.append(formatted_example)\n \n return examples\n \n def transform_format(self, dataset):\n \"\"\"\n <task specific>\n This function is to transform the dataset's format that fits the pred_model (e.g. question + options). \n It can be re-implemented in the task.py files.\n \"\"\"\n return dataset\n \n def get_split_task_dataset(self, origin_dataset, train_size=None, eval_size=150, test_size=0, seed=None, base_shuffle=True):\n \"\"\"\n Split the dataset into training set, eval set and testing set.\n Support either a list of question answer pairs or a dict with a default train-test split.\n train_set and eval_set may have overlap.\n \"\"\"\n if isinstance(origin_dataset, dict):\n train_set, eval_set, test_set = self.split_dict_dataset(\n origin_dataset, \n seed=seed, \n train_size=train_size,\n eval_size=eval_size,\n test_size=test_size,\n base_shuffle=base_shuffle\n )\n elif isinstance(origin_dataset, list):\n train_set, eval_set, test_set = self.split_list_dataset(\n origin_dataset, \n seed=seed, \n train_size=train_size,\n eval_size=eval_size,\n test_size=test_size,\n base_shuffle=base_shuffle\n )\n else:\n raise ValueError(f'Dataset type {type(origin_dataset)} is not supported.')\n \n dataset = dict(train=train_set, eval=eval_set, test=test_set)\n return dataset\n \n def split_dict_dataset(self, dataset, train_size=None, eval_size=150, test_size=0, seed=None, base_shuffle=True):\n train_set = dataset['train']\n\n test_set = []\n if 'test' in dataset.keys():\n test_set = dataset['test']\n elif 'validation' in dataset.keys():\n test_set = dataset['validation']\n elif 'valid' in dataset.keys():\n test_set = dataset['valid']\n \n if base_shuffle and seed is not None:\n if seed is not None:\n print(f'shuffle dataset seed {seed}')\n random.seed(seed)\n random.shuffle(train_set)\n \n eval_set = train_set[-eval_size:]\n if train_size is not None:\n train_set = train_set[:train_size]\n test_set = test_set[:test_size]\n return train_set, eval_set, test_set\n \n def split_list_dataset(self, dataset, train_size=None, eval_size=150, test_size=0, seed=None, base_shuffle=True):\n if base_shuffle and seed is not None:\n if seed is not None:\n print(f'shuffle dataset seed {seed}')\n random.seed(seed)\n random.shuffle(dataset)\n \n test_set = dataset[:test_size]\n dataset = dataset[test_size:]\n\n if train_size is not None:\n train_set = dataset[:train_size]\n eval_set = dataset[-eval_size:]\n \n return train_set, eval_set, test_set\n \n def _load_json_file(self, data_dir):\n if not (os.path.exists(data_dir) and data_dir.endswith('.json')):\n raise ValueError(f'json file {data_dir} does not exist.')\n \n with open(data_dir, 'r') as file:\n data = json.load(file)\n return data\n \n def build_task_dataset(self, dataset, TaskDataset=None):\n return TaskDataset(dataset=dataset)\n \n def get_dataloader(self, split, batch_size, shuffle=False):\n if self.TaskDataset is None:\n self.TaskDataset = BaseDataset\n \n if split not in self.dataset.keys():\n raise ValueError(f'Dataset split {split} does not exist.')\n \n dataset = self.build_task_dataset(self.dataset[split], TaskDataset=self.TaskDataset)\n \n return DataLoader(dataset, batch_size=batch_size, shuffle=shuffle)\n \n def get_dataset_size(self, split='test'):\n return len(self.dataset[split])\n\n def build_forward_prompts_completion(self, questions, cur_propmt):\n '''\n <task specific>\n The format of combining question and prompts.\n '''\n prompts = []\n if self.post_instruction:\n for question in questions:\n prompts.append(f'{question}\\n{cur_propmt}')\n else:\n for question in questions:\n prompts.append(f'{cur_propmt}\\n{question}\\n{self.answer_format_prompt}')#\n \n return prompts\n\n def clean_labels(self, labels):\n '''\n <task specific>\n Some tasks' labels are extracted from the answer.\n '''\n return labels\n \n def clean_response(self, response):\n '''\n <task specific>\n Extract the answers from pred_model's response.\n '''\n letters = string.ascii_uppercase[:self.option_num] + string.ascii_lowercase[:self.option_num]\n clean_pattern = r\"<answer>([\\s\\S]*?)<\\/answer>\"\n match = re.findall(clean_pattern, response.lower())\n if len(match) == 0:\n return 'N/A: Format error'\n\n answer = re.search(r\"\\([\" + letters + r\"]\\)\", match[-1])\n if answer is not None:\n return answer.group(0)[1].upper()\n answer = re.search(r\"[\" + letters + r\"]\", match[-1])\n if answer is None:\n return 'N/A: Format error'\n return answer[0].upper()\n \n def batch_clean_responses(self, responses):\n if not isinstance(responses, list):\n responses = list(responses)\n batch_answers = []\n for response in responses:\n batch_answers.append(self.clean_response(response))\n return batch_answers\n \n def cal_correct(self, preds, labels):\n '''\n <task specific>\n The function of comparing the predictions and labels.\n '''\n return list(np.array((np.array(preds) == np.array(labels))).astype(int))\n \n def cal_metric(self, preds, labels, questions=None):\n '''\n <task specific>\n Calculate the evaluation metric, e.g. Accuracy, F1 score.\n return a number / tuple of metrics\n '''\n correct = self.cal_correct(preds=preds, labels=labels)\n return np.mean(correct)\n \n def process_gradient_descent_output(self, gradient_descent_output):\n return gradient_descent_output" } ]
from .base_task import BaseDataset, BaseTask import re import string
2,232
# define task prompts for various datasets class CustomTask(BaseTask): def __init__(self, train_size, eval_size, test_size=None, task_name = "bigbench", task_description = "task from bigbench", data_dir='', seed=None, post_instruction=True,
# define task prompts for various datasets class CustomTask(BaseTask): def __init__(self, train_size, eval_size, test_size=None, task_name = "bigbench", task_description = "task from bigbench", data_dir='', seed=None, post_instruction=True,
TaskDataset=BaseDataset,
0
2023-11-03 19:14:00+00:00
4k
evaluable-ai/auto-eval
tests/models/candidate_models/test_candidate_model.py
[ { "identifier": "InputRow", "path": "evaluableai/data_model/input_row_object.py", "snippet": "class InputRow:\n def __init__(self, input_text, context, input_id=None):\n self._input_id = input_id if input_id is not None else uuid.uuid4()\n self._input_text = input_text\n self._context = context\n\n def __repr__(self):\n return (f\"InputObject(input_text={repr(self._input_text)}, \"\n f\"context={repr(self._context)}, input_id={repr(self._input_id)})\")\n\n @property\n def input_id(self):\n return self._input_id\n\n @input_id.setter\n def input_id(self, value):\n raise ValueError(\"input_id cannot be changed once set.\")\n\n @property\n def input_text(self):\n return self._input_text\n\n @input_text.setter\n def input_text(self, value):\n self._input_text = value\n\n @property\n def context(self):\n return self._context\n\n @context.setter\n def context(self, value):\n self._context = value\n\n @classmethod\n def from_csv(cls, csv_file_path, text_column, context_column, id_column=None):\n df = pd.read_csv(csv_file_path)\n return cls.from_dataframe(df, text_column, context_column, id_column)\n\n @classmethod\n def from_dataframe(cls, dataframe, text_column, context_column, id_column=None):\n input_objects = []\n for index, row in dataframe.iterrows():\n # Use the id_column if it's provided and not null, otherwise generate a new UUID\n input_id = row[id_column] if id_column and not pd.isnull(row[id_column]) else None\n input_object = cls(input_text=row[text_column], context=row[context_column], input_id=input_id)\n input_objects.append(input_object)\n return input_objects\n\n def __str__(self):\n # Convert the dictionary to a JSON string\n return self.to_dict()\n\n def to_dict(self):\n \"\"\"Converts the object properties to a dictionary.\"\"\"\n return {\n 'input_id': str(self._input_id), # Convert UUID to string\n 'input_text': self._input_text,\n 'context': self._context\n }" }, { "identifier": "ModelResponseObject", "path": "evaluableai/data_model/model_response_object.py", "snippet": "class ModelResponseObject:\n def __init__(self, response_id, response_text, input_row, model):\n self._response_id = response_id\n self._response_text = response_text\n self._input_row = input_row\n self._model = model\n\n @property\n def response_text(self):\n return self._response_text\n\n def get_candidate_model_name(self):\n return self._model.model_name\n\n def get_candidate_model_version(self):\n return self._model.model_version\n\n def get_input_text(self):\n return self._input_row.input_text\n\n def get_input_context(self):\n return self._input_row.context\n\n def get_input_id(self):\n return self._input_row.input_id\n\n def __repr__(self):\n return (f\"ModelResponseObject(response_id={repr(self._response_id)}, \"\n f\"response_text={repr(self._response_text)}, \"\n f\"input_object={repr(self._input_row)}, \"\n f\"model={repr(self._model)})\")\n\n def __str__(self):\n return self.to_dict()\n\n def to_dict(self):\n \"\"\"Converts the object properties to a dictionary.\"\"\"\n return {\n 'response_id': str(self._response_id),\n 'response_text': self._response_text,\n 'input_row': self._input_row.to_dict(),\n 'model_name': self.get_candidate_model_name(),\n 'model_version': self.get_candidate_model_version()\n }" }, { "identifier": "CandidateModel", "path": "evaluableai/models/candidate_models/candidate_model.py", "snippet": "class CandidateModel(Model):\n \"\"\"This class represents a wrapper for different candidate AI models.\"\"\"\n\n def __init__(self, model_name, model_version, api_key_env=None, api_endpoint=None, api_auth_token=None):\n \"\"\"Initialize a candidate model based on the given parameters.\"\"\"\n if model_name == CandidateModelName.CUSTOM:\n if not api_auth_token:\n raise ValueError(\"API auth token must be provided for custom models.\")\n self._instance = CustomModelClass(model_name, model_version, api_endpoint, api_auth_token)\n else:\n if not api_key_env:\n raise ValueError(\"API key environment variable must be provided for non-custom models.\")\n self._api_key = self._get_api_key(api_key_env)\n self._instance = self._create_instance(model_name, model_version, self._api_key)\n\n def _get_api_key(self, api_key_env):\n \"\"\"Retrieve the API key from the environment variable.\"\"\"\n api_key = os.getenv(api_key_env)\n if api_key is None:\n raise EnvironmentError(f\"API key environment variable '{api_key_env}' not found.\")\n return api_key\n\n def _create_instance(self, model_name, model_version, api_key):\n \"\"\"Create an instance of the specified model.\"\"\"\n try:\n if model_name == CandidateModelName.HUGGING_FACE:\n return HuggingFace(model_version, api_key)\n elif model_name == CandidateModelName.OPEN_AI:\n return OpenAICandidate(model_version, api_key)\n elif model_name == CandidateModelName.OPEN_AI_CHAT:\n return OpenAiChatCandidate(model_version, api_key)\n else:\n raise ValueError(f\"Invalid model name: {model_name}\")\n except (ValueError, EnvironmentError) as e:\n logging.error(\"Model instantiation failed: %s\", e)\n raise\n except Exception as e:\n logging.error(\"An unexpected error occurred during model instantiation: %s\", e)\n raise\n\n @property\n def model_name(self):\n \"\"\"Model name property.\"\"\"\n return self._instance.model_name;\n\n @property\n def api_key(self):\n \"\"\"API key property.\"\"\"\n return self._instance.api_key;\n\n @property\n def model_version(self):\n \"\"\"Model version property.\"\"\"\n return self._instance.model_version\n\n def generate_response(self, input_frame):\n \"\"\"Generates a response from the model for the given input frame.\"\"\"\n try:\n return self._instance.generate_response(input_frame)\n except Exception as e:\n logging.error(\"Error generating response: %s\", e)\n raise\n\n @property\n def response_list(self):\n \"\"\"Property to get the response list from the model instance.\"\"\"\n try:\n return self._instance.response_list\n except Exception as e:\n logging.error(\"Error accessing response list: %s\", e)" }, { "identifier": "CandidateModelName", "path": "evaluableai/models/candidate_models/candidate_model_names.py", "snippet": "class CandidateModelName(Enum):\n HUGGING_FACE = 'huggging_face'\n OPEN_AI = 'open_ai'\n OPEN_AI_CHAT = 'open_ai_chat'\n CUSTOM = 'custom'" } ]
import unittest from unittest.mock import Mock, patch from evaluableai.data_model.input_row_object import InputRow from evaluableai.data_model.model_response_object import ModelResponseObject from evaluableai.models.candidate_models.candidate_model import CandidateModel from evaluableai.models.candidate_models.candidate_model_names import CandidateModelName
1,833
class TestCandidateModel(unittest.TestCase): @patch('os.getenv') def setUp(self, mock_getenv): # Mock environment variable for API key mock_getenv.return_value = 'test_api_key' # Mock parameters self.model_name = CandidateModelName.OPEN_AI self.model_version = 'gpt-3' self.api_key_env = 'TEST_API_KEY_ENV' # Create an instance of CandidateModel
class TestCandidateModel(unittest.TestCase): @patch('os.getenv') def setUp(self, mock_getenv): # Mock environment variable for API key mock_getenv.return_value = 'test_api_key' # Mock parameters self.model_name = CandidateModelName.OPEN_AI self.model_version = 'gpt-3' self.api_key_env = 'TEST_API_KEY_ENV' # Create an instance of CandidateModel
self.candidate_model = CandidateModel(self.model_name, self.model_version, api_key_env=self.api_key_env)
2
2023-11-06 01:26:17+00:00
4k
WilianZilv/streamlit_superapp
streamlit_superapp/widgets.py
[ { "identifier": "Navigation", "path": "streamlit_superapp/navigation.py", "snippet": "class Navigation:\n hide_page_title = False\n hide_home_button = False\n hide_back_button = False\n hide_index_description = False\n hide_breadcrumbs = False\n use_query_params = True\n\n @staticmethod\n def initialize():\n if \"session_id\" not in ss:\n ss.session_id = \"global_session\"\n\n ss[\"navigation\"] = Navigation\n\n PageLoader.initialize()\n\n path = Navigation.current_path()\n\n page = Navigation.find_page(path)\n\n if page is None:\n page = Navigation.root()\n path = page.path\n\n if page.index is not None:\n if not page.index:\n children = page.children\n if len(children):\n page = children[0]\n path = page.path\n\n result = handle_redirect(page)\n\n if isinstance(result, tuple):\n page, path = result\n\n if page is None:\n not_found(path)\n st.stop()\n raise Exception(\"Streamlit Super App not configured.\")\n\n if page.access is not None:\n params = Navigation.discover_params(page.access, page)\n if not page.access(**params):\n page = Navigation.root()\n path = page.path\n\n if page.access is None:\n guard_page = page.parent\n\n while True:\n if guard_page is None:\n break\n\n if guard_page.access is not None:\n params = Navigation.discover_params(guard_page.access, guard_page)\n if not guard_page.access(**params):\n page = Navigation.root()\n path = page.path\n\n break\n guard_page = guard_page.parent\n\n Navigation.go(path)\n\n parent = page.parent\n\n if parent is not None:\n with st.sidebar:\n if not Navigation.hide_home_button or not Navigation.hide_back_button:\n c1, c2 = st.columns(2)\n\n if not Navigation.hide_home_button:\n with c1:\n components.go_home_link()\n\n if not Navigation.hide_back_button:\n with c2:\n components.go_back_link()\n\n if parent.search:\n components.search(page)\n\n if parent.sidebar is not None:\n components.sidebar(page, variant=parent.sidebar)\n\n if not Navigation.hide_breadcrumbs:\n components.breadcrumbs(Navigation.current_path())\n\n if \"do_rerun\" not in ss:\n ss.do_rerun = False\n\n if not ss.do_rerun:\n Navigation.render_page(page)\n\n if ss.do_rerun:\n ss.do_rerun = False\n st.rerun()\n\n @staticmethod\n def pages(verify_access=True) -> List[\"Page\"]:\n pages: List[Page] = ss.pages\n\n if not verify_access:\n return pages\n\n _pages: List[Page] = []\n\n for page in pages:\n if page.access is True:\n continue\n\n if callable(page.access):\n params = Navigation.discover_params(page.access, page)\n\n if not page.access(**params):\n continue\n\n _pages.append(page)\n\n return _pages\n\n @staticmethod\n def previous_path(path: Optional[str] = None):\n current_path = path\n if current_path is None:\n current_path = Navigation.current_path()\n\n if \".\" not in current_path:\n return current_path\n\n tree = current_path.split(\".\")\n path = \".\".join(tree[:-1])\n\n page = Navigation.find_page(path)\n\n if page is None:\n return current_path\n\n if page.index is not None:\n if not page.index:\n return Navigation.previous_path(page.path)\n\n return path\n\n @staticmethod\n def go(path: Union[str, Page]):\n page = cast(Page, path)\n\n if isinstance(path, str):\n page = Navigation.find_page(path)\n if page is None:\n page = Navigation.root()\n\n if not isinstance(path, str):\n path = path.path\n\n previous_path = Navigation.current_path(path)\n\n ss[\"navigation:previous_path\"] = previous_path\n\n page_changed = previous_path != path\n\n if Navigation.use_query_params:\n st.experimental_set_query_params(path=path)\n else:\n path_state = State(\"navigation:path\", default_value=path)\n path_state.initial_value = path\n\n page_state = State(\"navigation:current_page\", default_value=page)\n page_state.initial_value = page\n\n if page_changed:\n State.save_all()\n # print(\"go:\", previous_path, \"->\", path)\n ss[\"do_rerun\"] = True\n\n @staticmethod\n def current_path(default: str = PageLoader.root):\n if Navigation.use_query_params:\n return st.experimental_get_query_params().get(\"path\", [default])[0]\n\n path_state = State(\"navigation:path\", default_value=default)\n\n return path_state.initial_value\n\n @staticmethod\n def current_page():\n page_state = State[Page](\"navigation:current_page\", default_value=None)\n\n return page_state.initial_value\n\n @staticmethod\n def find_page(path: str):\n if \"pages\" not in ss:\n PageLoader.initialize()\n\n pages = Navigation.pages(verify_access=False)\n\n for page in pages:\n if page.path == path:\n return page\n\n @staticmethod\n def root():\n root = Navigation.find_page(PageLoader.root)\n if root is None:\n not_configured()\n st.stop()\n raise Exception(\"Streamlit Super App not configured.\")\n\n return root\n\n @staticmethod\n def inject(**kwargs):\n if \"page\" in kwargs or \"navigation\" in kwargs:\n raise Exception(\"Cannot inject 'page' or 'navigation'.\")\n\n previous = ss.get(\"navigation:inject\", None)\n\n ss[\"navigation:inject\"] = kwargs\n\n if previous != kwargs:\n st.rerun()\n\n @staticmethod\n def discover_params(func: Callable, page: Page):\n signature = inspect.signature(func).parameters\n\n params = {}\n\n if \"page\" in signature:\n params[\"page\"] = page\n\n if \"navigation\" in signature:\n params[\"navigation\"] = Navigation\n\n if \"navigation:inject\" in ss:\n for key, value in ss[\"navigation:inject\"].items():\n if key in signature:\n params[key] = value\n\n for key, value in signature.items():\n if key not in params:\n params[key] = None\n\n return params\n\n @staticmethod\n def render_page(page: Page):\n params = Navigation.discover_params(page.main, page)\n\n if not Navigation.hide_page_title:\n st.header(page.icon + \" \" + page.name)\n\n return page.main(**params)" }, { "identifier": "State", "path": "streamlit_superapp/state.py", "snippet": "class State(Generic[T]):\n name: str\n default_value: Optional[T] = None\n\n def __init__(\n self,\n name: str,\n default_value: Optional[T] = None,\n key: Optional[Union[Page, str]] = None,\n cache: bool = True,\n ):\n if key is not None:\n if not isinstance(key, str):\n key = key.path\n\n name = f\"{key}:{name}\"\n\n updated_name = f\"updated:{name}\"\n key_name = f\"key:{name}\"\n previous_name = f\"previous:{name}\"\n default_name = f\"default:{name}\"\n restored_name = f\"restored:{name}\"\n\n if STATES_KEY not in ss:\n ss[STATES_KEY] = {}\n\n ss[STATES_KEY][name] = self\n\n if default_value is None:\n self.default_value = Store.get(default_name, None)\n\n if default_value is not None:\n self.default_value = Store.set(default_name, default_value)\n\n if restored_name not in ss and cache:\n Store.restore(key_name, str(uuid4()))\n Store.restore(updated_name, default_value)\n ss[name] = ss[updated_name]\n ss[previous_name] = ss[updated_name]\n\n ss[restored_name] = True\n\n self.key = Store.get(key_name, str(uuid4()))\n Store.set(key_name, self.key)\n\n self.name = name\n self.updated_name = updated_name\n self.key_name = key_name\n self.previous_name = previous_name\n self.default_name = default_name\n self.restored_name = restored_name\n\n @staticmethod\n def save_all():\n if STATES_KEY not in ss:\n return\n\n [state.save() for state in ss[STATES_KEY].values()]\n\n def save(self):\n Store.set(self.name, ss.get(self.updated_name, self.default_value))\n\n @property\n def initial_value(self) -> T:\n return cast(T, ss.get(self.name, self.default_value))\n\n @initial_value.setter\n def initial_value(self, value: T):\n Store.set(self.name, value)\n Store.set(self.updated_name, value)\n\n self.key = Store.set(self.key_name, str(uuid4()))\n\n @property\n def value(self) -> T:\n return cast(T, ss.get(self.updated_name, ss.get(self.name, self.default_value)))\n\n @value.setter\n def value(self, value: T):\n self.bind(value)\n\n @property\n def previous_value(self) -> T:\n return cast(T, ss.get(self.previous_name, self.default_value))\n\n def bind(self, value: Optional[T]):\n previous_value = self.value\n\n Store.set(self.previous_name, previous_value)\n Store.set(self.updated_name, value)\n\n return previous_value" }, { "identifier": "Page", "path": "streamlit_superapp/typing.py", "snippet": "class Page(Protocol):\n path: str\n main: Callable\n name: str\n icon: str\n description: Optional[str] = None\n tag: Optional[str] = None\n order: Optional[str] = None\n sidebar: Optional[Literal[\"selectbox\", \"radio\"]] = None\n index: Optional[bool] = None\n search: Optional[bool] = None\n hidden: bool = False\n access: Optional[Callable] = None\n redirect: Optional[Union[Callable, Tuple[Callable, str]]] = None\n\n def serializable_dict(self) -> dict:\n ...\n\n @property\n def is_active(self) -> bool:\n ...\n\n @property\n def parent(self) -> Optional[\"Page\"]:\n ...\n\n @property\n def children(self) -> List[\"Page\"]:\n ...\n\n @property\n def neighbors(self) -> List[\"Page\"]:\n ...\n\n @property\n def nearest_gallery(self) -> Optional[\"Page\"]:\n ..." } ]
from typing import Literal, Optional, Union from streamlit_superapp.navigation import Navigation from streamlit_superapp.state import State from streamlit.type_util import Key, LabelVisibility from streamlit.runtime.state.common import WidgetCallback, WidgetArgs, WidgetKwargs from streamlit_superapp.typing import Page import streamlit as st
2,808
def experimental_text_input( label: str, value: str = "", max_chars: Optional[int] = None,
def experimental_text_input( label: str, value: str = "", max_chars: Optional[int] = None,
key: Optional[Union[Page, str]] = None,
2
2023-11-05 00:03:57+00:00
4k
bytedance/cryostar
cryostar/cli_tools/sak.py
[ { "identifier": "bt_read_pdb", "path": "cryostar/utils/pdb_tools.py", "snippet": "def bt_read_pdb(file_path: Union[str, Path]):\n \"\"\"Read pdb file by biotite, return all models as AtomArrayStack\n\n Parameters\n ----------\n file_path: pdb file path\n\n Returns\n -------\n atom_arr_stack: biotite AtomArrayStack containing all models\n\n \"\"\"\n file_ext = _get_file_ext(file_path)\n if file_ext == \".pdb\":\n f = PDBFile.read(file_path)\n atom_arr_stack = f.get_structure()\n elif file_ext == \".cif\":\n f = PDBxFile.read(file_path)\n atom_arr_stack = get_structure(f)\n else:\n raise NotImplementedError(\"Only support .pdb, .cif extension.\")\n return atom_arr_stack" }, { "identifier": "bt_save_pdb", "path": "cryostar/utils/pdb_tools.py", "snippet": "def bt_save_pdb(file_path: Union[str, Path], array: Union[AtomArray, AtomArrayStack], **kwargs):\n \"\"\"Save biotite AtomArray or AtomArrayStack to pdb file\n\n Parameters\n ----------\n file_path: save file path\n array: the structure to be saved\n kwargs: additional parameters to be passed, always empty\n\n \"\"\"\n bt_struc.io.save_structure(file_path, array, **kwargs)" }, { "identifier": "save_mrc", "path": "cryostar/utils/mrc_tools.py", "snippet": "def save_mrc(vol,\n path,\n voxel_size: Union[int, float, Tuple, np.recarray] = None,\n origin: Union[int, float, Tuple, np.recarray] = None):\n \"\"\"\n Save volumetric data to mrc file, set voxel_size, origin.\n See Also: https://mrcfile.readthedocs.io/en/stable/source/mrcfile.html#mrcfile.mrcobject.MrcObject.voxel_size\n Args:\n vol: density volume\n path: save path\n voxel_size: a single number, a 3-tuple (x, y ,z) or a modified version of the voxel_size array, default 1.\n origin: a single number, a 3-tuple (x, y ,z) or a modified version of the origin array, default 0.\n\n \"\"\"\n with mrcfile.new(path, overwrite=True) as m:\n m.set_data(vol)\n\n if voxel_size is not None:\n m.voxel_size = voxel_size\n\n if origin is not None:\n m.header.origin = origin" }, { "identifier": "Polymer", "path": "cryostar/utils/polymer.py", "snippet": "class Polymer:\n chain_id: np.ndarray\n res_id: np.ndarray\n res_name: np.ndarray\n coord: np.ndarray\n atom_name: np.ndarray\n element: np.ndarray\n num_electron: np.ndarray\n\n def __init__(self, num):\n self.chain_id = np.empty(num, dtype=\"U4\")\n self.res_id = np.zeros(num, dtype=int)\n self.res_name = np.empty(num, dtype=\"U3\")\n self.coord = np.zeros((num, 3), dtype=np.float32)\n self.atom_name = np.empty(num, dtype=\"U6\")\n self.element = np.empty(num, dtype=\"U2\")\n self.num_electron = np.zeros(num, dtype=int)\n\n def __setitem__(self, index, kwargs):\n assert set(kwargs.keys()).issubset(f.name for f in dataclasses.fields(self))\n for k, v in kwargs.items():\n getattr(self, k)[index] = v\n\n def __getitem__(self, index):\n return {f.name: getattr(self, f.name)[index] for f in dataclasses.fields(self)}\n\n def __len__(self):\n return len(self.chain_id)\n\n @property\n def num_amino_acids(self):\n return np.sum(np.isin(self.atom_name, AA_ATOMS))\n\n @property\n def num_nucleotides(self):\n return np.sum(np.isin(self.atom_name, NT_ATOMS))\n\n @property\n def num_chains(self):\n return len(np.unique(self.chain_id))\n\n @classmethod\n def from_atom_arr(cls, atom_arr):\n assert isinstance(atom_arr, struc.AtomArray)\n\n nt_arr = atom_arr[struc.filter_nucleotides(atom_arr)]\n aa_arr = atom_arr[struc.filter_amino_acids(atom_arr)]\n\n num = 0\n if len(aa_arr) > 0:\n num += struc.get_residue_count(aa_arr)\n if len(nt_arr) > 0:\n for res in struc.residue_iter(nt_arr):\n valid_atoms = set(res.atom_name).intersection(NT_ATOMS)\n if len(valid_atoms) <= 0:\n raise UserWarning(f\"Nucleotides doesn't contain {' or '.join(NT_ATOMS)}.\")\n else:\n num += len(valid_atoms)\n meta = cls(num)\n\n def _update_res(tmp_res, kind=\"aa\"):\n nonlocal pos\n\n if kind == \"aa\":\n using_atom_names = AA_ATOMS\n filtered_res = tmp_res[struc.filter_peptide_backbone(tmp_res)]\n elif kind == \"nt\":\n using_atom_names = NT_ATOMS\n filtered_res = tmp_res\n else:\n raise NotImplemented\n\n valid_atom_names = set(tmp_res.atom_name).intersection(using_atom_names)\n\n for select_atom_name in valid_atom_names:\n meta[pos] = {\n \"chain_id\": tmp_res.chain_id[0],\n \"res_id\": tmp_res.res_id[0],\n \"res_name\": tmp_res.res_name[0],\n \"coord\": filtered_res[filtered_res.atom_name == select_atom_name].coord,\n \"atom_name\": select_atom_name,\n \"element\": filtered_res[filtered_res.atom_name == select_atom_name].element[0],\n \"num_electron\": get_num_electrons(tmp_res) // len(valid_atom_names)\n }\n pos += 1\n\n def _update(tmp_arr, kind=\"aa\"):\n nonlocal pos\n for chain in struc.chain_iter(tmp_arr):\n for tmp_res in struc.residue_iter(chain):\n _update_res(tmp_res, kind)\n\n pos = 0\n\n if len(aa_arr) > 0:\n _update(aa_arr, kind=\"aa\")\n if len(nt_arr) > 0:\n _update(nt_arr, kind=\"nt\")\n\n assert pos == num\n return meta\n\n @classmethod\n def from_pdb(cls, file_path):\n atom_arr = bt_read_pdb(file_path)\n if atom_arr.stack_depth() > 1:\n print(\"PDB file contains more than 1 models, select the 1st model\")\n atom_arr = atom_arr[0]\n return Polymer.from_atom_arr(atom_arr)\n\n def to_atom_arr(self):\n num = len(self)\n atom_arr = struc.AtomArray(num)\n atom_arr.coord = self.coord\n\n for f in dataclasses.fields(self):\n if f.name != \"coord\" and f.name in atom_arr.get_annotation_categories():\n atom_arr.set_annotation(f.name, getattr(self, f.name))\n # atom_arr.atom_name[atom_arr.atom_name == \"R\"] = \"CB\"\n return atom_arr" }, { "identifier": "EMAN2Grid", "path": "cryostar/gmm/gmm.py", "snippet": "class EMAN2Grid(BaseGrid):\n \"\"\"EMAN2 style grid.\n origin set to -(side_shape // 2) * voxel_size\n\n \"\"\"\n\n def __init__(self, side_shape, voxel_size):\n origin = -side_shape // 2 * voxel_size\n super().__init__(side_shape=side_shape, voxel_size=voxel_size, origin=origin)" }, { "identifier": "Gaussian", "path": "cryostar/gmm/gmm.py", "snippet": "class Gaussian:\n mus: Union[torch.Tensor, np.ndarray]\n sigmas: Union[torch.Tensor, np.ndarray]\n amplitudes: Union[torch.Tensor, np.ndarray]" }, { "identifier": "canonical_density", "path": "cryostar/gmm/gmm.py", "snippet": "def canonical_density(gauss: Gaussian, line_grid: Grid):\n vol = batch_canonical_density(\n Gaussian(mus=gauss.mus[None], sigmas=gauss.sigmas[None], amplitudes=gauss.amplitudes[None]), line_grid)\n return vol.squeeze(0)" } ]
import sys import os.path as osp import mrcfile import numpy as np import torch from pathlib import Path from cryostar.utils.pdb_tools import bt_read_pdb, bt_save_pdb from cryostar.utils.mrc_tools import save_mrc from cryostar.utils.polymer import Polymer from cryostar.gmm.gmm import EMAN2Grid, Gaussian, canonical_density
3,381
def show_mrc_info(): """ Show meta info of an .mrc file. Usage: show_mrc_info <mrc_file_path.mrc> """ if len(sys.argv) != 2: print("In order to view information from your .mrc file, please use the correct command format " "as:\nshow_mrc_info <mrc_file_path.mrc>") sys.exit(1) if sys.argv[1] in ("-h", "--help"): print(show_mrc_info.__doc__) return mrc_file_path = str(sys.argv[1]) _check_valid_file_path(mrc_file_path) with mrcfile.open(mrc_file_path) as m: print(f"The mrcfile contains volume data with:\n" f"shape (nz, ny, nx): {m.data.shape}\n" f"voxel_size/A (x, y, z): {m.voxel_size}\n" f"origin/A (x, y, z): {m.header.origin}") def center_origin(): """ Centers the origin of PDB and MRC file This function moves the origin of coordinates for both PDB and MRC files to the center of the MRC three-dimensional data matrix, so that the center of the 3D data matrix becomes (0,0,0). It then saves the adjusted files in the current directory with a '_centered' suffix. Usage: center_origin <reference_structure_path.pdb> <consensus_map_path.mrc> Args: reference_structure_path (str): The path to the input PDB file. consensus_map_path (str): The path to the input MRC file. """ if len(sys.argv) != 3: if len(sys.argv) == 2 and sys.argv[1] in ("-h", "--help"): print(center_origin.__doc__) return else: print("please use the correct command format as:\n" "center_origin <reference_structure_path.pdb> <consensus_map_path.mrc>") sys.exit(1) pdb_file_path = str(sys.argv[1]) mrc_file_path = str(sys.argv[2]) _check_valid_file_path(pdb_file_path) _check_valid_file_path(mrc_file_path) with mrcfile.open(mrc_file_path) as m: if m.voxel_size.x == m.voxel_size.y == m.voxel_size.z and np.all(np.asarray(m.data.shape) == m.data.shape[0]): new_origin = (- m.data.shape[0] // 2 * m.voxel_size.x, ) * 3 else: print("The voxel sizes or shapes differ across the three axes in the three-dimensional data.") new_origin = (- m.data.shape[2] // 2 * m.voxel_size.x, - m.data.shape[1] // 2 * m.voxel_size.y, - m.data.shape[0] // 2 * m.voxel_size.z) save_mrc(m.data.copy(), _get_file_name(mrc_file_path) + "_centered.mrc", m.voxel_size, new_origin) print(f"Result centered MRC saved to {_get_file_name(mrc_file_path)}_centered.mrc.") atom_arr = bt_read_pdb(pdb_file_path)[0] atom_arr.coord += np.asarray(new_origin) bt_save_pdb(_get_file_name(pdb_file_path) + "_centered.pdb", atom_arr) print(f"Result centered PDB saved to {_get_file_name(pdb_file_path)}_centered.pdb.") def generate_gaussian_density(): """ Generate Gaussian density corresponding to a given PDB file Note that the input PDB file must be centered before. Usages: generate_gaussian_density <pdb_file_path.pdb> <shape> <apix> generate_gaussian_density <pdb_file_path.pdb> <shape> <apix> [<save_path.mrc>] Args: pdb_file_path (str): The path to the input PDB file. shape (int): An integer that represents the shape of the Gaussian density. apix (float): A floating-point value that reflects the pixel size in Angstrom. save_path (str, optional): The path to save the resultant Gaussian density. If not provided, the function will store the data in the current working directory. """ if len(sys.argv) not in (4, 5): if len(sys.argv) == 2 and sys.argv[1] in ("-h", "--help"): print(generate_gaussian_density.__doc__) return else: print("please use the correct command format as:\n" "generate_gaussian_density <pdb_file_path.pdb> <shape> <apix>\n" "or generate_gaussian_density <pdb_file_path.pdb> <shape> <apix> [<save_path.mrc>]") sys.exit(1) # input params pdb_file_path = str(sys.argv[1]) shape = int(sys.argv[2]) apix = float(sys.argv[3]) if len(sys.argv) == 5: save_path = str(sys.argv[4]) else: save_path = _get_file_name(pdb_file_path) + "_gaussian.mrc" _check_valid_file_path(pdb_file_path) # atom_arr = bt_read_pdb(pdb_file_path)[0] meta = Polymer.from_atom_arr(atom_arr) ref_centers = torch.from_numpy(meta.coord).float() ref_amps = torch.from_numpy(meta.num_electron).float() ref_sigmas = torch.ones_like(ref_amps) ref_sigmas.fill_(2.)
def _check_valid_file_path(file_path): if not (Path(file_path).is_file() and Path(file_path).exists()): print(f"{file_path} is not a valid file path.") sys.exit(1) def _get_file_name(file_path): return osp.splitext(osp.basename(file_path))[0] def show_mrc_info(): """ Show meta info of an .mrc file. Usage: show_mrc_info <mrc_file_path.mrc> """ if len(sys.argv) != 2: print("In order to view information from your .mrc file, please use the correct command format " "as:\nshow_mrc_info <mrc_file_path.mrc>") sys.exit(1) if sys.argv[1] in ("-h", "--help"): print(show_mrc_info.__doc__) return mrc_file_path = str(sys.argv[1]) _check_valid_file_path(mrc_file_path) with mrcfile.open(mrc_file_path) as m: print(f"The mrcfile contains volume data with:\n" f"shape (nz, ny, nx): {m.data.shape}\n" f"voxel_size/A (x, y, z): {m.voxel_size}\n" f"origin/A (x, y, z): {m.header.origin}") def center_origin(): """ Centers the origin of PDB and MRC file This function moves the origin of coordinates for both PDB and MRC files to the center of the MRC three-dimensional data matrix, so that the center of the 3D data matrix becomes (0,0,0). It then saves the adjusted files in the current directory with a '_centered' suffix. Usage: center_origin <reference_structure_path.pdb> <consensus_map_path.mrc> Args: reference_structure_path (str): The path to the input PDB file. consensus_map_path (str): The path to the input MRC file. """ if len(sys.argv) != 3: if len(sys.argv) == 2 and sys.argv[1] in ("-h", "--help"): print(center_origin.__doc__) return else: print("please use the correct command format as:\n" "center_origin <reference_structure_path.pdb> <consensus_map_path.mrc>") sys.exit(1) pdb_file_path = str(sys.argv[1]) mrc_file_path = str(sys.argv[2]) _check_valid_file_path(pdb_file_path) _check_valid_file_path(mrc_file_path) with mrcfile.open(mrc_file_path) as m: if m.voxel_size.x == m.voxel_size.y == m.voxel_size.z and np.all(np.asarray(m.data.shape) == m.data.shape[0]): new_origin = (- m.data.shape[0] // 2 * m.voxel_size.x, ) * 3 else: print("The voxel sizes or shapes differ across the three axes in the three-dimensional data.") new_origin = (- m.data.shape[2] // 2 * m.voxel_size.x, - m.data.shape[1] // 2 * m.voxel_size.y, - m.data.shape[0] // 2 * m.voxel_size.z) save_mrc(m.data.copy(), _get_file_name(mrc_file_path) + "_centered.mrc", m.voxel_size, new_origin) print(f"Result centered MRC saved to {_get_file_name(mrc_file_path)}_centered.mrc.") atom_arr = bt_read_pdb(pdb_file_path)[0] atom_arr.coord += np.asarray(new_origin) bt_save_pdb(_get_file_name(pdb_file_path) + "_centered.pdb", atom_arr) print(f"Result centered PDB saved to {_get_file_name(pdb_file_path)}_centered.pdb.") def generate_gaussian_density(): """ Generate Gaussian density corresponding to a given PDB file Note that the input PDB file must be centered before. Usages: generate_gaussian_density <pdb_file_path.pdb> <shape> <apix> generate_gaussian_density <pdb_file_path.pdb> <shape> <apix> [<save_path.mrc>] Args: pdb_file_path (str): The path to the input PDB file. shape (int): An integer that represents the shape of the Gaussian density. apix (float): A floating-point value that reflects the pixel size in Angstrom. save_path (str, optional): The path to save the resultant Gaussian density. If not provided, the function will store the data in the current working directory. """ if len(sys.argv) not in (4, 5): if len(sys.argv) == 2 and sys.argv[1] in ("-h", "--help"): print(generate_gaussian_density.__doc__) return else: print("please use the correct command format as:\n" "generate_gaussian_density <pdb_file_path.pdb> <shape> <apix>\n" "or generate_gaussian_density <pdb_file_path.pdb> <shape> <apix> [<save_path.mrc>]") sys.exit(1) # input params pdb_file_path = str(sys.argv[1]) shape = int(sys.argv[2]) apix = float(sys.argv[3]) if len(sys.argv) == 5: save_path = str(sys.argv[4]) else: save_path = _get_file_name(pdb_file_path) + "_gaussian.mrc" _check_valid_file_path(pdb_file_path) # atom_arr = bt_read_pdb(pdb_file_path)[0] meta = Polymer.from_atom_arr(atom_arr) ref_centers = torch.from_numpy(meta.coord).float() ref_amps = torch.from_numpy(meta.num_electron).float() ref_sigmas = torch.ones_like(ref_amps) ref_sigmas.fill_(2.)
grid = EMAN2Grid(side_shape=shape, voxel_size=apix)
4
2023-11-06 07:15:26+00:00
4k
xyongLu/SBCFormer
transforms_factory.py
[ { "identifier": "_pil_interp", "path": "transforms.py", "snippet": "def _pil_interp(method):\n if method == 'bicubic':\n return Image.BICUBIC\n elif method == 'lanczos':\n return Image.LANCZOS\n elif method == 'hamming':\n return Image.HAMMING\n else:\n # default bilinear, do we want to allow nearest?\n return Image.BILINEAR" }, { "identifier": "RandomResizedCropAndInterpolation", "path": "transforms.py", "snippet": "class RandomResizedCropAndInterpolation:\n \"\"\"Crop the given PIL Image to random size and aspect ratio with random interpolation.\n\n A crop of random size (default: of 0.08 to 1.0) of the original size and a random\n aspect ratio (default: of 3/4 to 4/3) of the original aspect ratio is made. This crop\n is finally resized to given size.\n This is popularly used to train the Inception networks.\n\n Args:\n size: expected output size of each edge\n scale: range of size of the origin size cropped\n ratio: range of aspect ratio of the origin aspect ratio cropped\n interpolation: Default: PIL.Image.BILINEAR\n \"\"\"\n\n def __init__(self, size, scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.),\n interpolation='bilinear'):\n if isinstance(size, (list, tuple)):\n self.size = tuple(size)\n else:\n self.size = (size, size)\n if (scale[0] > scale[1]) or (ratio[0] > ratio[1]):\n warnings.warn(\"range should be of kind (min, max)\")\n\n if interpolation == 'random':\n self.interpolation = _RANDOM_INTERPOLATION\n else:\n self.interpolation = _pil_interp_torch10(interpolation)\n self.scale = scale\n self.ratio = ratio\n\n @staticmethod\n def get_params(img, scale, ratio):\n \"\"\"Get parameters for ``crop`` for a random sized crop.\n\n Args:\n img (PIL Image): Image to be cropped.\n scale (tuple): range of size of the origin size cropped\n ratio (tuple): range of aspect ratio of the origin aspect ratio cropped\n\n Returns:\n tuple: params (i, j, h, w) to be passed to ``crop`` for a random\n sized crop.\n \"\"\"\n area = img.size[0] * img.size[1]\n\n for attempt in range(10):\n target_area = random.uniform(*scale) * area\n log_ratio = (math.log(ratio[0]), math.log(ratio[1]))\n aspect_ratio = math.exp(random.uniform(*log_ratio))\n\n w = int(round(math.sqrt(target_area * aspect_ratio)))\n h = int(round(math.sqrt(target_area / aspect_ratio)))\n\n if w <= img.size[0] and h <= img.size[1]:\n i = random.randint(0, img.size[1] - h)\n j = random.randint(0, img.size[0] - w)\n return i, j, h, w\n\n # Fallback to central crop\n in_ratio = img.size[0] / img.size[1]\n if in_ratio < min(ratio):\n w = img.size[0]\n h = int(round(w / min(ratio)))\n elif in_ratio > max(ratio):\n h = img.size[1]\n w = int(round(h * max(ratio)))\n else: # whole image\n w = img.size[0]\n h = img.size[1]\n i = (img.size[1] - h) // 2\n j = (img.size[0] - w) // 2\n return i, j, h, w\n\n def __call__(self, img):\n \"\"\"\n Args:\n img (PIL Image): Image to be cropped and resized.\n\n Returns:\n PIL Image: Randomly cropped and resized image.\n \"\"\"\n i, j, h, w = self.get_params(img, self.scale, self.ratio)\n if isinstance(self.interpolation, (tuple, list)):\n interpolation = random.choice(self.interpolation)\n else:\n interpolation = self.interpolation\n # print(\"interpolation: {}\".format(interpolation))\n return F.resized_crop(img, i, j, h, w, self.size, interpolation)\n\n def __repr__(self):\n if isinstance(self.interpolation, (tuple, list)):\n interpolate_str = ' '.join([_pil_interpolation_to_str[x] for x in self.interpolation])\n else:\n interpolate_str = _pil_interpolation_to_str[self.interpolation]\n format_string = self.__class__.__name__ + '(size={0}'.format(self.size)\n format_string += ', scale={0}'.format(tuple(round(s, 4) for s in self.scale))\n format_string += ', ratio={0}'.format(tuple(round(r, 4) for r in self.ratio))\n format_string += ', interpolation={0})'.format(interpolate_str)\n return format_string" }, { "identifier": "ToNumpy", "path": "transforms.py", "snippet": "class ToNumpy:\n\n def __call__(self, pil_img):\n np_img = np.array(pil_img, dtype=np.uint8)\n if np_img.ndim < 3:\n np_img = np.expand_dims(np_img, axis=-1)\n np_img = np.rollaxis(np_img, 2) # HWC to CHW\n return np_img" }, { "identifier": "ToTensor", "path": "transforms.py", "snippet": "class ToTensor:\n\n def __init__(self, dtype=torch.float32):\n self.dtype = dtype\n\n def __call__(self, pil_img):\n np_img = np.array(pil_img, dtype=np.uint8)\n if np_img.ndim < 3:\n np_img = np.expand_dims(np_img, axis=-1)\n np_img = np.rollaxis(np_img, 2) # HWC to CHW\n return torch.from_numpy(np_img).to(dtype=self.dtype)" } ]
import math import torch import torchvision.transforms.functional as F from torchvision import transforms from timm.data.constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, DEFAULT_CROP_PCT from timm.data.auto_augment import rand_augment_transform, augment_and_mix_transform, auto_augment_transform from transforms import _pil_interp, RandomResizedCropAndInterpolation, ToNumpy, ToTensor from timm.data.random_erasing import RandomErasing from timm.data.tf_preprocessing import TfPreprocessTransform
2,027
""" Transforms Factory Factory methods for building image transforms for use with TIMM (PyTorch Image Models) Hacked together by / Copyright 2020 Ross Wightman """ # from timm.data.transforms import _pil_interp, RandomResizedCropAndInterpolation, ToNumpy, ToTensor def transforms_noaug_train( img_size=224, interpolation='bilinear', use_prefetcher=False, mean=IMAGENET_DEFAULT_MEAN, std=IMAGENET_DEFAULT_STD, ): if interpolation == 'random': # random interpolation not supported with no-aug interpolation = 'bilinear' tfl = [ transforms.Resize(img_size, _pil_interp(interpolation)), transforms.CenterCrop(img_size) ] if use_prefetcher: # prefetcher and collate will handle tensor conversion and norm tfl += [ToNumpy()] else: tfl += [ transforms.ToTensor(), transforms.Normalize( mean=torch.tensor(mean), std=torch.tensor(std)) ] return transforms.Compose(tfl) def transforms_imagenet_train( img_size=224, scale=None, ratio=None, hflip=0.5, vflip=0., color_jitter=0.4, auto_augment=None, interpolation='random', use_prefetcher=False, mean=IMAGENET_DEFAULT_MEAN, std=IMAGENET_DEFAULT_STD, re_prob=0., re_mode='const', re_count=1, re_num_splits=0, separate=False, ): """ If separate==True, the transforms are returned as a tuple of 3 separate transforms for use in a mixing dataset that passes * all data through the first (primary) transform, called the 'clean' data * a portion of the data through the secondary transform * normalizes and converts the branches above with the third, final transform """ scale = tuple(scale or (0.08, 1.0)) # default imagenet scale range ratio = tuple(ratio or (3./4., 4./3.)) # default imagenet ratio range primary_tfl = [
""" Transforms Factory Factory methods for building image transforms for use with TIMM (PyTorch Image Models) Hacked together by / Copyright 2020 Ross Wightman """ # from timm.data.transforms import _pil_interp, RandomResizedCropAndInterpolation, ToNumpy, ToTensor def transforms_noaug_train( img_size=224, interpolation='bilinear', use_prefetcher=False, mean=IMAGENET_DEFAULT_MEAN, std=IMAGENET_DEFAULT_STD, ): if interpolation == 'random': # random interpolation not supported with no-aug interpolation = 'bilinear' tfl = [ transforms.Resize(img_size, _pil_interp(interpolation)), transforms.CenterCrop(img_size) ] if use_prefetcher: # prefetcher and collate will handle tensor conversion and norm tfl += [ToNumpy()] else: tfl += [ transforms.ToTensor(), transforms.Normalize( mean=torch.tensor(mean), std=torch.tensor(std)) ] return transforms.Compose(tfl) def transforms_imagenet_train( img_size=224, scale=None, ratio=None, hflip=0.5, vflip=0., color_jitter=0.4, auto_augment=None, interpolation='random', use_prefetcher=False, mean=IMAGENET_DEFAULT_MEAN, std=IMAGENET_DEFAULT_STD, re_prob=0., re_mode='const', re_count=1, re_num_splits=0, separate=False, ): """ If separate==True, the transforms are returned as a tuple of 3 separate transforms for use in a mixing dataset that passes * all data through the first (primary) transform, called the 'clean' data * a portion of the data through the secondary transform * normalizes and converts the branches above with the third, final transform """ scale = tuple(scale or (0.08, 1.0)) # default imagenet scale range ratio = tuple(ratio or (3./4., 4./3.)) # default imagenet ratio range primary_tfl = [
RandomResizedCropAndInterpolation(img_size, scale=scale, ratio=ratio, interpolation=interpolation)]
1
2023-11-06 03:31:47+00:00
4k
kakaobrain/cxr-clip
cxrclip/data/datasets/imagetext.py
[ { "identifier": "load_transform", "path": "cxrclip/data/data_utils.py", "snippet": "def load_transform(split: str = \"train\", transform_config: Dict = None):\n assert split in {\"train\", \"valid\", \"test\", \"aug\"}\n\n config = []\n if transform_config:\n if split in transform_config:\n config = transform_config[split]\n image_transforms = []\n\n for name in config:\n if hasattr(transforms, name):\n tr_ = getattr(transforms, name)\n else:\n tr_ = getattr(albumentations, name)\n tr = tr_(**config[name])\n image_transforms.append(tr)\n\n return image_transforms" }, { "identifier": "transform_image", "path": "cxrclip/data/data_utils.py", "snippet": "def transform_image(image_transforms, image: Union[Image.Image, np.ndarray], normalize=\"huggingface\"):\n for tr in image_transforms:\n if isinstance(tr, albumentations.BasicTransform):\n image = np.array(image) if not isinstance(image, np.ndarray) else image\n image = tr(image=image)[\"image\"]\n else:\n image = transforms.ToPILImage()(image) if not isinstance(image, Image.Image) else image\n image = tr(image)\n\n if normalize == \"huggingface\":\n image = transforms.ToTensor()(image)\n image = transforms.Normalize(mean=[0.5] * 3, std=[0.5] * 3)(image)\n\n elif normalize == \"imagenet\":\n image = transforms.ToTensor()(image)\n image = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(image)\n\n else:\n raise KeyError(f\"Not supported Normalize: {normalize}\")\n\n return image" }, { "identifier": "generate_report_from_labels", "path": "cxrclip/prompt/prompts.py", "snippet": "def generate_report_from_labels(labels, prompt_json, deterministic=False, num_negs=0, name=\"chexpert\"):\n if name == \"chexpert\":\n positive, negative, uncertain = labels\n\n elif name == \"chest14\":\n positive = labels\n if num_negs:\n negative = random.sample(list(set(constants.CHEST14_TASKS) - set(positive)), k=num_negs)\n if \"Effusion\" in negative:\n negative = [neg.replace(\"Effusion\", \"Pleural Effusion\") for neg in negative]\n else:\n negative = []\n uncertain = []\n\n if \"Effusion\" in positive:\n positive = [pos.replace(\"Effusion\", \"Pleural Effusion\") for pos in positive]\n\n # validation loss control\n if deterministic:\n if not positive:\n positive = [\"No Finding\"]\n negative, uncertain = [], []\n\n report = []\n if prompt_json:\n for pos in positive:\n cand = prompt_json[pos][\"pos\"]\n sentence = cand[0] if deterministic else random.choice(cand)\n if len(sentence) > 0:\n report.append(sentence)\n\n for neg in negative:\n cand = prompt_json[neg][\"neg\"]\n sentence = cand[0] if deterministic else random.choice(cand)\n if len(sentence) > 0:\n report.append(sentence)\n\n for unc in uncertain:\n cand = prompt_json[unc][\"unc\"]\n sentence = cand[0] if deterministic else random.choice(cand)\n if len(sentence) > 0:\n report.append(sentence)\n\n if not deterministic:\n random.shuffle(report)\n\n report = \" \".join(report)\n return report" } ]
import ast import json import random import numpy as np import pandas as pd import torch from typing import Dict, List from nltk import tokenize from PIL import Image from torch.utils.data.dataset import Dataset from cxrclip.data.data_utils import load_transform, transform_image from cxrclip.prompt.prompts import generate_report_from_labels
2,048
): super().__init__() self.name = name self.split = split self.text_max_length = text_max_length self.text_sampling = text_sampling self.data_frac = data_frac self.num_negs = num_negs self.normalize = normalize self.tokenizer = tokenizer self.image_transforms = load_transform(split=split, transform_config=transform_config) if prompt_from_json: with open("datasets/train_prompts_all.json") as f: self.prompt_json = json.load(f) else: self.prompt_json = False assert data_path.endswith(".csv") self.df = pd.read_csv(data_path) if data_frac < 1.0: self.df = self.df.sample(frac=self.data_frac, random_state=1, ignore_index=True) self.loss_config = {k: v for k, v in loss_config.items()} self.image_view_aug = True self.image_aug_other_image = True self.image_aug_transforms = self.image_transforms self.has_backtranslated = hasattr(self.df, "text_augment") def __len__(self): return len(self.df) def __getitem__(self, index): if hasattr(self.df, "AP"): # AP / PA / Lateral try: view_list = ast.literal_eval(self.df["view"][index]) except Exception: view_list = [self.df["view"][index]] if len(view_list) > 2: view_list = np.random.choice(view_list, size=2, replace=False) image_path_list = [] for view in view_list: try: image_path_list = ast.literal_eval(self.df[view][index]) except Exception: image_path_list = [self.df[view][index]] image_path = np.random.choice(image_path_list, size=1)[0] image_path_list.append(image_path) else: if len(view_list) == 1: tag = view_list[0] else: tag = "image" try: image_path_list = ast.literal_eval(self.df[tag][index]) except Exception: image_path_list = [self.df[tag][index]] if self.split == "train": if self.image_aug_other_image and len(image_path_list) > 1: image_path_list = np.random.choice(image_path_list, size=2, replace=False) else: image_path_list = np.random.choice(image_path_list, size=1) else: try: image_path_list = ast.literal_eval(self.df["image"][index]) except Exception: image_path_list = [self.df["image"][index]] image_original = Image.open(image_path_list[0]).convert("RGB") image = transform_image(self.image_transforms, image_original, normalize=self.normalize) if self.image_view_aug: if len(image_path_list) > 1: image_original = Image.open(image_path_list[1]).convert("RGB") image_view = transform_image(self.image_aug_transforms, image_original, normalize=self.normalize) # Get Text or Prompt if hasattr(self.df, "text"): try: text_list = ast.literal_eval(self.df["text"][index]) except Exception: text_list = self.df["text"][index] if self.has_backtranslated: try: text_aug_list = ast.literal_eval(self.df["text_augment"][index]) except Exception: text_aug_list = self.df["text_augment"][index] if len(text_list) >= 2: indexes = np.random.randint(len(text_list), size=2) # Multiple section text = text_aug_list[indexes[0]] if random.random() < 0.5 and self.has_backtranslated else text_list[indexes[0]] text2 = text_aug_list[indexes[1]] if random.random() < 0.5 and self.has_backtranslated else text_list[indexes[1]] else: if random.random() < 0.5: text = text_list[0] text2 = text_aug_list[0] if self.has_backtranslated else text_list[0] else: text = text_aug_list[0] if self.has_backtranslated else text_list[0] text2 = text_list[0] if self.split == "train": # Text shuffle augment for _text in [text, text2]: _text_list = tokenize.sent_tokenize(_text, language="english") random.shuffle(_text_list) _text = " ".join(_text_list) # Get Two Prompts per sample. elif hasattr(self.df, "text_label"): labels = ast.literal_eval(self.df["text_label"][index])
class ImageTextDataset(Dataset): def __init__( self, tokenizer, name: str, data_path: str, split: str, text_max_length: int = 256, text_sampling: str = "random", loss_config: Dict = None, transform_config: Dict = None, prompt_from_json: bool = False, data_frac: float = 1.0, num_negs: int = 0, normalize: str = "huggingface", **kwargs ): super().__init__() self.name = name self.split = split self.text_max_length = text_max_length self.text_sampling = text_sampling self.data_frac = data_frac self.num_negs = num_negs self.normalize = normalize self.tokenizer = tokenizer self.image_transforms = load_transform(split=split, transform_config=transform_config) if prompt_from_json: with open("datasets/train_prompts_all.json") as f: self.prompt_json = json.load(f) else: self.prompt_json = False assert data_path.endswith(".csv") self.df = pd.read_csv(data_path) if data_frac < 1.0: self.df = self.df.sample(frac=self.data_frac, random_state=1, ignore_index=True) self.loss_config = {k: v for k, v in loss_config.items()} self.image_view_aug = True self.image_aug_other_image = True self.image_aug_transforms = self.image_transforms self.has_backtranslated = hasattr(self.df, "text_augment") def __len__(self): return len(self.df) def __getitem__(self, index): if hasattr(self.df, "AP"): # AP / PA / Lateral try: view_list = ast.literal_eval(self.df["view"][index]) except Exception: view_list = [self.df["view"][index]] if len(view_list) > 2: view_list = np.random.choice(view_list, size=2, replace=False) image_path_list = [] for view in view_list: try: image_path_list = ast.literal_eval(self.df[view][index]) except Exception: image_path_list = [self.df[view][index]] image_path = np.random.choice(image_path_list, size=1)[0] image_path_list.append(image_path) else: if len(view_list) == 1: tag = view_list[0] else: tag = "image" try: image_path_list = ast.literal_eval(self.df[tag][index]) except Exception: image_path_list = [self.df[tag][index]] if self.split == "train": if self.image_aug_other_image and len(image_path_list) > 1: image_path_list = np.random.choice(image_path_list, size=2, replace=False) else: image_path_list = np.random.choice(image_path_list, size=1) else: try: image_path_list = ast.literal_eval(self.df["image"][index]) except Exception: image_path_list = [self.df["image"][index]] image_original = Image.open(image_path_list[0]).convert("RGB") image = transform_image(self.image_transforms, image_original, normalize=self.normalize) if self.image_view_aug: if len(image_path_list) > 1: image_original = Image.open(image_path_list[1]).convert("RGB") image_view = transform_image(self.image_aug_transforms, image_original, normalize=self.normalize) # Get Text or Prompt if hasattr(self.df, "text"): try: text_list = ast.literal_eval(self.df["text"][index]) except Exception: text_list = self.df["text"][index] if self.has_backtranslated: try: text_aug_list = ast.literal_eval(self.df["text_augment"][index]) except Exception: text_aug_list = self.df["text_augment"][index] if len(text_list) >= 2: indexes = np.random.randint(len(text_list), size=2) # Multiple section text = text_aug_list[indexes[0]] if random.random() < 0.5 and self.has_backtranslated else text_list[indexes[0]] text2 = text_aug_list[indexes[1]] if random.random() < 0.5 and self.has_backtranslated else text_list[indexes[1]] else: if random.random() < 0.5: text = text_list[0] text2 = text_aug_list[0] if self.has_backtranslated else text_list[0] else: text = text_aug_list[0] if self.has_backtranslated else text_list[0] text2 = text_list[0] if self.split == "train": # Text shuffle augment for _text in [text, text2]: _text_list = tokenize.sent_tokenize(_text, language="english") random.shuffle(_text_list) _text = " ".join(_text_list) # Get Two Prompts per sample. elif hasattr(self.df, "text_label"): labels = ast.literal_eval(self.df["text_label"][index])
text = generate_report_from_labels(
2
2023-11-01 07:24:52+00:00
4k
mihirp1998/Diffusion-TTA
diff_tta/models/build.py
[ { "identifier": "get_obj_from_str", "path": "diff_tta/utils.py", "snippet": "def get_obj_from_str(string, reload=False):\n \"\"\"A helper function to instantiate a class from a config object.\n See https://github.com/CompVis/stable-diffusion/blob/main/ldm/util.py\n \"\"\"\n module, cls = string.rsplit(\".\", 1)\n if reload:\n module_imp = importlib.import_module(module)\n importlib.reload(module_imp)\n return getattr(importlib.import_module(module, package=None), cls)" }, { "identifier": "ClipClassifier", "path": "diff_tta/models/clip_classifier.py", "snippet": "class ClipClassifier(nn.Module):\n def __init__(self, classes, class_arch):\n super().__init__()\n\n imagenet_templates = [\n 'a photo of a {}.',\n ]\n if class_arch == \"clipr50\":\n model, _ = clip.load(\"RN50\",jit=False)\n elif class_arch == \"clipr101\":\n model, _ = clip.load(\"RN101\",jit=False)\n elif class_arch == \"clipb32\":\n model, _ = clip.load(\"ViT-B/32\",jit=False)\n elif class_arch == \"clipb16\":\n model, _ = clip.load(\"ViT-B/16\",jit=False)\n elif class_arch == \"clipl14\":\n model, _ = clip.load(\"ViT-L/14\",jit=False)\n\n\n self.final_fc = nn.Linear(768,1000,bias=False)\n with torch.no_grad():\n zeroshot_weights = zeroshot_classifier(classes, imagenet_templates, model)\n self.final_fc.weight.data = zeroshot_weights.T\n self.model = model\n\n def forward(self, images):\n image_features = self.model.encode_image(images)\n logits = 100. * self.final_fc(image_features)\n return logits" }, { "identifier": "utils", "path": "diff_tta/utils.py", "snippet": "class UnNormalize(object):\nclass VQVAEUnNormalize(UnNormalize):\n def __init__(self, mean, std):\n def __call__(self, tensor):\n def __call__(self, tensor):\ndef mean_list(l):\ndef segment_mean(x, index):\ndef get_class_sd_features(tokenizer, text_encoder, input, device):\ndef prepare_class_text_embeddings(device,\n tokenizer=None,\n text_encoder=None,\n class_names=None):\ndef initiate_time_steps(step, total_timestep, batch_size, config):\ndef instantiate_from_config(config):\ndef get_obj_from_str(string, reload=False):" } ]
import torch import torch.nn as nn import torchvision from diffusers import ( AutoencoderKL, UNet2DConditionModel, DDPMScheduler, StableDiffusionPipeline, EulerDiscreteScheduler ) from transformers import CLIPTextModel, CLIPTokenizer from diff_tta.utils import get_obj_from_str from diff_tta.models.DiT.models import DiT_XL_2 from diff_tta.models.DiT.download import find_model from diff_tta.models.clip_classifier import ClipClassifier from diff_tta.models.DiT.diffusion import create_diffusion from diff_tta import utils
1,983
return vae, model, diffusion, image_renormalizer def load_sd_model(config, device, classes): """Load Stable Diffusion model""" dtype = torch.float32 image_renormalizer = utils.VQVAEUnNormalize( mean=config.input.mean, std=config.input.std ) if config.model.sd_version == '1-4': if config.model.use_flash: model_id = "CompVis/stable-diffusion-v1-4" scheduler = EulerDiscreteScheduler.from_pretrained( model_id, subfolder="scheduler" ) pipe = StableDiffusionPipeline.from_pretrained( model_id, scheduler=scheduler, torch_dtype=dtype ).to(device) pipe.enable_xformers_memory_efficient_attention() vae = pipe.vae.to(device) tokenizer = pipe.tokenizer text_encoder = pipe.text_encoder.to(device) unet = pipe.unet.to(device) else: vae = AutoencoderKL.from_pretrained( f"CompVis/stable-diffusion-v{config.model.sd_version}", subfolder="vae", torch_dtype=dtype ).to(device) tokenizer = CLIPTokenizer.from_pretrained( "openai/clip-vit-large-patch14" ) text_encoder = CLIPTextModel.from_pretrained( "openai/clip-vit-large-patch14", torch_dtype=dtype ).to(device) unet = UNet2DConditionModel.from_pretrained( f"CompVis/stable-diffusion-v{config.model.sd_version}", subfolder="unet", torch_dtype=dtype ).to(device) scheduler_config = get_scheduler_config(config) scheduler = DDPMScheduler( num_train_timesteps=scheduler_config['num_train_timesteps'], beta_start=scheduler_config['beta_start'], beta_end=scheduler_config['beta_end'], beta_schedule=scheduler_config['beta_schedule'] ) elif config.model.sd_version == '2-1': model_id = "stabilityai/stable-diffusion-2-1-base" scheduler = EulerDiscreteScheduler.from_pretrained( model_id, subfolder="scheduler" ) pipe = StableDiffusionPipeline.from_pretrained( model_id, scheduler=scheduler, torch_dtype=dtype ).to(device) pipe.enable_xformers_memory_efficient_attention() vae = pipe.vae.to(device) tokenizer = pipe.tokenizer text_encoder = pipe.text_encoder.to(device) unet = pipe.unet.to(device) else: raise NotImplementedError class_text_embeddings = utils.prepare_class_text_embeddings( device, tokenizer, text_encoder, class_names=classes ) class_text_embeddings = class_text_embeddings.detach() if config.model.adapt_only_classifier: for m in [vae, text_encoder, unet]: for param in m.parameters(): param.requires_grad = False for m in [vae, text_encoder]: for param in m.parameters(): param.requires_grad = False return (vae, tokenizer, text_encoder, unet, scheduler, image_renormalizer, class_text_embeddings) def get_scheduler_config(config): assert config.model.sd_version in {'1-4', '2-1'} if config.model.sd_version == '1-4': schedule_config = { "_class_name": "PNDMScheduler", "_diffusers_version": "0.7.0.dev0", "beta_end": 0.012, "beta_schedule": "scaled_linear", "beta_start": 0.00085, "num_train_timesteps": 1000, "set_alpha_to_one": False, "skip_prk_steps": True, "steps_offset": 1, "trained_betas": None, "clip_sample": False } elif config.model.sd_version == '2-1': schedule_config = { "_class_name": "EulerDiscreteScheduler", "_diffusers_version": "0.10.2", "beta_end": 0.012, "beta_schedule": "scaled_linear", "beta_start": 0.00085, "clip_sample": False, "num_train_timesteps": 1000, "prediction_type": "epsilon", "set_alpha_to_one": False, "skip_prk_steps": True, "steps_offset": 1, # todo "trained_betas": None } else: raise NotImplementedError return schedule_config def get_class_model(config, classes): """Load classification model""" if "clip" in config.model.class_arch:
def load_dit_model(config, device): """Load DiT model""" #@param ["stabilityai/sd-vae-ft-mse", "stabilityai/sd-vae-ft-ema"] vae_model = "stabilityai/sd-vae-ft-ema" image_size = config.input.sd_img_res latent_size = int(image_size) // 8 model = DiT_XL_2(input_size=latent_size).to(device) state_dict = find_model(f"DiT-XL-2-{image_size}x{image_size}.pt") model.load_state_dict(state_dict) model.eval() # important! vae = AutoencoderKL.from_pretrained(vae_model).to(device) vae.eval() # default: 1000 steps, linear noise schedule diffusion = create_diffusion(timestep_respacing="") image_renormalizer = utils.VQVAEUnNormalize( mean=config.input.mean, std=config.input.std ) if config.model.adapt_only_classifier: for m in [vae, model]: for param in m.parameters(): param.requires_grad = False if config.model.freeze_vae: for m in [vae]: for param in m.parameters(): param.requires_grad = False return vae, model, diffusion, image_renormalizer def load_sd_model(config, device, classes): """Load Stable Diffusion model""" dtype = torch.float32 image_renormalizer = utils.VQVAEUnNormalize( mean=config.input.mean, std=config.input.std ) if config.model.sd_version == '1-4': if config.model.use_flash: model_id = "CompVis/stable-diffusion-v1-4" scheduler = EulerDiscreteScheduler.from_pretrained( model_id, subfolder="scheduler" ) pipe = StableDiffusionPipeline.from_pretrained( model_id, scheduler=scheduler, torch_dtype=dtype ).to(device) pipe.enable_xformers_memory_efficient_attention() vae = pipe.vae.to(device) tokenizer = pipe.tokenizer text_encoder = pipe.text_encoder.to(device) unet = pipe.unet.to(device) else: vae = AutoencoderKL.from_pretrained( f"CompVis/stable-diffusion-v{config.model.sd_version}", subfolder="vae", torch_dtype=dtype ).to(device) tokenizer = CLIPTokenizer.from_pretrained( "openai/clip-vit-large-patch14" ) text_encoder = CLIPTextModel.from_pretrained( "openai/clip-vit-large-patch14", torch_dtype=dtype ).to(device) unet = UNet2DConditionModel.from_pretrained( f"CompVis/stable-diffusion-v{config.model.sd_version}", subfolder="unet", torch_dtype=dtype ).to(device) scheduler_config = get_scheduler_config(config) scheduler = DDPMScheduler( num_train_timesteps=scheduler_config['num_train_timesteps'], beta_start=scheduler_config['beta_start'], beta_end=scheduler_config['beta_end'], beta_schedule=scheduler_config['beta_schedule'] ) elif config.model.sd_version == '2-1': model_id = "stabilityai/stable-diffusion-2-1-base" scheduler = EulerDiscreteScheduler.from_pretrained( model_id, subfolder="scheduler" ) pipe = StableDiffusionPipeline.from_pretrained( model_id, scheduler=scheduler, torch_dtype=dtype ).to(device) pipe.enable_xformers_memory_efficient_attention() vae = pipe.vae.to(device) tokenizer = pipe.tokenizer text_encoder = pipe.text_encoder.to(device) unet = pipe.unet.to(device) else: raise NotImplementedError class_text_embeddings = utils.prepare_class_text_embeddings( device, tokenizer, text_encoder, class_names=classes ) class_text_embeddings = class_text_embeddings.detach() if config.model.adapt_only_classifier: for m in [vae, text_encoder, unet]: for param in m.parameters(): param.requires_grad = False for m in [vae, text_encoder]: for param in m.parameters(): param.requires_grad = False return (vae, tokenizer, text_encoder, unet, scheduler, image_renormalizer, class_text_embeddings) def get_scheduler_config(config): assert config.model.sd_version in {'1-4', '2-1'} if config.model.sd_version == '1-4': schedule_config = { "_class_name": "PNDMScheduler", "_diffusers_version": "0.7.0.dev0", "beta_end": 0.012, "beta_schedule": "scaled_linear", "beta_start": 0.00085, "num_train_timesteps": 1000, "set_alpha_to_one": False, "skip_prk_steps": True, "steps_offset": 1, "trained_betas": None, "clip_sample": False } elif config.model.sd_version == '2-1': schedule_config = { "_class_name": "EulerDiscreteScheduler", "_diffusers_version": "0.10.2", "beta_end": 0.012, "beta_schedule": "scaled_linear", "beta_start": 0.00085, "clip_sample": False, "num_train_timesteps": 1000, "prediction_type": "epsilon", "set_alpha_to_one": False, "skip_prk_steps": True, "steps_offset": 1, # todo "trained_betas": None } else: raise NotImplementedError return schedule_config def get_class_model(config, classes): """Load classification model""" if "clip" in config.model.class_arch:
class_model = ClipClassifier(classes, config.model.class_arch)
1
2023-11-07 21:09:50+00:00
4k
pofey/MemAI-Flow
memflow/main.py
[ { "identifier": "CuboxErrorException", "path": "memflow/exceptions.py", "snippet": "class CuboxErrorException(RuntimeError):\n def __init__(self, message):\n self.message = message" }, { "identifier": "LOGGING_CONFIG", "path": "memflow/common/logging.py", "snippet": "LOGGING_CONFIG = {\n 'version': 1,\n 'disable_existing_loggers': False,\n 'formatters': {\n 'default': {\n 'format': '%(asctime)s - %(name)s - %(levelname)s - [%(threadName)s] - %(message)s',\n },\n },\n 'handlers': {\n 'console': {\n 'class': 'logging.StreamHandler',\n 'level': 'INFO',\n 'formatter': 'default',\n },\n 'file': {\n 'class': 'logging.handlers.TimedRotatingFileHandler',\n 'level': 'INFO',\n 'formatter': 'default',\n 'filename': f\"{os.environ.get('WORKDIR')}/logs/app.log\",\n 'when': 'D',\n 'interval': 1,\n 'backupCount': 7,\n },\n },\n 'loggers': {\n '': { # root logger\n 'handlers': ['console', 'file'],\n 'level': 'INFO',\n 'propagate': True,\n },\n 'apscheduler': { # Specific logger for apscheduler\n 'handlers': ['console', 'file'],\n 'level': 'ERROR', # Set to WARNING to suppress INFO and DEBUG messages\n 'propagate': False, # Do not propagate to root logger\n },\n 'httpx': { # Specific logger for httpx\n 'handlers': ['console', 'file'],\n 'level': 'ERROR', # Set to WARNING to suppress INFO and DEBUG messages\n 'propagate': False, # Do not propagate to root logger\n },\n }\n}" }, { "identifier": "MemApi", "path": "memflow/memapi.py", "snippet": "class MemApi:\n def __init__(self, api_key: str):\n self.api_key = api_key\n self.headers = {\n \"Authorization\": \"ApiAccessToken \" + self.api_key,\n }\n\n @retry(wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(3))\n def create_mem(self, content: str):\n params = {\n \"content\": content\n }\n r = httpx.post(CREATE_MEM_API, json=params, headers=self.headers)\n r.raise_for_status()\n return r.json()" }, { "identifier": "create_all", "path": "memflow/databases.py", "snippet": "def create_all():\n \"\"\"\n 自动初始化数据库引擎和ORM框架\n 会自动生成模型定义的结构为数据表\n :return:\n \"\"\"\n Base.metadata.create_all(engine)" }, { "identifier": "json_200", "path": "memflow/common/response.py", "snippet": "def json_200(data: Union[bool, list, dict, str, None] = None, message: Union[str, None] = None) -> Response:\n \"\"\"\n 返回http_status=200的结果\n :param data: 返回结果\n :param message: 消息\n :return:\n \"\"\"\n if not message:\n message = \"success\"\n if data:\n if isinstance(data, list):\n if len(data) > 0 and 'to_dict' in dir(data[0]):\n data = [i.to_dict() for i in data]\n elif 'to_dict' in dir(data):\n data = data.to_dict()\n return PlainTextResponse(\n media_type=\"application/json\",\n status_code=status.HTTP_200_OK,\n content=json.dumps({\n 'success': True,\n 'errorCode': 0,\n 'message': message,\n 'data': data,\n }, cls=CustomJSONEncoder),\n )" }, { "identifier": "json_500", "path": "memflow/common/response.py", "snippet": "def json_500(data: Union[bool, list, dict, str, None] = None, message: Union[str, None] = None) -> Response:\n \"\"\"\n 返回http_status=500的结果\n :param data: 返回结果\n :param message: 消息\n :return:\n \"\"\"\n if not message:\n message = \"success\"\n return JSONResponse(\n status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,\n content={\n 'success': False,\n 'errorCode': 1,\n 'message': message,\n 'data': data,\n }\n )" }, { "identifier": "json_with_status", "path": "memflow/common/response.py", "snippet": "def json_with_status(status_code: int, data: Union[bool, list, dict, str, None] = None,\n message: Union[str, None] = None) -> Response:\n \"\"\"\n 返回自定义statuscode的结果\n :param data: 返回结果\n :param message: 消息\n :return:\n \"\"\"\n if not message:\n message = \"success\"\n return JSONResponse(\n status_code=status_code,\n content={\n 'success': False,\n 'errorCode': 1,\n 'message': message,\n 'data': data,\n }\n )" } ]
import os import logging.config import inject import httpx import uvicorn from memflow.exceptions import CuboxErrorException from apscheduler.schedulers.background import BackgroundScheduler from fastapi.exceptions import RequestValidationError from memflow.common.logging import LOGGING_CONFIG from memflow.memapi import MemApi from starlette.exceptions import HTTPException from fastapi import FastAPI from memflow.databases import create_all from memflow.common.response import json_200, json_500, json_with_status from memflow.models import * from memflow.tasks.cuboxsynctask import CuboxSyncTask
1,705
""" 程序启动入口类 """ if not os.environ.get("WORKDIR"): workdir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'data') else: workdir = os.environ.get("WORKDIR") if not os.path.exists(workdir): os.makedirs(workdir) log_dir = os.path.join(workdir, 'logs') if not os.path.exists(log_dir): os.makedirs(log_dir) os.environ["WORKDIR"] = workdir logging.config.dictConfig(LOGGING_CONFIG) scheduler = BackgroundScheduler(daemon=True) log = logging.getLogger(__name__) # 初始化ORM框架 create_all() app = FastAPI() # 加载所有fastapi的接口路由 @app.get("/") async def root(): """ 默认首页 :return: """ return json_200(message='memflow server') @app.exception_handler(RequestValidationError) async def unprocessable_entity_handler(request, exc: RequestValidationError): return json_with_status( status_code=422, message='Parameter error', data=dict(exc.errors()) ) @app.exception_handler(HTTPException) async def http_exception_handler(request, exc): return json_with_status(status_code=exc.status_code, message=exc.detail) @app.exception_handler(httpx.HTTPStatusError) async def http_status_exception_handler(request, e: httpx.HTTPStatusError): msg = e.response.json().get('error', {}).get('message') log.error('http status exception: ' + msg, exc_info=True) return json_500(message=msg) @app.exception_handler(Exception) async def universal_exception_handler(request, exc): log.error('universal_exception_handler', exc_info=True) return json_500(message=str(exc)) def config(binder): api_key = os.environ.get("MEM_API_KEY") if not api_key:
""" 程序启动入口类 """ if not os.environ.get("WORKDIR"): workdir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'data') else: workdir = os.environ.get("WORKDIR") if not os.path.exists(workdir): os.makedirs(workdir) log_dir = os.path.join(workdir, 'logs') if not os.path.exists(log_dir): os.makedirs(log_dir) os.environ["WORKDIR"] = workdir logging.config.dictConfig(LOGGING_CONFIG) scheduler = BackgroundScheduler(daemon=True) log = logging.getLogger(__name__) # 初始化ORM框架 create_all() app = FastAPI() # 加载所有fastapi的接口路由 @app.get("/") async def root(): """ 默认首页 :return: """ return json_200(message='memflow server') @app.exception_handler(RequestValidationError) async def unprocessable_entity_handler(request, exc: RequestValidationError): return json_with_status( status_code=422, message='Parameter error', data=dict(exc.errors()) ) @app.exception_handler(HTTPException) async def http_exception_handler(request, exc): return json_with_status(status_code=exc.status_code, message=exc.detail) @app.exception_handler(httpx.HTTPStatusError) async def http_status_exception_handler(request, e: httpx.HTTPStatusError): msg = e.response.json().get('error', {}).get('message') log.error('http status exception: ' + msg, exc_info=True) return json_500(message=msg) @app.exception_handler(Exception) async def universal_exception_handler(request, exc): log.error('universal_exception_handler', exc_info=True) return json_500(message=str(exc)) def config(binder): api_key = os.environ.get("MEM_API_KEY") if not api_key:
raise CuboxErrorException("MEM_API_KEY not found, please set it in env")
0
2023-11-08 10:02:00+00:00
4k
audioshake/alt-eval
src/alt_eval/metrics.py
[ { "identifier": "ErrorVisualizer", "path": "src/alt_eval/error_visualizer.py", "snippet": "class ErrorVisualizer:\n def __init__(self) -> None:\n self._ht_list: list[str] = []\n\n def process_chunk(\n self, chunk_ref: list[Token], chunk_hyp: list[Token], chunk_type: str\n ) -> None:\n if chunk_type == \"equal\":\n for token_ref, token_hyp in zip(chunk_ref, chunk_hyp):\n if token_ref.text != token_hyp.text:\n assert token_ref.text.lower() == token_hyp.text.lower()\n self._process_token(token_ref, \"ref-case\")\n self._process_token(token_hyp, \"hyp-case\")\n else:\n self._process_token(token_ref, \"hit\")\n else:\n for token_hyp in chunk_hyp:\n self._process_token(token_hyp, \"hyp\")\n for token_ref in chunk_ref:\n self._process_token(token_ref, \"ref\")\n\n def _process_token(self, token: Token, class_: str) -> None:\n classes = \" \".join([\"token\", class_] + [\"token-\" + tag for tag in token.tags])\n self._ht_list.append(f'<span class=\"{classes}\">{html.escape(token.text)}</span>')\n if class_[:3] in [\"ref\", \"hit\"] and {LINE, SECT} & token.tags:\n self._ht_list.append(\"<br>\")\n\n def get_html(self) -> str:\n return \" \".join(self._ht_list)" }, { "identifier": "LINE", "path": "src/alt_eval/tokenizer.py", "snippet": "LINE = \"line_break\"" }, { "identifier": "PAREN", "path": "src/alt_eval/tokenizer.py", "snippet": "PAREN = \"parenthesis\"" }, { "identifier": "PUNCT", "path": "src/alt_eval/tokenizer.py", "snippet": "PUNCT = \"punctuation\"" }, { "identifier": "SECT", "path": "src/alt_eval/tokenizer.py", "snippet": "SECT = \"section_break\"" }, { "identifier": "LyricsTokenizer", "path": "src/alt_eval/tokenizer.py", "snippet": "class LyricsTokenizer:\n \"\"\"A Moses-based tokenizer for lyrics.\n\n The tokenizer perfoms non-text character removal, Unicode normalization and punctuation\n normalization as pre-processing. Tokenization is done line by line and with special\n handling of apostrophes (elisions, contractions) and line breaks.\n \"\"\"\n\n def __init__(self) -> None:\n self._tokenizers: dict[str, MosesTokenizer] = {}\n self._punct_normalizers: dict[str, MosesPunctNormalizer] = {}\n\n self._non_text_re = re.compile(r\"[^\\w\\s\\n\\p{P}]\")\n self._empty_line_re = re.compile(r\"\\n[^\\S\\n]+\\n\")\n self._newlines_re = re.compile(r\"(\\n+)\")\n self._word_boundary_apos_re = re.compile(r\"\\b'\\B|\\B'\\b\")\n self._end_punctuation_re = re.compile(r\"\\W\\s+$\")\n self._contraction_de_re = re.compile( # German contractions, e.g. geht's, wie'n\n r\"(?P<a>)(?P<b>'s)\\b|\\b(?P<a>wie|für)(?P<b>'n)\\b\", flags=re.IGNORECASE\n )\n\n def __call__(self, text: str, language: str = \"en\") -> list[Token]:\n \"\"\"\n Tokenize the given text.\n\n Args:\n text: A string to tokenize.\n language: A language code supported by `sacremoses`: either an ISO 639-1 language code,\n or \"cjk\" for Chinese, Japanese and Korean.\n\n Returns:\n A list of `Token` objects.\n \"\"\"\n if language not in self._tokenizers:\n self._tokenizers[language] = MosesTokenizer(lang=language)\n self._punct_normalizers[language] = MosesPunctNormalizer(lang=language)\n tokenizer = self._tokenizers[language]\n punct_normalizer = self._punct_normalizers[language]\n\n text = self._non_text_re.sub(\" \", text)\n text = unicodedata.normalize(\"NFC\", text)\n text = text.rstrip(\"\\n\")\n text = self._empty_line_re.sub(\"\\n\\n\", text)\n\n result = []\n for line in self._newlines_re.split(text):\n if self._newlines_re.match(line):\n result.append(\"\\n\")\n # >= 2 newlines are considered as a section break\n if line.count(\"\\n\") >= 2:\n result.append(\"\\n\\n\")\n elif line.strip():\n # Ensure the line ends with punctuation to make the tokenizer treat it as\n # a sentence\n remove_last = False\n if not self._end_punctuation_re.search(line):\n remove_last = True\n line += \" .\"\n\n line = punct_normalizer.normalize(line)\n\n if language in [\"en\", \"fr\", \"it\"]:\n # Protect apostrophes at word boundaries to prevent the tokenizer from\n # interpreting them as quotes\n line = self._word_boundary_apos_re.sub(\"@@apos@@\", line)\n else:\n # For languages where the tokenizer doesn't handle apostrophes within words,\n # protect all apostrophes\n line = line.replace(\"'\", \"@@apos@@\")\n\n line = tokenizer.tokenize(\n line.strip(),\n return_str=True,\n escape=False,\n aggressive_dash_splits=True,\n protected_patterns=[r\"\\*+\", r\"@@apos@@\"],\n )\n\n if remove_last:\n assert line.endswith(\" .\"), line\n line = line[:-2]\n\n # Post-process apostrophes\n line = line.replace(\"@@apos@@\", \"'\")\n if language == \"de\":\n # Split contractions\n line = self._contraction_de_re.sub(r\"\\g<a> \\g<b>\", line)\n\n result.extend(line.strip().split())\n\n return to_rich_tokens(result)" }, { "identifier": "Token", "path": "src/alt_eval/tokenizer.py", "snippet": "class Token:\n \"\"\"A \"rich\" token (with a set of associated tags).\"\"\"\n\n text: str\n tags: set = field(default_factory=set)" }, { "identifier": "tokens_as_words", "path": "src/alt_eval/tokenizer.py", "snippet": "def tokens_as_words(tokens: list[Token]) -> list[Token]:\n \"\"\"Process a list of rich tokens to filter out any non-word characters.\"\"\"\n result: list[Token] = []\n for token in tokens:\n if WORD in token.tags:\n token = copy.deepcopy(token)\n token.text = re.sub(r\"[^\\w']\", \"\", token.text)\n result.append(token)\n return result" } ]
import collections import iso639 import jiwer from dataclasses import dataclass from typing import Any, Optional, Union from .error_visualizer import ErrorVisualizer from .tokenizer import LINE, PAREN, PUNCT, SECT, LyricsTokenizer, Token, tokens_as_words
2,342
IDENTITY_TRANSFORM = jiwer.Compose([]) @dataclass class EditOpCounts: """A counter for edit operations (hits, substitutions, deletions, insertions).""" H: int = 0 S: int = 0 D: int = 0 I: int = 0 def process_alignment_chunk( reference: list[Token], hypothesis: list[Token], chunk_type: str, counts: dict[Any, EditOpCounts], count_substitutions: bool = True, ) -> None: """Count tag-specific edit operations in a chunk of an alignment.""" if chunk_type == "delete": assert len(hypothesis) == 0 for token in reference: for tag in token.tags: counts[tag].D += 1 elif chunk_type == "insert": assert len(reference) == 0 for token in hypothesis: for tag in token.tags: counts[tag].I += 1 elif chunk_type in ["substitute", "equal"]: assert len(reference) == len(hypothesis) for token_ref, token_hyp in zip(reference, hypothesis): common_tags = token_ref.tags & token_hyp.tags if count_substitutions else set() for tag in token_ref.tags - common_tags: counts[tag].D += 1 for tag in token_hyp.tags - common_tags: counts[tag].I += 1 if chunk_type == "substitute": for tag in common_tags: counts[tag].S += 1 elif chunk_type == "equal": for tag in common_tags: counts[tag].H += 1 else: assert False, f"Unhandled chunk type: {chunk_type}" def process_alignments( references: list[list[Token]], hypotheses: list[list[Token]], alignments: list[list[jiwer.AlignmentChunk]], count_substitutions: bool = True, visualize_errors: bool = False, ) -> tuple[dict[Any, EditOpCounts], dict[str, int], Optional[list[str]]]: """Count tag-specific edit operations in a list of alignments.""" edit_counts = collections.defaultdict(EditOpCounts) error_counts = collections.defaultdict(int) vis_htmls = [] if visualize_errors else None for i in range(len(references)): visualizer = ErrorVisualizer() if visualize_errors else None for chunk in alignments[i]: chunk_hyp = hypotheses[i][chunk.hyp_start_idx : chunk.hyp_end_idx] chunk_ref = references[i][chunk.ref_start_idx : chunk.ref_end_idx] process_alignment_chunk( chunk_ref, chunk_hyp, chunk.type, edit_counts, count_substitutions=count_substitutions, ) if visualize_errors: visualizer.process_chunk(chunk_ref, chunk_hyp, chunk.type) if chunk.type == "equal": for token_ref, token_hyp in zip(chunk_ref, chunk_hyp): if token_ref.text != token_hyp.text: assert token_ref.text.lower() == token_hyp.text.lower() error_counts["case"] += 1 if visualize_errors: vis_htmls.append(visualizer.get_html()) return edit_counts, error_counts, vis_htmls def compute_word_metrics( references: list[list[Token]], hypotheses: list[list[Token]], count_substitutions: bool = True, visualize_errors: bool = False, ) -> dict[str, Any]:
IDENTITY_TRANSFORM = jiwer.Compose([]) @dataclass class EditOpCounts: """A counter for edit operations (hits, substitutions, deletions, insertions).""" H: int = 0 S: int = 0 D: int = 0 I: int = 0 def process_alignment_chunk( reference: list[Token], hypothesis: list[Token], chunk_type: str, counts: dict[Any, EditOpCounts], count_substitutions: bool = True, ) -> None: """Count tag-specific edit operations in a chunk of an alignment.""" if chunk_type == "delete": assert len(hypothesis) == 0 for token in reference: for tag in token.tags: counts[tag].D += 1 elif chunk_type == "insert": assert len(reference) == 0 for token in hypothesis: for tag in token.tags: counts[tag].I += 1 elif chunk_type in ["substitute", "equal"]: assert len(reference) == len(hypothesis) for token_ref, token_hyp in zip(reference, hypothesis): common_tags = token_ref.tags & token_hyp.tags if count_substitutions else set() for tag in token_ref.tags - common_tags: counts[tag].D += 1 for tag in token_hyp.tags - common_tags: counts[tag].I += 1 if chunk_type == "substitute": for tag in common_tags: counts[tag].S += 1 elif chunk_type == "equal": for tag in common_tags: counts[tag].H += 1 else: assert False, f"Unhandled chunk type: {chunk_type}" def process_alignments( references: list[list[Token]], hypotheses: list[list[Token]], alignments: list[list[jiwer.AlignmentChunk]], count_substitutions: bool = True, visualize_errors: bool = False, ) -> tuple[dict[Any, EditOpCounts], dict[str, int], Optional[list[str]]]: """Count tag-specific edit operations in a list of alignments.""" edit_counts = collections.defaultdict(EditOpCounts) error_counts = collections.defaultdict(int) vis_htmls = [] if visualize_errors else None for i in range(len(references)): visualizer = ErrorVisualizer() if visualize_errors else None for chunk in alignments[i]: chunk_hyp = hypotheses[i][chunk.hyp_start_idx : chunk.hyp_end_idx] chunk_ref = references[i][chunk.ref_start_idx : chunk.ref_end_idx] process_alignment_chunk( chunk_ref, chunk_hyp, chunk.type, edit_counts, count_substitutions=count_substitutions, ) if visualize_errors: visualizer.process_chunk(chunk_ref, chunk_hyp, chunk.type) if chunk.type == "equal": for token_ref, token_hyp in zip(chunk_ref, chunk_hyp): if token_ref.text != token_hyp.text: assert token_ref.text.lower() == token_hyp.text.lower() error_counts["case"] += 1 if visualize_errors: vis_htmls.append(visualizer.get_html()) return edit_counts, error_counts, vis_htmls def compute_word_metrics( references: list[list[Token]], hypotheses: list[list[Token]], count_substitutions: bool = True, visualize_errors: bool = False, ) -> dict[str, Any]:
references = [tokens_as_words(tokens) for tokens in references]
7
2023-11-01 14:37:15+00:00
4k
zamaniamin/fastapi-shop
demo.py
[ { "identifier": "FakeUser", "path": "apps/accounts/faker/data.py", "snippet": "class FakeUser(BaseFakeAccount):\n\n @classmethod\n def populate_members(cls):\n \"\"\"\n Create an admin and a user.\n \"\"\"\n\n # --- admin ---\n user, access_token = FakeAccount.verified_registration()\n user_data = {\n 'email': '[email protected]',\n 'first_name': cls.fake.first_name(),\n 'last_name': cls.fake.last_name(),\n 'is_superuser': True,\n 'role': 'admin'\n }\n\n UserManager.update_user(user.id, **user_data)\n\n # --- user ---\n user, access_token = FakeAccount.verified_registration()\n user_data = {\n 'email': '[email protected]',\n 'first_name': cls.fake.first_name(),\n 'last_name': cls.fake.last_name()\n }\n\n UserManager.update_user(user.id, **user_data)\n\n @classmethod\n def populate_admin(cls):\n \"\"\"\n Create an admin and generate an access token too.\n \"\"\"\n\n user, access_token = FakeAccount.verified_registration()\n user_data = {\n 'first_name': cls.fake.first_name(),\n 'last_name': cls.fake.last_name(),\n 'is_superuser': True,\n 'role': 'admin'\n }\n\n user = UserManager.update_user(user.id, **user_data)\n return user, access_token\n\n @classmethod\n def populate_user(cls):\n \"\"\"\n Create a new user and generate an access token too.\n \"\"\"\n\n user, access_token = FakeAccount.verified_registration()\n user_data = {\n 'first_name': cls.fake.first_name(),\n 'last_name': cls.fake.last_name()\n }\n\n user = UserManager.update_user(user.id, **user_data)\n return user, access_token" }, { "identifier": "FakeProduct", "path": "apps/products/faker/data.py", "snippet": "class FakeProduct:\n \"\"\"\n Populates the database with fake products.\n \"\"\"\n\n fake = Faker()\n\n options = ['color', 'size', 'material', 'Style']\n option_color_items = ['red', 'green', 'black', 'blue', 'yellow']\n option_size_items = ['S', 'M', 'L', 'XL', 'XXL']\n option_material_items = ['Cotton', 'Nylon', 'Plastic', 'Wool', 'Leather']\n option_style_items = ['Casual', 'Formal']\n\n def fill_products(self):\n \"\"\"\n For generating fake products as demo.\n \"\"\"\n self.fake.add_provider(lorem)\n\n @classmethod\n def generate_name(cls):\n return cls.fake.text(max_nb_chars=25)\n\n @classmethod\n def generate_description(cls):\n return cls.fake.paragraph(nb_sentences=5)\n\n @staticmethod\n def get_random_price():\n return round(random.uniform(1, 100), 2)\n\n @staticmethod\n def get_random_stock():\n return random.randint(0, 100)\n\n @classmethod\n def generate_uniq_options(cls):\n return [\n {\n \"option_name\": \"color\",\n \"items\": cls.option_color_items[:2]\n },\n {\n \"option_name\": \"size\",\n \"items\": cls.option_size_items[:2]\n },\n {\n \"option_name\": \"material\",\n \"items\": cls.option_material_items[:2]\n }\n ]\n\n @classmethod\n def get_payload(cls):\n payload = {\n 'product_name': cls.generate_name(),\n 'description': cls.generate_description(),\n 'status': 'active',\n 'price': cls.get_random_price(),\n 'stock': cls.get_random_stock()\n }\n return payload.copy()\n\n @classmethod\n def get_payload_with_options(cls):\n payload = {\n 'product_name': cls.generate_name(),\n 'description': cls.generate_description(),\n 'status': 'active',\n 'price': cls.get_random_price(),\n 'stock': cls.get_random_stock(),\n 'options': cls.generate_uniq_options()\n }\n return payload.copy()\n\n @classmethod\n def populate_product(cls) -> tuple[dict[str, str | int], Product]:\n \"\"\"\n Crete a product without options.\n \"\"\"\n\n product_data = cls.get_payload()\n return product_data.copy(), ProductService.create_product(product_data, get_obj=True)\n\n @classmethod\n def populate_product_with_options(cls, get_product_obj=True) -> tuple[dict[str, str | int], Product | dict]:\n \"\"\"\n Crete a product with options. (with all fields)\n \"\"\"\n\n product_data = cls.get_payload_with_options()\n return product_data.copy(), ProductService.create_product(product_data, get_obj=get_product_obj)\n\n @classmethod\n async def populate_product_with_media(cls):\n payload: dict\n product: Product\n\n # --- create a product ---\n payload, product = cls.populate_product()\n payload['alt'] = 'Test Alt Text'\n\n # --- get demo images ---\n upload = FakeMedia.populate_images_for_product(upload_file=True, product_id=product.id)\n\n # --- attach media to product ---\n media = ProductService.create_media(product.id, payload['alt'], upload)\n if media:\n return payload, product\n\n @classmethod\n async def populate_product_with_options_media(cls):\n \"\"\"\n Crete a product with options and attach some media to it.\n \"\"\"\n\n payload: dict\n product: Product\n\n # --- create a product ---\n payload, product = cls.populate_product_with_options()\n payload['alt'] = 'Test Alt Text'\n\n # --- get demo images ---\n upload = FakeMedia.populate_images_for_product(upload_file=True, product_id=product.id)\n\n # --- attach media to product ---\n media = ProductService.create_media(product.id, payload['alt'], upload)\n if media:\n return payload, product\n\n @classmethod\n async def populate_30_products(cls):\n\n # --- create 12 products with media ---\n # TODO generate random options for variable-products\n for i in range(6):\n await cls.populate_product_with_options_media()\n for i in range(6):\n await cls.populate_product_with_media()\n\n # --- create 18 products without media ---\n for i in range(9):\n cls.populate_product()\n for i in range(9):\n cls.populate_product_with_options()" } ]
import asyncio from apps.accounts.faker.data import FakeUser from apps.products.faker.data import FakeProduct from fastapi import FastAPI from config.database import DatabaseManager from config.routers import RouterManager
1,655
if __name__ == "__main__": # init models DatabaseManager().create_database_tables() # init FastAPI app = FastAPI() # init routers RouterManager(app).import_routers() # --- Demo Users ---
if __name__ == "__main__": # init models DatabaseManager().create_database_tables() # init FastAPI app = FastAPI() # init routers RouterManager(app).import_routers() # --- Demo Users ---
FakeUser.populate_members()
0
2023-11-06 04:46:03+00:00
4k
lukas-clarke/eight_sleep
custom_components/eight_sleep/config_flow.py
[ { "identifier": "EightSleep", "path": "custom_components/eight_sleep/pyEight/eight.py", "snippet": "class EightSleep:\n \"\"\"Eight sleep API object.\"\"\"\n\n def __init__(\n self,\n email: str,\n password: str,\n timezone: str,\n client_id: str = None,\n client_secret: str = None,\n client_session: ClientSession | None = None,\n check_auth: bool = False,\n ) -> None:\n \"\"\"Initialize eight sleep class.\"\"\"\n self._email = email\n self._password = password\n # If client_id isn't set, use the default value\n if not client_id:\n client_id = \"0894c7f33bb94800a03f1f4df13a4f38\"\n self._client_id = client_id\n # client_secret isn't required for current Eight Sleep API auth\n # but can't be empty value, so setting random string if not set\n if not client_secret:\n client_secret = \"ASDF\"\n self._client_secret = client_secret\n\n self.timezone = timezone\n\n self.users: dict[str, EightUser] = {}\n\n self._user_id: str | None = None\n self._token: str | None = None\n self._token_expiration: datetime | None = None\n self._device_ids: list[str] = []\n self._is_pod: bool = False\n\n # Setup 10 element list\n self._device_json_list: list[dict] = []\n\n self._api_session = client_session\n self._internal_session: bool = False\n\n if check_auth:\n self._get_auth()\n\n # Stop on exit\n atexit.register(self.at_exit)\n\n def at_exit(self) -> None:\n \"\"\"Run at exit.\"\"\"\n try:\n loop = asyncio.get_running_loop()\n asyncio.run_coroutine_threadsafe(self.stop(), loop).result()\n except RuntimeError:\n asyncio.run(self.stop())\n\n @property\n def token(self) -> str | None:\n \"\"\"Return session token.\"\"\"\n return self._token\n\n @property\n def user_id(self) -> str | None:\n \"\"\"Return user ID of the logged in user.\"\"\"\n return self._user_id\n\n @property\n def device_id(self) -> str | None:\n \"\"\"Return devices id.\"\"\"\n return self._device_ids[0]\n\n @property\n def device_data(self) -> dict:\n \"\"\"Return current raw device_data json.\"\"\"\n return self._device_json_list[0]\n\n @property\n def device_data_history(self) -> list[dict]:\n \"\"\"Return full raw device_data json list.\"\"\"\n return self._device_json_list\n\n @property\n def need_priming(self) -> bool:\n return self.device_data[\"needsPriming\"]\n\n @property\n def is_priming(self) -> bool:\n return self.device_data[\"priming\"]\n\n @property\n def has_water(self) -> bool:\n return self.device_data[\"hasWater\"]\n\n @property\n def last_prime(self):\n return self.convert_string_to_datetime(self.device_data[\"lastPrime\"])\n\n @property\n def is_pod(self) -> bool:\n \"\"\"Return if device is a POD.\"\"\"\n return self._is_pod\n\n def convert_string_to_datetime(self, datetime_str):\n datetime_str = str(datetime_str).strip()\n # Convert string to datetime object.\n try:\n # Try to parse the first format\n datetime_object = datetime.strptime(datetime_str, \"%Y-%m-%dT%H:%M:%SZ\")\n except ValueError:\n try:\n # Try to parse the second format\n datetime_object = datetime.strptime(\n datetime_str, \"%Y-%m-%dT%H:%M:%S.%fZ\"\n )\n except ValueError:\n # Handle if neither format is matched\n raise ValueError(f\"Unsupported date string format for {datetime_str}\")\n\n # Set the timezone to UTC\n utc_timezone = pytz.UTC\n datetime_object_utc = datetime_object.replace(tzinfo=utc_timezone)\n # Set the timezone to a specific timezone\n timezone = pytz.timezone(self.timezone)\n return datetime_object_utc.astimezone(timezone)\n\n async def _get_auth(self) -> Token:\n data = {\n \"client_id\": self._client_id,\n \"client_secret\": self._client_secret,\n \"grant_type\": \"password\",\n \"username\": self._email,\n \"password\": self._password,\n }\n async with httpx.AsyncClient() as client:\n response = await client.post(\n AUTH_URL,\n headers=DEFAULT_AUTH_HEADERS,\n json=data,\n timeout=DEFAULT_TIMEOUT,\n )\n if response.status_code == 200:\n access_token_str = response.json()[\"access_token\"]\n expiration_seconds_int = (\n float(response.json()[\"expires_in\"]) + time.time()\n )\n main_id = response.json()[\"userId\"]\n return Token(access_token_str, expiration_seconds_int, main_id)\n else:\n raise RequestError(\n f\"Auth request failed with status code: {response.status_code}\"\n )\n\n @property\n async def token(self) -> Token:\n \"\"\"Return session token.\"\"\"\n if not self._token:\n await self.refresh_token()\n\n if time.time() + TOKEN_TIME_BUFFER_SECONDS > self._token.expiration:\n await self.refresh_token()\n\n return self._token\n\n async def refresh_token(self):\n self._token = await self._get_auth()\n\n def fetch_user_id(self, side: str) -> str | None:\n \"\"\"Return the user_id for the specified bed side.\"\"\"\n return next(\n (user_id for user_id, user in self.users.items() if user.side == side),\n None,\n )\n\n async def update_user_data(self) -> None:\n \"\"\"Update data for users.\"\"\"\n for obj in self.users.values():\n await obj.update_user()\n\n async def start(self) -> bool:\n \"\"\"Start api initialization.\"\"\"\n _LOGGER.debug(\"Initializing pyEight.\")\n if not self._api_session:\n self._api_session = ClientSession()\n self._internal_session = True\n\n await self.token\n await self.fetch_device_list()\n await self.assign_users()\n return True\n\n async def stop(self) -> None:\n \"\"\"Stop api session.\"\"\"\n if self._internal_session and self._api_session:\n _LOGGER.debug(\"Closing eight sleep api session.\")\n await self._api_session.close()\n self._api_session = None\n elif self._internal_session:\n _LOGGER.debug(\"No-op because session hasn't been created\")\n else:\n _LOGGER.debug(\"No-op because session is being managed outside of pyEight\")\n\n async def fetch_device_list(self) -> None:\n \"\"\"Fetch list of devices.\"\"\"\n url = f\"{CLIENT_API_URL}/users/me\"\n\n dlist = await self.api_request(\"get\", url)\n self._device_ids = dlist[\"user\"][\"devices\"]\n\n if \"cooling\" in dlist[\"user\"][\"features\"]:\n self._is_pod = True\n\n _LOGGER.debug(\"Devices: %s, POD: %s\", self._device_ids, self._is_pod)\n\n async def assign_users(self) -> None:\n \"\"\"Update device properties.\"\"\"\n device_id = self._device_ids[0]\n url = f\"{CLIENT_API_URL}/devices/{device_id}?filter=ownerId,leftUserId,rightUserId\"\n\n data = await self.api_request(\"get\", url)\n for side in (\"left\", \"right\"):\n user_id = data[\"result\"].get(f\"{side}UserId\")\n if user_id is not None and user_id not in self.users:\n user = self.users[user_id] = EightUser(self, user_id, side)\n await user.update_user_profile()\n\n @property\n def room_temperature(self) -> float | None:\n \"\"\"Return room temperature for both sides of bed.\"\"\"\n # Check which side is active, if both are return the average\n tmp = None\n tmp2 = None\n for user in self.users.values():\n if user.current_values[\"processing\"]:\n if tmp is None:\n tmp = user.current_values[\"room_temp\"]\n else:\n tmp = (tmp + user.current_values[\"room_temp\"]) / 2\n else:\n if tmp2 is None:\n tmp2 = user.current_values[\"room_temp\"]\n else:\n tmp2 = (tmp2 + user.current_values[\"room_temp\"]) / 2\n\n if tmp is not None:\n return tmp\n\n # If tmp2 is None we will just return None\n return tmp2\n\n def handle_device_json(self, data: dict[str, Any]) -> None:\n \"\"\"Manage the device json list.\"\"\"\n self._device_json_list = [data, *self._device_json_list][:10]\n\n async def update_device_data(self) -> None:\n \"\"\"Update device data json.\"\"\"\n url = f\"{CLIENT_API_URL}/devices/{self.device_id}\"\n\n device_resp = await self.api_request(\"get\", url)\n # Want to keep last 10 readings so purge the last after we add\n self.handle_device_json(device_resp[\"result\"])\n for obj in self.users.values():\n obj.dynamic_presence()\n\n async def api_request(\n self,\n method: str,\n url: str,\n params: dict[str, Any] | None = None,\n data: dict[str, Any] | None = None,\n input_headers=None,\n ) -> Any:\n \"\"\"Make api request.\"\"\"\n if input_headers is not None:\n headers = input_headers\n else:\n headers = DEFAULT_API_HEADERS\n\n token = await self.token\n headers.update({\"authorization\": f\"Bearer {token.bearer_token}\"})\n try:\n assert self._api_session\n resp = await self._api_session.request(\n method,\n url,\n headers=headers,\n params=params,\n json=data,\n timeout=CLIENT_TIMEOUT,\n raise_for_status=True,\n )\n if resp.status == 401:\n # refresh token and try again if request in unauthorized\n await self.refresh_token()\n return await self.api_request(method, url, params, data, input_headers)\n return await resp.json()\n\n except (ClientError, asyncio.TimeoutError, ConnectionRefusedError) as err:\n _LOGGER.error(f\"Error {method}ing Eight data. {err}s\")\n raise RequestError from err" }, { "identifier": "RequestError", "path": "custom_components/eight_sleep/pyEight/exceptions.py", "snippet": "class RequestError(BaseEightSleepError):\n \"\"\"Exception for eight sleep request failures.\"\"\"" }, { "identifier": "DOMAIN", "path": "custom_components/eight_sleep/const.py", "snippet": "DOMAIN = \"eight_sleep\"" } ]
import logging import voluptuous as vol from typing import Any from .pyEight.eight import EightSleep from .pyEight.exceptions import RequestError from homeassistant import config_entries from homeassistant.const import ( CONF_PASSWORD, CONF_USERNAME, CONF_CLIENT_ID, CONF_CLIENT_SECRET, ) from homeassistant.data_entry_flow import FlowResult from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.selector import ( TextSelector, TextSelectorConfig, TextSelectorType, ) from .const import DOMAIN
3,042
"""Config flow for Eight Sleep integration.""" from __future__ import annotations _LOGGER = logging.getLogger(__name__) STEP_USER_DATA_SCHEMA = vol.Schema( { vol.Required(CONF_USERNAME): TextSelector( TextSelectorConfig(type=TextSelectorType.EMAIL) ), vol.Required(CONF_PASSWORD): TextSelector( TextSelectorConfig(type=TextSelectorType.PASSWORD) ), vol.Optional(CONF_CLIENT_ID): TextSelector( TextSelectorConfig(type=TextSelectorType.TEXT) ), vol.Optional(CONF_CLIENT_SECRET): TextSelector( TextSelectorConfig(type=TextSelectorType.PASSWORD) ), } ) class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """Handle a config flow for Eight Sleep.""" VERSION = 1 async def _validate_data(self, config: dict[str, str]) -> str | None: """Validate input data and return any error.""" await self.async_set_unique_id(config[CONF_USERNAME].lower()) self._abort_if_unique_id_configured() if CONF_CLIENT_ID in config: client_id = config[CONF_CLIENT_ID] else: client_id = None if CONF_CLIENT_SECRET in config: client_secret = config[CONF_CLIENT_SECRET] else: client_secret = None eight = EightSleep( config[CONF_USERNAME], config[CONF_PASSWORD], self.hass.config.time_zone, client_id, client_secret, client_session=async_get_clientsession(self.hass), ) try: await eight.refresh_token()
"""Config flow for Eight Sleep integration.""" from __future__ import annotations _LOGGER = logging.getLogger(__name__) STEP_USER_DATA_SCHEMA = vol.Schema( { vol.Required(CONF_USERNAME): TextSelector( TextSelectorConfig(type=TextSelectorType.EMAIL) ), vol.Required(CONF_PASSWORD): TextSelector( TextSelectorConfig(type=TextSelectorType.PASSWORD) ), vol.Optional(CONF_CLIENT_ID): TextSelector( TextSelectorConfig(type=TextSelectorType.TEXT) ), vol.Optional(CONF_CLIENT_SECRET): TextSelector( TextSelectorConfig(type=TextSelectorType.PASSWORD) ), } ) class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """Handle a config flow for Eight Sleep.""" VERSION = 1 async def _validate_data(self, config: dict[str, str]) -> str | None: """Validate input data and return any error.""" await self.async_set_unique_id(config[CONF_USERNAME].lower()) self._abort_if_unique_id_configured() if CONF_CLIENT_ID in config: client_id = config[CONF_CLIENT_ID] else: client_id = None if CONF_CLIENT_SECRET in config: client_secret = config[CONF_CLIENT_SECRET] else: client_secret = None eight = EightSleep( config[CONF_USERNAME], config[CONF_PASSWORD], self.hass.config.time_zone, client_id, client_secret, client_session=async_get_clientsession(self.hass), ) try: await eight.refresh_token()
except RequestError as err:
1
2023-11-01 16:15:52+00:00
4k
jkulhanek/nerfbaselines
nerfbaselines/communication.py
[ { "identifier": "Method", "path": "nerfbaselines/types.py", "snippet": "class Method(Protocol):\n @classmethod\n def install(cls):\n \"\"\"\n Install the method.\n \"\"\"\n pass\n\n @abstractmethod\n def get_info(self) -> MethodInfo:\n \"\"\"\n Get method defaults for the trainer.\n\n Returns:\n Method info.\n \"\"\"\n return MethodInfo()\n\n @abstractmethod\n def render(self, cameras: Cameras, progress_callback: Optional[ProgressCallback] = None) -> Iterable[RenderOutput]: # [h w c]\n \"\"\"\n Render images.\n\n Args:\n cameras: Cameras.\n progress_callback: Callback for progress.\n \"\"\"\n raise NotImplementedError()\n\n @abstractmethod\n def setup_train(self, train_dataset: Dataset, *, num_iterations: int):\n \"\"\"\n Setup training data, model, optimizer, etc.\n\n Args:\n train_dataset: Training dataset.\n num_iterations: Number of iterations to train.\n \"\"\"\n raise NotImplementedError()\n\n @abstractmethod\n def train_iteration(self, step: int):\n \"\"\"\n Train one iteration.\n\n Args:\n step: Current step.\n \"\"\"\n raise NotImplementedError()\n\n @abstractmethod\n def save(self, path: Path):\n \"\"\"\n Save model.\n\n Args:\n path: Path to save.\n \"\"\"\n raise NotImplementedError()" }, { "identifier": "MethodInfo", "path": "nerfbaselines/types.py", "snippet": "class MethodInfo:\n loaded_step: Optional[int] = None\n num_iterations: Optional[int] = None\n batch_size: Optional[int] = None\n eval_batch_size: Optional[int] = None\n required_features: FrozenSet[DatasetFeature] = field(default_factory=frozenset)\n supported_camera_models: FrozenSet = field(default_factory=lambda: frozenset((CameraModel.PINHOLE,)))" }, { "identifier": "NB_PREFIX", "path": "nerfbaselines/types.py", "snippet": "NB_PREFIX = os.path.expanduser(os.environ.get(\"NB_PREFIX\", \"~/.cache/nerfbaselines\"))" }, { "identifier": "partialmethod", "path": "nerfbaselines/utils.py", "snippet": "def partialmethod(func, *args, **kwargs):\n def wrapped(self, *args2, **kwargs2):\n return func(self, *args, *args2, **kwargs, **kwargs2)\n\n wrapped.__original_func__ = func # type: ignore\n wrapped.__args__ = args # type: ignore\n wrapped.__kwargs__ = kwargs # type: ignore\n return wrapped" }, { "identifier": "cancellable", "path": "nerfbaselines/utils.py", "snippet": "def cancellable(fn=None, mark_only=False):\n def wrap(fn):\n if getattr(fn, \"__cancellable__\", False):\n return fn\n if mark_only:\n fn.__cancellable__ = True\n return fn\n\n if inspect.isgeneratorfunction(fn):\n\n @wraps(fn)\n def wrapped(*args, cancellation_token: Optional[CancellationToken] = None, **kwargs):\n if cancellation_token is not None:\n yield from cancellation_token.invoke(fn, *args, **kwargs)\n else:\n yield from fn(*args, **kwargs)\n\n else:\n\n @wraps(fn)\n def wrapped(*args, cancellation_token: Optional[CancellationToken] = None, **kwargs):\n if cancellation_token is not None:\n return cancellation_token.invoke(fn, *args, **kwargs)\n else:\n return fn(*args, **kwargs)\n\n wrapped.__cancellable__ = True\n return wrapped\n\n return wrap if fn is None else wrap(fn)" }, { "identifier": "CancellationToken", "path": "nerfbaselines/utils.py", "snippet": "class CancellationToken:\n def __init__(self):\n self._cancelled = False\n\n def cancel(self):\n self._cancelled = True\n\n @property\n def cancelled(self):\n return self._cancelled\n\n def _trace(self, frame, event, arg):\n if event == \"line\":\n if self.cancelled:\n raise CancelledException\n return self._trace\n\n def _invoke_generator(self, fn, *args, **kwargs):\n try:\n sys.settrace(self._trace)\n for r in fn(*args, **kwargs):\n yield r\n finally:\n sys.settrace(None)\n\n def invoke(self, fn, *args, **kwargs):\n if inspect.isgeneratorfunction(fn):\n return self._invoke_generator(fn, *args, **kwargs)\n\n try:\n sys.settrace(self._trace)\n return fn(*args, **kwargs)\n finally:\n sys.settrace(None)" }, { "identifier": "CancelledException", "path": "nerfbaselines/utils.py", "snippet": "class CancelledException(Exception):\n pass" } ]
import importlib import types import subprocess import tempfile import pickle import base64 import os import shutil import hashlib import traceback import inspect import random import secrets import logging from threading import Thread from pathlib import Path from time import sleep from typing import Optional, Tuple, Type, List, Dict from dataclasses import dataclass, field, is_dataclass from multiprocessing.connection import Listener, Client, Connection from queue import Queue, Empty from .types import Method, MethodInfo from .types import NB_PREFIX # noqa: F401 from .utils import partialmethod, cancellable, CancellationToken, CancelledException
2,809
thread = Thread(target=handler, daemon=True) thread.start() while not cancellation_token.cancelled: try: msg = input_queue.get(timeout=0.1) except Empty: continue message = msg["message"] mid = msg["id"] if message == "get": logging.debug(f"Obtaining property {msg['property']}") try: result = getattr(method, msg["property"]) if cancellation_token.cancelled: break output_queue.put({"message": "result", "id": mid, "result": result}) except Exception as e: # pylint: disable=broad-except traceback.print_exc() logging.error(f"Error while obtaining property {msg['property']}") if cancellation_token.cancelled: break output_queue.put({"message": "error", "id": mid, "error": _remap_error(e)}) elif message == "call": try: method_or_fn = msg.get("function", msg.get("method")) if "function" in msg: logging.debug(f"Calling function {msg['function']}") splitter = msg["function"].rindex(".") package, fnname = msg["function"][:splitter], msg["function"][splitter + 1 :] fn = getattr(importlib.import_module(package), fnname) else: logging.debug(f"Calling method {msg['method']}") fn = getattr(method, msg["method"]) kwargs = inject_callables(msg["kwargs"], output_queue, mid) args = inject_callables(msg["args"], output_queue, mid) if msg["cancellable"]: fn = cancellable(fn) kwargs["cancellation_token"] = cancellation_tokens[mid] result = fn(*args, **kwargs) if inspect.isgeneratorfunction(fn): for r in result: if cancellation_token.cancelled: break output_queue.put({"message": "yield", "id": mid, "yield": r}) result = None if cancellation_token.cancelled: break output_queue.put({"message": "result", "id": mid, "result": result}) except Exception as e: # pylint: disable=broad-except if not isinstance(e, CancelledException): traceback.print_exc() logging.error(f"Error while calling method/function {method_or_fn} from") if cancellation_token.cancelled: break output_queue.put({"message": "error", "id": mid, "error": _remap_error(e)}) cancellation_tokens.pop(mid, None) else: logging.error(f"Unknown message {msg}") output_queue.put({"message": "error", "id": mid, "error": _remap_error(RuntimeError(f"Unknown message {msg}"))}) logging.info("Client disconnected, shutting down") class RemoteCallable: def __init__(self, i): self.id = i def replace_callables(obj, callables, depth=0): if callable(obj): is_host = getattr(obj, "__host__", depth == 0) if is_host: callables.append(obj) return RemoteCallable(len(callables) - 1) else: return obj if isinstance(obj, dict): return {k: replace_callables(v, callables, depth + 1) for k, v in obj.items()} if isinstance(obj, (list, tuple)): return obj.__class__((replace_callables(v, callables, depth + 1) for v in obj)) if is_dataclass(obj): return obj.__class__(**{k: replace_callables(v, callables, depth + 1) for k, v in obj.__dict__.items()}) return obj def inject_callables(obj, output_queue, my_id): if isinstance(obj, RemoteCallable): def callback(*args, **kwargs): output_queue.put({"message": "callback", "id": my_id, "callback": obj.id, "args": args, "kwargs": kwargs}) return callback if isinstance(obj, dict): return {k: inject_callables(v, output_queue, my_id) for k, v in obj.items()} if isinstance(obj, (list, tuple)): return obj.__class__((inject_callables(v, output_queue, my_id) for v in obj)) if is_dataclass(obj): return obj.__class__(**{k: inject_callables(v, output_queue, my_id) for k, v in obj.__dict__.items()}) return obj class RemoteMethod(Method): def __init__(self, *args, checkpoint: Optional[Path] = None, connection_params: Optional[ConnectionParams] = None, **kwargs): self.connection_params = connection_params or ConnectionParams() self._client: Optional[Connection] = None self._message_counter = 0 self.args = args self.kwargs = kwargs self.checkpoint = checkpoint self._cancellation_tokens = {} @property def encoded_args(self): kwargs = self.kwargs if self.checkpoint is not None: checkpoint = self.checkpoint kwargs = dict(**self.kwargs, checkpoint=checkpoint) return base64.b64encode(pickle.dumps((self.args, kwargs))).decode("ascii")
PACKAGE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @dataclass class ConnectionParams: port: int = field(default_factory=lambda: random.randint(10000, 20000)) authkey: bytes = field(default_factory=lambda: secrets.token_hex(64).encode("ascii")) def _report_ready(): pass def _remap_error(e: Exception): if e.__class__.__module__ == "builtins": return e elif e.__class__.__module__.startswith(_remap_error.__module__.split(".")[0]): return e # Remap exception return RuntimeError(f"Exception {e.__class__.__name__}: {e}") def start_backend(method: Method, params: ConnectionParams, address: str = "localhost"): cancellation_token = CancellationToken() cancellation_tokens = {} input_queue = Queue(maxsize=3) output_queue = Queue(maxsize=32) def handler(): with Listener((address, params.port), authkey=params.authkey) as listener: _report_ready() logging.info("Waiting for connection") with listener.accept() as conn: logging.info(f"Connection accepted from {listener.last_accepted}") while not conn.closed and not cancellation_token.cancelled: if conn.poll(): msg = conn.recv() message = msg["message"] mid = msg["id"] # do something with msg if message == "close": conn.send({"message": "close_ack", "id": mid}) cancellation_token.cancel() break if message == "cancel": # if mid in cancellation_tokens: conn.send({"message": "cancel_ack", "id": mid}) if mid in cancellation_tokens: cancellation_tokens[mid].cancel() elif message in {"call", "get"}: if msg.get("cancellable", False): cancellation_tokens[mid] = CancellationToken() input_queue.put(msg) elif not output_queue.empty(): conn.send(output_queue.get()) else: sleep(0.0001) thread = Thread(target=handler, daemon=True) thread.start() while not cancellation_token.cancelled: try: msg = input_queue.get(timeout=0.1) except Empty: continue message = msg["message"] mid = msg["id"] if message == "get": logging.debug(f"Obtaining property {msg['property']}") try: result = getattr(method, msg["property"]) if cancellation_token.cancelled: break output_queue.put({"message": "result", "id": mid, "result": result}) except Exception as e: # pylint: disable=broad-except traceback.print_exc() logging.error(f"Error while obtaining property {msg['property']}") if cancellation_token.cancelled: break output_queue.put({"message": "error", "id": mid, "error": _remap_error(e)}) elif message == "call": try: method_or_fn = msg.get("function", msg.get("method")) if "function" in msg: logging.debug(f"Calling function {msg['function']}") splitter = msg["function"].rindex(".") package, fnname = msg["function"][:splitter], msg["function"][splitter + 1 :] fn = getattr(importlib.import_module(package), fnname) else: logging.debug(f"Calling method {msg['method']}") fn = getattr(method, msg["method"]) kwargs = inject_callables(msg["kwargs"], output_queue, mid) args = inject_callables(msg["args"], output_queue, mid) if msg["cancellable"]: fn = cancellable(fn) kwargs["cancellation_token"] = cancellation_tokens[mid] result = fn(*args, **kwargs) if inspect.isgeneratorfunction(fn): for r in result: if cancellation_token.cancelled: break output_queue.put({"message": "yield", "id": mid, "yield": r}) result = None if cancellation_token.cancelled: break output_queue.put({"message": "result", "id": mid, "result": result}) except Exception as e: # pylint: disable=broad-except if not isinstance(e, CancelledException): traceback.print_exc() logging.error(f"Error while calling method/function {method_or_fn} from") if cancellation_token.cancelled: break output_queue.put({"message": "error", "id": mid, "error": _remap_error(e)}) cancellation_tokens.pop(mid, None) else: logging.error(f"Unknown message {msg}") output_queue.put({"message": "error", "id": mid, "error": _remap_error(RuntimeError(f"Unknown message {msg}"))}) logging.info("Client disconnected, shutting down") class RemoteCallable: def __init__(self, i): self.id = i def replace_callables(obj, callables, depth=0): if callable(obj): is_host = getattr(obj, "__host__", depth == 0) if is_host: callables.append(obj) return RemoteCallable(len(callables) - 1) else: return obj if isinstance(obj, dict): return {k: replace_callables(v, callables, depth + 1) for k, v in obj.items()} if isinstance(obj, (list, tuple)): return obj.__class__((replace_callables(v, callables, depth + 1) for v in obj)) if is_dataclass(obj): return obj.__class__(**{k: replace_callables(v, callables, depth + 1) for k, v in obj.__dict__.items()}) return obj def inject_callables(obj, output_queue, my_id): if isinstance(obj, RemoteCallable): def callback(*args, **kwargs): output_queue.put({"message": "callback", "id": my_id, "callback": obj.id, "args": args, "kwargs": kwargs}) return callback if isinstance(obj, dict): return {k: inject_callables(v, output_queue, my_id) for k, v in obj.items()} if isinstance(obj, (list, tuple)): return obj.__class__((inject_callables(v, output_queue, my_id) for v in obj)) if is_dataclass(obj): return obj.__class__(**{k: inject_callables(v, output_queue, my_id) for k, v in obj.__dict__.items()}) return obj class RemoteMethod(Method): def __init__(self, *args, checkpoint: Optional[Path] = None, connection_params: Optional[ConnectionParams] = None, **kwargs): self.connection_params = connection_params or ConnectionParams() self._client: Optional[Connection] = None self._message_counter = 0 self.args = args self.kwargs = kwargs self.checkpoint = checkpoint self._cancellation_tokens = {} @property def encoded_args(self): kwargs = self.kwargs if self.checkpoint is not None: checkpoint = self.checkpoint kwargs = dict(**self.kwargs, checkpoint=checkpoint) return base64.b64encode(pickle.dumps((self.args, kwargs))).decode("ascii")
def get_info(self) -> MethodInfo:
1
2023-11-07 20:22:35+00:00
4k
ultraleap/leapc-python-bindings
leapc-python-api/src/leap/events.py
[ { "identifier": "LeapCStruct", "path": "leapc-python-api/src/leap/cstruct.py", "snippet": "class LeapCStruct:\n \"\"\"Base class for objects which wrap around some raw C Data\n\n Classes which inherit from this should only be loose wrappers around\n some struct from the LeapC API.\n\n :param data: The raw CData\n \"\"\"\n\n def __init__(self, data: ffi.CData):\n self._data = data\n\n @property\n def c_data(self) -> ffi.CData:\n \"\"\"Get the raw C data\"\"\"\n return self._data" }, { "identifier": "FrameHeader", "path": "leapc-python-api/src/leap/datatypes.py", "snippet": "class FrameHeader(LeapCStruct):\n @property\n def frame_id(self):\n return self._data.frame_id\n\n @property\n def timestamp(self):\n return self._data.timestamp" }, { "identifier": "Hand", "path": "leapc-python-api/src/leap/datatypes.py", "snippet": "class Hand(LeapCStruct):\n @property\n def id(self):\n return self._data.id\n\n @property\n def flags(self):\n return self._data.flags\n\n @property\n def type(self):\n return HandType(self._data.type)\n\n @property\n def confidence(self):\n return self._data.confidence\n\n @property\n def visible_time(self):\n return self._data.visible_time\n\n @property\n def pinch_distance(self):\n return self._data.pinch_distance\n\n @property\n def grab_angle(self):\n return self._data.grab_angle\n\n @property\n def pinch_strength(self):\n return self._data.pinch_strength\n\n @property\n def grab_strength(self):\n return self._data.grab_strength\n\n @property\n def palm(self):\n return Palm(self._data.palm)\n\n @property\n def thumb(self):\n return Digit(self._data.thumb)\n\n @property\n def index(self):\n return Digit(self._data.index)\n\n @property\n def middle(self):\n return Digit(self._data.middle)\n\n @property\n def ring(self):\n return Digit(self._data.ring)\n\n @property\n def pinky(self):\n return Digit(self._data.pinky)\n\n @property\n def digits(self):\n return [self.thumb, self.index, self.middle, self.ring, self.pinky]\n\n @property\n def arm(self):\n return Bone(self._data.arm)" }, { "identifier": "Vector", "path": "leapc-python-api/src/leap/datatypes.py", "snippet": "class Vector(LeapCStruct):\n def __getitem__(self, idx):\n return self._data.v[idx]\n\n def __iter__(self):\n return [self._data.v[i] for i in range(3)].__iter__()\n\n @property\n def x(self):\n return self._data.x\n\n @property\n def y(self):\n return self._data.y\n\n @property\n def z(self):\n return self._data.z" }, { "identifier": "Image", "path": "leapc-python-api/src/leap/datatypes.py", "snippet": "class Image(LeapCStruct):\n @property\n def matrix_version(self):\n return self._data.matrix_version" }, { "identifier": "Device", "path": "leapc-python-api/src/leap/device.py", "snippet": "class Device:\n def __init__(self, device_ref=None, *, device=None, owner=None):\n \"\"\"A Device is usually constructed from a LEAP_DEVICE_REF object.\n\n Some functions require the device to be opened before they can be\n called.\n\n If a DeviceLost event occurs, this can be created from a LEAP_DEVICE\n object. In this case the Device is already open and does not need to\n be closed by the user.\n\n The 'owner' argument is a CFFI object that must be kept alive\n for the device ref to remain valid. It should never be used from\n within the class.\n \"\"\"\n self._device_ref = device_ref\n self._device = device\n self._owner = owner\n\n @property\n def c_data_device_ref(self):\n \"\"\"Get the LEAP_DEVICE_REF object for this object\"\"\"\n return self._device_ref\n\n @property\n def c_data_device(self):\n \"\"\"Get the LEAP_DEVICE object for this object\n\n If the device is not open, returns None\n \"\"\"\n return self._device\n\n @property\n def id(self):\n if self._device_ref is None:\n # The device must have been returned from a DeviceLostEvent\n # This means it does not have an id, so return None\n return\n return self._device_ref.id\n\n @contextmanager\n def open(self):\n if self._device is not None:\n raise LeapCannotOpenDeviceError(\"Device is already open\")\n\n device_ptr = ffi.new(\"LEAP_DEVICE*\")\n success_or_raise(libleapc.LeapOpenDevice, self._device_ref, device_ptr)\n self._device = device_ptr[0]\n try:\n yield self\n finally:\n self._device = None\n libleapc.LeapCloseDevice(device_ptr[0])\n\n def get_info(self):\n \"\"\"Get a DeviceInfo object containing information about this device\n\n Requires the Device to be open.\n Raises DeviceNotOpenException if the device is not open.\n \"\"\"\n if self._device is None:\n raise DeviceNotOpenException()\n info_ptr = ffi.new(\"LEAP_DEVICE_INFO*\")\n info_ptr.size = ffi.sizeof(info_ptr[0])\n info_ptr.serial = ffi.NULL\n success_or_raise(libleapc.LeapGetDeviceInfo, self._device, info_ptr)\n info_ptr.serial = ffi.new(\"char[]\", info_ptr.serial_length)\n success_or_raise(libleapc.LeapGetDeviceInfo, self._device, info_ptr)\n return DeviceInfo(info_ptr[0])\n\n def get_camera_count(self):\n if not self._device:\n raise DeviceNotOpenException()\n camera_count_ptr = ffi.new(\"uint8_t *\")\n success_or_raise(libleapc.LeapGetDeviceCameraCount, self._device, camera_count_ptr)\n return camera_count_ptr[0]" }, { "identifier": "DeviceStatusInfo", "path": "leapc-python-api/src/leap/device.py", "snippet": "class DeviceStatusInfo:\n def __init__(self, status: ffi.CData):\n \"\"\"Create the DeviceStatusInfo\n\n :param status: The CData defining the status\n \"\"\"\n self._status_flags = get_enum_entries(DeviceStatus, status)\n\n @staticmethod\n def _get_flags(status_int):\n return get_enum_entries(DeviceStatus, status_int)\n\n def check(self, flag: DeviceStatus):\n \"\"\"Check if the flag is in the current flags\n\n :param flag: The flag to check\n \"\"\"\n return flag in self._status_flags\n\n @property\n def flags(self):\n return self._status_flags" }, { "identifier": "EventType", "path": "leapc-python-api/src/leap/enums.py", "snippet": "class EventType(metaclass=LeapEnum):\n pass" }, { "identifier": "get_enum_entries", "path": "leapc-python-api/src/leap/enums.py", "snippet": "def get_enum_entries(enum_type, flags):\n \"\"\"Interpret the flags as a bitwise combination of enum values\n\n Returns a list of enum entries which are present in the 'flags'.\n \"\"\"\n return list(filter(lambda entry: entry.value & flags != 0, enum_type))" }, { "identifier": "TrackingMode", "path": "leapc-python-api/src/leap/enums.py", "snippet": "class TrackingMode(metaclass=LeapEnum):\n pass" }, { "identifier": "PolicyFlag", "path": "leapc-python-api/src/leap/enums.py", "snippet": "class PolicyFlag(metaclass=LeapEnum):\n pass" }, { "identifier": "IMUFlag", "path": "leapc-python-api/src/leap/enums.py", "snippet": "class IMUFlag(metaclass=LeapEnum):\n pass" } ]
from .cstruct import LeapCStruct from .datatypes import FrameHeader, Hand, Vector, Image from .device import Device, DeviceStatusInfo from .enums import EventType, get_enum_entries, TrackingMode, PolicyFlag, IMUFlag from leapc_cffi import ffi
3,418
self._info = FrameHeader(data.info) self._tracking_frame_id = data.tracking_frame_id self._num_hands = data.nHands self._framerate = data.framerate # Copy hands to safe region of memory to protect against use-after-free (UAF) self._hands = ffi.new("LEAP_HAND[2]") ffi.memmove(self._hands, data.pHands, ffi.sizeof("LEAP_HAND") * data.nHands) @property def info(self): return self._info @property def timestamp(self): return self._info.timestamp @property def tracking_frame_id(self): return self._tracking_frame_id @property def hands(self): return [Hand(self._hands[i]) for i in range(self._num_hands)] @property def framerate(self): return self._framerate class ImageRequestErrorEvent(Event): _EVENT_TYPE = EventType.ImageRequestError _EVENT_ATTRIBUTE = "pointer" class ImageCompleteEvent(Event): _EVENT_TYPE = EventType.ImageComplete _EVENT_ATTRIBUTE = "pointer" class LogEvent(Event): _EVENT_TYPE = EventType.LogEvent _EVENT_ATTRIBUTE = "log_event" class DeviceLostEvent(Event): _EVENT_TYPE = EventType.DeviceLost _EVENT_ATTRIBUTE = "device_event" def __init__(self, data): super().__init__(data) self._device = Device(data.device) self._status = DeviceStatusInfo(data.status) @property def device(self): return self._device @property def status(self): return self._status class ConfigResponseEvent(Event): _EVENT_TYPE = EventType.ConfigResponse _EVENT_ATTRIBUTE = "config_response_event" class ConfigChangeEvent(Event): _EVENT_TYPE = EventType.ConfigChange _EVENT_ATTRIBUTE = "config_change_event" class DeviceStatusChangeEvent(Event): _EVENT_TYPE = EventType.DeviceStatusChange _EVENT_ATTRIBUTE = "device_status_change_event" def __init__(self, data): super().__init__(data) self._device = Device(data.device) self._last_status = DeviceStatusInfo(data.last_status) self._status = DeviceStatusInfo(data.status) @property def device(self): return self._device @property def last_status(self): return self._last_status @property def status(self): return self._status class DroppedFrameEvent(Event): _EVENT_TYPE = EventType.DroppedFrame _EVENT_ATTRIBUTE = "dropped_frame_event" class ImageEvent(Event): _EVENT_TYPE = EventType.Image _EVENT_ATTRIBUTE = "image_event" def __init__(self, data): super().__init__(data) self._images = data.image @property def image(self): return [Image(self._images[0]), Image(self._images[1])] class PointMappingChangeEvent(Event): _EVENT_TYPE = EventType.PointMappingChange _EVENT_ATTRIBUTE = "point_mapping_change_event" class TrackingModeEvent(Event):
"""Classes for each of the LeapC Events These are created so that the members can be accessed as our custom Python objects instead of C Objects. """ class EventMetadata(LeapCStruct): def __init__(self, data): super().__init__(data) self._event_type = EventType(data.type) self._device_id = data.device_id @property def event_type(self): return self._event_type @property def device_id(self): return self._device_id class Event(LeapCStruct): """Base class for Events Events have extra 'type' and 'metadata' properties. If the Event is constructed using the default constructor, the metadata is not populated. If the event is constructed using a `LEAP_CONNECTION_MESSAGE*` via the `from_connection_message` method, extra metadata will be available on the event. """ # The type of event this class corresponds to _EVENT_TYPE = EventType.EventTypeNone # The member on the `LEAP_CONNECTION_MESSAGE` that corresponds to the # event data. _EVENT_MESSAGE_ATTRIBUTE = "pointer" def __init__(self, data): super().__init__(data) self._metadata = None @classmethod def from_connection_message(cls, c_message): """Construct an Event from a LEAP_CONNECTION_MESSAGE* object Constructing an event in this way populates the event metadata. """ if EventType(c_message.type) != cls._EVENT_TYPE: raise ValueError("Incorect event type") event = cls(getattr(c_message, cls._EVENT_ATTRIBUTE)) event._metadata = EventMetadata(c_message) return event @classmethod def _get_event_cdata(cls, c_message): return getattr(c_message, cls._EVENT_ATTRIBUTE) @property def metadata(self): return self._metadata @property def type(self): return self._EVENT_TYPE class NoneEvent(Event): _EVENT_TYPE = EventType.EventTypeNone _EVENT_ATTRIBUTE = "pointer" class ConnectionEvent(Event): _EVENT_TYPE = EventType.Connection _EVENT_ATTRIBUTE = "connection_event" class ConnectionLostEvent(Event): _EVENT_TYPE = EventType.ConnectionLost _EVENT_ATTRIBUTE = "connection_lost_event" class DeviceEvent(Event): _EVENT_TYPE = EventType.Device _EVENT_ATTRIBUTE = "device_event" def __init__(self, data): super().__init__(data) self._device = Device(data.device) self._status = DeviceStatusInfo(data.status) @property def device(self): return self._device @property def status(self): return self._status class DeviceFailureEvent(Event): _EVENT_TYPE = EventType.DeviceFailure _EVENT_ATTRIBUTE = "device_failure_event" def __init__(self, data): super().__init__(data) self._device = Device(device=data.hDevice) self._status = DeviceStatusInfo(data.status) @property def device(self): return self._device @property def status(self): return self._status class PolicyEvent(Event): _EVENT_TYPE = EventType.Policy _EVENT_ATTRIBUTE = "policy_event" def __init__(self, data): super().__init__(data) self._flags = data.current_policy @property def current_policy_flags(self): return get_enum_entries(PolicyFlag, self._flags) class TrackingEvent(Event): _EVENT_TYPE = EventType.Tracking _EVENT_ATTRIBUTE = "tracking_event" def __init__(self, data): super().__init__(data) self._info = FrameHeader(data.info) self._tracking_frame_id = data.tracking_frame_id self._num_hands = data.nHands self._framerate = data.framerate # Copy hands to safe region of memory to protect against use-after-free (UAF) self._hands = ffi.new("LEAP_HAND[2]") ffi.memmove(self._hands, data.pHands, ffi.sizeof("LEAP_HAND") * data.nHands) @property def info(self): return self._info @property def timestamp(self): return self._info.timestamp @property def tracking_frame_id(self): return self._tracking_frame_id @property def hands(self): return [Hand(self._hands[i]) for i in range(self._num_hands)] @property def framerate(self): return self._framerate class ImageRequestErrorEvent(Event): _EVENT_TYPE = EventType.ImageRequestError _EVENT_ATTRIBUTE = "pointer" class ImageCompleteEvent(Event): _EVENT_TYPE = EventType.ImageComplete _EVENT_ATTRIBUTE = "pointer" class LogEvent(Event): _EVENT_TYPE = EventType.LogEvent _EVENT_ATTRIBUTE = "log_event" class DeviceLostEvent(Event): _EVENT_TYPE = EventType.DeviceLost _EVENT_ATTRIBUTE = "device_event" def __init__(self, data): super().__init__(data) self._device = Device(data.device) self._status = DeviceStatusInfo(data.status) @property def device(self): return self._device @property def status(self): return self._status class ConfigResponseEvent(Event): _EVENT_TYPE = EventType.ConfigResponse _EVENT_ATTRIBUTE = "config_response_event" class ConfigChangeEvent(Event): _EVENT_TYPE = EventType.ConfigChange _EVENT_ATTRIBUTE = "config_change_event" class DeviceStatusChangeEvent(Event): _EVENT_TYPE = EventType.DeviceStatusChange _EVENT_ATTRIBUTE = "device_status_change_event" def __init__(self, data): super().__init__(data) self._device = Device(data.device) self._last_status = DeviceStatusInfo(data.last_status) self._status = DeviceStatusInfo(data.status) @property def device(self): return self._device @property def last_status(self): return self._last_status @property def status(self): return self._status class DroppedFrameEvent(Event): _EVENT_TYPE = EventType.DroppedFrame _EVENT_ATTRIBUTE = "dropped_frame_event" class ImageEvent(Event): _EVENT_TYPE = EventType.Image _EVENT_ATTRIBUTE = "image_event" def __init__(self, data): super().__init__(data) self._images = data.image @property def image(self): return [Image(self._images[0]), Image(self._images[1])] class PointMappingChangeEvent(Event): _EVENT_TYPE = EventType.PointMappingChange _EVENT_ATTRIBUTE = "point_mapping_change_event" class TrackingModeEvent(Event):
_EVENT_TYPE = EventType.TrackingMode
9
2023-11-08 13:35:40+00:00
4k
UMass-Foundation-Model/CoVLM
YOLOX/yolox/models/darknet.py
[ { "identifier": "BaseConv", "path": "YOLOX/yolox/models/network_blocks.py", "snippet": "class BaseConv(nn.Module):\n \"\"\"A Conv2d -> Batchnorm -> silu/leaky relu block\"\"\"\n\n def __init__(\n self, in_channels, out_channels, ksize, stride, groups=1, bias=False, act=\"silu\"\n ):\n super().__init__()\n # same padding\n pad = (ksize - 1) // 2\n self.conv = nn.Conv2d(\n in_channels,\n out_channels,\n kernel_size=ksize,\n stride=stride,\n padding=pad,\n groups=groups,\n bias=bias,\n )\n self.bn = nn.BatchNorm2d(out_channels)\n self.act = get_activation(act, inplace=True)\n\n def forward(self, x):\n return self.act(self.bn(self.conv(x)))\n\n def fuseforward(self, x):\n return self.act(self.conv(x))" }, { "identifier": "CSPLayer", "path": "YOLOX/yolox/models/network_blocks.py", "snippet": "class CSPLayer(nn.Module):\n \"\"\"C3 in yolov5, CSP Bottleneck with 3 convolutions\"\"\"\n\n def __init__(\n self,\n in_channels,\n out_channels,\n n=1,\n shortcut=True,\n expansion=0.5,\n depthwise=False,\n act=\"silu\",\n ):\n \"\"\"\n Args:\n in_channels (int): input channels.\n out_channels (int): output channels.\n n (int): number of Bottlenecks. Default value: 1.\n \"\"\"\n # ch_in, ch_out, number, shortcut, groups, expansion\n super().__init__()\n hidden_channels = int(out_channels * expansion) # hidden channels\n self.conv1 = BaseConv(in_channels, hidden_channels, 1, stride=1, act=act)\n self.conv2 = BaseConv(in_channels, hidden_channels, 1, stride=1, act=act)\n self.conv3 = BaseConv(2 * hidden_channels, out_channels, 1, stride=1, act=act)\n module_list = [\n Bottleneck(\n hidden_channels, hidden_channels, shortcut, 1.0, depthwise, act=act\n )\n for _ in range(n)\n ]\n self.m = nn.Sequential(*module_list)\n\n def forward(self, x):\n x_1 = self.conv1(x)\n x_2 = self.conv2(x)\n x_1 = self.m(x_1)\n x = torch.cat((x_1, x_2), dim=1)\n return self.conv3(x)" }, { "identifier": "DWConv", "path": "YOLOX/yolox/models/network_blocks.py", "snippet": "class DWConv(nn.Module):\n \"\"\"Depthwise Conv + Conv\"\"\"\n\n def __init__(self, in_channels, out_channels, ksize, stride=1, act=\"silu\"):\n super().__init__()\n self.dconv = BaseConv(\n in_channels,\n in_channels,\n ksize=ksize,\n stride=stride,\n groups=in_channels,\n act=act,\n )\n self.pconv = BaseConv(\n in_channels, out_channels, ksize=1, stride=1, groups=1, act=act\n )\n\n def forward(self, x):\n x = self.dconv(x)\n return self.pconv(x)" }, { "identifier": "Focus", "path": "YOLOX/yolox/models/network_blocks.py", "snippet": "class Focus(nn.Module):\n \"\"\"Focus width and height information into channel space.\"\"\"\n\n def __init__(self, in_channels, out_channels, ksize=1, stride=1, act=\"silu\"):\n super().__init__()\n self.conv = BaseConv(in_channels * 4, out_channels, ksize, stride, act=act)\n\n def forward(self, x):\n # shape of x (b,c,w,h) -> y(b,4c,w/2,h/2)\n patch_top_left = x[..., ::2, ::2]\n patch_top_right = x[..., ::2, 1::2]\n patch_bot_left = x[..., 1::2, ::2]\n patch_bot_right = x[..., 1::2, 1::2]\n x = torch.cat(\n (\n patch_top_left,\n patch_bot_left,\n patch_top_right,\n patch_bot_right,\n ),\n dim=1,\n )\n return self.conv(x)" }, { "identifier": "ResLayer", "path": "YOLOX/yolox/models/network_blocks.py", "snippet": "class ResLayer(nn.Module):\n \"Residual layer with `in_channels` inputs.\"\n\n def __init__(self, in_channels: int):\n super().__init__()\n mid_channels = in_channels // 2\n self.layer1 = BaseConv(\n in_channels, mid_channels, ksize=1, stride=1, act=\"lrelu\"\n )\n self.layer2 = BaseConv(\n mid_channels, in_channels, ksize=3, stride=1, act=\"lrelu\"\n )\n\n def forward(self, x):\n out = self.layer2(self.layer1(x))\n return x + out" }, { "identifier": "SPPBottleneck", "path": "YOLOX/yolox/models/network_blocks.py", "snippet": "class SPPBottleneck(nn.Module):\n \"\"\"Spatial pyramid pooling layer used in YOLOv3-SPP\"\"\"\n\n def __init__(\n self, in_channels, out_channels, kernel_sizes=(5, 9, 13), activation=\"silu\"\n ):\n super().__init__()\n hidden_channels = in_channels // 2\n self.conv1 = BaseConv(in_channels, hidden_channels, 1, stride=1, act=activation)\n self.m = nn.ModuleList(\n [\n nn.MaxPool2d(kernel_size=ks, stride=1, padding=ks // 2)\n for ks in kernel_sizes\n ]\n )\n conv2_channels = hidden_channels * (len(kernel_sizes) + 1)\n self.conv2 = BaseConv(conv2_channels, out_channels, 1, stride=1, act=activation)\n\n def forward(self, x):\n x = self.conv1(x)\n x = torch.cat([x] + [m(x) for m in self.m], dim=1)\n x = self.conv2(x)\n return x" } ]
from torch import nn from .network_blocks import BaseConv, CSPLayer, DWConv, Focus, ResLayer, SPPBottleneck
2,496
#!/usr/bin/env python # -*- encoding: utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. class Darknet(nn.Module): # number of blocks from dark2 to dark5. depth2blocks = {21: [1, 2, 2, 1], 53: [2, 8, 8, 4]} def __init__( self, depth, in_channels=3, stem_out_channels=32, out_features=("dark3", "dark4", "dark5"), ): """ Args: depth (int): depth of darknet used in model, usually use [21, 53] for this param. in_channels (int): number of input channels, for example, use 3 for RGB image. stem_out_channels (int): number of output channels of darknet stem. It decides channels of darknet layer2 to layer5. out_features (Tuple[str]): desired output layer name. """ super().__init__() assert out_features, "please provide output features of Darknet" self.out_features = out_features self.stem = nn.Sequential( BaseConv(in_channels, stem_out_channels, ksize=3, stride=1, act="lrelu"), *self.make_group_layer(stem_out_channels, num_blocks=1, stride=2), ) in_channels = stem_out_channels * 2 # 64 num_blocks = Darknet.depth2blocks[depth] # create darknet with `stem_out_channels` and `num_blocks` layers. # to make model structure more clear, we don't use `for` statement in python. self.dark2 = nn.Sequential( *self.make_group_layer(in_channels, num_blocks[0], stride=2) ) in_channels *= 2 # 128 self.dark3 = nn.Sequential( *self.make_group_layer(in_channels, num_blocks[1], stride=2) ) in_channels *= 2 # 256 self.dark4 = nn.Sequential( *self.make_group_layer(in_channels, num_blocks[2], stride=2) ) in_channels *= 2 # 512 self.dark5 = nn.Sequential( *self.make_group_layer(in_channels, num_blocks[3], stride=2), *self.make_spp_block([in_channels, in_channels * 2], in_channels * 2), ) def make_group_layer(self, in_channels: int, num_blocks: int, stride: int = 1): "starts with conv layer then has `num_blocks` `ResLayer`" return [ BaseConv(in_channels, in_channels * 2, ksize=3, stride=stride, act="lrelu"), *[(ResLayer(in_channels * 2)) for _ in range(num_blocks)], ] def make_spp_block(self, filters_list, in_filters): m = nn.Sequential( *[ BaseConv(in_filters, filters_list[0], 1, stride=1, act="lrelu"), BaseConv(filters_list[0], filters_list[1], 3, stride=1, act="lrelu"), SPPBottleneck( in_channels=filters_list[1], out_channels=filters_list[0], activation="lrelu", ), BaseConv(filters_list[0], filters_list[1], 3, stride=1, act="lrelu"), BaseConv(filters_list[1], filters_list[0], 1, stride=1, act="lrelu"), ] ) return m def forward(self, x): outputs = {} x = self.stem(x) outputs["stem"] = x x = self.dark2(x) outputs["dark2"] = x x = self.dark3(x) outputs["dark3"] = x x = self.dark4(x) outputs["dark4"] = x x = self.dark5(x) outputs["dark5"] = x return {k: v for k, v in outputs.items() if k in self.out_features} class CSPDarknet(nn.Module): def __init__( self, dep_mul, wid_mul, out_features=("dark3", "dark4", "dark5"), depthwise=False, act="silu", ): super().__init__() assert out_features, "please provide output features of Darknet" self.out_features = out_features
#!/usr/bin/env python # -*- encoding: utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. class Darknet(nn.Module): # number of blocks from dark2 to dark5. depth2blocks = {21: [1, 2, 2, 1], 53: [2, 8, 8, 4]} def __init__( self, depth, in_channels=3, stem_out_channels=32, out_features=("dark3", "dark4", "dark5"), ): """ Args: depth (int): depth of darknet used in model, usually use [21, 53] for this param. in_channels (int): number of input channels, for example, use 3 for RGB image. stem_out_channels (int): number of output channels of darknet stem. It decides channels of darknet layer2 to layer5. out_features (Tuple[str]): desired output layer name. """ super().__init__() assert out_features, "please provide output features of Darknet" self.out_features = out_features self.stem = nn.Sequential( BaseConv(in_channels, stem_out_channels, ksize=3, stride=1, act="lrelu"), *self.make_group_layer(stem_out_channels, num_blocks=1, stride=2), ) in_channels = stem_out_channels * 2 # 64 num_blocks = Darknet.depth2blocks[depth] # create darknet with `stem_out_channels` and `num_blocks` layers. # to make model structure more clear, we don't use `for` statement in python. self.dark2 = nn.Sequential( *self.make_group_layer(in_channels, num_blocks[0], stride=2) ) in_channels *= 2 # 128 self.dark3 = nn.Sequential( *self.make_group_layer(in_channels, num_blocks[1], stride=2) ) in_channels *= 2 # 256 self.dark4 = nn.Sequential( *self.make_group_layer(in_channels, num_blocks[2], stride=2) ) in_channels *= 2 # 512 self.dark5 = nn.Sequential( *self.make_group_layer(in_channels, num_blocks[3], stride=2), *self.make_spp_block([in_channels, in_channels * 2], in_channels * 2), ) def make_group_layer(self, in_channels: int, num_blocks: int, stride: int = 1): "starts with conv layer then has `num_blocks` `ResLayer`" return [ BaseConv(in_channels, in_channels * 2, ksize=3, stride=stride, act="lrelu"), *[(ResLayer(in_channels * 2)) for _ in range(num_blocks)], ] def make_spp_block(self, filters_list, in_filters): m = nn.Sequential( *[ BaseConv(in_filters, filters_list[0], 1, stride=1, act="lrelu"), BaseConv(filters_list[0], filters_list[1], 3, stride=1, act="lrelu"), SPPBottleneck( in_channels=filters_list[1], out_channels=filters_list[0], activation="lrelu", ), BaseConv(filters_list[0], filters_list[1], 3, stride=1, act="lrelu"), BaseConv(filters_list[1], filters_list[0], 1, stride=1, act="lrelu"), ] ) return m def forward(self, x): outputs = {} x = self.stem(x) outputs["stem"] = x x = self.dark2(x) outputs["dark2"] = x x = self.dark3(x) outputs["dark3"] = x x = self.dark4(x) outputs["dark4"] = x x = self.dark5(x) outputs["dark5"] = x return {k: v for k, v in outputs.items() if k in self.out_features} class CSPDarknet(nn.Module): def __init__( self, dep_mul, wid_mul, out_features=("dark3", "dark4", "dark5"), depthwise=False, act="silu", ): super().__init__() assert out_features, "please provide output features of Darknet" self.out_features = out_features
Conv = DWConv if depthwise else BaseConv
2
2023-11-07 04:23:57+00:00
4k
HKU-BAL/ClairS-TO
clairs/predict.py
[ { "identifier": "output_vcf_from_probability", "path": "clairs/call_variants.py", "snippet": "ACGT = 'ACGT'\n AU, CU, GU, TU = acgt_count\n FAU, FCU, FGU, FTU = int(input_list_forward_acgt_count_ori[0]), int(input_list_forward_acgt_count_ori[1]), int(input_list_forward_acgt_count_ori[2]), int(input_list_forward_acgt_count_ori[3])\n RAU, RCU, RGU, RTU = int(input_list_reverse_acgt_count_ori[0]), int(input_list_reverse_acgt_count_ori[1]), int(input_list_reverse_acgt_count_ori[2]), int(input_list_reverse_acgt_count_ori[3])\n AU, CU, GU, TU = int(input_list_acgt_count_ori[0]), int(input_list_acgt_count_ori[1]), int(input_list_acgt_count_ori[2]), int(input_list_acgt_count_ori[3])\n AD = None\n AD = str(tumor_supported_reads_count) if is_reference else str(tumor_ref_num) + ',' + str(\n tumor_supported_reads_count)\ndef filtration_value_from(quality_score_for_pass, quality_score, is_reference=False, is_variant=False):\ndef quality_score_from(probability, int_format=False, use_phred_qual=True):\ndef argmax(l):\ndef decode_acgt_count(alt_dict, ref_base=None, tumor_coverage=None):\ndef output_vcf_from_probability(\n chromosome,\n position,\n reference_base,\n tumor_alt_info,\n input_forward_acgt_count_ori,\n input_reverse_acgt_count_ori,\n probabilities_a,\n probabilities_c,\n probabilities_g,\n probabilities_t,\n probabilities_na,\n probabilities_nc,\n probabilities_ng,\n probabilities_nt,\n likelihood_data_info_list,\n output_config=None,\n vcf_writer=None,\n):\n def decode_alt_info(alt_info):\n def rank_variant_alt(tumor_alt_info_dict, tumor_read_depth):\n def decode_alt_info(alt_info_dict, read_depth):\ndef call_variants_from_probability(args):\ndef main():" }, { "identifier": "IUPAC_base_to_ACGT_base_dict", "path": "shared/utils.py", "snippet": "BASIC_BASES = set(\"ACGTU\")\nWARNING = '\\033[93m'\nERROR = '\\033[91m'\nENDC = '\\033[0m'\ndef log_error(log):\ndef log_warning(log):\ndef is_file_exists(file_name, suffix=\"\"):\ndef is_folder_exists(folder_name, suffix=\"\"):\ndef legal_range_from(param_name, x, min_num=None, max_num=None, exit_out_of_range=False):\ndef file_path_from(file_name, suffix=\"\", exit_on_not_found=False, sep=\"\", allow_none=False, is_directory=False):\ndef folder_path_from(folder_name, create_not_found=True, exit_on_not_found=False):\ndef is_command_exists(command):\ndef executable_command_string_from(command_to_execute, exit_on_not_found=False):\ndef subprocess_popen(args, stdin=None, stdout=PIPE, stderr=stderr, bufsize=8388608):\ndef str_none(v):\ndef str2bool(v):\ndef region_from(ctg_name, ctg_start=None, ctg_end=None):\ndef reference_sequence_from(samtools_execute_command, fasta_file_path, regions):\ndef vcf_candidates_from(vcf_fn, contig_name=None):\ndef candidate_position_generator_from(\n candidate,\n flanking_base_num,\n begin_to_end\n):\ndef samtools_mpileup_generator_from(\n candidate,\n flanking_base_num,\n begin_to_end\n):\ndef samtools_view_process_from(\n ctg_name,\n ctg_start,\n ctg_end,\n samtools,\n bam_file_path\n):\n def __init__(self, ctg_name=None,\n genotype1=None,\n genotype2=None,\n pos=None,\n ref_base=None,\n alt_base=None,\n candidate=False,\n cigar_count=None,\n confident_variant=False,\n depth=None,\n alt_list=None,\n af=None,\n filter=None,\n af_list=None,\n alt_type_mapping_dict=None,\n extra_infos=\"\",\n qual=None,\n row_str=None):\n def update_info(self, ref_base, alt_base, genotype, extra_infos=\"\"):\n def __init__(self, pos, ref_base, depth, af_list, alt_dict, tumor_alt_dict, extra_infos=\"\"):\n def __init__(self, handle):\n def __del__(self):\nclass Position(object):\nclass AltInfos(object):\nclass TensorStdout(object):" } ]
import sys import os import numpy as np import logging import torch import shlex import shared.param as param from time import time from argparse import ArgumentParser, SUPPRESS from threading import Thread from sys import stderr from subprocess import PIPE, run, Popen from clairs.call_variants import output_vcf_from_probability, OutputConfig from shared.utils import IUPAC_base_to_ACGT_base_dict as BASE2ACGT, BASIC_BASES, str2bool, file_path_from, log_error, \ log_warning, subprocess_popen, TensorStdout from shared.vcf import VcfWriter
1,641
# BSD 3-Clause License # # Copyright 2023 The University of Hong Kong, Department of Computer Science # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. def batches_from(iterable, item_from, batch_size=1): iterable = iter(iterable) while True: chunk = [] for _ in range(batch_size): try: chunk.append(item_from(next(iterable))) except StopIteration: yield chunk return yield chunk def print_output_message( output_file, chromosome, position, reference_base, tumor_alt_info, input_forward_acgt_count_ori, input_reverse_acgt_count_ori, probabilities_a, probabilities_c, probabilities_g, probabilities_t, probabilities_na, probabilities_nc, probabilities_ng, probabilities_nt, extra_infomation_string="" ): global call_fn if call_fn is not None:
# BSD 3-Clause License # # Copyright 2023 The University of Hong Kong, Department of Computer Science # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. def batches_from(iterable, item_from, batch_size=1): iterable = iter(iterable) while True: chunk = [] for _ in range(batch_size): try: chunk.append(item_from(next(iterable))) except StopIteration: yield chunk return yield chunk def print_output_message( output_file, chromosome, position, reference_base, tumor_alt_info, input_forward_acgt_count_ori, input_reverse_acgt_count_ori, probabilities_a, probabilities_c, probabilities_g, probabilities_t, probabilities_na, probabilities_nc, probabilities_ng, probabilities_nt, extra_infomation_string="" ): global call_fn if call_fn is not None:
output_vcf_from_probability(
0
2023-11-07 04:39:16+00:00
4k
the-siesta-group/edfio
tests/test_programming_guidelines.py
[ { "identifier": "decode_float", "path": "edfio/_header_field.py", "snippet": "def decode_float(field: bytes) -> float:\n value = float(decode_str(field))\n if math.isinf(value):\n raise ValueError(f\"Field value is outside float range: {decode_str(field)}\")\n return value" }, { "identifier": "EdfSignal", "path": "edfio/edf.py", "snippet": "class EdfSignal:\n \"\"\"A single EDF signal.\n\n Attributes that might break the signal or file on modification (i.e.,\n `sampling_frequency`, `physical_range`, `digital_range`, `samples_per_data_record`,\n and `reserved`) can not be set after instantiation.\n\n To reduce memory consumption, signal data is always stored as a 16-bit integer array\n containing the digital values that would be written to the corresponding EDF file.\n Therefore, it is expected that `EdfSignal.data` does not match the physical\n values passed during instantiation exactly.\n\n Parameters\n ----------\n data : npt.NDArray[np.float64]\n The signal data (physical values).\n sampling_frequency : float\n The sampling frequency in Hz.\n label : str, default: `\"\"`\n The signal's label, e.g., `\"EEG Fpz-Cz\"` or `\"Body temp\"`.\n transducer_type : str, default: `\"\"`\n The transducer type, e.g., `\"AgAgCl electrode\"`.\n physical_dimension : str, default: `\"\"`\n The physical dimension, e.g., `\"uV\"` or `\"degreeC\"`\n physical_range : tuple[float, float] | None, default: None\n The physical range given as a tuple of `(physical_min, physical_max)`. If\n `None`, this is determined from the data.\n digital_range : tuple[int, int], default: `(-32768, 32767)`\n The digital range given as a tuple of `(digital_min, digital_max)`. Uses the\n maximum resolution of 16-bit integers by default.\n prefiltering : str, default: `\"\"`\n The signal prefiltering, e.g., `\"HP:0.1Hz LP:75Hz\"`.\n \"\"\"\n\n _label = RawHeaderFieldStr(16, is_settable=True)\n transducer_type = RawHeaderFieldStr(80, is_settable=True)\n \"\"\"Transducer type, e.g., `\"AgAgCl electrode\"`.\"\"\"\n physical_dimension = RawHeaderFieldStr(8, is_settable=True)\n \"\"\"Physical dimension, e.g., `\"uV\"` or `\"degreeC\"`.\"\"\"\n physical_min = RawHeaderFieldFloat(8)\n \"\"\"Physical minimum, e.g., `-500` or `34`.\"\"\"\n physical_max = RawHeaderFieldFloat(8)\n \"\"\"Physical maximum, e.g., `500` or `40`.\"\"\"\n digital_min = RawHeaderFieldInt(8)\n \"\"\"Digital minimum, e.g., `-2048`.\"\"\"\n digital_max = RawHeaderFieldInt(8)\n \"\"\"Digital maximum, e.g., `2047`.\"\"\"\n prefiltering = RawHeaderFieldStr(80, is_settable=True)\n \"\"\"Signal prefiltering, e.g., `\"HP:0.1Hz LP:75Hz\"`.\"\"\"\n samples_per_data_record = RawHeaderFieldInt(8)\n \"\"\"\n Number of samples in each data record.\n\n For newly instantiated :class:`EdfSignal` objects, this is only set once\n :meth:`Edf.write` is called.\n \"\"\"\n reserved = RawHeaderFieldStr(32)\n \"\"\"Reserved signal header field, always `\"\"`\"\"\"\n\n def __init__(\n self,\n data: npt.NDArray[np.float64],\n sampling_frequency: float,\n *,\n label: str = \"\",\n transducer_type: str = \"\",\n physical_dimension: str = \"\",\n physical_range: tuple[float, float] | None = None,\n digital_range: tuple[int, int] = (-32768, 32767),\n prefiltering: str = \"\",\n ):\n self._sampling_frequency = sampling_frequency\n self.label = label\n self.transducer_type = transducer_type\n self.physical_dimension = physical_dimension\n self.prefiltering = prefiltering\n self._reserved = EdfSignal.reserved.encode(\"\")\n if not np.all(np.isfinite(data)):\n raise ValueError(\"Signal data must contain only finite values\")\n self._set_physical_range(physical_range, data)\n self._set_digital_range(digital_range)\n self._set_data(data)\n\n def __repr__(self) -> str:\n info = f\"{self.sampling_frequency:g}Hz\"\n if self.label:\n info = f\"{self.label} \" + info\n return f\"<EdfSignal {info}>\"\n\n @classmethod\n def _from_raw_header(\n cls,\n sampling_frequency: float,\n *,\n _label: bytes,\n transducer_type: bytes,\n physical_dimension: bytes,\n physical_min: bytes,\n physical_max: bytes,\n digital_min: bytes,\n digital_max: bytes,\n prefiltering: bytes,\n samples_per_data_record: bytes,\n reserved: bytes,\n ) -> EdfSignal:\n sig = object.__new__(cls)\n sig._sampling_frequency = sampling_frequency\n sig._label = EdfSignal._label.decode(_label) # type: ignore[attr-defined]\n sig._transducer_type = transducer_type # type: ignore[attr-defined]\n sig._physical_dimension = physical_dimension # type: ignore[attr-defined]\n sig._physical_min = physical_min # type: ignore[attr-defined]\n sig._physical_max = physical_max # type: ignore[attr-defined]\n sig._digital_min = digital_min # type: ignore[attr-defined]\n sig._digital_max = digital_max # type: ignore[attr-defined]\n sig._prefiltering = prefiltering # type: ignore[attr-defined]\n sig._samples_per_data_record = samples_per_data_record # type: ignore[attr-defined]\n sig._reserved = reserved # type: ignore[attr-defined]\n return sig\n\n @classmethod\n def from_hypnogram(\n cls,\n stages: npt.NDArray[np.float64],\n stage_duration: float = 30,\n *,\n label: str = \"\",\n ) -> EdfSignal:\n \"\"\"Create an EDF signal from a hypnogram, with scaling according to EDF specs.\n\n According to the EDF FAQ [1]_, use integer numbers 0, 1, 2, 3, 4, 5, 6, and 9\n for sleep stages W, 1, 2, 3, 4, R, MT, und unscored, respectively. The digital\n range is set to `(0, 9)`.\n\n Parameters\n ----------\n stages : npt.NDArray[np.float64]\n The sleep stages, coded as integer numbers.\n stage_duration : float, default: `30`\n The duration of each sleep stage in seconds, used to set the sampling\n frequency to its inverse.\n label : str, default: `\"\"`\n The signal's label.\n\n Returns\n -------\n EdfSignal\n The resulting :class:`EdfSignal` object.\n\n References\n ----------\n .. [1] EDF FAQ, https://www.edfplus.info/specs/edffaq.html\n \"\"\"\n allowed_stages = {0, 1, 2, 3, 4, 5, 6, 9}\n if invalid_stages := set(stages) - allowed_stages:\n raise ValueError(f\"stages contains invalid values: {invalid_stages}\")\n return EdfSignal(\n data=stages,\n sampling_frequency=1 / stage_duration,\n label=label,\n physical_range=(0, 9),\n digital_range=(0, 9),\n )\n\n @property\n def label(self) -> str:\n \"\"\"Signal label, e.g., `\"EEG Fpz-Cz\"` or `\"Body temp\"`.\"\"\"\n return self._label\n\n @label.setter\n def label(self, label: str) -> None:\n if label == \"EDF Annotations\":\n raise ValueError(\"Ordinary signal label must not be 'EDF Annotations'.\")\n self._label = label\n\n @property\n def physical_range(self) -> FloatRange:\n \"\"\"The physical range as a tuple of `(physical_min, physical_max)`.\"\"\"\n return FloatRange(self.physical_min, self.physical_max)\n\n @property\n def digital_range(self) -> IntRange:\n \"\"\"The digital range as a tuple of `(digital_min, digital_max)`.\"\"\"\n return IntRange(self.digital_min, self.digital_max)\n\n @property\n def sampling_frequency(self) -> float:\n \"\"\"The sampling frequency in Hz.\"\"\"\n return self._sampling_frequency\n\n @property\n def data(self) -> npt.NDArray[np.float64]:\n \"\"\"\n Numpy array containing the physical signal values as floats.\n\n To simplify avoiding inconsistencies between signal data and header fields,\n individual values in the returned array can not be modified. Use\n :meth:`EdfSignal.update_data` to overwrite with new physical data.\n \"\"\"\n try:\n gain, offset = calculate_gain_and_offset(\n self.digital_min,\n self.digital_max,\n self.physical_min,\n self.physical_max,\n )\n except ZeroDivisionError:\n data = self._digital.astype(np.float64)\n warnings.warn(\n f\"Digital minimum equals digital maximum ({self.digital_min}) for {self.label}, returning uncalibrated signal.\"\n )\n except ValueError:\n data = self._digital.astype(np.float64)\n else:\n data = (self._digital + offset) * gain\n data.setflags(write=False)\n return data\n\n def update_data(\n self,\n data: npt.NDArray[np.float64],\n *,\n keep_physical_range: bool = False,\n sampling_frequency: float | None = None,\n ) -> None:\n \"\"\"\n Overwrite physical signal values with an array of equal length.\n\n Parameters\n ----------\n data : npt.NDArray[np.float64]\n The new physical data.\n keep_physical_range : bool, default: False\n If `True`, the `physical_range` is not modified to accomodate the new data.\n sampling_frequency : float | None, default: None\n If not `None`, the `sampling_frequency` is updated to the new value. The new\n data must match the expected length for the new sampling frequency.\n \"\"\"\n expected_length = len(self._digital)\n if (\n sampling_frequency is not None\n and sampling_frequency != self._sampling_frequency\n ):\n expected_length = self._get_expected_new_length(sampling_frequency)\n if len(data) != expected_length:\n raise ValueError(\n f\"Signal lengths must match: got {len(data)}, expected {len(self._digital)}.\"\n )\n physical_range = self.physical_range if keep_physical_range else None\n self._set_physical_range(physical_range, data)\n if sampling_frequency is not None:\n self._sampling_frequency = sampling_frequency\n self._set_data(data)\n\n def _get_expected_new_length(self, sampling_frequency: float) -> int:\n if sampling_frequency <= 0:\n raise ValueError(\n f\"Sampling frequency must be positive, got {sampling_frequency}\"\n )\n current_length = len(self._digital)\n expected_length_f = (\n sampling_frequency / self._sampling_frequency * current_length\n )\n if not math.isclose(expected_length_f, round(expected_length_f), rel_tol=1e-10):\n raise ValueError(\n f\"Sampling frequency of {sampling_frequency} results in non-integer number of samples ({expected_length_f})\"\n )\n return round(expected_length_f)\n\n def _set_digital_range(self, digital_range: tuple[int, int]) -> None:\n digital_range = IntRange(*digital_range)\n if digital_range.min == digital_range.max:\n raise ValueError(\n f\"Digital minimum ({digital_range.min}) must differ from digital maximum ({digital_range.max}).\"\n )\n self._digital_min = EdfSignal.digital_min.encode(digital_range.min)\n self._digital_max = EdfSignal.digital_max.encode(digital_range.max)\n\n def _set_physical_range(\n self,\n physical_range: tuple[float, float] | None,\n data: npt.NDArray[np.float64],\n ) -> None:\n if physical_range is None:\n physical_range = FloatRange(data.min(), data.max())\n if physical_range.min == physical_range.max:\n physical_range = FloatRange(physical_range.min, physical_range.max + 1)\n else:\n physical_range = FloatRange(*physical_range)\n if physical_range.min == physical_range.max:\n raise ValueError(\n f\"Physical minimum ({physical_range.min}) must differ from physical maximum ({physical_range.max}).\"\n )\n data_min = data.min()\n data_max = data.max()\n if data_min < physical_range.min or data_max > physical_range.max:\n raise ValueError(\n f\"Signal range [{data_min}, {data_max}] out of physical range: [{physical_range.min}, {physical_range.max}]\"\n )\n self._physical_min = EdfSignal.physical_min.encode(\n round_float_to_8_characters(physical_range.min, math.floor)\n )\n self._physical_max = EdfSignal.physical_max.encode(\n round_float_to_8_characters(physical_range.max, math.ceil)\n )\n\n def _set_data(self, data: npt.NDArray[np.float64]) -> None:\n gain, offset = calculate_gain_and_offset(\n self.digital_min,\n self.digital_max,\n self.physical_min,\n self.physical_max,\n )\n self._digital = np.round(data / gain - offset).astype(np.int16)" } ]
import numpy as np import pytest from edfio._header_field import decode_float from edfio.edf import EdfSignal
3,498
""" Tests to verify the adherence to the EDF(+) programming guidelines: https://www.edfplus.info/specs/guidelines.html """ @pytest.mark.parametrize( ("field", "value"), [ # (b"1E2345 ", 1), # mentioned in guidelines, but exceeds the range of double (b"+012E+34", 12e34), (b"-1.34E09", -1.34e9), (b"+1.23E-5", 1.23e-5), # guidelines mention "+1.234E-5", but that has 9 chars ], ) def test_g2a_float_decode_different_formats(field: bytes, value: float):
""" Tests to verify the adherence to the EDF(+) programming guidelines: https://www.edfplus.info/specs/guidelines.html """ @pytest.mark.parametrize( ("field", "value"), [ # (b"1E2345 ", 1), # mentioned in guidelines, but exceeds the range of double (b"+012E+34", 12e34), (b"-1.34E09", -1.34e9), (b"+1.23E-5", 1.23e-5), # guidelines mention "+1.234E-5", but that has 9 chars ], ) def test_g2a_float_decode_different_formats(field: bytes, value: float):
assert decode_float(field) == value
0
2023-11-09 09:53:27+00:00
4k
microsoft/folx
folx/jvp.py
[ { "identifier": "JAC_DIM", "path": "folx/api.py", "snippet": "T = TypeVar(\"T\", bound=PyTree[Array])\nR = TypeVar(\"R\", bound=PyTree[Array])\nJAC_DIM = 0 # should be either 0 or -1. TODO: switching is not support.\n GENERAL = 0\n LINEAR_IN_FIRST = 1\n LINEAR_IN_ONE = 2 | LINEAR_IN_FIRST\n LINEAR = 4 | LINEAR_IN_ONE\n REDUCTION = 8\n MULTIPLICATION = 16 | LINEAR_IN_ONE\n DOT_PRODUCT = 32 | REDUCTION | MULTIPLICATION\n INDEXING = 64 | LINEAR\n SCATTER = 128\n JOIN_JVP = 256\nclass FwdJacobian(NamedTuple):\nclass FwdLaplArray(NamedTuple):\nclass FwdLaplArgs(NamedTuple):\nclass MergeFn(Protocol):\nclass ForwardLaplacianFns(NamedTuple):\nclass JvpFn(Protocol):\nclass CustomTraceJacHessianJac(Protocol):\nclass ForwardLaplacian(Protocol):\nclass FunctionFlags(IntFlag):\n def weak(self) -> bool:\n def unique_idx(self):\n def materialize_for_idx(self, idx, max_idx: int | None = None):\n def aggregate(x, indices):\n def get_index_mask(self, outputs):\n def get_indices(mask, out_mask):\n def data_shape(self):\n def construct_jac_for(self, idx):\n def dense_array(self) -> Array:\n def max_n(self) -> int:\n def as_dense(self):\n def dense_or_sparse(self) -> Array:\n def sparse(self) -> Array:\n def mask(self) -> np.ndarray:\n def ndim(self) -> int:\n def from_dense(cls, array):\n def __add__(self, other):\n def astype(self, dtype):\n def shape(self):\n def ndim(self):\n def dense_jacobian(self):\n def is_jacobian_weak(self):\n def sparse_jacobian(self):\n def jacobian_mask(self):\n def dense(self):\n def astype(self, dtype):\ndef IS_LPL_ARR(x):\ndef IS_LEAF(x):\n def x(self) -> Arrays:\n def jacobian(self) -> tuple[FwdJacobian, ...]:\n def dense_jacobian(self) -> Arrays:\n def sparse_jacobian(self) -> Arrays:\n def jacobian_mask(self):\n def all_jacobian_weak(self) -> bool:\n def any_jacobian_weak(self) -> bool:\n def dense(self):\n def laplacian(self) -> Arrays:\n def one_hot_sparse_jacobian(self):\n def __len__(self) -> int:\n def __call__(self, args: Arrays, extra: ExtraArgs) -> Arrays:\n def __call__(self, primals: Arrays, tangents: Arrays) -> tuple[Array, Array]:\n def __call__(self, args: FwdLaplArgs, extra_args: ExtraArgs, merge: MergeFn, materialize_idx: Array) -> PyTree[Array]:\n def __call__(self, *args: ArrayOrFwdLaplArray, sparsity_threshold: int , **kwargs) -> PyTree[ArrayOrFwdLaplArray]:" }, { "identifier": "tree_concat", "path": "folx/tree_utils.py", "snippet": "def tree_concat(trees: Sequence[T], axis: int = 0) -> T:\n return jtu.tree_map(lambda *args: jnp.concatenate(args, axis=axis), *trees)" }, { "identifier": "tree_expand", "path": "folx/tree_utils.py", "snippet": "def tree_expand(tree: T, axis) -> T:\n return jtu.tree_map(lambda x: jnp.expand_dims(x, axis), tree)" }, { "identifier": "tree_take", "path": "folx/tree_utils.py", "snippet": "def tree_take(tree: T, idx, axis) -> T:\n def take(x):\n indices = idx\n if isinstance(indices, slice):\n slices = [slice(None)] * x.ndim\n slices[axis] = idx\n return x[tuple(slices)]\n return jnp.take(x, indices, axis)\n return jtu.tree_map(take, tree)" }, { "identifier": "broadcast_except", "path": "folx/utils.py", "snippet": "def broadcast_except(arrs, axis):\n \"\"\"\n Broadcasts all arrays to the same shape except for the specified axes.\n\n Args:\n - arrs: sequence of arrays\n - axes: tuple of integers specifying the axes to exclude from broadcasting\n Returns:\n - np.ndarray: sequence of arrays with the same shape except for the specified axes\n \"\"\"\n if axis < 0:\n axis += jtu.tree_leaves(arrs)[0].ndim\n pre_shapes = [x.shape[:axis] for x in jtu.tree_leaves(arrs)]\n post_shapes = [x.shape[axis + 1 :] for x in jtu.tree_leaves(arrs)]\n max_pre = np.broadcast_shapes(*pre_shapes)\n max_post = np.broadcast_shapes(*post_shapes)\n\n def broadcast(a):\n broadcast = np.broadcast_to if isinstance(a, np.ndarray) else jnp.broadcast_to\n moveaxis = np.moveaxis if isinstance(a, np.ndarray) else jnp.moveaxis\n out = broadcast(moveaxis(a, axis, -1), (*max_pre, *max_post, a.shape[axis]))\n return moveaxis(out, -1, axis) # type: ignore\n\n return jtu.tree_map(broadcast, arrs)" }, { "identifier": "broadcast_dim", "path": "folx/utils.py", "snippet": "def broadcast_dim(xs: Sequence[np.ndarray] | Sequence[Array], fill_value, axis):\n \"\"\"\n Broadcasts all arrays to the same at the last dimension\n by repeating.\n \"\"\"\n if axis < 0:\n axis += jtu.tree_leaves(xs)[0].ndim\n leaves, tree_def = jtu.tree_flatten(xs)\n max_dim = max([x.shape[axis] for x in leaves])\n return tree_def.unflatten(\n [\n jnp.concatenate(\n [\n x,\n np.full(\n (*x.shape[:axis], max_dim - x.shape[axis], *x.shape[axis + 1 :]),\n fill_value,\n dtype=x.dtype,\n ),\n ],\n axis=axis,\n )\n for x in xs\n ]\n )" }, { "identifier": "extend_jacobians", "path": "folx/utils.py", "snippet": "def extend_jacobians(*x: Array, axis):\n \"\"\"\n Extends the given arrays to the same shape by appending zeros.\n \"\"\"\n if len(x) == 1:\n return x\n if axis < 0:\n axis += jtu.tree_leaves(x)[0].ndim\n max_dim = max([a.shape[axis] for a in x])\n if all(a.shape[axis] == max_dim for a in x):\n return x\n result = []\n for a in x:\n a_shape = list(a.shape)\n if a_shape[axis] < max_dim:\n a_shape[axis] = max_dim - a.shape[axis]\n a = jnp.concatenate([a, jnp.zeros(a_shape, dtype=a.dtype)], axis=axis)\n result.append(a)\n return tuple(result)" }, { "identifier": "get_jacobian_for_reduction", "path": "folx/utils.py", "snippet": "def get_jacobian_for_reduction(jacs: Sequence[FwdJacobian], axes):\n # The idea is to first rearrange the jacobians such that all batch dimensions\n # are reduced to one, all reduction dimensions are reduced to one and as last\n # we have the jacobian dimension. Then we can simply count for each output\n # on which inputs it depends. We can use this to construct a mapping from the\n # original jacobian to the reduced jacobian.\n jacs = tuple(jacs)\n axes = np.array(axes, dtype=int)\n axes[axes < 0] += jacs[0].data.ndim - 1\n\n if np.array(axes).ndim == 1:\n axes = axes[None]\n if len(axes) != len(jacs):\n axes = np.repeat(axes, len(jacs), axis=0)\n\n # Match shapes for masks\n masks = broadcast_except(tuple(map(lambda x: x.mask, jacs)), axis=JAC_DIM)\n # Compute a bunch of shapes and sizes\n reduction_shapes = tuple(tuple(np.array(m.shape[1:])[a]) for a, m in zip(axes, masks))\n assert all(len(reduction_shapes[0]) == len(s) for s in reduction_shapes)\n reduction_size = np.prod(reduction_shapes[0], dtype=int)\n\n jac_reduced_axes = tuple(\n (*[x + int(x >= JAC_DIM) for x in a], JAC_DIM)\n for a in axes # the first dim is the same for all arrays\n )\n kept_axes = tuple(np.setdiff1d(np.arange(masks[0].ndim), a) for a in jac_reduced_axes)\n kept_shapes = tuple(tuple(np.array(m.shape)[a]) for a, m in zip(kept_axes, masks))\n assert all(kept_shapes[0] == s for s in kept_shapes)\n kept_shape = kept_shapes[0]\n kept_size = np.prod(kept_shape, dtype=int)\n\n inv_orders = tuple(\n tuple(np.argsort((*kept_axes[i], *jac_reduced_axes[i]))) for i in range(len(jacs))\n )\n\n # Let's rearrange masks and data such that all batch dimensions are reduced\n # to one, all reduction dimensions are reduced to one and as last we have the\n # jacobian dimension.\n def rearrange(\n mask,\n kept_axes,\n jac_reduced_axes,\n ):\n transpose = np.transpose if isinstance(mask, np.ndarray) else jnp.transpose\n return transpose(mask, (*kept_axes, *jac_reduced_axes)).reshape(\n kept_size, reduction_size, -1\n )\n\n masks = jtu.tree_map(rearrange, masks, kept_axes, jac_reduced_axes)\n\n # Determine for each element the outputs.\n mask = np.concatenate(masks, axis=-1)\n out_mask_list = [np.unique(m) for m in mask]\n out_mask_list = [m[m != -1] for m in out_mask_list]\n max_unique = max([m.size for m in out_mask_list])\n\n # Here we extend each mask to the same size by appending -1.\n out_mask = np.stack(\n [\n np.concatenate([m, np.full(max_unique - m.size, -1, dtype=np.int32)])\n for m in out_mask_list\n ]\n )\n\n # Let's reconstruct the original order for the output mask\n out_masks = tuple(\n np.transpose(\n out_mask.reshape(*kept_shape, *([1] * len(reduction_shapes[0])), -1), inv_order\n )\n for inv_order in inv_orders\n )\n\n # Materialize the needed jacobian\n jacobians = tuple(\n jac.materialize_for_idx(\n jac.get_index_mask(mask),\n max_idx=max_unique,\n )\n for jac, mask in zip(jacs, out_masks)\n )\n # Remove all contracted dimensions again\n out_mask = out_masks[0].reshape(-1, *kept_shape)\n return jacobians, out_mask" }, { "identifier": "np_concatenate_brdcast", "path": "folx/utils.py", "snippet": "def np_concatenate_brdcast(arrs, axis):\n \"\"\"\n Concatenates the given arrays along the given axis.\n Before concatenation, the arrays are broadcasted to the same shape.\n\n Args:\n - arrs: sequence of arrays\n - axis: axis along which to concatenate\n Returns:\n - np.ndarray: np.ndarray where all arrays are broadcasted to the same shape\n \"\"\"\n return np.concatenate(broadcast_except(arrs, axis), axis=axis)" } ]
import functools import logging import jax import jax.core as core import jax.numpy as jnp import jax.tree_util as jtu import numpy as np from multiprocessing import Value from typing import TypeVar from .api import ( JAC_DIM, Array, Axes, ExtraArgs, ForwardFn, FunctionFlags, FwdJacobian, FwdLaplArgs, FwdLaplArray, MergeFn, PyTree, ) from .tree_utils import tree_concat, tree_expand, tree_take from .utils import ( broadcast_except, broadcast_dim, extend_jacobians, get_jacobian_for_reduction, np_concatenate_brdcast, )
2,943
R = TypeVar("R", bound=PyTree[Array]) def sparse_jvp( fwd: ForwardFn, laplace_args: FwdLaplArgs, extra_args: ExtraArgs,
R = TypeVar("R", bound=PyTree[Array]) def sparse_jvp( fwd: ForwardFn, laplace_args: FwdLaplArgs, extra_args: ExtraArgs,
merge: MergeFn,
0
2023-11-07 16:32:46+00:00
4k
shuttworth/NICE-SLAM-Easyread
src/Tracker.py
[ { "identifier": "get_camera_from_tensor", "path": "src/common.py", "snippet": "def get_camera_from_tensor(inputs):\n \"\"\"\n Convert quaternion and translation to transformation matrix.\n\n \"\"\"\n N = len(inputs.shape)\n if N == 1:\n inputs = inputs.unsqueeze(0)\n quad, T = inputs[:, :4], inputs[:, 4:]\n R = quad2rotation(quad)\n RT = torch.cat([R, T[:, :, None]], 2)\n if N == 1:\n RT = RT[0]\n return RT" }, { "identifier": "get_samples", "path": "src/common.py", "snippet": "def get_samples(H0, H1, W0, W1, n, H, W, fx, fy, cx, cy, c2w, depth, color, device):\n \"\"\"\n Get n rays from the image region H0..H1, W0..W1.\n c2w is its camera pose and depth/color is the corresponding image tensor.\n\n \"\"\"\n i, j, sample_depth, sample_color = get_sample_uv(\n H0, H1, W0, W1, n, depth, color, device=device)\n rays_o, rays_d = get_rays_from_uv(i, j, c2w, H, W, fx, fy, cx, cy, device)\n return rays_o, rays_d, sample_depth, sample_color" }, { "identifier": "get_tensor_from_camera", "path": "src/common.py", "snippet": "def get_tensor_from_camera(RT, Tquad=False):\n \"\"\"\n Convert transformation matrix to quaternion and translation.\n\n \"\"\"\n gpu_id = -1\n if type(RT) == torch.Tensor:\n if RT.get_device() != -1:\n RT = RT.detach().cpu()\n gpu_id = RT.get_device()\n RT = RT.numpy()\n from mathutils import Matrix\n R, T = RT[:3, :3], RT[:3, 3]\n rot = Matrix(R)\n quad = rot.to_quaternion()\n if Tquad:\n tensor = np.concatenate([T, quad], 0)\n else:\n tensor = np.concatenate([quad, T], 0)\n tensor = torch.from_numpy(tensor).float()\n if gpu_id != -1:\n tensor = tensor.to(gpu_id)\n return tensor" }, { "identifier": "get_dataset", "path": "src/utils/datasets.py", "snippet": "def get_dataset(cfg, args, scale, device='cuda:0'):\n return dataset_dict[cfg['dataset']](cfg, args, scale, device=device)" }, { "identifier": "Visualizer", "path": "src/utils/Visualizer.py", "snippet": "class Visualizer(object):\n \"\"\"\n Visualize intermediate results, render out depth, color and depth uncertainty images.\n It can be called per iteration, which is good for debugging (to see how each tracking/mapping iteration performs).\n\n \"\"\"\n\n def __init__(self, freq, inside_freq, vis_dir, renderer, verbose, device='cuda:0'):\n self.freq = freq\n self.device = device\n self.vis_dir = vis_dir\n self.verbose = verbose\n self.renderer = renderer\n self.inside_freq = inside_freq\n os.makedirs(f'{vis_dir}', exist_ok=True)\n\n def vis(self, idx, iter, gt_depth, gt_color, c2w_or_camera_tensor, c,\n decoders):\n \"\"\"\n Visualization of depth, color images and save to file.\n\n Args:\n idx (int): current frame index.\n iter (int): the iteration number.\n gt_depth (tensor): ground truth depth image of the current frame.\n gt_color (tensor): ground truth color image of the current frame.\n c2w_or_camera_tensor (tensor): camera pose, represented in \n camera to world matrix or quaternion and translation tensor.\n c (dicts): feature grids.\n decoders (nn.module): decoders.\n \"\"\"\n with torch.no_grad():\n if (idx % self.freq == 0) and (iter % self.inside_freq == 0):\n gt_depth_np = gt_depth.cpu().numpy()\n gt_color_np = gt_color.cpu().numpy()\n if len(c2w_or_camera_tensor.shape) == 1:\n bottom = torch.from_numpy(\n np.array([0, 0, 0, 1.]).reshape([1, 4])).type(\n torch.float32).to(self.device)\n c2w = get_camera_from_tensor(\n c2w_or_camera_tensor.clone().detach())\n c2w = torch.cat([c2w, bottom], dim=0)\n else:\n c2w = c2w_or_camera_tensor\n\n depth, uncertainty, color = self.renderer.render_img(\n c,\n decoders,\n c2w,\n self.device,\n stage='color',\n gt_depth=gt_depth)\n depth_np = depth.detach().cpu().numpy()\n color_np = color.detach().cpu().numpy()\n depth_residual = np.abs(gt_depth_np - depth_np)\n depth_residual[gt_depth_np == 0.0] = 0.0\n color_residual = np.abs(gt_color_np - color_np)\n color_residual[gt_depth_np == 0.0] = 0.0\n\n fig, axs = plt.subplots(2, 3)\n fig.tight_layout()\n max_depth = np.max(gt_depth_np)\n axs[0, 0].imshow(gt_depth_np, cmap=\"plasma\",\n vmin=0, vmax=max_depth)\n axs[0, 0].set_title('Input Depth')\n axs[0, 0].set_xticks([])\n axs[0, 0].set_yticks([])\n axs[0, 1].imshow(depth_np, cmap=\"plasma\",\n vmin=0, vmax=max_depth)\n axs[0, 1].set_title('Generated Depth')\n axs[0, 1].set_xticks([])\n axs[0, 1].set_yticks([])\n axs[0, 2].imshow(depth_residual, cmap=\"plasma\",\n vmin=0, vmax=max_depth)\n axs[0, 2].set_title('Depth Residual')\n axs[0, 2].set_xticks([])\n axs[0, 2].set_yticks([])\n gt_color_np = np.clip(gt_color_np, 0, 1)\n color_np = np.clip(color_np, 0, 1)\n color_residual = np.clip(color_residual, 0, 1)\n axs[1, 0].imshow(gt_color_np, cmap=\"plasma\")\n axs[1, 0].set_title('Input RGB')\n axs[1, 0].set_xticks([])\n axs[1, 0].set_yticks([])\n axs[1, 1].imshow(color_np, cmap=\"plasma\")\n axs[1, 1].set_title('Generated RGB')\n axs[1, 1].set_xticks([])\n axs[1, 1].set_yticks([])\n axs[1, 2].imshow(color_residual, cmap=\"plasma\")\n axs[1, 2].set_title('RGB Residual')\n axs[1, 2].set_xticks([])\n axs[1, 2].set_yticks([])\n plt.subplots_adjust(wspace=0, hspace=0)\n plt.savefig(\n f'{self.vis_dir}/{idx:05d}_{iter:04d}.jpg', bbox_inches='tight', pad_inches=0.2)\n plt.clf()\n\n if self.verbose:\n print(\n f'Saved rendering visualization of color/depth image at {self.vis_dir}/{idx:05d}_{iter:04d}.jpg')" } ]
import copy import os import time import numpy as np import torch from colorama import Fore, Style from torch.autograd import Variable from torch.utils.data import DataLoader from tqdm import tqdm from src.common import (get_camera_from_tensor, get_samples, get_tensor_from_camera) from src.utils.datasets import get_dataset from src.utils.Visualizer import Visualizer
2,306
class Tracker(object): def __init__(self, cfg, args, slam ): self.cfg = cfg self.args = args self.scale = cfg['scale'] self.coarse = cfg['coarse'] self.occupancy = cfg['occupancy'] self.sync_method = cfg['sync_method'] self.idx = slam.idx self.nice = slam.nice self.bound = slam.bound self.mesher = slam.mesher self.output = slam.output self.verbose = slam.verbose self.shared_c = slam.shared_c self.renderer = slam.renderer self.gt_c2w_list = slam.gt_c2w_list self.low_gpu_mem = slam.low_gpu_mem self.mapping_idx = slam.mapping_idx self.mapping_cnt = slam.mapping_cnt self.shared_decoders = slam.shared_decoders self.estimate_c2w_list = slam.estimate_c2w_list self.cam_lr = cfg['tracking']['lr'] self.device = cfg['tracking']['device'] self.num_cam_iters = cfg['tracking']['iters'] self.gt_camera = cfg['tracking']['gt_camera'] self.tracking_pixels = cfg['tracking']['pixels'] self.seperate_LR = cfg['tracking']['seperate_LR'] self.w_color_loss = cfg['tracking']['w_color_loss'] self.ignore_edge_W = cfg['tracking']['ignore_edge_W'] self.ignore_edge_H = cfg['tracking']['ignore_edge_H'] self.handle_dynamic = cfg['tracking']['handle_dynamic'] self.use_color_in_tracking = cfg['tracking']['use_color_in_tracking'] self.const_speed_assumption = cfg['tracking']['const_speed_assumption'] self.every_frame = cfg['mapping']['every_frame'] self.no_vis_on_first_frame = cfg['mapping']['no_vis_on_first_frame'] self.prev_mapping_idx = -1 self.frame_reader = get_dataset( cfg, args, self.scale, device=self.device) self.n_img = len(self.frame_reader) self.frame_loader = DataLoader( self.frame_reader, batch_size=1, shuffle=False, num_workers=1)
class Tracker(object): def __init__(self, cfg, args, slam ): self.cfg = cfg self.args = args self.scale = cfg['scale'] self.coarse = cfg['coarse'] self.occupancy = cfg['occupancy'] self.sync_method = cfg['sync_method'] self.idx = slam.idx self.nice = slam.nice self.bound = slam.bound self.mesher = slam.mesher self.output = slam.output self.verbose = slam.verbose self.shared_c = slam.shared_c self.renderer = slam.renderer self.gt_c2w_list = slam.gt_c2w_list self.low_gpu_mem = slam.low_gpu_mem self.mapping_idx = slam.mapping_idx self.mapping_cnt = slam.mapping_cnt self.shared_decoders = slam.shared_decoders self.estimate_c2w_list = slam.estimate_c2w_list self.cam_lr = cfg['tracking']['lr'] self.device = cfg['tracking']['device'] self.num_cam_iters = cfg['tracking']['iters'] self.gt_camera = cfg['tracking']['gt_camera'] self.tracking_pixels = cfg['tracking']['pixels'] self.seperate_LR = cfg['tracking']['seperate_LR'] self.w_color_loss = cfg['tracking']['w_color_loss'] self.ignore_edge_W = cfg['tracking']['ignore_edge_W'] self.ignore_edge_H = cfg['tracking']['ignore_edge_H'] self.handle_dynamic = cfg['tracking']['handle_dynamic'] self.use_color_in_tracking = cfg['tracking']['use_color_in_tracking'] self.const_speed_assumption = cfg['tracking']['const_speed_assumption'] self.every_frame = cfg['mapping']['every_frame'] self.no_vis_on_first_frame = cfg['mapping']['no_vis_on_first_frame'] self.prev_mapping_idx = -1 self.frame_reader = get_dataset( cfg, args, self.scale, device=self.device) self.n_img = len(self.frame_reader) self.frame_loader = DataLoader( self.frame_reader, batch_size=1, shuffle=False, num_workers=1)
self.visualizer = Visualizer(freq=cfg['tracking']['vis_freq'], inside_freq=cfg['tracking']['vis_inside_freq'],
4
2023-11-07 05:09:36+00:00
4k
TianrongChen/DMSB
runner.py
[ { "identifier": "MMD_loss", "path": "metrics.py", "snippet": "class MMD_loss(torch.nn.Module):\n '''\n fork from: https://github.com/ZongxianLee/MMD_Loss.Pytorch\n '''\n def __init__(self, kernel_mul = 2.0, kernel_num = 5):\n super(MMD_loss, self).__init__()\n self.kernel_num = kernel_num\n self.kernel_mul = kernel_mul\n self.fix_sigma = None\n return\n def guassian_kernel(self, source, target, kernel_mul=2.0, kernel_num=5, fix_sigma=None):\n n_samples = int(source.size()[0])+int(target.size()[0])\n total = torch.cat([source, target], dim=0)\n\n total0 = total.unsqueeze(0).expand(int(total.size(0)), int(total.size(0)), int(total.size(1)))\n total1 = total.unsqueeze(1).expand(int(total.size(0)), int(total.size(0)), int(total.size(1)))\n L2_distance = ((total0-total1)**2).sum(2) \n if fix_sigma:\n bandwidth = fix_sigma\n else:\n bandwidth = torch.sum(L2_distance.data) / (n_samples**2-n_samples)\n bandwidth /= kernel_mul ** (kernel_num // 2)\n bandwidth_list = [bandwidth * (kernel_mul**i) for i in range(kernel_num)]\n kernel_val = [torch.exp(-L2_distance / bandwidth_temp) for bandwidth_temp in bandwidth_list]\n return sum(kernel_val)\n\n def forward(self, source, target):\n batch_size = int(source.size()[0])\n kernels = self.guassian_kernel(source, target, kernel_mul=self.kernel_mul, kernel_num=self.kernel_num, fix_sigma=self.fix_sigma)\n XX = kernels[:batch_size, :batch_size]\n YY = kernels[batch_size:, batch_size:]\n XY = kernels[:batch_size, batch_size:]\n YX = kernels[batch_size:, :batch_size]\n loss = torch.mean(XX + YY - XY -YX)\n return loss" }, { "identifier": "compute_metrics", "path": "metrics.py", "snippet": "def compute_metrics(opt, pred_traj, ref_data, metrics, runner,stage):\n '''\n pred_traj: [batch_size, interval, data_dim] torch.Tensor\n ref_data: [num_dist, batch_size, data_dim], torch.Tensor, we use whole ref data which is similar to FID computation\n The reference data and prediction are all the marignals. We delete the leave one out (--LOO) marginal during the training, but we still evaluate them during here.\n '''\n sample_size = 1000\n dist_time = np.linspace(0, opt.interval-1, opt.num_dist).astype(int) #we delete a distribution when LOO during training, so num_dist is same as original marginal\n pred_idx = np.random.choice(pred_traj.shape[0], sample_size, replace=False) #random sample from batch\n pred_data = pred_traj[pred_idx][:,dist_time,0:opt.data_dim[0]] # [samp_bs, num_dist, data_dim] \n pred_data = pred_data.transpose(1,0,2)/opt.data_scale # [num_dist, samp_bs, data_dim]\n \n for metric_idx, metric in enumerate(metrics): #loop over metrics\n avg_metric = 0\n for idx,(pred,ref) in enumerate(zip(pred_data, ref_data)):\n if idx==0:\n continue # First marginal does not need to be evaluate. We do not generate it, just ground truth.\n if opt.metrics[metric_idx] == 'MMD': \n ref_idx = np.random.choice(ref.shape[0], sample_size, replace=False)\n ref = torch.Tensor(ref[ref_idx])\n pred = torch.Tensor(pred)\n\n loss = metric(pred,ref)\n avg_metric += loss\n print(util.green('{} for time{} is {}'.format(opt.metrics[metric_idx], idx,loss)))\n runner.log_tb(stage, loss, '{}_t{}'.format(opt.metrics[metric_idx],idx),'SB_forward')\n\n avg_metric = avg_metric/(opt.num_dist-1)\n print('AVERAGE {} IS {}'.format(opt.metrics[metric_idx],avg_metric))\n runner.log_tb(stage, avg_metric, '{}_avg'.format(opt.metrics[metric_idx]), 'SB_forward') \n\n return pred_data" }, { "identifier": "metric_build", "path": "metrics.py", "snippet": "def metric_build(opt):\n metrics = {\n 'SWD':sliced_wasserstein_distance,\n 'MMD':MMD_loss(),\n 'MWD':max_sliced_wasserstein_distance\n }\n return [metrics.get(key) for key in opt.metrics]" }, { "identifier": "compute_sb_DSB_train", "path": "loss.py", "snippet": "def compute_sb_DSB_train(opt, label, label_aux,dyn, ts, ms, policy_opt, return_z=False, itr=None):\n \"\"\" Implementation of Eq (18,19) in our main paper.\n \"\"\"\n dt = dyn.dt\n zs = policy_opt(ms,ts)\n g_ts = dyn.g(ts)\n g_ts = g_ts[:,None,None,None] if util.is_image_dataset(opt) else g_ts[:,None]\n loss = torch.nn.functional.mse_loss(g_ts*dt*zs,label)\n return loss, zs if return_z else loss" } ]
import os, time, gc import numpy as np import torch import torch.nn.functional as F import policy import sde import data import util from torch.optim import SGD, RMSprop, Adagrad, AdamW, lr_scheduler, Adam from torch.utils.tensorboard import SummaryWriter from torch_ema import ExponentialMovingAverage from metrics import MMD_loss,compute_metrics,metric_build from loss import compute_sb_DSB_train from ipdb import set_trace as debug
3,473
return self.optimizer_f, self.ema_f, self.sched_f elif z == self.z_b: return self.optimizer_b, self.ema_b, self.sched_b else: raise RuntimeError() @torch.no_grad() def sample_train_data(self, opt, policy_opt, policy_impt, reused_sampler, rollout=None, resample=None): # reuse or sample training ms and zs try: reused_traj = next(reused_sampler) train_ms, train_zs = reused_traj[:,0,...], reused_traj[:,1,...] print('generate train data from [{}]!'.format(util.green('reused samper'))) except: _, ema, _ = self.get_optimizer_ema_sched(policy_opt) _, ema_impt, _ = self.get_optimizer_ema_sched(policy_impt) with ema.average_parameters(), ema_impt.average_parameters(): policy_impt = freeze_policy(policy_impt) policy_opt = freeze_policy(policy_opt) corrector = (lambda x,t: policy_impt(x,t) + policy_opt(x,t)) if opt.use_corrector else None ms, zs, _, labels, ts = self.dyn.sample_traj(self.ts, policy_impt, corrector=corrector, rollout=rollout, resample=resample) train_ms = ms.detach().cpu(); del ms train_zs = zs.detach().cpu(); del zs train_labels = labels.detach().cpu(); del labels train_ts = ts.detach().cpu(); del ts print('generate train data from [{}]!'.format(util.red('sampling'))) assert train_ms.shape[0] == opt.samp_bs assert train_ms.shape[1] == len(train_ts) gc.collect() return train_ms, train_zs, train_ts, train_labels def sb_alternate_train(self, opt): reused_sampler = self.evaluate(opt, 0, rollout = [0,opt.num_dist-1], resample=False,ode_samp=False) bridge_ep = boundry_ep = opt.num_epoch if opt.problem_name =='petal': bridge_ep = 1 #Special handle for petal. the distance between distributions are too close. for stage in range(opt.num_stage): self.sb_alternate_train_stage( opt, stage, boundry_ep, 'backward', rollout = [0,opt.num_dist-1], resample=True # train backward Kboundary ) self.sb_alternate_train_stage( opt, stage, boundry_ep, 'forward', rollout = [0,opt.num_dist-1], resample=True # train forward Kboundary ) self.sb_alternate_train_stage( opt, stage, bridge_ep, 'backward', rollout = [0,opt.num_dist-1], resample=False #Train K bridge backward ) self.sb_alternate_train_stage( opt, stage, boundry_ep, 'forward', rollout = [0,opt.num_dist-1], resample=True #Train forward Kboundary ) self.sb_alternate_train_stage( opt, stage, boundry_ep, 'backward', rollout = [0,opt.num_dist-1], resample=True #Train backward Kboundary ) self.sb_alternate_train_stage( opt, stage, bridge_ep, 'forward', rollout = [0,opt.num_dist-1], resample=False #Train K bridge forward ) reused_sampler = self.evaluate(opt, stage+1, rollout = [0,opt.num_dist-1],resample=False) if opt.log_tb: self.writer.close() def sb_alternate_train_stage(self, opt, stage, epoch, direction, reused_sampler=None, rollout=False, resample=True): policy_opt, policy_impt = { 'forward': [self.z_f, self.z_b], # train forwad, sample from backward 'backward': [self.z_b, self.z_f], # train backward, sample from forward }.get(direction) for ep in range(epoch): # prepare training data train_ms, train_zs, train_ts, train_labels = self.sample_train_data( opt, policy_opt, policy_impt, reused_sampler, rollout=rollout, resample=resample ) # train one epoch policy_impt = freeze_policy(policy_impt) policy_opt = activate_policy(policy_opt) self.DSB_alternate_train_ep( opt, ep, stage, direction, train_ms, train_zs, train_ts, train_labels, policy_opt, epoch ) def DSB_alternate_train_ep( self, opt, ep, stage, direction, train_xs, train_zs, train_ts, train_labels, policy, num_epoch ): assert train_xs.shape[0] == opt.samp_bs assert train_zs.shape[0] == opt.samp_bs assert direction == policy.direction optimizer, ema, sched = self.get_optimizer_ema_sched(policy) use_amp=opt.use_amp scaler = torch.cuda.amp.GradScaler(enabled=use_amp) for it in range(opt.num_itr): # -------- sample x_idx and t_idx \in [0, interval] -------- samp_m_idx = torch.randint(opt.samp_bs, (opt.train_bs_x,),device='cpu') samp_t_idx = util.time_sample(opt.interval, policy.direction, opt.train_bs_t) if opt.use_arange_t: samp_t_idx = util.time_arange(train_ts.shape[0], policy.direction) # -------- build sample -------- sign=1 if policy.direction=='forward' else -1 ts = train_ts[samp_t_idx].detach().to(opt.device) ms = train_xs[samp_m_idx][:, samp_t_idx, ...].to(opt.device) zs_impt = train_zs[samp_m_idx][:, samp_t_idx+sign, ...].to(opt.device) train_label = train_labels[samp_m_idx][:, samp_t_idx+sign, ...].to(opt.device) optimizer.zero_grad(set_to_none=True) # -------- handle for batch_x and batch_t --------- # (batch, T, xdim) --> (batch*T, xdim) ms = util.flatten_dim01(ms) zs_impt = util.flatten_dim01(zs_impt) train_label = util.flatten_dim01(train_label) ts = ts.repeat(opt.train_bs_x) assert ms.shape[0] == ts.shape[0] assert zs_impt.shape[0] == ts.shape[0] # -------- compute loss and backprop -------- with torch.cuda.amp.autocast(enabled=use_amp):
def build_optimizer_ema_sched(opt, policy): direction = policy.direction optim_name = { 'Adam': Adam, 'AdamW': AdamW, 'Adagrad': Adagrad, 'RMSprop': RMSprop, 'SGD': SGD, }.get(opt.optimizer) optim_dict = { "lr": opt.lr_f if direction=='forward' else opt.lr_b, 'weight_decay':opt.l2_norm, } if opt.optimizer == 'SGD': optim_dict['momentum'] = 0.9 optimizer = optim_name(policy.parameters(), **optim_dict) ema = ExponentialMovingAverage(policy.parameters(), decay=0.999) if opt.lr_gamma < 1.0: sched = lr_scheduler.StepLR(optimizer, step_size=opt.lr_step, gamma=opt.lr_gamma) else: sched = None return optimizer, ema, sched def freeze_policy(policy): for p in policy.parameters(): p.requires_grad = False policy.eval() return policy def activate_policy(policy): for p in policy.parameters(): p.requires_grad = True policy.train() return policy class Runner(): def __init__(self,opt): super(Runner,self).__init__() self.start_time = time.time() self.ts = torch.linspace(opt.t0, opt.T, opt.interval) self.x_dists = data.build(opt) # for visualize training data if opt.problem_name == 'petal' or opt.problem_name =='RNAsc': self.x_data = [dist.ground_truth for dist in self.x_dists] #Initialize velocity, all gaussian self.v_dists = {dist:opt.v_scale*torch.randn(opt.samp_bs, *opt.data_dim) for dist in range(len(self.x_dists))} # Build metrics self.metrics = metric_build(opt) # build dynamics, forward (z_f) and backward (z_b) policies and corresponding optimizer self.dyn = sde.build(opt, self.x_dists, self.v_dists) self.z_f = policy.build(opt, self.dyn, 'forward') # p -> q self.z_b = policy.build(opt, self.dyn, 'backward') # q -> p self.optimizer_f, self.ema_f, self.sched_f = build_optimizer_ema_sched(opt, self.z_f) self.optimizer_b, self.ema_b, self.sched_b = build_optimizer_ema_sched(opt, self.z_b) if opt.load: util.restore_checkpoint(opt, self, opt.load) self.dyn.prev_v_boundary = self.v_dists # tensorboard related things if opt.log_tb: self.it_f = 0 self.it_b = 0 self.writer =SummaryWriter( log_dir =os.path.join('runs', opt.dir) ) def update_count(self, direction): if direction == 'forward': self.it_f += 1 return self.it_f elif direction == 'backward': self.it_b += 1 return self.it_b else: raise RuntimeError() def get_optimizer_ema_sched(self, z): if z == self.z_f: return self.optimizer_f, self.ema_f, self.sched_f elif z == self.z_b: return self.optimizer_b, self.ema_b, self.sched_b else: raise RuntimeError() @torch.no_grad() def sample_train_data(self, opt, policy_opt, policy_impt, reused_sampler, rollout=None, resample=None): # reuse or sample training ms and zs try: reused_traj = next(reused_sampler) train_ms, train_zs = reused_traj[:,0,...], reused_traj[:,1,...] print('generate train data from [{}]!'.format(util.green('reused samper'))) except: _, ema, _ = self.get_optimizer_ema_sched(policy_opt) _, ema_impt, _ = self.get_optimizer_ema_sched(policy_impt) with ema.average_parameters(), ema_impt.average_parameters(): policy_impt = freeze_policy(policy_impt) policy_opt = freeze_policy(policy_opt) corrector = (lambda x,t: policy_impt(x,t) + policy_opt(x,t)) if opt.use_corrector else None ms, zs, _, labels, ts = self.dyn.sample_traj(self.ts, policy_impt, corrector=corrector, rollout=rollout, resample=resample) train_ms = ms.detach().cpu(); del ms train_zs = zs.detach().cpu(); del zs train_labels = labels.detach().cpu(); del labels train_ts = ts.detach().cpu(); del ts print('generate train data from [{}]!'.format(util.red('sampling'))) assert train_ms.shape[0] == opt.samp_bs assert train_ms.shape[1] == len(train_ts) gc.collect() return train_ms, train_zs, train_ts, train_labels def sb_alternate_train(self, opt): reused_sampler = self.evaluate(opt, 0, rollout = [0,opt.num_dist-1], resample=False,ode_samp=False) bridge_ep = boundry_ep = opt.num_epoch if opt.problem_name =='petal': bridge_ep = 1 #Special handle for petal. the distance between distributions are too close. for stage in range(opt.num_stage): self.sb_alternate_train_stage( opt, stage, boundry_ep, 'backward', rollout = [0,opt.num_dist-1], resample=True # train backward Kboundary ) self.sb_alternate_train_stage( opt, stage, boundry_ep, 'forward', rollout = [0,opt.num_dist-1], resample=True # train forward Kboundary ) self.sb_alternate_train_stage( opt, stage, bridge_ep, 'backward', rollout = [0,opt.num_dist-1], resample=False #Train K bridge backward ) self.sb_alternate_train_stage( opt, stage, boundry_ep, 'forward', rollout = [0,opt.num_dist-1], resample=True #Train forward Kboundary ) self.sb_alternate_train_stage( opt, stage, boundry_ep, 'backward', rollout = [0,opt.num_dist-1], resample=True #Train backward Kboundary ) self.sb_alternate_train_stage( opt, stage, bridge_ep, 'forward', rollout = [0,opt.num_dist-1], resample=False #Train K bridge forward ) reused_sampler = self.evaluate(opt, stage+1, rollout = [0,opt.num_dist-1],resample=False) if opt.log_tb: self.writer.close() def sb_alternate_train_stage(self, opt, stage, epoch, direction, reused_sampler=None, rollout=False, resample=True): policy_opt, policy_impt = { 'forward': [self.z_f, self.z_b], # train forwad, sample from backward 'backward': [self.z_b, self.z_f], # train backward, sample from forward }.get(direction) for ep in range(epoch): # prepare training data train_ms, train_zs, train_ts, train_labels = self.sample_train_data( opt, policy_opt, policy_impt, reused_sampler, rollout=rollout, resample=resample ) # train one epoch policy_impt = freeze_policy(policy_impt) policy_opt = activate_policy(policy_opt) self.DSB_alternate_train_ep( opt, ep, stage, direction, train_ms, train_zs, train_ts, train_labels, policy_opt, epoch ) def DSB_alternate_train_ep( self, opt, ep, stage, direction, train_xs, train_zs, train_ts, train_labels, policy, num_epoch ): assert train_xs.shape[0] == opt.samp_bs assert train_zs.shape[0] == opt.samp_bs assert direction == policy.direction optimizer, ema, sched = self.get_optimizer_ema_sched(policy) use_amp=opt.use_amp scaler = torch.cuda.amp.GradScaler(enabled=use_amp) for it in range(opt.num_itr): # -------- sample x_idx and t_idx \in [0, interval] -------- samp_m_idx = torch.randint(opt.samp_bs, (opt.train_bs_x,),device='cpu') samp_t_idx = util.time_sample(opt.interval, policy.direction, opt.train_bs_t) if opt.use_arange_t: samp_t_idx = util.time_arange(train_ts.shape[0], policy.direction) # -------- build sample -------- sign=1 if policy.direction=='forward' else -1 ts = train_ts[samp_t_idx].detach().to(opt.device) ms = train_xs[samp_m_idx][:, samp_t_idx, ...].to(opt.device) zs_impt = train_zs[samp_m_idx][:, samp_t_idx+sign, ...].to(opt.device) train_label = train_labels[samp_m_idx][:, samp_t_idx+sign, ...].to(opt.device) optimizer.zero_grad(set_to_none=True) # -------- handle for batch_x and batch_t --------- # (batch, T, xdim) --> (batch*T, xdim) ms = util.flatten_dim01(ms) zs_impt = util.flatten_dim01(zs_impt) train_label = util.flatten_dim01(train_label) ts = ts.repeat(opt.train_bs_x) assert ms.shape[0] == ts.shape[0] assert zs_impt.shape[0] == ts.shape[0] # -------- compute loss and backprop -------- with torch.cuda.amp.autocast(enabled=use_amp):
loss, zs = compute_sb_DSB_train(
3
2023-11-05 21:12:37+00:00
4k
mileswyn/SAMIHS
models/segment_anything/predictor.py
[ { "identifier": "Sam", "path": "models/segment_anything/modeling/sam.py", "snippet": "class Sam(nn.Module):\n mask_threshold: float = 0.0\n image_format: str = \"RGB\"\n\n def __init__(\n self,\n image_encoder: ImageEncoderViT,\n prompt_encoder: PromptEncoder,\n mask_decoder: MaskDecoder,\n pixel_mean: List[float] = [123.675, 116.28, 103.53],\n pixel_std: List[float] = [58.395, 57.12, 57.375],\n ) -> None:\n \"\"\"\n SAM predicts object masks from an image and input prompts.\n\n Arguments:\n image_encoder (ImageEncoderViT): The backbone used to encode the\n image into image embeddings that allow for efficient mask prediction.\n prompt_encoder (PromptEncoder): Encodes various types of input prompts.\n mask_decoder (MaskDecoder): Predicts masks from the image embeddings\n and encoded prompts.\n pixel_mean (list(float)): Mean values for normalizing pixels in the input image.\n pixel_std (list(float)): Std values for normalizing pixels in the input image.\n \"\"\"\n super().__init__()\n self.image_encoder = image_encoder\n self.prompt_encoder = prompt_encoder\n self.mask_decoder = mask_decoder\n self.register_buffer(\"pixel_mean\", torch.Tensor(pixel_mean).view(-1, 1, 1), False)\n self.register_buffer(\"pixel_std\", torch.Tensor(pixel_std).view(-1, 1, 1), False)\n\n for param in self.image_encoder.parameters():\n param.requires_grad = False\n for param in self.prompt_encoder.parameters():\n param.requires_grad = False\n\n @property\n def device(self) -> Any:\n return self.pixel_mean.device\n\n @torch.no_grad()\n def forward_sam(\n self,\n batched_input: List[Dict[str, Any]],\n multimask_output: bool,\n ) -> List[Dict[str, torch.Tensor]]:\n \"\"\"\n Predicts masks end-to-end from provided images and prompts.\n If prompts are not known in advance, using SamPredictor is\n recommended over calling the model directly.\n\n Arguments:\n batched_input (list(dict)): A list over input images, each a\n dictionary with the following keys. A prompt key can be\n excluded if it is not present.\n 'image': The image as a torch tensor in 3xHxW format,\n already transformed for input to the model.\n 'original_size': (tuple(int, int)) The original size of\n the image before transformation, as (H, W).\n 'point_coords': (torch.Tensor) Batched point prompts for\n this image, with shape BxNx2. Already transformed to the\n input frame of the model.\n 'point_labels': (torch.Tensor) Batched labels for point prompts,\n with shape BxN.\n 'boxes': (torch.Tensor) Batched box inputs, with shape Bx4.\n Already transformed to the input frame of the model.\n 'mask_inputs': (torch.Tensor) Batched mask inputs to the model,\n in the form Bx1xHxW.\n multimask_output (bool): Whether the model should predict multiple\n disambiguating masks, or return a single mask.\n\n Returns:\n (list(dict)): A list over input images, where each element is\n as dictionary with the following keys.\n 'masks': (torch.Tensor) Batched binary mask predictions,\n with shape BxCxHxW, where B is the number of input prompts,\n C is determined by multimask_output, and (H, W) is the\n original size of the image.\n 'iou_predictions': (torch.Tensor) The model's predictions\n of mask quality, in shape BxC.\n 'low_res_logits': (torch.Tensor) Low resolution logits with\n shape BxCxHxW, where H=W=256. Can be passed as mask input\n to subsequent iterations of prediction.\n \"\"\"\n input_images = torch.stack([self.preprocess(x[\"image\"]) for x in batched_input], dim=0)\n image_embeddings = self.image_encoder(input_images)\n\n outputs = []\n for image_record, curr_embedding in zip(batched_input, image_embeddings):\n if \"point_coords\" in image_record:\n points = (image_record[\"point_coords\"], image_record[\"point_labels\"])\n else:\n points = None\n sparse_embeddings, dense_embeddings = self.prompt_encoder(\n points=points,\n boxes=image_record.get(\"boxes\", None),\n masks=image_record.get(\"mask_inputs\", None),\n )\n low_res_masks, iou_predictions = self.mask_decoder(\n image_embeddings=curr_embedding.unsqueeze(0),\n image_pe=self.prompt_encoder.get_dense_pe(),\n sparse_prompt_embeddings=sparse_embeddings,\n dense_prompt_embeddings=dense_embeddings,\n multimask_output=multimask_output,\n )\n masks = self.postprocess_masks(\n low_res_masks,\n input_size=image_record[\"image\"].shape[-2:],\n original_size=image_record[\"original_size\"],\n )\n masks = masks > self.mask_threshold\n outputs.append(\n {\n \"masks\": masks,\n \"iou_predictions\": iou_predictions,\n \"low_res_logits\": low_res_masks,\n }\n )\n return outputs\n \n def forward(\n self, \n imgs: torch.Tensor,\n pt: Tuple[torch.Tensor, torch.Tensor],\n bbox: torch.Tensor # b 4\n ) -> torch.Tensor:\n imge= self.image_encoder(imgs)\n if bbox is not None:\n if len(bbox.shape) == 2:\n bbox = bbox[:, None, :]\n se, de = self.prompt_encoder(\n points=pt,\n boxes=bbox,\n masks=None,\n )\n low_res_masks, _ = self.mask_decoder(\n image_embeddings=imge,\n image_pe=self.prompt_encoder.get_dense_pe(), \n sparse_prompt_embeddings=se,\n dense_prompt_embeddings=de, \n multimask_output=False,\n )\n masks = F.interpolate(low_res_masks, (self.image_encoder.img_size, self.image_encoder.img_size), mode=\"bilinear\", align_corners=False)\n outputs = {\"low_res_logits\": low_res_masks, \"masks\": low_res_masks}\n return outputs\n\n def postprocess_masks(\n self,\n masks: torch.Tensor,\n input_size: Tuple[int, ...],\n original_size: Tuple[int, ...],\n ) -> torch.Tensor:\n \"\"\"\n Remove padding and upscale masks to the original image size.\n\n Arguments:\n masks (torch.Tensor): Batched masks from the mask_decoder,\n in BxCxHxW format.\n input_size (tuple(int, int)): The size of the image input to the\n model, in (H, W) format. Used to remove padding.\n original_size (tuple(int, int)): The original size of the image\n before resizing for input to the model, in (H, W) format.\n\n Returns:\n (torch.Tensor): Batched masks in BxCxHxW format, where (H, W)\n is given by original_size.\n \"\"\"\n masks = F.interpolate(\n masks,\n (self.image_encoder.img_size, self.image_encoder.img_size),\n mode=\"bilinear\",\n align_corners=False,\n )\n masks = masks[..., : input_size[0], : input_size[1]]\n masks = F.interpolate(masks, original_size, mode=\"bilinear\", align_corners=False)\n return masks\n\n def preprocess(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"Normalize pixel values and pad to a square input.\"\"\"\n # Normalize colors\n x = (x - self.pixel_mean) / self.pixel_std\n\n # Pad\n h, w = x.shape[-2:]\n padh = self.image_encoder.img_size - h\n padw = self.image_encoder.img_size - w\n x = F.pad(x, (0, padw, 0, padh))\n return x" }, { "identifier": "ResizeLongestSide", "path": "models/segment_anything/utils/transforms.py", "snippet": "class ResizeLongestSide:\n \"\"\"\n Resizes images to the longest side 'target_length', as well as provides\n methods for resizing coordinates and boxes. Provides methods for\n transforming both numpy array and batched torch tensors.\n \"\"\"\n\n def __init__(self, target_length: int) -> None:\n self.target_length = target_length\n\n def apply_image(self, image: np.ndarray) -> np.ndarray:\n \"\"\"\n Expects a numpy array with shape HxWxC in uint8 format.\n \"\"\"\n target_size = self.get_preprocess_shape(image.shape[0], image.shape[1], self.target_length)\n return np.array(resize(to_pil_image(image), target_size))\n\n def apply_coords(self, coords: np.ndarray, original_size: Tuple[int, ...]) -> np.ndarray:\n \"\"\"\n Expects a numpy array of length 2 in the final dimension. Requires the\n original image size in (H, W) format.\n \"\"\"\n old_h, old_w = original_size\n new_h, new_w = self.get_preprocess_shape(\n original_size[0], original_size[1], self.target_length\n )\n coords = deepcopy(coords).astype(float)\n coords[..., 0] = coords[..., 0] * (new_w / old_w)\n coords[..., 1] = coords[..., 1] * (new_h / old_h)\n return coords\n\n def apply_boxes(self, boxes: np.ndarray, original_size: Tuple[int, ...]) -> np.ndarray:\n \"\"\"\n Expects a numpy array shape Bx4. Requires the original image size\n in (H, W) format.\n \"\"\"\n boxes = self.apply_coords(boxes.reshape(-1, 2, 2), original_size)\n return boxes.reshape(-1, 4)\n\n def apply_image_torch(self, image: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Expects batched images with shape BxCxHxW and float format. This\n transformation may not exactly match apply_image. apply_image is\n the transformation expected by the model.\n \"\"\"\n # Expects an image in BCHW format. May not exactly match apply_image.\n target_size = self.get_preprocess_shape(image.shape[2], image.shape[3], self.target_length)\n return F.interpolate(\n image, target_size, mode=\"bilinear\", align_corners=False, antialias=True\n )\n\n def apply_coords_torch(\n self, coords: torch.Tensor, original_size: Tuple[int, ...]\n ) -> torch.Tensor:\n \"\"\"\n Expects a torch tensor with length 2 in the last dimension. Requires the\n original image size in (H, W) format.\n \"\"\"\n old_h, old_w = original_size\n new_h, new_w = self.get_preprocess_shape(\n original_size[0], original_size[1], self.target_length\n )\n coords = deepcopy(coords).to(torch.float)\n coords[..., 0] = coords[..., 0] * (new_w / old_w)\n coords[..., 1] = coords[..., 1] * (new_h / old_h)\n return coords\n\n def apply_boxes_torch(\n self, boxes: torch.Tensor, original_size: Tuple[int, ...]\n ) -> torch.Tensor:\n \"\"\"\n Expects a torch tensor with shape Bx4. Requires the original image\n size in (H, W) format.\n \"\"\"\n boxes = self.apply_coords_torch(boxes.reshape(-1, 2, 2), original_size)\n return boxes.reshape(-1, 4)\n\n @staticmethod\n def get_preprocess_shape(oldh: int, oldw: int, long_side_length: int) -> Tuple[int, int]:\n \"\"\"\n Compute the output size given input size and target long side length.\n \"\"\"\n scale = long_side_length * 1.0 / max(oldh, oldw)\n newh, neww = oldh * scale, oldw * scale\n neww = int(neww + 0.5)\n newh = int(newh + 0.5)\n return (newh, neww)" } ]
import numpy as np import torch from models.segment_anything.modeling import Sam from typing import Optional, Tuple from .utils.transforms import ResizeLongestSide
2,996
# Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. class SamPredictor: def __init__( self,
# Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. class SamPredictor: def __init__( self,
sam_model: Sam,
0
2023-11-09 07:26:33+00:00
4k
silicx/ObjectConceptLearning
models/OCRN_intervention.py
[ { "identifier": "OcrnBaseModel", "path": "models/base_models.py", "snippet": "class OcrnBaseModel(nn.Module):\n\n def __init__(self, dataset, args):\n super(OcrnBaseModel, self).__init__()\n\n self.args = args\n self.num_obj = len(dataset.objs)\n self.num_attr = len(dataset.attrs)\n self.num_aff = dataset.num_aff\n\n # submodules\n if args.data_type == \"feature\":\n self.backbone = None\n self.feat_dim = dataset.feature_dim\n else:\n self.backbone, self.feat_dim = load_backbone(args.backbone_type, args.backbone_weight)\n\n # prior information\n prior_info = torch.load(f\"features/OCL_{args.backbone_type}/obj_prior.t7\")\n self.register_buffer(\"mean_obj_features\",\n prior_info[\"mean_obj_features\"] ) # (n_obj, dim)\n\n \n # preproc P(O)\n if args.obj_prior_type == \"default\":\n pass\n elif args.obj_prior_type == \"step\":\n sep = np.linspace(0, self.num_obj, args.obj_prior_bins, dtype=int).tolist()\n frequency = prior_info[\"freqency\"].numpy()\n order = frequency.argsort()\n for i,j in zip(sep[:-1], sep[1:]):\n ids = order[i:j]\n frequency[ids] = frequency[ids].mean()\n prior_info[\"freqency\"] = torch.from_numpy(frequency)\n else:\n raise NotImplementedError(args.obj_prior_type)\n\n self.register_buffer(\"obj_frequence\", \n prior_info[\"freqency\"] ) # (n_obj,)\n assert len(prior_info[\"freqency\"].size())==1\n \n\n CA = json.load(open('data/resources/OCL_category_annot.json'))\n self.register_buffer(\"category_attr\",\n torch.Tensor([ CA[o]['attr'] for o in dataset.objs ]).float() )\n self.register_buffer(\"category_aff\",\n torch.Tensor([ CA[o]['aff'] for o in dataset.objs ]).float() )\n\n print(f\"CA: attr={self.category_attr.shape}, aff={self.category_aff.shape}\")\n\n # loss weight\n if args.loss_class_weight:\n class_weight = json.load(open(\"data/resources/OCL_weight.json\"))\n self.register_buffer(\"obj_loss_wgt\", torch.tensor(class_weight[\"obj_weight\"]))\n self.register_buffer(\"attr_loss_wgt\", torch.tensor(class_weight[\"attr_weight\"]))\n self.register_buffer(\"aff_loss_wgt\", torch.tensor(class_weight[\"aff_weight\"]))\n else:\n self.obj_loss_wgt, self.attr_loss_wgt, self.aff_loss_wgt = None, None, None\n\n self.pos_weight_attr = None\n self.pos_weight_aff = None\n \n \n\n # losses\n if args.positive_bce:\n self.attr_bce = PositiveBCELoss(class_weight=self.attr_loss_wgt)\n self.aff_bce = PositiveBCELoss(class_weight=self.aff_loss_wgt)\n else:\n self.attr_bce = nn.BCEWithLogitsLoss(weight=self.attr_loss_wgt, pos_weight=self.pos_weight_attr)\n self.aff_bce = nn.BCEWithLogitsLoss(weight=self.aff_loss_wgt, pos_weight=self.pos_weight_aff)\n \n self.pair_prob_bce = nn.BCELoss()" }, { "identifier": "MLP", "path": "models/base_models.py", "snippet": "class MLP(nn.Module):\n \"\"\"Multi-layer perceptron, 1 layers as default. No activation after last fc\"\"\"\n\n def __init__(self, inp_dim, out_dim, hidden_layers=[], batchnorm=True, bias=True, out_relu=False, out_bn=False):\n super(MLP, self).__init__()\n\n inner_bias = bias and (not batchnorm)\n\n mod = []\n if hidden_layers is not None:\n last_dim = inp_dim\n for hid_dim in hidden_layers:\n mod.append(nn.Linear(last_dim, hid_dim, bias=inner_bias))\n if batchnorm:\n mod.append(nn.BatchNorm1d(hid_dim))\n mod.append(nn.ReLU(inplace=True))\n last_dim = hid_dim\n\n mod.append(nn.Linear(last_dim, out_dim, bias=bias))\n if out_bn:\n mod.append(nn.BatchNorm1d(out_dim))\n if out_relu:\n mod.append(nn.ReLU(inplace=True))\n\n self.mod = nn.Sequential(*mod)\n\n def forward(self, x):\n output = self.mod(x)\n return output" }, { "identifier": "ParallelMLP", "path": "models/base_models.py", "snippet": "class ParallelMLP(nn.Module):\n def __init__(self, inp_dim, out_dim, num_para, hidden_layers=[], layernorm=True, bias=True, share_last_fc=False, out_relu=False):\n super().__init__()\n inner_bias = bias\n\n mod = []\n if hidden_layers is not None:\n last_dim = inp_dim\n for hid_dim in hidden_layers:\n mod.append(ParallelLinear(last_dim, hid_dim, num_para, bias=inner_bias))\n\n if layernorm:\n mod.append(nn.LayerNorm(hid_dim))\n mod.append(nn.ReLU(inplace=True))\n last_dim = hid_dim\n\n if share_last_fc:\n mod.append(nn.Linear(last_dim, out_dim, bias=inner_bias))\n else:\n mod.append(ParallelLinear(last_dim, out_dim, num_para, bias=inner_bias))\n \n if out_relu:\n mod.append(nn.ReLU(inplace=True))\n\n self.mod = nn.Sequential(*mod)\n\n def forward(self, x):\n output = self.mod(x)\n return output" }, { "identifier": "Aggregator", "path": "models/base_models.py", "snippet": "class Aggregator(nn.Module):\n def __init__(self, method, args=None, num_para=None):\n super().__init__()\n self.support = ['sum', 'mean', 'max', 'concat']\n self.method = method\n\n if method not in self.support:\n raise NotImplementedError(\n 'Not supported aggregation method [%s].\\nWe only support: %s' % (method, self.support))\n\n if method == \"concat\":\n self.compression = nn.Linear(args.parallel_attr_rep_dim*num_para, args.aggr_rep_dim, bias=False)\n self.relu = nn.ReLU(inplace=True)\n\n if method == \"qkv\":\n raise NotImplementedError()\n\n def forward(self, tensor, mask=None, mask_method=\"zero\"):\n \"\"\"\n :param tensor: bz * n * dim\n :param mask: bz * n\n :return: bz * dim\n \"\"\"\n \n\n if mask is not None:\n if len(mask.size())==2:\n mask = mask.unsqueeze(-1)\n else:\n mask = mask.unsqueeze(-1).unsqueeze(0)\n\n if mask_method == \"zero\":\n tensor = tensor * mask\n elif mask_method == \"random\":\n rdm = torch.randn_like(tensor).to(tensor.device)\n tensor = torch.where(mask.expand_as(tensor), tensor, rdm)\n else:\n raise NotImplementedError(mask_method)\n\n if self.method == 'sum':\n return tensor.sum(1)\n elif self.method == 'mean':\n return tensor.mean(1)\n elif self.method == 'max':\n return tensor.max(1).values\n elif self.method == 'concat':\n out = tensor.reshape(tensor.shape[0], -1)\n out = self.compression(out)\n out = self.relu(out)\n return out" }, { "identifier": "build_counterfactual", "path": "models/base_models.py", "snippet": "def build_counterfactual(causal, num_attr, num_aff):\n '''\n :param causal: [ N, 3 ] (inst_id, attr_id, aff_id)\n :param num_attr:\n :param num_aff:\n :return:\n counterfactual_inst_id : tensor [ M ] index of instance in batch\n counterfactual_attr_mask: tensor [ M, num_attr ] which attr to be skipped\n counterfactual_aff_mask: tensor [ M, num_aff ] which aff will be affected after counterfactual\n '''\n orig_size = causal.shape[0]\n unique_inst_att_pair = torch.unique(causal[:, :2], dim=0)\n reduce_size = unique_inst_att_pair.shape[0]\n counterfactual_inst_id = unique_inst_att_pair[:, 0]\n counterfactual_attr_mask = onehot(unique_inst_att_pair[:, 1], num_attr, causal.device)\n space_mapping = torch.all(\n causal[:, :2].unsqueeze(0).expand(reduce_size, orig_size, 2) == \\\n unique_inst_att_pair[:, :2].unsqueeze(1).expand(reduce_size, orig_size, 2),\n dim=2\n ).float()\n counterfactual_aff_mask = torch.matmul(space_mapping, onehot(causal[:, 2], num_aff, causal.device))\n\n return counterfactual_inst_id, counterfactual_attr_mask, counterfactual_aff_mask" }, { "identifier": "CounterfactualHingeLoss", "path": "models/base_models.py", "snippet": "class CounterfactualHingeLoss(nn.Module):\n def __init__(self, margin=0.1):\n super().__init__()\n self.margin = margin\n\n def forward(self, cf_prob, orig_prob, gt_label, cf_label_mask):\n loss = torch.where(\n gt_label == 1,\n cf_prob - (orig_prob - self.margin),\n (orig_prob + self.margin) - cf_prob\n )\n # loss[loss < 0] = 0\n loss = nn.functional.relu(loss, inplace=True)\n\n loss = loss * cf_label_mask\n loss = loss.mean(0).sum()\n return loss" } ]
from typing import final from models.base_models import OcrnBaseModel, MLP, ParallelMLP, Aggregator, build_counterfactual, CounterfactualHingeLoss import torch import torch.nn as nn import math
3,346
@final class FullSelfAttention(nn.Module): def __init__(self, feat_dim, cond_dim, hidden_dim, args): """ output = f(input, condition) in_dim/cond_dim/out_dim = dimension of input/condition/output fc_in_hid/fc_cond_hid = hidden layers of fc after input/condition fc_out_hid = hidden layers of fc before output """ super(FullSelfAttention, self).__init__() fc_in_hid = args.fc_pre fc_cond_hid = args.fc_att fc_out_hid = args.fc_compress self.fc_feat_Q = MLP(feat_dim, hidden_dim, fc_in_hid, args.batchnorm, bias=False) self.fc_feat_V = MLP(feat_dim, hidden_dim, fc_in_hid, args.batchnorm, bias=False) self.fc_feat_K = MLP(feat_dim, hidden_dim, fc_in_hid, args.batchnorm, bias=False) self.fc_cond_Q = MLP(cond_dim, hidden_dim, fc_cond_hid, args.batchnorm, bias=False) self.fc_cond_V = MLP(cond_dim, hidden_dim, fc_cond_hid, args.batchnorm, bias=False) self.fc_cond_K = MLP(cond_dim, hidden_dim, fc_cond_hid, args.batchnorm, bias=False) self.rtemp = 1.0/math.sqrt(hidden_dim) self.fc_out = MLP(2*hidden_dim, feat_dim, fc_out_hid, args.batchnorm, out_relu=args.out_relu) def forward(self, feat, cond, in_postproc=lambda x:x, cond_postproc=lambda x:x): feat_Q = in_postproc( self.fc_feat_Q(feat) ) # (bz*obj, hid_dim) feat_V = in_postproc( self.fc_feat_V(feat) ) feat_K = in_postproc( self.fc_feat_K(feat) ) cond_Q = cond_postproc( self.fc_cond_Q(cond) ) cond_V = cond_postproc( self.fc_cond_V(cond) ) cond_K = cond_postproc( self.fc_cond_K(cond) ) K_diff = (feat_K - cond_K) * self.rtemp KQ_ff_fc = (feat_Q * K_diff).sum(-1) # (bz*obj, ) KQ_cf_cc = (cond_Q * K_diff).sum(-1) feat_att_f = torch.sigmoid(KQ_ff_fc).unsqueeze(-1) cond_att_f = torch.sigmoid(KQ_cf_cc).unsqueeze(-1) V_diff = (feat_V - cond_V) hid_feat = V_diff*feat_att_f + cond_V hid_cond = V_diff*cond_att_f + cond_V hidden = torch.cat([hid_feat, hid_cond], -1) out = self.fc_out(hidden) return out # @final class Model(OcrnBaseModel): def __init__(self, dataset, args): super(Model, self).__init__(dataset, args) # model param self.fc_feat2attr = MLP(self.feat_dim, args.attr_rep_dim, args.fc_feat2attr, args.batchnorm, out_relu=args.out_relu, out_bn=args.batchnorm) self.fc_feat2aff = MLP(self.feat_dim + args.attr_rep_dim, args.aff_rep_dim, args.fc_feat2aff, args.batchnorm, out_relu=args.out_relu, out_bn=args.batchnorm) self.attr_instantialize = FullSelfAttention(args.attr_rep_dim, self.feat_dim, args.attr_hidden_rep_dim, args=args) self.aff_instantialize = FullSelfAttention(args.aff_rep_dim, self.feat_dim + args.aggr_rep_dim, args.aff_hidden_rep_dim, args=args) self.aggregator = Aggregator(self.args.aggregation, args, self.num_attr) self.parallel_attr_feat = ParallelMLP( args.attr_out_rep_dim, args.parallel_attr_rep_dim, num_para=self.num_attr, hidden_layers=args.fc_para_feat, layernorm=args.layernorm, out_relu=args.out_relu) self.attr_auxIA_classifier = ParallelMLP(args.parallel_attr_rep_dim, 1, num_para=self.num_attr, hidden_layers=args.fc_cls, layernorm=args.layernorm, share_last_fc=True) self.attr_IA_classifier = MLP(args.attr_rep_dim, self.num_attr, hidden_layers=args.fc_cls, batchnorm=args.batchnorm) self.aff_IA_classifier = MLP(args.aff_rep_dim, self.num_aff, hidden_layers=args.fc_cls, batchnorm=args.batchnorm) assert args.sep_CA_cls self.attr_CA_classifier = MLP(args.attr_rep_dim, self.num_attr, hidden_layers=args.fc_cls, batchnorm=args.batchnorm) self.aff_CA_classifier = MLP(args.aff_rep_dim, self.num_aff, hidden_layers=args.fc_cls, batchnorm=args.batchnorm) self.mseloss = torch.nn.MSELoss()
@final class FullSelfAttention(nn.Module): def __init__(self, feat_dim, cond_dim, hidden_dim, args): """ output = f(input, condition) in_dim/cond_dim/out_dim = dimension of input/condition/output fc_in_hid/fc_cond_hid = hidden layers of fc after input/condition fc_out_hid = hidden layers of fc before output """ super(FullSelfAttention, self).__init__() fc_in_hid = args.fc_pre fc_cond_hid = args.fc_att fc_out_hid = args.fc_compress self.fc_feat_Q = MLP(feat_dim, hidden_dim, fc_in_hid, args.batchnorm, bias=False) self.fc_feat_V = MLP(feat_dim, hidden_dim, fc_in_hid, args.batchnorm, bias=False) self.fc_feat_K = MLP(feat_dim, hidden_dim, fc_in_hid, args.batchnorm, bias=False) self.fc_cond_Q = MLP(cond_dim, hidden_dim, fc_cond_hid, args.batchnorm, bias=False) self.fc_cond_V = MLP(cond_dim, hidden_dim, fc_cond_hid, args.batchnorm, bias=False) self.fc_cond_K = MLP(cond_dim, hidden_dim, fc_cond_hid, args.batchnorm, bias=False) self.rtemp = 1.0/math.sqrt(hidden_dim) self.fc_out = MLP(2*hidden_dim, feat_dim, fc_out_hid, args.batchnorm, out_relu=args.out_relu) def forward(self, feat, cond, in_postproc=lambda x:x, cond_postproc=lambda x:x): feat_Q = in_postproc( self.fc_feat_Q(feat) ) # (bz*obj, hid_dim) feat_V = in_postproc( self.fc_feat_V(feat) ) feat_K = in_postproc( self.fc_feat_K(feat) ) cond_Q = cond_postproc( self.fc_cond_Q(cond) ) cond_V = cond_postproc( self.fc_cond_V(cond) ) cond_K = cond_postproc( self.fc_cond_K(cond) ) K_diff = (feat_K - cond_K) * self.rtemp KQ_ff_fc = (feat_Q * K_diff).sum(-1) # (bz*obj, ) KQ_cf_cc = (cond_Q * K_diff).sum(-1) feat_att_f = torch.sigmoid(KQ_ff_fc).unsqueeze(-1) cond_att_f = torch.sigmoid(KQ_cf_cc).unsqueeze(-1) V_diff = (feat_V - cond_V) hid_feat = V_diff*feat_att_f + cond_V hid_cond = V_diff*cond_att_f + cond_V hidden = torch.cat([hid_feat, hid_cond], -1) out = self.fc_out(hidden) return out # @final class Model(OcrnBaseModel): def __init__(self, dataset, args): super(Model, self).__init__(dataset, args) # model param self.fc_feat2attr = MLP(self.feat_dim, args.attr_rep_dim, args.fc_feat2attr, args.batchnorm, out_relu=args.out_relu, out_bn=args.batchnorm) self.fc_feat2aff = MLP(self.feat_dim + args.attr_rep_dim, args.aff_rep_dim, args.fc_feat2aff, args.batchnorm, out_relu=args.out_relu, out_bn=args.batchnorm) self.attr_instantialize = FullSelfAttention(args.attr_rep_dim, self.feat_dim, args.attr_hidden_rep_dim, args=args) self.aff_instantialize = FullSelfAttention(args.aff_rep_dim, self.feat_dim + args.aggr_rep_dim, args.aff_hidden_rep_dim, args=args) self.aggregator = Aggregator(self.args.aggregation, args, self.num_attr) self.parallel_attr_feat = ParallelMLP( args.attr_out_rep_dim, args.parallel_attr_rep_dim, num_para=self.num_attr, hidden_layers=args.fc_para_feat, layernorm=args.layernorm, out_relu=args.out_relu) self.attr_auxIA_classifier = ParallelMLP(args.parallel_attr_rep_dim, 1, num_para=self.num_attr, hidden_layers=args.fc_cls, layernorm=args.layernorm, share_last_fc=True) self.attr_IA_classifier = MLP(args.attr_rep_dim, self.num_attr, hidden_layers=args.fc_cls, batchnorm=args.batchnorm) self.aff_IA_classifier = MLP(args.aff_rep_dim, self.num_aff, hidden_layers=args.fc_cls, batchnorm=args.batchnorm) assert args.sep_CA_cls self.attr_CA_classifier = MLP(args.attr_rep_dim, self.num_attr, hidden_layers=args.fc_cls, batchnorm=args.batchnorm) self.aff_CA_classifier = MLP(args.aff_rep_dim, self.num_aff, hidden_layers=args.fc_cls, batchnorm=args.batchnorm) self.mseloss = torch.nn.MSELoss()
self.hinge = CounterfactualHingeLoss(args.counterfactual_margin)
5
2023-11-07 13:03:27+00:00
4k
tianhaowuhz/human-assisting-dex-grasp
Algorithms/ppo/module.py
[ { "identifier": "Pointnet2Backbone", "path": "Networks/pointnet2/pointnet2_backbone.py", "snippet": "class Pointnet2Backbone(nn.Module):\n r\"\"\"\n Backbone network for point cloud feature learning.\n Based on Pointnet++ single-scale grouping network. \n \n Parameters\n ----------\n input_feature_dim: int\n Number of input channels in the feature descriptor for each point.\n e.g. 3 for RGB.\n \"\"\"\n def __init__(self, input_feature_dim=0, feature_dim_coff=1):\n super().__init__()\n\n self.input_feature_dim = input_feature_dim\n\n self.sa1 = PointnetSAModule(\n npoint=512,\n radius=0.04,\n nsample=32,\n mlp=[input_feature_dim, 64*feature_dim_coff, 64*feature_dim_coff, 128*feature_dim_coff],\n use_xyz=True,\n )\n\n self.sa2 = PointnetSAModule(\n npoint=256,\n radius=0.1,\n nsample=16,\n mlp=[128*feature_dim_coff, 128*feature_dim_coff, 128*feature_dim_coff, 256*feature_dim_coff],\n use_xyz=True,\n )\n\n self.sa3 = PointnetSAModule(\n npoint=None,\n radius=None,\n nsample=None,\n mlp=[256*feature_dim_coff, 256*feature_dim_coff, 512*feature_dim_coff, 1024*feature_dim_coff],\n use_xyz=True,\n )\n\n def _break_up_pc(self, pc):\n xyz = pc[..., 0:3].contiguous()\n features = (\n pc[..., 3:].transpose(1, 2).contiguous()\n if pc.size(-1) > 3 else None\n )\n\n return xyz, features\n\n def forward(self, pointcloud: torch.cuda.FloatTensor, end_points=None):\n r\"\"\"\n Forward pass of the network\n\n Parameters\n ----------\n pointcloud: Variable(torch.cuda.FloatTensor)\n (B, N, 3 + input_feature_dim) tensor\n Point cloud to run predicts on\n Each point in the point-cloud MUST\n be formated as (x, y, z, features...)\n\n Returns\n ----------\n end_points: {XXX_xyz, XXX_features, XXX_inds}\n XXX_xyz: float32 Tensor of shape (B,K,3)\n XXX_features: float32 Tensor of shape (B,D,K)\n XXX_inds: int64 Tensor of shape (B,K) values in [0,N-1]\n \"\"\"\n xyz, features = self._break_up_pc(pointcloud)\n\n # --------- 3 SET ABSTRACTION LAYERS ---------\n xyz, features = self.sa1(xyz, features)\n\n xyz, features = self.sa2(xyz, features) \n\n xyz, features = self.sa3(xyz, features) \n\n return features, xyz" }, { "identifier": "PointNetEncoder", "path": "Networks/pointnet.py", "snippet": "class PointNetEncoder(nn.Module):\n def __init__(self, num_points=1024, global_feat=True, in_dim=3, out_dim=1024, feature_transform=False, **args):\n super(PointNetEncoder, self).__init__()\n self.num_points = num_points\n self.out_dim = out_dim\n self.feature_transform = feature_transform\n # self.stn = STN3d(in_dim=in_dim)\n self.stn = STNkd(k=in_dim)\n self.conv1 = torch.nn.Conv1d(in_dim, 64, 1)\n self.conv2 = torch.nn.Conv1d(64, 128, 1)\n self.conv3 = torch.nn.Conv1d(128, 512, 1)\n self.conv4 = torch.nn.Conv1d(512, out_dim, 1)\n self.global_feat = global_feat\n if self.feature_transform:\n self.fstn = STNkd(k=64)\n\n def forward(self, x, **args):\n n_pts = x.shape[2]\n trans = self.stn(x)\n x = x.transpose(2, 1)\n x = torch.bmm(x, trans)\n x = x.transpose(2, 1)\n x = F.relu(self.conv1(x))\n\n if self.feature_transform:\n trans_feat = self.fstn(x)\n x = x.transpose(2, 1)\n x = torch.bmm(x, trans_feat)\n x = x.transpose(2, 1)\n\n pointfeat = x\n x = F.relu(self.conv2(x))\n x = F.relu(self.conv3(x))\n x = self.conv4(x)\n x = torch.max(x, 2, keepdim=True)[0]\n x = x.view(-1, self.out_dim)\n if self.global_feat:\n return x, 0, 0\n else:\n x = x.view(-1, self.out_dim, 1).repeat(1, 1, n_pts)\n return torch.cat([x, pointfeat], 1)" } ]
import numpy as np import torch import torch.nn as nn from torch.distributions import MultivariateNormal from Networks.pointnet2.pointnet2_backbone import Pointnet2Backbone from Networks.pointnet import PointNetEncoder from ipdb import set_trace
3,048
activation, nn.Linear(actor_hidden_dim, actor_hidden_dim), activation, nn.Linear(actor_hidden_dim, actor_hidden_dim), activation, ) # pointcloud feature encoder self.actor_obj_global_enc = nn.Sequential( nn.Linear(self.points_per_object, actor_hidden_dim), activation, ) # mlp output if self.disentangle_hand: if 'gf' in self.sub_obs_type: total_feat_num = 2 + 1 + 1 else: total_feat_num = 2 + 1 else: total_feat_num = 1 + 1 if self.disentangle_hand: self.actor_mlp1 = nn.Sequential( nn.Linear(actor_hidden_dim*total_feat_num, actor_hidden_dim), activation, ) else: self.actor_mlp1 = nn.Sequential( nn.Linear(actor_hidden_dim*total_feat_num, actor_hidden_dim), activation, nn.Linear(actor_hidden_dim, actor_hidden_dim), activation, ) # norm output action if self.norm_action: self.actor_mlp2 = nn.Sequential( nn.Linear(actor_hidden_dim, *actions_shape), get_activation("tanh"), ) else: self.actor_mlp2 = nn.Sequential( nn.Linear(actor_hidden_dim, *actions_shape), ) ''' critic layer ''' # state encoder if self.disentangle_hand: self.critic_hand_joint_global_enc = nn.Sequential( nn.Linear(self.hand_joint_dim + self.fingertip_dim + self.objpose_dim + self.diso2o_dim + self.goal_dim, critic_hidden_dim), activation, ) self.critic_hand_wrist_global_enc = nn.Sequential( nn.Linear(self.hand_wrist_dim, critic_hidden_dim), activation, ) if 'gf' in self.sub_obs_type: self.critic_grad_enc = nn.Sequential( nn.Linear(*actions_shape, critic_hidden_dim), activation, ) else: self.state_dim = self.hand_joint_dim + self.hand_wrist_dim + self.fingertip_dim + self.objpose_dim + self.diso2o_dim + self.goal_dim + self.gf_dim self.critic_hand_global_enc = nn.Sequential( nn.Linear(self.state_dim, critic_hidden_dim), activation, nn.Linear(critic_hidden_dim, critic_hidden_dim), activation, nn.Linear(critic_hidden_dim, critic_hidden_dim), activation, ) # pointcloud feature encoder self.critic_obj_global_enc = nn.Sequential( nn.Linear(self.points_per_object, critic_hidden_dim), activation, ) # mlp output if self.disentangle_hand: self.critic_mlp1 = nn.Sequential( nn.Linear(critic_hidden_dim*total_feat_num, critic_hidden_dim), activation, ) if args.exp_name == 'ilad': self.additional_critic_mlp1 = nn.Sequential( nn.Linear(critic_hidden_dim*total_feat_num + self.action_dim, critic_hidden_dim), activation, ) else: self.critic_mlp1 = nn.Sequential( nn.Linear(critic_hidden_dim*total_feat_num, critic_hidden_dim), activation, nn.Linear(critic_hidden_dim, critic_hidden_dim), activation, ) if args.exp_name == 'ilad': self.additional_critic_mlp1 = nn.Sequential( nn.Linear(critic_hidden_dim*total_feat_num + self.action_dim, critic_hidden_dim), activation, nn.Linear(critic_hidden_dim, 1), ) self.critic_mlp2 = nn.Sequential( nn.Linear(critic_hidden_dim, 1), ) ''' shared layer ''' if self.shared_pointnet: if self.pointnet_type == 'pt': self.pointnet_enc = PointNetEncoder() elif self.pointnet_type == 'pt2':
local = False class ActorCritic(nn.Module): def __init__(self, obs_shape, states_shape, actions_shape, initial_std, model_cfg, asymmetric=False, state_base=False, stack_frame_number=3, sub_obs_type=None, num_fingertip=None, pointnet_type='pt2', envs=None, hand_pcl=False, hand_model=None, args=None): super(ActorCritic, self).__init__() # network parameter self.asymmetric = asymmetric self.state_base = state_base self.stack_frame_number = stack_frame_number self.sub_obs_type = sub_obs_type self.num_fingertip = num_fingertip self.disentangle_hand = model_cfg['distengle'] self.norm_action = model_cfg['norm_action'] self.action_scale = model_cfg['action_scale'] self.pointnet_type = pointnet_type self.envs = envs self.hand_pcl = hand_pcl self.hand_model = hand_model ''' init network: current we set self.state_base = False, only set true for pure state input ''' if not self.state_base: # get model cfg if model_cfg is None: self.hand_joint_dim = 18 self.hand_wrist_dim = 7 * self.stack_frame_number actor_hidden_dim = 256 critic_hidden_dim = 256 activation = get_activation("selu") self.shared_pointnet = True self.points_per_object = 1024 else: # get input dim self.hand_joint_dim = model_cfg['hand_joint_dim'] self.hand_wrist_dim = model_cfg['hand_wrist_dim'] * self.stack_frame_number # fingertip obs dim if "fingertipjoint" in self.sub_obs_type: self.fingertip_dim = self.num_fingertip-1 else: self.fingertip_dim = 0 if "disfingertip" in self.sub_obs_type: self.fingertip_dim += self.num_fingertip*1 elif "absfingertip" in self.sub_obs_type: self.fingertip_dim += self.num_fingertip*3 # obj pose obs dim if "objpose" in self.sub_obs_type: self.objpose_dim = 7 else: self.objpose_dim = 0 # diso2o obs dim if "diso2o" in self.sub_obs_type: self.diso2o_dim = 1 else: self.diso2o_dim = 0 # goal obs dim if "goal" in self.sub_obs_type: self.goal_dim = 18 else: self.goal_dim = 0 # gf obs dim if 'gf' in self.sub_obs_type: self.gf_dim = actions_shape[0] else: self.gf_dim = 0 # network parameter actor_hidden_dim = model_cfg['pi_hid_sizes'] critic_hidden_dim = model_cfg['vf_hid_sizes'] activation = get_activation(model_cfg['activation']) self.shared_pointnet = model_cfg['shared_pointnet'] self.points_per_object = model_cfg['points_per_object'] self.action_dim = actions_shape[0] ''' actor layer ''' # state encoder if self.disentangle_hand: self.actor_hand_joint_global_enc = nn.Sequential( nn.Linear(self.hand_joint_dim + self.fingertip_dim + self.objpose_dim + self.diso2o_dim + self.goal_dim, actor_hidden_dim), activation, ) self.actor_hand_wrist_global_enc = nn.Sequential( nn.Linear(self.hand_wrist_dim, actor_hidden_dim), activation, ) if 'gf' in self.sub_obs_type: self.actor_grad_enc = nn.Sequential( nn.Linear(*actions_shape, actor_hidden_dim), activation, ) else: self.state_dim = self.hand_joint_dim + self.hand_wrist_dim + self.fingertip_dim + self.objpose_dim + self.diso2o_dim + self.goal_dim + self.gf_dim self.actor_hand_global_enc = nn.Sequential( nn.Linear(self.state_dim, actor_hidden_dim), activation, nn.Linear(actor_hidden_dim, actor_hidden_dim), activation, nn.Linear(actor_hidden_dim, actor_hidden_dim), activation, ) # pointcloud feature encoder self.actor_obj_global_enc = nn.Sequential( nn.Linear(self.points_per_object, actor_hidden_dim), activation, ) # mlp output if self.disentangle_hand: if 'gf' in self.sub_obs_type: total_feat_num = 2 + 1 + 1 else: total_feat_num = 2 + 1 else: total_feat_num = 1 + 1 if self.disentangle_hand: self.actor_mlp1 = nn.Sequential( nn.Linear(actor_hidden_dim*total_feat_num, actor_hidden_dim), activation, ) else: self.actor_mlp1 = nn.Sequential( nn.Linear(actor_hidden_dim*total_feat_num, actor_hidden_dim), activation, nn.Linear(actor_hidden_dim, actor_hidden_dim), activation, ) # norm output action if self.norm_action: self.actor_mlp2 = nn.Sequential( nn.Linear(actor_hidden_dim, *actions_shape), get_activation("tanh"), ) else: self.actor_mlp2 = nn.Sequential( nn.Linear(actor_hidden_dim, *actions_shape), ) ''' critic layer ''' # state encoder if self.disentangle_hand: self.critic_hand_joint_global_enc = nn.Sequential( nn.Linear(self.hand_joint_dim + self.fingertip_dim + self.objpose_dim + self.diso2o_dim + self.goal_dim, critic_hidden_dim), activation, ) self.critic_hand_wrist_global_enc = nn.Sequential( nn.Linear(self.hand_wrist_dim, critic_hidden_dim), activation, ) if 'gf' in self.sub_obs_type: self.critic_grad_enc = nn.Sequential( nn.Linear(*actions_shape, critic_hidden_dim), activation, ) else: self.state_dim = self.hand_joint_dim + self.hand_wrist_dim + self.fingertip_dim + self.objpose_dim + self.diso2o_dim + self.goal_dim + self.gf_dim self.critic_hand_global_enc = nn.Sequential( nn.Linear(self.state_dim, critic_hidden_dim), activation, nn.Linear(critic_hidden_dim, critic_hidden_dim), activation, nn.Linear(critic_hidden_dim, critic_hidden_dim), activation, ) # pointcloud feature encoder self.critic_obj_global_enc = nn.Sequential( nn.Linear(self.points_per_object, critic_hidden_dim), activation, ) # mlp output if self.disentangle_hand: self.critic_mlp1 = nn.Sequential( nn.Linear(critic_hidden_dim*total_feat_num, critic_hidden_dim), activation, ) if args.exp_name == 'ilad': self.additional_critic_mlp1 = nn.Sequential( nn.Linear(critic_hidden_dim*total_feat_num + self.action_dim, critic_hidden_dim), activation, ) else: self.critic_mlp1 = nn.Sequential( nn.Linear(critic_hidden_dim*total_feat_num, critic_hidden_dim), activation, nn.Linear(critic_hidden_dim, critic_hidden_dim), activation, ) if args.exp_name == 'ilad': self.additional_critic_mlp1 = nn.Sequential( nn.Linear(critic_hidden_dim*total_feat_num + self.action_dim, critic_hidden_dim), activation, nn.Linear(critic_hidden_dim, 1), ) self.critic_mlp2 = nn.Sequential( nn.Linear(critic_hidden_dim, 1), ) ''' shared layer ''' if self.shared_pointnet: if self.pointnet_type == 'pt': self.pointnet_enc = PointNetEncoder() elif self.pointnet_type == 'pt2':
self.pointnet_enc = Pointnet2Backbone() # for pointnet2
0
2023-11-09 06:08:40+00:00
4k
DaveParr/starpilot
tests/test_utils.py
[ { "identifier": "get_repo_contents", "path": "starpilot/utils/utils.py", "snippet": "def get_repo_contents(\n repos: List[Repository], g: Github, include_readmes: bool = False\n) -> List[Dict]:\n repo_infos = []\n for repo in track(repos, description=\"Reading the stars...\"):\n repo_info = {}\n repo_slug = repo.full_name\n repo_info[\"id\"] = repo_slug\n repo_info[\"name\"] = repo.name\n repo_info[\"url\"] = repo.html_url\n\n if (owner := repo.owner.name) is not None:\n repo_info[\"owner\"] = owner\n\n if (repo.organization) is not None:\n if (organization := repo.organization.name) is not None:\n repo_info[\"organization\"] = organization\n else:\n logger.info(\"No organization name\", repo=repo_slug)\n\n # get the repo languages\n repo_info[\"languages\"] = []\n for language in repo.get_languages():\n repo_info[\"languages\"].append(language)\n\n if len(repo_info[\"languages\"]) == 0:\n logger.info(\"No languages\", repo=repo_slug)\n\n if (description := repo.description) is not None:\n repo_info[\"description\"] = description\n else:\n logger.info(\"No description\", repo=repo_slug)\n\n if not (topics := repo.get_topics()) == []:\n repo_info[\"topics\"] = topics\n\n if include_readmes:\n repo_info[\"readme\"] = {}\n try:\n readme = repo.get_contents(\"README.md\")\n repo_info[\"readme\"][\"type\"] = \"md\"\n repo_info[\"readme\"][\"content\"] = readme.decoded_content.decode(\"utf-8\")\n except UnknownObjectException:\n try:\n readme = repo.get_contents(\"README.rst\")\n repo_info[\"readme\"][\"type\"] = \"rst\"\n repo_info[\"readme\"][\"content\"] = readme.decoded_content.decode(\n \"utf-8\"\n )\n except UnknownObjectException:\n continue\n\n repo_info[\"vectorstore_document\"] = []\n\n # use description as main content, and include topics and languages only if present\n\n if repo_info.get(\"description\"):\n content = {\n \"name\": repo_info.get(\"name\"),\n \"description\": repo_info.get(\"description\"),\n }\n if repo_info.get(\"topics\"):\n content[\"topics\"] = repo_info.get(\"topics\")\n if repo_info.get(\"languages\"):\n content[\"languages\"] = repo_info.get(\"languages\")\n\n repo_info[\"vectorstore_document\"].append(\n {\n # use description as content, and topics and languages if present\n \"content\": content,\n \"url\": repo_info.get(\"url\"),\n \"description\": repo_info.get(\"description\"),\n \"name\": repo_info.get(\"name\"),\n \"topics\": repo_info.get(\"topics\"),\n \"languages\": repo_info.get(\"languages\"),\n }\n )\n\n repo_infos.append(repo_info)\n logger.debug(\"Using repo\", repo=repo_slug)\n else:\n logger.warning(\"Repo has no relevant information to use\", repo=repo_slug)\n\n return repo_infos" }, { "identifier": "get_user_starred_repos", "path": "starpilot/utils/utils.py", "snippet": "def get_user_starred_repos(\n user: str, g: Github, num_repos: Optional[int] = None\n) -> List[Repository]:\n \"\"\"\n Get the starred repos for a user\n\n If there is no github api key set, this will start to work, but will be rapidly rate limited.\n \"\"\"\n starred_repos = []\n for repo in track(\n g.get_user(user).get_starred(), description=\"Spotting the stars...\"\n ):\n starred_repos.append(repo)\n\n # IDEA: there could be a threshold for star count below which repos are removed\n starred_repos.sort(key=lambda repo: repo.stargazers_count, reverse=True)\n\n if num_repos is not None:\n starred_repos = starred_repos[:num_repos]\n\n return starred_repos" } ]
from unittest.mock import Mock from starpilot.utils.utils import get_repo_contents, get_user_starred_repos import pytest import os import github
1,699
def test_get_user_starred_repos_mocked(): # Mock the necessary objects class MockRepo: def __init__(self, stargazers_count): self.stargazers_count = stargazers_count class MockUser: def get_starred(self): return [MockRepo(10), MockRepo(5), MockRepo(8), MockRepo(3), MockRepo(7)] class MockGithub: def get_user(self, user): return MockUser() # Call the function under test result = get_user_starred_repos("testuser", MockGithub(), num_repos=3) # Assert the expected result assert len(result) == 3 assert result[0].stargazers_count == 10 assert result[1].stargazers_count == 8 assert result[2].stargazers_count == 7 @pytest.mark.vcr() def test_get_user_starred_repos_vcr(): github_client = github.Github(os.getenv("GITHUB_API_KEY")) result = get_user_starred_repos("DaveParr", github_client, num_repos=3) assert len(result) == 3 assert isinstance(result[0], github.Repository.Repository) def test_get_repo_contents_with_readmes(): # Mock the necessary objects class MockRepo: def __init__( self, full_name, name, html_url, owner, organization, description, topics, languages, ): self.full_name = full_name self.name = name self.html_url = html_url self.owner = owner self.organization = organization self.description = description self.topics = topics self.languages = languages def get_languages(self): return self.languages def get_topics(self): return self.topics def get_contents(self, path): if path == "README.md": return Mock(decoded_content=b"Mock README content") elif path == "README.rst": return Mock(decoded_content=b"Mock README content") else: raise UnknownObjectException class MockGithub: def __init__(self, repos): self.repos = repos def get_repo(self, full_name): for repo in self.repos: if repo.full_name == full_name: return repo # Create mock repositories repos = [ MockRepo( "user/repo1", "repo1", "https://github.com/user/repo1", Mock(name="owner"), Mock(name="organization"), "Repo 1 description", ["topic1", "topic2"], ["Python", "JavaScript"], ), MockRepo( "user/repo2", "repo2", "https://github.com/user/repo2", Mock(name="owner"), None, "Repo 2 description", [], ["Python"], ), MockRepo( "user/repo3", "repo3", "https://github.com/user/repo3", Mock(name="owner"), Mock(name="organization"), None, ["topic1"], [], ), ] # Mock the Github client github_client = MockGithub(repos) # Call the function under test
def test_get_user_starred_repos_mocked(): # Mock the necessary objects class MockRepo: def __init__(self, stargazers_count): self.stargazers_count = stargazers_count class MockUser: def get_starred(self): return [MockRepo(10), MockRepo(5), MockRepo(8), MockRepo(3), MockRepo(7)] class MockGithub: def get_user(self, user): return MockUser() # Call the function under test result = get_user_starred_repos("testuser", MockGithub(), num_repos=3) # Assert the expected result assert len(result) == 3 assert result[0].stargazers_count == 10 assert result[1].stargazers_count == 8 assert result[2].stargazers_count == 7 @pytest.mark.vcr() def test_get_user_starred_repos_vcr(): github_client = github.Github(os.getenv("GITHUB_API_KEY")) result = get_user_starred_repos("DaveParr", github_client, num_repos=3) assert len(result) == 3 assert isinstance(result[0], github.Repository.Repository) def test_get_repo_contents_with_readmes(): # Mock the necessary objects class MockRepo: def __init__( self, full_name, name, html_url, owner, organization, description, topics, languages, ): self.full_name = full_name self.name = name self.html_url = html_url self.owner = owner self.organization = organization self.description = description self.topics = topics self.languages = languages def get_languages(self): return self.languages def get_topics(self): return self.topics def get_contents(self, path): if path == "README.md": return Mock(decoded_content=b"Mock README content") elif path == "README.rst": return Mock(decoded_content=b"Mock README content") else: raise UnknownObjectException class MockGithub: def __init__(self, repos): self.repos = repos def get_repo(self, full_name): for repo in self.repos: if repo.full_name == full_name: return repo # Create mock repositories repos = [ MockRepo( "user/repo1", "repo1", "https://github.com/user/repo1", Mock(name="owner"), Mock(name="organization"), "Repo 1 description", ["topic1", "topic2"], ["Python", "JavaScript"], ), MockRepo( "user/repo2", "repo2", "https://github.com/user/repo2", Mock(name="owner"), None, "Repo 2 description", [], ["Python"], ), MockRepo( "user/repo3", "repo3", "https://github.com/user/repo3", Mock(name="owner"), Mock(name="organization"), None, ["topic1"], [], ), ] # Mock the Github client github_client = MockGithub(repos) # Call the function under test
result = get_repo_contents(repos, github_client, include_readmes=True)
0
2023-11-07 20:03:08+00:00
4k
Josephrp/LablabAutogen
app.py
[ { "identifier": "BingPlugin", "path": "plugins/sk_bing_plugin.py", "snippet": "class BingPlugin:\n \"\"\"\n A plugin to search Bing.\n \"\"\"\n\n def __init__(self, bing_api_key: str):\n self.bing = BingConnector(api_key=bing_api_key)\n if not bing_api_key or bing_api_key == \"...\":\n raise Exception(\"Bing API key is not set\")\n\n @sk_function(\n description=\"Use Bing to find a page about a topic. The return is a URL of the page found.\",\n name=\"find_web_page_about\",\n input_description=\"Two comma separated values: #1 Offset from the first result (default zero), #2 The topic to search, e.g. '0,who won the F1 title in 2023?'.\",\n )\n async def find_web_page_about(self, input: str) -> str:\n \"\"\"\n A native function that uses Bing to find a page URL about a topic.\n To simplify the integration with Autogen, the input parameter is a string with two comma separated\n values, rather than the usual context dictionary.\n \"\"\"\n\n # Input validation, the error message can help self-correct the input\n if \",\" not in input:\n raise ValueError(\"The input argument must contain a comma, e.g. '0,who won the F1 title in 2023?'\")\n\n parts = input.split(\",\", 1)\n result = await self.bing.search_url_async(query=parts[1], num_results=1, offset=parts[0])\n if result:\n return result[0]\n else:\n return f\"Nothing found, try again or try to adjust the topic.\"" }, { "identifier": "WebPagesPlugin", "path": "plugins/sk_web_pages_plugin.py", "snippet": "class WebPagesPlugin:\n \"\"\"\n A plugin to interact with web pages, e.g. download the text content of a page.\n \"\"\"\n\n @sk_function(\n description=\"Fetch the text content of a webpage. The return is a string containing all the text.\",\n name=\"fetch_webpage\",\n input_description=\"URL of the page to fetch.\",\n )\n async def fetch_webpage(self, input: str) -> str:\n \"\"\"\n A native function that fetches the text content of a webpage.\n HTML tags are removed, and empty lines are compacted.\n \"\"\"\n if not input:\n raise ValueError(\"url cannot be `None` or empty\")\n async with aiohttp.ClientSession() as session:\n async with session.get(input, raise_for_status=True) as response:\n html = await response.text()\n soup = BeautifulSoup(html, features=\"html.parser\")\n # remove some elements\n for el in soup([\"script\", \"style\", \"iframe\", \"img\", \"video\", \"audio\"]):\n el.extract()\n\n # get text and compact empty lines\n text = soup.get_text()\n return re.sub(r\"[\\r\\n][\\r\\n]{2,}\", \"\\n\\n\", text)" }, { "identifier": "AutoGenPlanner", "path": "planning/autogen_planner.py", "snippet": "class AutoGenPlanner:\n \"\"\"\n Semantic Kernel planner using Conversational Programming via AutoGen.\n Leverages OpenAI Function Calling and AutoGen agents to solve tasks using\n loaded Semantic Kernel plugins. Supports functions with a single string parameter.\n Tested with GPT 3.5 Turbo and GPT 4, primarily uses GPT 3.5 Turbo for performance.\n \"\"\"\n\n ASSISTANT_PERSONA = (\n f\"Only use provided functions. Do not ask the user for other actions. \"\n f\"Use functions to find unavailable information. \"\n f\"Today's date: {datetime.date.today().strftime('%B %d, %Y')}. \"\n f\"Reply TERMINATE when the task is done.\"\n )\n\n def __init__(self, kernel: semantic_kernel.Kernel, llm_config: Dict = None, builder_config_path: str = None):\n self.kernel = kernel\n self.llm_config = llm_config or {}\n self.builder_config_path = builder_config_path\n self.validate_llm_config()\n self.builder = self.create_builder()\n\n def create_builder(self) -> autogen.agentchat.contrib.agent_builder.AgentBuilder:\n \"\"\"\n Create an instance of AgentBuilder.\n \"\"\"\n if not self.builder_config_path:\n raise ValueError(\"Builder config path is required to create AgentBuilder.\")\n return autogen.agentchat.contrib.agent_builder.AgentBuilder(\n config_path=self.builder_config_path,\n builder_model='gpt-4-1106-preview',\n agent_model='gpt-4-1106-preview'\n )\n\n def build_agents_for_task(self, task_description: str):\n \"\"\"\n Build agents for a specific task using the AgentBuilder.\n Args:\n task_description (str): A description of the task for which agents are to be built.\n \"\"\"\n try:\n agent_list, agent_configs = self.builder.build(task_description, self.__get_autogen_config(), coding=True)\n print(f\"Agents built successfully for task: '{task_description}'\")\n return agent_list, agent_configs\n except Exception as e:\n print(f\"Error in building agents for task '{task_description}': {e}\")\n\n def create_assistant_agent(self, name: str, persona: str = ASSISTANT_PERSONA) -> autogen.AssistantAgent:\n return autogen.AssistantAgent(name=name, system_message=persona, llm_config=self.__get_autogen_config())\n\n def create_user_agent(\n self, name: str, max_auto_reply: Optional[int] = None, human_input: Optional[str] = \"ALWAYS\"\n ) -> autogen.UserProxyAgent:\n return autogen.UserProxyAgent(\n name=name,\n human_input_mode=human_input,\n max_consecutive_auto_reply=max_auto_reply,\n function_map=self.__get_function_map(),\n )\n\n def validate_llm_config(self):\n if self.llm_config.get(\"type\") == \"openai\":\n if not self.llm_config.get(\"openai_api_key\"):\n raise ValueError(\"OpenAI API key is required for OpenAI LLM.\")\n elif self.llm_config.get(\"type\") == \"azure\":\n required_keys = [\"azure_api_key\", \"azure_deployment\", \"azure_endpoint\"]\n if any(key not in self.llm_config for key in required_keys):\n raise ValueError(\"Azure OpenAI API configuration is incomplete.\")\n else:\n raise ValueError(\"LLM type not provided, must be 'openai' or 'azure'.\")\n\n def update_llm_config(self, new_config: Dict):\n self.llm_config = new_config\n self.validate_llm_config()\n\n def load_semantic_kernel_plugins(self, plugins: List[str]):\n \"\"\"\n Load Semantic Kernel plugins into the kernel.\n Args:\n plugins (List[str]): A list of plugin names to load.\n \"\"\"\n for plugin in plugins:\n try:\n self.kernel.import_skill(plugin)\n print(f\"Plugin '{plugin}' loaded successfully.\")\n except Exception as e:\n print(f\"Error loading plugin '{plugin}': {e}\")\n\n def __get_autogen_config(self) -> Dict:\n if self.llm_config[\"type\"] == \"openai\":\n return {\n \"functions\": self.__get_function_definitions(),\n \"config_list\": [{\"model\": \"gpt-3.5-turbo\", \"api_key\": self.llm_config[\"openai_api_key\"]}]\n }\n elif self.llm_config[\"type\"] == \"azure\":\n return {\n \"functions\": self.__get_function_definitions(),\n \"config_list\": [{\n \"model\": self.llm_config[\"azure_deployment\"],\n \"api_type\": \"azure\",\n \"api_key\": self.llm_config[\"azure_api_key\"],\n \"api_base\": self.llm_config[\"azure_endpoint\"],\n \"api_version\": \"2023-08-01-preview\"\n }]\n }\n\n def __get_function_definitions(self) -> List:\n functions = []\n sk_functions = self.kernel.skills.get_functions_view()\n for ns, funcs in {**sk_functions.native_functions, **sk_functions.semantic_functions}.items():\n for f in funcs:\n if len(f.parameters) == 1 and f.parameters[0].type_ == \"string\":\n functions.append({\n \"name\": f.name,\n \"description\": f.description,\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {f.parameters[0].name: {\"description\": f.parameters[0].description, \"type\": \"string\"}},\n \"required\": [f.parameters[0].name]\n }\n })\n return functions\n\n def __get_function_map(self) -> Dict:\n function_map = {}\n sk_functions = self.kernel.skills.get_functions_view()\n for ns, funcs in {**sk_functions.native_functions, **sk_functions.semantic_functions}.items():\n for f in funcs:\n function_map[f.name] = self.kernel.skills.get_function(f.skill_name, f.name)\n return function_map" } ]
import gradio as gr import os import semantic_kernel from pydantic import BaseModel, ValidationError from plugins.sk_bing_plugin import BingPlugin from plugins.sk_web_pages_plugin import WebPagesPlugin from planning.autogen_planner import AutoGenPlanner from web_search_client import WebSearchClient from web_search_client.models import SafeSearch from azure.core.credentials import AzureKeyCredential from semantic_kernel.core_skills.text_skill import TextSkill from semantic_kernel.planning.basic_planner import BasicPlanner from dotenv import load_dotenv
2,359
load_dotenv() OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") BING_API_KEY = os.getenv("BING_API_KEY") AZURE_API_KEY = os.getenv("AZURE_API_KEY") llm_config = { "type": "openai", # "azure" or "openai" "openai_api_key": OPENAI_API_KEY, # OpenAI API Key "azure_deployment": "", # Azure OpenAI deployment name "azure_api_key": AZURE_API_KEY, # Azure OpenAI API key in the Azure portal "azure_endpoint": "" # Endpoint URL for Azure OpenAI, e.g. https://contoso.openai.azure.com/ } kernel = semantic_kernel.Kernel()
load_dotenv() OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") BING_API_KEY = os.getenv("BING_API_KEY") AZURE_API_KEY = os.getenv("AZURE_API_KEY") llm_config = { "type": "openai", # "azure" or "openai" "openai_api_key": OPENAI_API_KEY, # OpenAI API Key "azure_deployment": "", # Azure OpenAI deployment name "azure_api_key": AZURE_API_KEY, # Azure OpenAI API key in the Azure portal "azure_endpoint": "" # Endpoint URL for Azure OpenAI, e.g. https://contoso.openai.azure.com/ } kernel = semantic_kernel.Kernel()
kernel.import_skill(BingPlugin(BING_API_KEY))
0
2023-11-03 16:29:40+00:00
4k
ApolloAuto/apollo-model-centerpoint
paddle3d/models/backbones/sac.py
[ { "identifier": "manager", "path": "paddle3d/apis/manager.py", "snippet": "class ComponentManager:\n def __init__(self, *, name: str, description: str = ''):\n def __len__(self):\n def __repr__(self):\n def __getitem__(self, item: str):\n def components_dict(self) -> dict:\n def name(self) -> str:\n def description(self) -> str:\n def _add_single_component(self, component: Callable):\n def add_component(self, components: Union[Callable, Iterable[Callable]]\n ) -> Union[Callable, Iterable[Callable]]:\nVOXEL_ENCODERS = ComponentManager(name=\"voxel_encoders\")\nMIDDLE_ENCODERS = ComponentManager(name=\"middle_encoders\")\nBACKBONES = ComponentManager(name=\"backbones\")\nMODELS = ComponentManager(name=\"models\")\nNECKS = ComponentManager(name=\"necks\")\nHEADS = ComponentManager(name=\"heads\")\nLOSSES = ComponentManager(name=\"losses\")\nDATASETS = ComponentManager(name=\"datasets\")\nTRANSFORMS = ComponentManager(name=\"transforms\")\nLR_SCHEDULERS = ComponentManager(name=\"lr_schedulers\")\nOPTIMIZERS = ComponentManager(name=\"optimizers\")\nVOXELIZERS = ComponentManager(name=\"voxelizers\")\nPOINT_ENCODERS = ComponentManager(name=\"point_encoders\")\nPOSITIONAL_ENCODING = ComponentManager(name=\"POSITIONAL_ENCODING\")\nTRANSFORMERS = ComponentManager(name=\"TRANSFORMERS\")\nTRANSFORMER_ENCODERS = ComponentManager(name=\"TRANSFORMER_ENCODERS\")\nTRANSFORMER_ENCODER_LAYERS = ComponentManager(name=\"TRANSFORMER_ENCODER_LAYERS\")\nATTENTIONS = ComponentManager(name=\"ATTENTIONS\")\nBBOX_CODERS = ComponentManager(name=\"BBOX_CODERS\")\nBBOX_ASSIGNERS = ComponentManager(name=\"BBOX_ASSIGNERS\")\nMATCH_COSTS = ComponentManager(name=\"MATCH_COSTS\")\nBBOX_SAMPLERS = ComponentManager(name=\"BBOX_SAMPLERS\")\nTRANSFORMER_DECODER_LAYERS = ComponentManager(name=\"TRANSFORMER_DECODER_LAYERS\")\nTRANSFORMER_DECODERS = ComponentManager(name=\"TRANSFORMER_DECODERS\")" }, { "identifier": "param_init", "path": "paddle3d/models/layers/param_init.py", "snippet": "def constant_init(param, **kwargs):\ndef normal_init(param, **kwargs):\ndef uniform_init(param, a, b):\ndef xavier_normal_init(tensor, gain=1, reverse=False):\ndef kaiming_normal_init(tensor,\n a=0,\n mode='fan_in',\n nonlinearity='leaky_relu',\n reverse=False):\ndef kaiming_uniform_init(param,\n a=0,\n mode='fan_in',\n nonlinearity='leaky_relu',\n reverse=False):\ndef xavier_uniform_init(param, gain=1., reverse=False):\ndef _calculate_fan_in_and_fan_out(tensor, reverse=False):\ndef _calculate_correct_fan(tensor, mode, reverse=False):\ndef _calculate_gain(nonlinearity, param=None):\ndef _no_grad_uniform_(tensor, a, b):\ndef _no_grad_normal_(tensor, mean, std):\ndef reset_parameters(m, reverse=False):\ndef init_bias_by_prob(prob):" }, { "identifier": "checkpoint", "path": "paddle3d/utils/checkpoint.py", "snippet": "def load_pretrained_model_from_url(model: paddle.nn.Layer,\n url: str,\n overwrite: bool = False):\ndef load_pretrained_model_from_path(model: paddle.nn.Layer, path: str):\ndef load_pretrained_model_from_state_dict(model: paddle.nn.Layer,\n state_dict: dict):\ndef load_pretrained_model(model: paddle.nn.Layer,\n pretrained_model: Union[dict, str]):" } ]
import math import paddle import paddle.nn as nn import paddle.nn.functional as F from paddle3d.apis import manager from paddle3d.models.layers import param_init from paddle3d.utils import checkpoint
1,654
# Copyright (c) 2022 PaddlePaddle 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. __all__ = ["SACRangeNet21", "SACRangeNet53"] class SACRangeNet(nn.Layer): """ Backbone of SqueezeSegV3. RangeNet++ architecture with Spatially-Adaptive Convolution (SAC). For RangeNet++, please refer to: Milioto, A., et al. “RangeNet++: Fast and Accurate LiDAR Semantic Segmentation.” IEEE/RSJ Intl. Conf. on Intelligent Robots and Systems (IROS), 2019. For SAC, please refer to: Xu, Chenfeng, et al. “SqueezeSegV3: Spatially-Adaptive Convolution for Efficient Point-Cloud Segmentation.” CoRR, vol. abs/2004.01803, 2020, https://arxiv.org/abs/2004.01803. Args: in_channels (int): The number of channels of input. num_layers (int, optional): The depth of SACRangeNet. Defaults to 53. encoder_dropout_prob (float, optional): Dropout probability for dropout layers in encoder. Defaults to 0.01. decoder_dropout_prob (float, optional): Dropout probability for dropout layers in decoder. Defaults to 0.01. bn_momentum (float, optional): Momentum for batch normalization. Defaults to 0.99. pretrained (str, optional): Path to pretrained model. Defaults to None. """ # TODO(will-jl944): Currently only SAC-ISK is implemented. def __init__(self, in_channels: int, num_layers: int = 53, encoder_dropout_prob: float = .01, decoder_dropout_prob: float = .01, bn_momentum: float = .99, pretrained: str = None): supported_layers = {21, 53} assert num_layers in supported_layers, "Invalid number of layers ({}) for SACRangeNet backbone, " \ "supported values are {}.".format(num_layers, supported_layers) super().__init__() self.in_channels = in_channels self.pretrained = pretrained if num_layers == 21: num_stage_blocks = (1, 1, 2, 2, 1) elif num_layers == 53: num_stage_blocks = (1, 2, 8, 8, 4) self.encoder = Encoder( in_channels, num_stage_blocks, encoder_dropout_prob, bn_momentum=bn_momentum) self.decoder = Decoder(decoder_dropout_prob, bn_momentum=bn_momentum) self.init_weight() def forward(self, inputs): feature, short_cuts = self.encoder(inputs) feature_list = self.decoder(feature, short_cuts) return feature_list def init_weight(self): if self.pretrained is not None: checkpoint.load_pretrained_model(self, self.pretrained) else: for layer in self.sublayers(): if isinstance(layer, (nn.Conv2D, nn.Conv2DTranspose)):
# Copyright (c) 2022 PaddlePaddle 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. __all__ = ["SACRangeNet21", "SACRangeNet53"] class SACRangeNet(nn.Layer): """ Backbone of SqueezeSegV3. RangeNet++ architecture with Spatially-Adaptive Convolution (SAC). For RangeNet++, please refer to: Milioto, A., et al. “RangeNet++: Fast and Accurate LiDAR Semantic Segmentation.” IEEE/RSJ Intl. Conf. on Intelligent Robots and Systems (IROS), 2019. For SAC, please refer to: Xu, Chenfeng, et al. “SqueezeSegV3: Spatially-Adaptive Convolution for Efficient Point-Cloud Segmentation.” CoRR, vol. abs/2004.01803, 2020, https://arxiv.org/abs/2004.01803. Args: in_channels (int): The number of channels of input. num_layers (int, optional): The depth of SACRangeNet. Defaults to 53. encoder_dropout_prob (float, optional): Dropout probability for dropout layers in encoder. Defaults to 0.01. decoder_dropout_prob (float, optional): Dropout probability for dropout layers in decoder. Defaults to 0.01. bn_momentum (float, optional): Momentum for batch normalization. Defaults to 0.99. pretrained (str, optional): Path to pretrained model. Defaults to None. """ # TODO(will-jl944): Currently only SAC-ISK is implemented. def __init__(self, in_channels: int, num_layers: int = 53, encoder_dropout_prob: float = .01, decoder_dropout_prob: float = .01, bn_momentum: float = .99, pretrained: str = None): supported_layers = {21, 53} assert num_layers in supported_layers, "Invalid number of layers ({}) for SACRangeNet backbone, " \ "supported values are {}.".format(num_layers, supported_layers) super().__init__() self.in_channels = in_channels self.pretrained = pretrained if num_layers == 21: num_stage_blocks = (1, 1, 2, 2, 1) elif num_layers == 53: num_stage_blocks = (1, 2, 8, 8, 4) self.encoder = Encoder( in_channels, num_stage_blocks, encoder_dropout_prob, bn_momentum=bn_momentum) self.decoder = Decoder(decoder_dropout_prob, bn_momentum=bn_momentum) self.init_weight() def forward(self, inputs): feature, short_cuts = self.encoder(inputs) feature_list = self.decoder(feature, short_cuts) return feature_list def init_weight(self): if self.pretrained is not None: checkpoint.load_pretrained_model(self, self.pretrained) else: for layer in self.sublayers(): if isinstance(layer, (nn.Conv2D, nn.Conv2DTranspose)):
param_init.kaiming_uniform_init(
1
2023-11-08 07:08:03+00:00
4k
camlsys/fl-project-template
project/dispatch/dispatch.py
[ { "identifier": "dispatch_config", "path": "project/task/default/dispatch.py", "snippet": "def dispatch_config(\n cfg: DictConfig,\n) -> ConfigStructure | None:\n \"\"\"Dispatches the config function based on the config_structure in the config file.\n\n By default it simply takes the fit_config and evaluate_config\n dicts from the hydra config.\n Only change if a more complex behaviour\n (such as varying the config across rounds) is needed.\n\n Do not throw any errors based on not finding\n a given attribute in the configs under any circumstances.\n If you cannot match the config file,\n return None and the dispatch of the next task\n in the chain specified by project.dispatch will be used.\n\n Parameters\n ----------\n cfg : DictConfig\n The configuration for the config function.\n Loaded dynamically from the config file.\n\n Returns\n -------\n Optional[ConfigStructure]\n The fit_config and evaluate_config functions.\n Return None if you cannot match the cfg.\n \"\"\"\n # Select the values for the key with {} default at nested dicts\n # and None default at the final key\n fit_config: dict | None = cfg.get(\"task\", {}).get(\n \"fit_config\",\n None,\n )\n eval_config: dict | None = cfg.get(\"task\", {}).get(\n \"eval_config\",\n None,\n )\n\n # Only consider existing config dicts as matches\n if fit_config is not None and eval_config is not None:\n return get_on_fit_config_fn(\n cast(dict, OmegaConf.to_container(fit_config)),\n ), get_on_evaluate_config_fn(\n cast(dict, OmegaConf.to_container(eval_config)),\n )\n\n return None" }, { "identifier": "dispatch_data", "path": "project/task/default/dispatch.py", "snippet": "def dispatch_data(cfg: DictConfig) -> DataStructure | None:\n \"\"\"Dispatch the net and dataloader client/fed generator functions.\n\n Do not throw any errors based on not finding\n a given attribute in the configs under any circumstances.\n If you cannot match the config file,\n return None and the dispatch of the next task\n in the chain specified by project.dispatch will be used.\n\n Parameters\n ----------\n cfg : DictConfig\n The configuration for the data functions.\n Loaded dynamically from the config file.\n\n Returns\n -------\n Optional[DataStructure]\n The net generator, client dataloader generator and fed dataloader generator.\n Return None if you cannot match the cfg.\n \"\"\"\n # Select the value for the key with {} default at nested dicts\n # and None default at the final key\n client_model_and_data: str | None = cfg.get(\n \"task\",\n {},\n ).get(\"model_and_data\", None)\n\n # Only consider not None matches, case insensitive\n if client_model_and_data is not None and client_model_and_data.upper() == \"DEFAULT\":\n return (\n get_net,\n get_client_dataloader,\n get_fed_dataloader,\n )\n\n # Cannot match, send to next dispatch in chain\n return None" }, { "identifier": "dispatch_train", "path": "project/task/default/dispatch.py", "snippet": "def dispatch_train(\n cfg: DictConfig,\n) -> TrainStructure | None:\n \"\"\"Dispatch the train/test and fed test functions based on the config file.\n\n Do not throw any errors based on not finding\n a given attribute in the configs under any circumstances.\n If you cannot match the config file,\n return None and the dispatch of the next task\n in the chain specified by project.dispatch will be used.\n\n Parameters\n ----------\n cfg : DictConfig\n The configuration for the train function.\n Loaded dynamically from the config file.\n\n Returns\n -------\n Optional[TrainStructure]\n The train function, test function and the get_fed_eval_fn function.\n Return None if you cannot match the cfg.\n \"\"\"\n # Select the value for the key with None default\n train_structure: str | None = cfg.get(\"task\", {}).get(\n \"train_structure\",\n None,\n )\n\n # Only consider not None matches, case insensitive\n if train_structure is not None and train_structure.upper() == \"DEFAULT\":\n return train, test, get_fed_eval_fn\n\n # Cannot match, send to next dispatch in chain\n return None" }, { "identifier": "dispatch_config", "path": "project/task/mnist_classification/dispatch.py", "snippet": "def dispatch_train(\n cfg: DictConfig,\n) -> TrainStructure | None:\ndef dispatch_data(cfg: DictConfig) -> DataStructure | None:" }, { "identifier": "dispatch_data", "path": "project/task/mnist_classification/dispatch.py", "snippet": "def dispatch_data(cfg: DictConfig) -> DataStructure | None:\n \"\"\"Dispatch the train/test and fed test functions based on the config file.\n\n Do not throw any errors based on not finding a given attribute\n in the configs under any circumstances.\n\n If you cannot match the config file,\n return None and the dispatch of the next task\n in the chain specified by project.dispatch will be used.\n\n Parameters\n ----------\n cfg : DictConfig\n The configuration for the data functions.\n Loaded dynamically from the config file.\n\n Returns\n -------\n Optional[DataStructure]\n The net generator, client dataloader generator and fed dataloader generator.\n Return None if you cannot match the cfg.\n \"\"\"\n # Select the value for the key with {} default at nested dicts\n # and None default at the final key\n client_model_and_data: str | None = cfg.get(\n \"task\",\n {},\n ).get(\"model_and_data\", None)\n\n # Select the partition dir\n # if it does not exist data cannot be loaded\n # for MNIST and the dispatch should return None\n partition_dir: str | None = cfg.get(\"dataset\", {}).get(\n \"partition_dir\",\n None,\n )\n\n # Only consider situations where both are not None\n # otherwise data loading would failr later\n if client_model_and_data is not None and partition_dir is not None:\n # Obtain the dataloader generators\n # for the provided partition dir\n (\n client_dataloader_gen,\n fed_dataloater_gen,\n ) = get_dataloader_generators(\n Path(partition_dir),\n )\n\n # Case insensitive matches\n if client_model_and_data.upper() == \"MNIST_CNN\":\n return (\n get_net,\n client_dataloader_gen,\n fed_dataloater_gen,\n )\n elif client_model_and_data.upper() == \"MNIST_LR\":\n return (\n get_logistic_regression,\n client_dataloader_gen,\n fed_dataloater_gen,\n )\n\n # Cannot match, send to next dispatch in chain\n return None" }, { "identifier": "dispatch_train", "path": "project/task/mnist_classification/dispatch.py", "snippet": "def dispatch_train(\n cfg: DictConfig,\n) -> TrainStructure | None:\n \"\"\"Dispatch the train/test and fed test functions based on the config file.\n\n Do not throw any errors based on not finding a given attribute\n in the configs under any circumstances.\n\n If you cannot match the config file,\n return None and the dispatch of the next task\n in the chain specified by project.dispatch will be used.\n\n Parameters\n ----------\n cfg : DictConfig\n The configuration for the train function.\n Loaded dynamically from the config file.\n\n Returns\n -------\n Optional[TrainStructure]\n The train function, test function and the get_fed_eval_fn function.\n Return None if you cannot match the cfg.\n \"\"\"\n # Select the value for the key with None default\n train_structure: str | None = cfg.get(\"task\", {}).get(\n \"train_structure\",\n None,\n )\n\n # Only consider not None and uppercase matches\n if train_structure is not None and train_structure.upper() == \"MNIST\":\n return train, test, get_fed_eval_fn\n\n # Cannot match, send to next dispatch in chain\n return None" }, { "identifier": "ConfigStructure", "path": "project/types/common.py", "snippet": "" } ]
from collections.abc import Callable from omegaconf import DictConfig from project.task.default.dispatch import dispatch_config as dispatch_default_config from project.task.default.dispatch import dispatch_data as dispatch_default_data from project.task.default.dispatch import dispatch_train as dispatch_default_train from project.task.mnist_classification.dispatch import ( dispatch_config as dispatch_mnist_config, ) from project.task.mnist_classification.dispatch import ( dispatch_data as dispatch_mnist_data, ) from project.task.mnist_classification.dispatch import ( dispatch_train as dispatch_mnist_train, ) from project.types.common import ConfigStructure, DataStructure, TrainStructure
2,240
"""Dispatches the functionality of the task. This gives us the ability to dynamically choose functionality based on the hydra dict config without losing static type checking. """ def dispatch_train(cfg: DictConfig) -> TrainStructure: """Dispatch the train/test and fed test functions based on the config file. Functionality should be added to the dispatch.py file in the task folder. Statically specify the new dispatch function in the list, function order determines precedence if two different tasks may match the config. Parameters ---------- cfg : DictConfig The configuration for the train function. Loaded dynamically from the config file. Returns ------- TrainStructure The train function, test function and the get_fed_eval_fn function. """ # Create the list of task dispatches to try task_train_functions: list[Callable[[DictConfig], TrainStructure | None]] = [ dispatch_default_train,
"""Dispatches the functionality of the task. This gives us the ability to dynamically choose functionality based on the hydra dict config without losing static type checking. """ def dispatch_train(cfg: DictConfig) -> TrainStructure: """Dispatch the train/test and fed test functions based on the config file. Functionality should be added to the dispatch.py file in the task folder. Statically specify the new dispatch function in the list, function order determines precedence if two different tasks may match the config. Parameters ---------- cfg : DictConfig The configuration for the train function. Loaded dynamically from the config file. Returns ------- TrainStructure The train function, test function and the get_fed_eval_fn function. """ # Create the list of task dispatches to try task_train_functions: list[Callable[[DictConfig], TrainStructure | None]] = [ dispatch_default_train,
dispatch_mnist_train,
3
2023-11-08 15:31:44+00:00
4k
KAIST-AILab/palr
rlkit/torch/ppo/ppo_path_collector.py
[ { "identifier": "MdpPathCollector", "path": "rlkit/samplers/data_collector/path_collector.py", "snippet": "class MdpPathCollector(PathCollector):\n def __init__(\n self,\n env,\n policy,\n max_num_epoch_paths_saved=None,\n render=False,\n render_kwargs=None,\n rollout_fn=rollout,\n save_env_in_snapshot=True,\n stacksize=2\n ):\n if render_kwargs is None:\n render_kwargs = {}\n self._env = env\n self._policy = policy\n self._max_num_epoch_paths_saved = max_num_epoch_paths_saved\n self._epoch_paths = deque(maxlen=self._max_num_epoch_paths_saved)\n self._render = render\n self._render_kwargs = render_kwargs\n self._rollout_fn = rollout_fn\n self._stacksize = stacksize\n\n self._num_steps_total = 0\n self._num_paths_total = 0\n\n self._save_env_in_snapshot = save_env_in_snapshot\n\n def collect_new_paths(\n self,\n max_path_length,\n num_steps,\n discard_incomplete_paths,\n ):\n paths = []\n num_steps_collected = 0\n while num_steps_collected < num_steps:\n max_path_length_this_loop = min( # Do not go over num_steps\n max_path_length,\n num_steps - num_steps_collected,\n )\n path = self._rollout_fn(\n self._env,\n self._policy,\n max_path_length=max_path_length_this_loop,\n render=self._render,\n render_kwargs=self._render_kwargs,\n # stacksize=self._stacksize,\n )\n path_len = len(path['actions'])\n if (\n path_len != max_path_length\n and not path['dones'][-1]\n and discard_incomplete_paths\n ):\n break\n num_steps_collected += path_len\n paths.append(path)\n self._num_paths_total += len(paths)\n self._num_steps_total += num_steps_collected\n self._epoch_paths.extend(paths)\n return paths\n\n def get_epoch_paths(self):\n return self._epoch_paths\n\n def end_epoch(self, epoch):\n self._epoch_paths = deque(maxlen=self._max_num_epoch_paths_saved)\n\n def get_diagnostics(self):\n path_lens = [len(path['actions']) for path in self._epoch_paths]\n stats = OrderedDict([\n ('num steps total', self._num_steps_total),\n ('num paths total', self._num_paths_total),\n ])\n stats.update(create_stats_ordered_dict(\n \"path length\",\n path_lens,\n always_show_all_stats=True,\n ))\n return stats\n\n def get_snapshot(self):\n snapshot_dict = dict(\n policy=self._policy,\n )\n if self._save_env_in_snapshot:\n snapshot_dict['env'] = self._env\n return snapshot_dict" }, { "identifier": "rollout", "path": "rlkit/samplers/rollout_functions.py", "snippet": "def rollout(\n env,\n agent,\n max_path_length=np.inf,\n render=False,\n render_kwargs=None,\n preprocess_obs_for_policy_fn=None,\n get_action_kwargs=None,\n return_dict_obs=False,\n full_o_postprocess_func=None,\n reset_callback=None,\n):\n if render_kwargs is None:\n render_kwargs = {}\n if get_action_kwargs is None:\n get_action_kwargs = {}\n if preprocess_obs_for_policy_fn is None:\n preprocess_obs_for_policy_fn = lambda x: x\n raw_obs = []\n raw_next_obs = []\n observations = []\n actions = []\n rewards = []\n terminals = []\n dones = []\n agent_infos = []\n env_infos = []\n next_observations = []\n path_length = 0\n\n agent.reset()\n o = env.reset()\n\n if reset_callback:\n reset_callback(env, agent, o)\n if render:\n env.render(**render_kwargs)\n while path_length < max_path_length:\n raw_obs.append(o)\n o_for_agent = preprocess_obs_for_policy_fn(o)\n a, agent_info = agent.get_action(o_for_agent, **get_action_kwargs)\n\n if full_o_postprocess_func:\n full_o_postprocess_func(env, agent, o)\n\n next_o, r, done, env_info = env.step(copy.deepcopy(a))\n if render:\n env.render(**render_kwargs)\n observations.append(o)\n rewards.append(r)\n terminal = False\n if done:\n # terminal=False if TimeLimit caused termination\n if not env_info.pop('TimeLimit.truncated', False):\n terminal = True\n terminals.append(terminal)\n dones.append(done)\n actions.append(a)\n next_observations.append(next_o)\n raw_next_obs.append(next_o)\n agent_infos.append(agent_info)\n env_infos.append(env_info)\n path_length += 1\n if done:\n break\n o = next_o\n actions = np.array(actions)\n if len(actions.shape) == 1:\n actions = np.expand_dims(actions, 1)\n observations = np.array(observations)\n next_observations = np.array(next_observations)\n if return_dict_obs:\n observations = raw_obs\n next_observations = raw_next_obs\n rewards = np.array(rewards)\n if len(rewards.shape) == 1:\n rewards = rewards.reshape(-1, 1)\n return dict(\n observations=observations,\n actions=actions,\n rewards=rewards,\n next_observations=next_observations,\n terminals=np.array(terminals).reshape(-1, 1),\n dones=np.array(dones).reshape(-1, 1),\n agent_infos=agent_infos,\n env_infos=env_infos,\n full_observations=raw_obs,\n full_next_observations=raw_obs,\n )" }, { "identifier": "torch_ify", "path": "rlkit/torch/core.py", "snippet": "def torch_ify(np_array_or_other):\n if isinstance(np_array_or_other, np.ndarray):\n return ptu.from_numpy(np_array_or_other)\n else:\n return np_array_or_other" }, { "identifier": "np_ify", "path": "rlkit/torch/core.py", "snippet": "def np_ify(tensor_or_other):\n if isinstance(tensor_or_other, torch.autograd.Variable):\n return ptu.get_numpy(tensor_or_other)\n else:\n return tensor_or_other" } ]
from rlkit.samplers.data_collector.path_collector import MdpPathCollector from rlkit.samplers.rollout_functions import rollout from rlkit.torch.core import torch_ify, np_ify import numpy as np import torch
2,232
class PPOMdpPathCollector (MdpPathCollector): def __init__( self, env, policy, max_num_epoch_paths_saved=None, render=False, render_kwargs=None, calculate_advantages = False, vf = None, discount=0.99, gae_lambda=0.95 ): self.calculate_advantages = calculate_advantages self.vf = vf self.discount = discount self.gae_lambda = gae_lambda super().__init__( env, policy, max_num_epoch_paths_saved=None, render=False, render_kwargs=None ) """Generalized Advantage Estimator""" # also returns def add_advantages(self, path, path_len, flag): if flag: next_vf = self.vf(torch_ify(path["next_observations"])) cur_vf = self.vf(torch_ify(path["observations"])) rewards = torch_ify(path["rewards"]) term = (1 - torch_ify(path["terminals"].astype(np.float32))) delta = rewards + term * self.discount * next_vf - cur_vf advantages = torch.zeros((path_len)) returns = torch.zeros((path_len)) gae = 0 R = 0 for i in reversed(range(path_len)): # try: # advantages[i] = delta[i] + term[i] * (self.discount * self.gae_lambda) * gae.clone().detach().requires_grad_(True).float().cuda() # except: advantages[i] = delta[i] + term[i] * (self.discount * self.gae_lambda) * gae gae = advantages[i] # try: # returns[i] = rewards[i] + term[i] * self.discount * R.clone().detach().requires_grad_(True).float().cuda() # except: returns[i] = rewards[i] + term[i] * self.discount * R R = returns[i] advantages = np_ify(advantages) if advantages.std() != 0.0: advantages = (advantages - advantages.mean()) / advantages.std() else: advantages = (advantages - advantages.mean()) returns = np_ify(returns) else: advantages = np.zeros(path_len) returns = np.zeros(path_len) return dict( observations=path["observations"], actions=path["actions"], rewards=path["rewards"], next_observations=path["next_observations"], terminals=path["terminals"], agent_infos=path["agent_infos"], env_infos=path["env_infos"], advantages=advantages, returns=returns ) def collect_new_paths( self, max_path_length, num_steps, discard_incomplete_paths, ): paths = [] num_steps_collected = 0 while num_steps_collected < num_steps: max_path_length_this_loop = min( # Do not go over num_steps max_path_length, num_steps - num_steps_collected, )
class PPOMdpPathCollector (MdpPathCollector): def __init__( self, env, policy, max_num_epoch_paths_saved=None, render=False, render_kwargs=None, calculate_advantages = False, vf = None, discount=0.99, gae_lambda=0.95 ): self.calculate_advantages = calculate_advantages self.vf = vf self.discount = discount self.gae_lambda = gae_lambda super().__init__( env, policy, max_num_epoch_paths_saved=None, render=False, render_kwargs=None ) """Generalized Advantage Estimator""" # also returns def add_advantages(self, path, path_len, flag): if flag: next_vf = self.vf(torch_ify(path["next_observations"])) cur_vf = self.vf(torch_ify(path["observations"])) rewards = torch_ify(path["rewards"]) term = (1 - torch_ify(path["terminals"].astype(np.float32))) delta = rewards + term * self.discount * next_vf - cur_vf advantages = torch.zeros((path_len)) returns = torch.zeros((path_len)) gae = 0 R = 0 for i in reversed(range(path_len)): # try: # advantages[i] = delta[i] + term[i] * (self.discount * self.gae_lambda) * gae.clone().detach().requires_grad_(True).float().cuda() # except: advantages[i] = delta[i] + term[i] * (self.discount * self.gae_lambda) * gae gae = advantages[i] # try: # returns[i] = rewards[i] + term[i] * self.discount * R.clone().detach().requires_grad_(True).float().cuda() # except: returns[i] = rewards[i] + term[i] * self.discount * R R = returns[i] advantages = np_ify(advantages) if advantages.std() != 0.0: advantages = (advantages - advantages.mean()) / advantages.std() else: advantages = (advantages - advantages.mean()) returns = np_ify(returns) else: advantages = np.zeros(path_len) returns = np.zeros(path_len) return dict( observations=path["observations"], actions=path["actions"], rewards=path["rewards"], next_observations=path["next_observations"], terminals=path["terminals"], agent_infos=path["agent_infos"], env_infos=path["env_infos"], advantages=advantages, returns=returns ) def collect_new_paths( self, max_path_length, num_steps, discard_incomplete_paths, ): paths = [] num_steps_collected = 0 while num_steps_collected < num_steps: max_path_length_this_loop = min( # Do not go over num_steps max_path_length, num_steps - num_steps_collected, )
path = rollout(
1
2023-11-06 08:35:34+00:00
4k
JustlfC03/SCUNet-plusplus
train.py
[ { "identifier": "SwinUnet", "path": "networks/vision_transformer.py", "snippet": "class SwinUnet(nn.Module):\n def __init__(self, config, img_size=224, num_classes=21843, zero_head=False, vis=False):\n super(SwinUnet, self).__init__()\n self.num_classes = num_classes\n self.zero_head = zero_head\n self.config = config\n\n self.swin_unet = SwinTransformerSys(img_size=config.DATA.IMG_SIZE,\n patch_size=config.MODEL.SWIN.PATCH_SIZE,\n in_chans=config.MODEL.SWIN.IN_CHANS,\n num_classes=self.num_classes,\n embed_dim=config.MODEL.SWIN.EMBED_DIM,\n depths=config.MODEL.SWIN.DEPTHS,\n num_heads=config.MODEL.SWIN.NUM_HEADS,\n window_size=config.MODEL.SWIN.WINDOW_SIZE,\n mlp_ratio=config.MODEL.SWIN.MLP_RATIO,\n qkv_bias=config.MODEL.SWIN.QKV_BIAS,\n qk_scale=config.MODEL.SWIN.QK_SCALE,\n drop_rate=config.MODEL.DROP_RATE,\n drop_path_rate=config.MODEL.DROP_PATH_RATE,\n ape=config.MODEL.SWIN.APE,\n patch_norm=config.MODEL.SWIN.PATCH_NORM,\n use_checkpoint=config.TRAIN.USE_CHECKPOINT)\n\n def forward(self, x):\n if x.size()[1] == 1:\n x = x.repeat(1, 3, 1, 1)\n logits = self.swin_unet(x)\n return logits\n\n def load_from(self, config):\n pretrained_path = config.MODEL.PRETRAIN_CKPT\n if pretrained_path is not None:\n print(\"pretrained_path:{}\".format(pretrained_path))\n device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n pretrained_dict = torch.load(pretrained_path, map_location=device)\n if \"model\" not in pretrained_dict:\n print(\"---start load pretrained modle by splitting---\")\n pretrained_dict = {k[17:]: v for k, v in pretrained_dict.items()}\n for k in list(pretrained_dict.keys()):\n if \"output\" in k:\n print(\"delete key:{}\".format(k))\n del pretrained_dict[k]\n msg = self.swin_unet.load_state_dict(pretrained_dict, strict=False)\n # print(msg)\n return\n pretrained_dict = pretrained_dict['model']\n print(\"---start load pretrained modle of swin encoder---\")\n\n model_dict = self.swin_unet.state_dict()\n full_dict = copy.deepcopy(pretrained_dict)\n for k, v in pretrained_dict.items():\n if \"layers.\" in k:\n current_layer_num = 3 - int(k[7:8])\n current_k = \"layers_up.\" + str(current_layer_num) + k[8:]\n full_dict.update({current_k: v})\n for k in list(full_dict.keys()):\n if k in model_dict:\n if full_dict[k].shape != model_dict[k].shape:\n print(\"delete:{};shape pretrain:{};shape model:{}\".format(k, v.shape, model_dict[k].shape))\n del full_dict[k]\n\n msg = self.swin_unet.load_state_dict(full_dict, strict=False)\n # print(msg)\n else:\n print(\"none pretrain\")" }, { "identifier": "trainer_synapse", "path": "trainer.py", "snippet": "def trainer_synapse(args, model, snapshot_path):\n from datasets.dataset_synapse import Synapse_dataset, RandomGenerator\n logging.basicConfig(filename=snapshot_path + \"/log.txt\", level=logging.INFO,\n format='[%(asctime)s.%(msecs)03d] %(message)s', datefmt='%H:%M:%S')\n logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))\n logging.info(str(args))\n base_lr = args.base_lr\n num_classes = args.num_classes\n batch_size = args.batch_size * args.n_gpu\n max_iterations = args.max_iterations\n db_train = Synapse_dataset(base_dir=args.root_path, list_dir=args.list_dir, split=\"train\",\n transform=transforms.Compose(\n [RandomGenerator(output_size=[args.img_size, args.img_size])]))\n print(\"The length of train set is: {}\".format(len(db_train)))\n\n def worker_init_fn(worker_id):\n random.seed(args.seed + worker_id)\n\n trainloader = DataLoader(db_train, batch_size=batch_size, shuffle=True, num_workers=0, pin_memory=True,\n worker_init_fn=worker_init_fn)\n if args.n_gpu > 1:\n model = nn.DataParallel(model)\n model.train()\n ce_loss = CrossEntropyLoss()\n dice_loss = DiceLoss(num_classes)\n optimizer = optim.SGD(model.parameters(), lr=base_lr, momentum=0.9, weight_decay=0.0001)\n writer = SummaryWriter(snapshot_path + '/log')\n iter_num = 0\n max_epoch = args.max_epochs\n max_iterations = args.max_epochs * len(trainloader)\n logging.info(\"{} iterations per epoch. {} max iterations \".format(len(trainloader), max_iterations))\n best_performance = 0.0\n iterator = tqdm(range(max_epoch), ncols=70)\n for epoch_num in iterator:\n for i_batch, sampled_batch in enumerate(trainloader):\n image_batch, label_batch = sampled_batch['image'], sampled_batch['label']\n image_batch, label_batch = image_batch.cuda(), label_batch.cuda()\n outputs = model(image_batch)\n loss_ce = ce_loss(outputs, label_batch[:].long())\n loss_dice = dice_loss(outputs, label_batch, softmax=True)\n loss = 0.4 * loss_ce + 0.6 * loss_dice\n optimizer.zero_grad()\n loss.backward()\n optimizer.step()\n lr_ = base_lr * (1.0 - iter_num / max_iterations) ** 0.9\n for param_group in optimizer.param_groups:\n param_group['lr'] = lr_\n\n iter_num = iter_num + 1\n writer.add_scalar('info/lr', lr_, iter_num)\n writer.add_scalar('info/total_loss', loss, iter_num)\n writer.add_scalar('info/loss_ce', loss_ce, iter_num)\n\n logging.info('iteration %d : loss : %f, loss_ce: %f' % (iter_num, loss.item(), loss_ce.item()))\n\n if iter_num % 20 == 0:\n image = image_batch[1, 0:1, :, :]\n image = (image - image.min()) / (image.max() - image.min())\n writer.add_image('train/Image', image, iter_num)\n outputs = torch.argmax(torch.softmax(outputs, dim=1), dim=1, keepdim=True)\n writer.add_image('train/Prediction', outputs[1, ...] * 50, iter_num)\n labs = label_batch[1, ...].unsqueeze(0) * 50\n writer.add_image('train/GroundTruth', labs, iter_num)\n\n # save_interval = 50\n # if epoch_num > int(max_epoch / 2) and (epoch_num + 1) % save_interval == 0:\n # save_mode_path = os.path.join(snapshot_path, 'epoch_' + str(epoch_num) + '.pth')\n # torch.save(model.state_dict(), save_mode_path)\n # logging.info(\"save model to {}\".format(save_mode_path))\n #\n # if epoch_num >= max_epoch - 1:\n # save_mode_path = os.path.join(snapshot_path, 'epoch_' + str(epoch_num) + '.pth')\n # torch.save(model.state_dict(), save_mode_path)\n # logging.info(\"save model to {}\".format(save_mode_path))\n # iterator.close()\n # break\n\n save_interval = 2\n if (epoch_num + 1) % save_interval == 0:\n save_mode_path = os.path.join(snapshot_path, 'epoch_' + str(epoch_num) + '.pth')\n torch.save(model.state_dict(), save_mode_path)\n logging.info(\"save model to {}\".format(save_mode_path))\n\n if epoch_num >= max_epoch - 1:\n save_mode_path = os.path.join(snapshot_path, 'epoch_' + str(epoch_num) + '.pth')\n torch.save(model.state_dict(), save_mode_path)\n logging.info(\"save model to {}\".format(save_mode_path))\n iterator.close()\n break\n\n writer.close()\n return \"Training Finished!\"" }, { "identifier": "get_config", "path": "config.py", "snippet": "def get_config(args):\n \"\"\"Get a yacs CfgNode object with default values.\"\"\"\n # Return a clone so that the defaults will not be altered\n # This is for the \"local variable\" use pattern\n config = _C.clone()\n update_config(config, args)\n\n return config" } ]
import argparse import logging import os import random import numpy as np import torch import torch.backends.cudnn as cudnn from networks.vision_transformer import SwinUnet as ViT_seg from trainer import trainer_synapse from config import get_config
2,790
""" --dataset Synapse --cfg ./configs/swin_tiny_patch4_window7_224_lite.yaml --root_path ./datasets/Synapse --max_epochs 1500 --output_dir ./output --img_size 224 --base_lr 0.005 --batch_size 24 """ parser = argparse.ArgumentParser() parser.add_argument('--root_path', type=str, default='./datasets/Synapse/train_npz', help='root dir for data') parser.add_argument('--dataset', type=str, default='Synapse', help='experiment_name') parser.add_argument('--list_dir', type=str, default='./lists/lists_Synapse', help='list dir') # parser.add_argument('--num_classes', type=int, # default=9, help='output channel of network') parser.add_argument('--num_classes', type=int, default=2, help='output channel of network') parser.add_argument('--output_dir', default='./output', type=str, help='output dir') parser.add_argument('--max_iterations', type=int, default=30000, help='maximum epoch number to train') parser.add_argument('--max_epochs', type=int, default=1500, help='maximum epoch number to train') parser.add_argument('--batch_size', type=int, default=24, help='batch_size per gpu') parser.add_argument('--n_gpu', type=int, default=1, help='total gpu') parser.add_argument('--deterministic', type=int, default=1, help='whether use deterministic training') parser.add_argument('--base_lr', type=float, default=0.005, help='segmentation network learning rate') parser.add_argument('--img_size', type=int, default=224, help='input patch size of network input') parser.add_argument('--seed', type=int, default=1234, help='random seed') parser.add_argument('--cfg', type=str, required=True, metavar="FILE", help='path to config file', ) parser.add_argument( "--opts", help="Modify config options by adding 'KEY VALUE' pairs. ", default=None, nargs='+', ) parser.add_argument('--zip', action='store_true', help='use zipped dataset instead of folder dataset') parser.add_argument('--cache-mode', type=str, default='part', choices=['no', 'full', 'part'], help='no: no cache, ' 'full: cache all data, ' 'part: sharding the dataset into nonoverlapping pieces and only cache one piece') parser.add_argument('--resume', help='resume from checkpoint') parser.add_argument('--accumulation-steps', type=int, help="gradient accumulation steps") parser.add_argument('--use-checkpoint', action='store_true', help="whether to use gradient checkpointing to save memory") parser.add_argument('--amp-opt-level', type=str, default='O1', choices=['O0', 'O1', 'O2'], help='mixed precision opt level, if O0, no amp is used') parser.add_argument('--tag', help='tag of experiment') parser.add_argument('--eval', action='store_true', help='Perform evaluation only') parser.add_argument('--throughput', action='store_true', help='Test throughput only') args = parser.parse_args() if args.dataset == "Synapse": args.root_path = os.path.join(args.root_path, "train_npz")
""" --dataset Synapse --cfg ./configs/swin_tiny_patch4_window7_224_lite.yaml --root_path ./datasets/Synapse --max_epochs 1500 --output_dir ./output --img_size 224 --base_lr 0.005 --batch_size 24 """ parser = argparse.ArgumentParser() parser.add_argument('--root_path', type=str, default='./datasets/Synapse/train_npz', help='root dir for data') parser.add_argument('--dataset', type=str, default='Synapse', help='experiment_name') parser.add_argument('--list_dir', type=str, default='./lists/lists_Synapse', help='list dir') # parser.add_argument('--num_classes', type=int, # default=9, help='output channel of network') parser.add_argument('--num_classes', type=int, default=2, help='output channel of network') parser.add_argument('--output_dir', default='./output', type=str, help='output dir') parser.add_argument('--max_iterations', type=int, default=30000, help='maximum epoch number to train') parser.add_argument('--max_epochs', type=int, default=1500, help='maximum epoch number to train') parser.add_argument('--batch_size', type=int, default=24, help='batch_size per gpu') parser.add_argument('--n_gpu', type=int, default=1, help='total gpu') parser.add_argument('--deterministic', type=int, default=1, help='whether use deterministic training') parser.add_argument('--base_lr', type=float, default=0.005, help='segmentation network learning rate') parser.add_argument('--img_size', type=int, default=224, help='input patch size of network input') parser.add_argument('--seed', type=int, default=1234, help='random seed') parser.add_argument('--cfg', type=str, required=True, metavar="FILE", help='path to config file', ) parser.add_argument( "--opts", help="Modify config options by adding 'KEY VALUE' pairs. ", default=None, nargs='+', ) parser.add_argument('--zip', action='store_true', help='use zipped dataset instead of folder dataset') parser.add_argument('--cache-mode', type=str, default='part', choices=['no', 'full', 'part'], help='no: no cache, ' 'full: cache all data, ' 'part: sharding the dataset into nonoverlapping pieces and only cache one piece') parser.add_argument('--resume', help='resume from checkpoint') parser.add_argument('--accumulation-steps', type=int, help="gradient accumulation steps") parser.add_argument('--use-checkpoint', action='store_true', help="whether to use gradient checkpointing to save memory") parser.add_argument('--amp-opt-level', type=str, default='O1', choices=['O0', 'O1', 'O2'], help='mixed precision opt level, if O0, no amp is used') parser.add_argument('--tag', help='tag of experiment') parser.add_argument('--eval', action='store_true', help='Perform evaluation only') parser.add_argument('--throughput', action='store_true', help='Test throughput only') args = parser.parse_args() if args.dataset == "Synapse": args.root_path = os.path.join(args.root_path, "train_npz")
config = get_config(args)
2
2023-11-04 11:42:02+00:00
4k
corcel-api/cortex.t
validators/text_validator.py
[ { "identifier": "StreamPrompting", "path": "template/protocol.py", "snippet": "class StreamPrompting(bt.StreamingSynapse):\n\n messages: List[Dict[str, str]] = pydantic.Field(\n ...,\n title=\"Messages\",\n description=\"A list of messages in the StreamPrompting scenario, \"\n \"each containing a role and content. Immutable.\",\n allow_mutation=False,\n )\n\n required_hash_fields: List[str] = pydantic.Field(\n [\"messages\"],\n title=\"Required Hash Fields\",\n description=\"A list of required fields for the hash.\",\n allow_mutation=False,\n )\n\n seed: int = pydantic.Field(\n default=\"1234\",\n title=\"Seed\",\n description=\"Seed for text generation. This attribute is immutable and cannot be updated.\",\n )\n\n temperature: float = pydantic.Field(\n default=0.0001,\n title=\"Temperature\",\n description=\"Temperature for text generation. \"\n \"This attribute is immutable and cannot be updated.\",\n )\n\n max_tokens: int = pydantic.Field(\n default=2048,\n title=\"Max Tokens\",\n description=\"Max tokens for text generation. \"\n \"This attribute is immutable and cannot be updated.\",\n )\n\n top_p: float = pydantic.Field(\n default=0.001,\n title=\"Top_p\",\n description=\"Top_p for text generation. The sampler will pick one of \"\n \"the top p percent tokens in the logit distirbution. \"\n \"This attribute is immutable and cannot be updated.\",\n )\n\n top_k: int = pydantic.Field(\n default=1,\n title=\"Top_k\",\n description=\"Top_k for text generation. Sampler will pick one of \"\n \"the k most probablistic tokens in the logit distribtion. \"\n \"This attribute is immutable and cannot be updated.\",\n )\n\n completion: str = pydantic.Field(\n None,\n title=\"Completion\",\n description=\"Completion status of the current StreamPrompting object. \"\n \"This attribute is mutable and can be updated.\",\n )\n\n provider: str = pydantic.Field(\n default=\"OpenAI\",\n title=\"Provider\",\n description=\"The provider to use when calling for your response.\"\n )\n\n model: str = pydantic.Field(\n default=\"gpt-3.5-turbo\",\n title=\"model\",\n description=\"The model to use when calling provider for your response.\",\n )\n\n async def process_streaming_response(self, response: StreamingResponse) -> AsyncIterator[str]:\n if self.completion is None:\n self.completion = \"\"\n async for chunk in response.content.iter_any():\n tokens = chunk.decode(\"utf-8\")\n for token in tokens:\n if token:\n self.completion += token\n yield tokens\n\n def deserialize(self) -> str:\n return self.completion\n\n def extract_response_json(self, response: StreamingResponse) -> dict:\n headers = {\n k.decode(\"utf-8\"): v.decode(\"utf-8\")\n for k, v in response.__dict__[\"_raw_headers\"]\n }\n\n def extract_info(prefix: str) -> dict[str, str]:\n return {\n key.split(\"_\")[-1]: value\n for key, value in headers.items()\n if key.startswith(prefix)\n }\n\n return {\n \"name\": headers.get(\"name\", \"\"),\n \"timeout\": float(headers.get(\"timeout\", 0)),\n \"total_size\": int(headers.get(\"total_size\", 0)),\n \"header_size\": int(headers.get(\"header_size\", 0)),\n \"dendrite\": extract_info(\"bt_header_dendrite\"),\n \"axon\": extract_info(\"bt_header_axon\"),\n \"messages\": self.messages,\n \"completion\": self.completion,\n }" }, { "identifier": "call_openai", "path": "template/utils.py", "snippet": "async def call_openai(messages, temperature, model, seed=1234, max_tokens=2048, top_p=1) -> str:\n for _ in range(2):\n bt.logging.debug(f\"Calling Openai. Temperature = {temperature}, Model = {model}, Seed = {seed}, Messages = {messages}\")\n try:\n response = await client.chat.completions.create(\n model=model,\n messages=messages,\n temperature=temperature,\n seed=seed,\n max_tokens=max_tokens,\n top_p=top_p,\n )\n response = response.choices[0].message.content\n bt.logging.trace(f\"validator response is {response}\")\n return response\n\n except Exception as e:\n bt.logging.error(f\"Error when calling OpenAI: {traceback.format_exc()}\")\n await asyncio.sleep(0.5)\n\n return None" }, { "identifier": "get_question", "path": "template/utils.py", "snippet": "async def get_question(category, num_questions_needed):\n if category not in [\"text\", \"images\"]:\n raise ValueError(\"Invalid category. Must be 'text' or 'images'.\")\n\n question = await update_counters_and_get_new_list(category, \"questions\", num_questions_needed)\n return question" }, { "identifier": "call_anthropic", "path": "template/utils.py", "snippet": "async def call_anthropic(prompt, temperature, model, max_tokens=2048, top_p=1, top_k=10000):\n try:\n client = AsyncAnthropicBedrock()\n bt.logging.debug(f\"Calling Anthropic. Model = {model}, Prompt = {prompt}, Temperature = {temperature}, Max Tokens = {max_tokens}\")\n completion = await client.completions.create(\n model=model,\n max_tokens_to_sample=max_tokens,\n temperature=temperature,\n prompt=f\"{anthropic_bedrock.HUMAN_PROMPT} {prompt} {anthropic_bedrock.AI_PROMPT}\",\n top_p=top_p,\n top_k=top_k,\n )\n bt.logging.trace(f\"Validator response is {completion.completion}\")\n\n return completion.completion\n except Exception as e:\n bt.logging.error(f\"Error when calling Anthropic: {traceback.format_exc()}\")\n await asyncio.sleep(0.5)" } ]
import asyncio import random import bittensor as bt import torch import template.reward from typing import AsyncIterator, Tuple from base_validator import BaseValidator from template.protocol import StreamPrompting from template.utils import call_openai, get_question, call_anthropic
1,719
class TextValidator(BaseValidator): def __init__(self, dendrite, config, subtensor, wallet: bt.wallet): super().__init__(dendrite, config, subtensor, wallet, timeout=60) self.streaming = True self.query_type = "text" self.model = "gpt-3.5-turbo" # "gpt-4-1106-preview" self.max_tokens = 2048 self.temperature = 0.0001 self.weight = 1 self.seed = 1234 self.top_p = 0.01 self.top_k = 1 self.provider = "OpenAI" self.wandb_data = { "modality": "text", "prompts": {}, "responses": {}, "scores": {}, "timestamps": {}, } async def organic(self, metagraph, query: dict[str, list[dict[str, str]]]) -> AsyncIterator[tuple[int, str]]: for uid, messages in query.items():
class TextValidator(BaseValidator): def __init__(self, dendrite, config, subtensor, wallet: bt.wallet): super().__init__(dendrite, config, subtensor, wallet, timeout=60) self.streaming = True self.query_type = "text" self.model = "gpt-3.5-turbo" # "gpt-4-1106-preview" self.max_tokens = 2048 self.temperature = 0.0001 self.weight = 1 self.seed = 1234 self.top_p = 0.01 self.top_k = 1 self.provider = "OpenAI" self.wandb_data = { "modality": "text", "prompts": {}, "responses": {}, "scores": {}, "timestamps": {}, } async def organic(self, metagraph, query: dict[str, list[dict[str, str]]]) -> AsyncIterator[tuple[int, str]]: for uid, messages in query.items():
syn = StreamPrompting(messages=messages, model=self.model, seed=self.seed, max_tokens=self.max_tokens, temperature=self.temperature, provider=self.provider, top_p=self.top_p, top_k=self.top_k)
0
2023-11-06 10:35:34+00:00
4k
flatypus/flowchat
examples/natural_language_cli.py
[ { "identifier": "autodedent", "path": "flowchat/autodedent.py", "snippet": "def autodedent(*text_lines) -> str:\n \"\"\"Format multiline strings, including with multiple levels of indentation, to align with the first line.\n\n Example:\n\n code = '''\n def add(a, b):\n return a + b\n '''\n\n autodedent(\n \"What does this code do?\",\n code,\n \"Suggest a comment that describes what this code does.\"\n )\n \"\"\"\n text_lines = [i if isinstance(i, str) else str(i) for i in text_lines]\n return dedent('\\n'.join(text_lines)).strip(\"\\n\")" }, { "identifier": "Chain", "path": "flowchat/chain.py", "snippet": "class Chain:\n def __init__(self, model: str, api_key: str = None, environ_key=\"OPENAI_API_KEY\"):\n super().__init__()\n\n if type(model) is not str:\n raise TypeError(\n f\"Model argument must be a string, not {type(model)}\"\n )\n\n if api_key is not None and type(api_key) is not str:\n raise TypeError(\n f\"API key argument must be a string, not {type(api_key)}\"\n )\n\n if type(environ_key) is not str:\n raise TypeError(\n f\"Environment key argument must be a string, not {type(environ_key)}\"\n )\n\n if api_key is None:\n api_key = os.environ.get(environ_key)\n\n if not api_key:\n raise ValueError(\n \"OpenAI API key not found. Please set the OPENAI_API_KEY environment variable, \"\n \"pass in an api_key parameter, or set the environ_key parameter to the environment \"\n \"variable that contains your API key.\"\n )\n openai.api_key = api_key\n\n self.model = model\n self.system = None\n self.user_prompt = []\n self.model_response = None\n self.prompt_tokens = 0\n self.completion_tokens = 0\n\n def _query_api(self, function: callable, *args, max_query_time=None, **kwargs):\n \"\"\"Call the API for max_query_time seconds, and if it times out, it will retry.\"\"\"\n timeouted_function = timeout(\n dec_timeout=max_query_time, use_signals=False)(function)\n return timeouted_function(*args, **kwargs)\n\n def _try_query_and_parse(self, function: callable, json_schema, *args, max_query_time=None, **kwargs):\n \"\"\"Query and try to parse the response, and if it fails, it will retry.\"\"\"\n completion = self._query_api(\n function, *args, max_query_time=max_query_time, **kwargs)\n\n if completion is None:\n return None\n\n if kwargs.get('stream', False):\n return completion\n\n message = completion.choices[0].message.content\n\n if not json_schema is None:\n open_bracket = message.find('{')\n close_bracket = message.rfind('}')\n message = message[open_bracket:close_bracket+1]\n try:\n message = json.loads(message)\n except json.JSONDecodeError:\n raise Exception(\n \"Response was not in the expected JSON format. Please try again. Check that you haven't accidentally lowered the max_tokens parameter so that the response is truncated.\"\n )\n\n self.prompt_tokens += completion.usage.prompt_tokens\n self.completion_tokens += completion.usage.completion_tokens\n\n return message\n\n def _ask(\n self,\n system: Message,\n user_messages: List[Message],\n json_schema: Any = None,\n max_query_time=None,\n tries=-1,\n **params\n ):\n \"\"\"Ask a question to the chatbot with a system prompt and return the response.\"\"\"\n if not user_messages:\n return None\n\n messages = [\n system,\n *user_messages\n ] if system else user_messages\n\n message = retry(delay=1, logger=logging, tries=tries)(self._try_query_and_parse)(\n openai.chat.completions.create,\n json_schema=json_schema,\n messages=messages,\n max_query_time=max_query_time,\n **params\n )\n\n return message\n\n def _format_images(self, image: str | ImageFormat | Any):\n \"\"\"Format whatever image format we receive into the specific format that OpenAI's API expects.\"\"\"\n if isinstance(image, str):\n return {\"url\": image}\n elif not isinstance(image, dict):\n # not string or dict so assume PIL image\n # no specific file format, so default to PNG\n return {\"url\": _encode_image(image, \"PNG\")}\n else:\n # we've received an object then; encode the image if necessary\n if 'url' not in image:\n raise Exception(\n \"Image object must have a url property.\"\n )\n if isinstance(image['url'], str):\n url = image['url']\n else:\n file_format = image['format_type'] if 'format_type' in image else \"PNG\"\n url = _encode_image(image['url'], file_format)\n\n return {\n \"url\": url,\n **({\"detail\": image[\"detail\"]} if \"detail\" in image else {})\n }\n\n def unhook(self):\n \"\"\"Reset the chain's system and user prompt. The previous response is kept.\"\"\"\n self.system = None\n self.user_prompt = []\n return self\n\n def anchor(self, system_prompt: str):\n \"\"\"Set the chain's system prompt.\"\"\"\n if not isinstance(system_prompt, str):\n raise TypeError(\n f\"System prompt must be a string, not {type(system_prompt)}\"\n )\n\n self.system = {\"role\": \"system\", \"content\": system_prompt}\n return self\n\n def transform(self, function: Callable[[str], str]):\n \"\"\"Transform the chain's model response with a function.\"\"\"\n if not callable(function):\n raise TypeError(\n f\"Transform function must be callable, not {type(function)}\"\n )\n\n self.model_response = function(self.model_response)\n return self\n\n def link(self, modifier: Union[Callable[[str], None], str], model: str = None, assistant=False, images: str | Any | List[str | Any] | ImageFormat = None):\n \"\"\"Modify the chain's user prompt with a function, or just pass in a string to be added to the message list.\n\n For example:\n ```\n chain = (Chain()\n .anchor(\"Hello!\")\n .link(\"How are you?\")\n .pull().unhook()\n\n .link(lambda response: f\"What emotions characterize this response? {response}\")\n .pull()\n .log())\n ```\n \"\"\"\n if model is None:\n model = self.model\n\n if not callable(modifier) and not isinstance(modifier, str):\n raise TypeError(\n f\"Modifier must be callable or string, not {type(modifier)}\"\n )\n\n if isinstance(modifier, str) and modifier == \"\":\n raise ValueError(\n \"Modifier cannot be an empty string.\"\n )\n\n prompt = modifier(self.model_response) if callable(\n modifier) else modifier\n\n role = \"assistant\" if assistant else \"user\"\n\n if images is None:\n self.user_prompt.append({\"role\": role, \"content\": prompt})\n else:\n # images accepts a string (url), a PIL image, as well as a specific typed dict, or a list of any of these\n images = [images] if not isinstance(images, list) else images\n images = [\n {\"type\": \"image_url\", \"image_url\": self._format_images(image)}\n for image in images\n ]\n self.user_prompt.append(\n {\"role\": role, \"content\": [\n {\"type\": \"text\", \"text\": prompt},\n *images\n ]}\n )\n return self\n\n def pull(\n self,\n model: str = None,\n frequency_penalty: float | int = None,\n json_schema: Any = None,\n logit_bias: Dict[str, float | int] = None,\n max_query_time=None,\n max_tokens: float | int = None,\n n: float | int = None,\n presence_penalty: float | int = None,\n response_format: ResponseFormat = None,\n seed: int = None,\n stop: str | List[str] = None,\n temperature: float | int = None,\n top_p: float | int = None,\n tries: int = -1\n ):\n \"\"\"Make a request to the LLM and set the response.\"\"\"\n if model is None:\n model = self.model\n\n params = {\n 'frequency_penalty': frequency_penalty,\n 'logit_bias': logit_bias,\n 'max_query_time': max_query_time,\n 'max_tokens': max_tokens,\n 'model': model,\n 'n': n,\n 'presence_penalty': presence_penalty,\n 'response_format': response_format,\n 'seed': seed,\n 'stop': stop,\n 'temperature': temperature,\n 'top_p': top_p,\n }\n\n params = {k: v for k, v in params.items() if v is not None}\n\n if json_schema is not None:\n if not isinstance(json_schema, dict):\n raise TypeError(\n f\"JSON schema must be a dictionary, not {type(json_schema)}\"\n )\n\n params['response_format'] = {'type': 'json_object'}\n params['model'] = 'gpt-4-1106-preview'\n self.user_prompt[-1]['content'] += autodedent(\n \"You must respond in the following example JSON format. Remember to enclose the entire JSON object in curly braces:\",\n json.dumps(json_schema, indent=4)\n )\n\n response = self._ask(\n self.system, self.user_prompt,\n json_schema, tries=tries, **params\n )\n\n self.model_response = response\n return self\n\n def stream(\n self,\n plain_text_stream: bool = False,\n model: str = None,\n frequency_penalty: float | int = None,\n logit_bias: Dict[str, float | int] = None,\n max_query_time=None,\n max_tokens: float | int = None,\n n: float | int = None,\n presence_penalty: float | int = None,\n seed: int = None,\n stop: str | List[str] = None,\n temperature: float | int = None,\n top_p: float | int = None,\n ):\n \"\"\"Returns a generator that yields responses from the LLM.\"\"\"\n if model is None:\n model = self.model\n\n params = {\n 'frequency_penalty': frequency_penalty,\n 'logit_bias': logit_bias,\n 'max_query_time': max_query_time,\n 'max_tokens': max_tokens,\n 'model': model,\n 'n': n,\n 'presence_penalty': presence_penalty,\n 'seed': seed,\n 'stop': stop,\n 'temperature': temperature,\n 'top_p': top_p,\n 'stream': True\n }\n\n params = {k: v for k, v in params.items() if v is not None}\n\n if not plain_text_stream:\n return self._ask(\n self.system, self.user_prompt,\n None, **params\n )\n\n return (response.choices[0].delta.content\n for response in self._ask(self.system, self.user_prompt, None, **params))\n\n def last(self) -> str:\n \"\"\"Return the chain's last model response.\"\"\"\n return self.model_response\n\n def token_usage(self) -> int:\n \"\"\"Return the number of tokens used\"\"\"\n return self.prompt_tokens, self.completion_tokens\n\n def log(self):\n \"\"\"Log the chain's system prompt, user prompt, and model response.\"\"\"\n print('='*60)\n print(f\"System: {self.system}\")\n print(f\"User: {self.user_prompt}\")\n print(f\"Text: {self.model_response}\")\n print('='*60)\n print(\"\\n\")\n return self\n\n def log_tokens(self):\n \"\"\"Log the number of tokens used\"\"\"\n prompt, completion = self.token_usage()\n print(f\"Prompt tokens: {prompt}\")\n print(f\"Completion tokens: {completion}\")\n print(f\"Total tokens: {prompt + completion}\")\n return self" } ]
from flowchat import Chain, autodedent import os import subprocess
3,140
def execute_system_command(command): try: result = subprocess.run( command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True ) return result.stdout except subprocess.CalledProcessError as e: return e.stderr def main(): print("Welcome to the Natural Language Command Line Interface!") os_system_context = f"You are a shell interpreter assistant running on {os.name} operating system." while True: user_input = input("Please enter your command in natural language: ") # ========================================================================== # should_exit = ( Chain(model="gpt-3.5-turbo")
def execute_system_command(command): try: result = subprocess.run( command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True ) return result.stdout except subprocess.CalledProcessError as e: return e.stderr def main(): print("Welcome to the Natural Language Command Line Interface!") os_system_context = f"You are a shell interpreter assistant running on {os.name} operating system." while True: user_input = input("Please enter your command in natural language: ") # ========================================================================== # should_exit = ( Chain(model="gpt-3.5-turbo")
.link(autodedent(
0
2023-11-08 00:45:21+00:00
4k
WHU-USI3DV/PatchAugNet
place_recognition/Minkloc3D_V2/models/losses/loss.py
[ { "identifier": "TrainingParams", "path": "place_recognition/Minkloc3D_V2/misc/utils.py", "snippet": "class TrainingParams:\n \"\"\"\n Parameters for model training\n \"\"\"\n def __init__(self, params_path: str, model_params_path: str, debug: bool = False):\n \"\"\"\n Configuration files\n :param path: Training configuration file\n :param model_params: Model-specific configuration file\n \"\"\"\n\n assert os.path.exists(params_path), 'Cannot find configuration file: {}'.format(params_path)\n assert os.path.exists(model_params_path), 'Cannot find model-specific configuration file: {}'.format(model_params_path)\n self.params_path = params_path\n self.model_params_path = model_params_path\n self.debug = debug\n\n config = configparser.ConfigParser()\n\n config.read(self.params_path)\n params = config['DEFAULT']\n self.dataset_folder = params.get('dataset_folder')\n\n params = config['TRAIN']\n self.save_freq = params.getint('save_freq', 0) # Model saving frequency (in epochs)\n self.num_workers = params.getint('num_workers', 0)\n\n # Initial batch size for global descriptors (for both main and secondary datasets)\n self.batch_size = params.getint('batch_size', 64)\n # When batch_split_size is non-zero, multistage backpropagation is enabled\n self.batch_split_size = params.getint('batch_split_size', None)\n\n # Set batch_expansion_th to turn on dynamic batch sizing\n # When number of non-zero triplets falls below batch_expansion_th, expand batch size\n self.batch_expansion_th = params.getfloat('batch_expansion_th', None)\n if self.batch_expansion_th is not None:\n assert 0. < self.batch_expansion_th < 1., 'batch_expansion_th must be between 0 and 1'\n self.batch_size_limit = params.getint('batch_size_limit', 256)\n # Batch size expansion rate\n self.batch_expansion_rate = params.getfloat('batch_expansion_rate', 1.5)\n assert self.batch_expansion_rate > 1., 'batch_expansion_rate must be greater than 1'\n else:\n self.batch_size_limit = self.batch_size\n self.batch_expansion_rate = None\n\n self.val_batch_size = params.getint('val_batch_size', self.batch_size_limit)\n\n self.lr = params.getfloat('lr', 1e-3)\n self.epochs = params.getint('epochs', 20)\n self.optimizer = params.get('optimizer', 'Adam')\n self.scheduler = params.get('scheduler', 'MultiStepLR')\n if self.scheduler is not None:\n if self.scheduler == 'CosineAnnealingLR':\n self.min_lr = params.getfloat('min_lr')\n elif self.scheduler == 'MultiStepLR':\n if 'scheduler_milestones' in params:\n scheduler_milestones = params.get('scheduler_milestones')\n self.scheduler_milestones = [int(e) for e in scheduler_milestones.split(',')]\n else:\n self.scheduler_milestones = [self.epochs+1]\n else:\n raise NotImplementedError('Unsupported LR scheduler: {}'.format(self.scheduler))\n\n self.weight_decay = params.getfloat('weight_decay', None)\n self.loss = params.get('loss').lower()\n if 'contrastive' in self.loss:\n self.pos_margin = params.getfloat('pos_margin', 0.2)\n self.neg_margin = params.getfloat('neg_margin', 0.65)\n elif 'triplet' in self.loss:\n self.margin = params.getfloat('margin', 0.4) # Margin used in loss function\n elif self.loss == 'truncatedsmoothap':\n # Number of best positives (closest to the query) to consider\n self.positives_per_query = params.getint(\"positives_per_query\", 4)\n # Temperatures (annealing parameter) and numbers of nearest neighbours to consider\n self.tau1 = params.getfloat('tau1', 0.01)\n self.margin = params.getfloat('margin', None) # Margin used in loss function\n\n # Similarity measure: based on cosine similarity or Euclidean distance\n self.similarity = params.get('similarity', 'euclidean')\n assert self.similarity in ['cosine', 'euclidean']\n\n self.aug_mode = params.getint('aug_mode', 1) # Augmentation mode (1 is default)\n self.set_aug_mode = params.getint('set_aug_mode', 1) # Augmentation mode (1 is default)\n self.train_file = params.get('train_file')\n self.val_file = params.get('val_file', None)\n self.test_file = params.get('test_file', None)\n\n # Read model parameters\n self.model_params = ModelParams(self.model_params_path)\n self._check_params()\n\n def _check_params(self):\n assert os.path.exists(self.dataset_folder), 'Cannot access datasets: {}'.format(self.dataset_folder)\n\n def print(self):\n print('Parameters:')\n param_dict = vars(self)\n for e in param_dict:\n if e != 'model_params':\n print('{}: {}'.format(e, param_dict[e]))\n\n self.model_params.print()\n print('')" }, { "identifier": "TruncatedSmoothAP", "path": "place_recognition/Minkloc3D_V2/models/losses/truncated_smoothap.py", "snippet": "class TruncatedSmoothAP:\n def __init__(self, tau1: float = 0.01, similarity: str = 'cosine', positives_per_query: int = 4):\n # We reversed the notation compared to the paper (tau1 is sigmoid on similarity differences)\n # tau1: sigmoid temperature applied on similarity differences\n # positives_per_query: number of positives per query to consider\n # negatives_only: if True in denominator we consider positives and negatives; if False we consider all elements\n # (with except to the anchor itself)\n\n self.tau1 = tau1\n self.similarity = similarity\n self.positives_per_query = positives_per_query\n\n def __call__(self, embeddings, positives_mask, negatives_mask):\n device = embeddings.device\n\n positives_mask = positives_mask.to(device)\n negatives_mask = negatives_mask.to(device)\n\n # Ranking of the retrieval set\n # For each element we ignore elements that are neither positives nor negatives\n\n # Compute cosine similarity scores\n # 1st dimension corresponds to q, 2nd dimension to z\n s_qz = compute_aff(embeddings, similarity=self.similarity)\n\n # Find the positives_per_query closest positives for each query\n s_positives = s_qz.detach().clone()\n s_positives.masked_fill_(torch.logical_not(positives_mask), np.NINF)\n #closest_positives_ndx = torch.argmax(s_positives, dim=1).view(-1, 1) # Indices of closests positives for each query\n top_k = self.positives_per_query\n if top_k > embeddings.shape[0]:\n top_k = embeddings.shape[0]\n closest_positives_ndx = torch.topk(s_positives, k=top_k, dim=1, largest=True, sorted=True)[1]\n # closest_positives_ndx is (batch_size, positives_per_query) with positives_per_query closest positives\n # per each batch element\n\n n_positives = positives_mask.sum(dim=1) # Number of positives for each anchor\n\n # Compute the rank of each example x with respect to query element q as per Eq. (2)\n s_diff = s_qz.unsqueeze(1) - s_qz.gather(1, closest_positives_ndx).unsqueeze(2)\n s_sigmoid = sigmoid(s_diff, temp=self.tau1)\n\n # Compute the nominator in Eq. 2 and 5 - for q compute the ranking of each of its positives with respect to other positives of q\n # Filter out z not in Positives\n pos_mask = positives_mask.unsqueeze(1)\n pos_s_sigmoid = s_sigmoid * pos_mask\n\n # Filter out z on the same position as the positive (they have value = 0.5, as the similarity difference is zero)\n mask = torch.ones_like(pos_s_sigmoid).scatter(2, closest_positives_ndx.unsqueeze(2), 0.)\n pos_s_sigmoid = pos_s_sigmoid * mask\n\n # Compute the rank for each query and its positives_per_query closest positive examples with respect to other positives\n r_p = torch.sum(pos_s_sigmoid, dim=2) + 1.\n # r_p is (batch_size, positives_per_query) matrix\n\n # Consider only positives and negatives in the denominator\n # Compute the denominator in Eq. 5 - add sum of Indicator function for negatives (or non-positives)\n neg_mask = negatives_mask.unsqueeze(1)\n neg_s_sigmoid = s_sigmoid * neg_mask\n r_omega = r_p + torch.sum(neg_s_sigmoid, dim=2)\n\n # Compute R(i, S_p) / R(i, S_omega) ration in Eq. 2\n r = r_p / r_omega\n\n # Compute metrics mean ranking of the positive example, recall@1\n stats = {}\n # Mean number of positives per query\n stats['positives_per_query'] = n_positives.float().mean(dim=0).item()\n # Mean ranking of selected positive examples (closests positives)\n temp = s_diff.detach() > 0\n temp = torch.logical_and(temp[:, 0], negatives_mask) # Take the best positive\n hard_ranking = temp.sum(dim=1)\n stats['best_positive_ranking'] = hard_ranking.float().mean(dim=0).item()\n # Recall at 1~top_k and at 1%\n stats['recall'] = {}\n for i in range(1, self.positives_per_query+1):\n if i > top_k:\n stats['recall'][i] = stats['recall'][top_k]\n else:\n stats['recall'][i] = (hard_ranking <= i).float().mean(dim=0).item()\n\n # r is (N, positives_per_query) tensor\n # Zero entries not corresponding to real positives - this happens when the number of true positives is lower than positives_per_query\n valid_positives_mask = torch.gather(positives_mask, 1, closest_positives_ndx) # () tensor\n masked_r = r * valid_positives_mask\n n_valid_positives = valid_positives_mask.sum(dim=1)\n\n # Filter out rows (queries) without any positive to avoid division by zero\n valid_q_mask = n_valid_positives > 0\n masked_r = masked_r[valid_q_mask]\n\n ap = (masked_r.sum(dim=1) / n_valid_positives[valid_q_mask]).mean()\n loss = 1. - ap\n\n stats['loss'] = loss.item()\n stats['ap'] = ap.item()\n stats['avg_embedding_norm'] = embeddings.norm(dim=1).mean().item()\n return loss, stats" } ]
from pytorch_metric_learning import losses, reducers from pytorch_metric_learning.distances import LpDistance from place_recognition.Minkloc3D_V2.misc.utils import TrainingParams from place_recognition.Minkloc3D_V2.models.losses.loss_utils import * from place_recognition.Minkloc3D_V2.models.losses.truncated_smoothap import TruncatedSmoothAP
2,809
# Warsaw University of Technology def make_losses(params: TrainingParams): if params.loss == 'batchhardtripletmarginloss': # BatchHard mining with triplet margin loss # Expects input: embeddings, positives_mask, negatives_mask loss_fn = BatchHardTripletLossWithMasks(params.margin) elif params.loss == 'batchhardcontrastiveloss': loss_fn = BatchHardContrastiveLossWithMasks(params.pos_margin, params.neg_margin) elif params.loss == 'truncatedsmoothap':
# Warsaw University of Technology def make_losses(params: TrainingParams): if params.loss == 'batchhardtripletmarginloss': # BatchHard mining with triplet margin loss # Expects input: embeddings, positives_mask, negatives_mask loss_fn = BatchHardTripletLossWithMasks(params.margin) elif params.loss == 'batchhardcontrastiveloss': loss_fn = BatchHardContrastiveLossWithMasks(params.pos_margin, params.neg_margin) elif params.loss == 'truncatedsmoothap':
loss_fn = TruncatedSmoothAP(tau1=params.tau1, similarity=params.similarity,
1
2023-11-02 13:52:20+00:00
4k
WeiLab-Biology/DeepProSite
DeepProSite-main/edge_features.py
[ { "identifier": "gather_edges", "path": "self_attention.py", "snippet": "def gather_edges(edges, neighbor_idx):\n # Features [B,N,N,C] at Neighbor indices [B,N,K] => Neighbor features [B,N,K,C]\n neighbors = neighbor_idx.unsqueeze(-1).expand(-1, -1, -1, edges.size(-1))\n edge_features = torch.gather(edges, 2, neighbors)\n return edge_features" }, { "identifier": "gather_nodes", "path": "self_attention.py", "snippet": "def gather_nodes(nodes, neighbor_idx):\n # Features [B,N,C] at Neighbor indices [B,N,K] => [B,N,K,C]\n # Flatten and expand indices per batch [B,N,K] => [B,NK] => [B,NK,C]\n neighbors_flat = neighbor_idx.view((neighbor_idx.shape[0], -1))\n neighbors_flat = neighbors_flat.unsqueeze(-1).expand(-1, -1, nodes.size(2))\n # Gather and re-pack\n neighbor_features = torch.gather(nodes, 1, neighbors_flat)\n neighbor_features = neighbor_features.view(list(neighbor_idx.shape)[:3] + [-1])\n return neighbor_features" }, { "identifier": "Normalize", "path": "self_attention.py", "snippet": "class Normalize(nn.Module): \n def __init__(self, features, epsilon=1e-6):\n super(Normalize, self).__init__()\n self.gain = nn.Parameter(torch.ones(features))\n self.bias = nn.Parameter(torch.zeros(features))\n self.epsilon = epsilon\n\n def forward(self, x, dim=-1):\n mu = x.mean(dim, keepdim=True)\n sigma = torch.sqrt(x.var(dim, keepdim=True) + self.epsilon)\n gain = self.gain\n bias = self.bias\n # Reshape\n if dim != -1:\n shape = [1] * len(mu.size())\n shape[dim] = self.gain.size()[0]\n gain = gain.view(shape)\n bias = bias.view(shape)\n return gain * (x - mu) / (sigma + self.epsilon) + bias" } ]
import numpy as np import torch import torch.nn as nn import torch.nn.functional as F from self_attention import gather_edges, gather_nodes, Normalize
1,847
class PositionalEncodings(nn.Module): def __init__(self, num_embeddings): super(PositionalEncodings, self).__init__() self.num_embeddings = num_embeddings def forward(self, E_idx): # i-j N_batch = E_idx.size(0) N_nodes = E_idx.size(1) N_neighbors = E_idx.size(2) ii = torch.arange(N_nodes, dtype=torch.float32).view((1, -1, 1)).cuda() d = (E_idx.float() - ii).unsqueeze(-1) # Original Transformer frequencies frequency = torch.exp( torch.arange(0, self.num_embeddings, 2, dtype=torch.float32) * -(np.log(10000.0) / self.num_embeddings)).cuda() angles = d * frequency.view((1,1,1,-1)) E = torch.cat((torch.cos(angles), torch.sin(angles)), -1) return E # [N_batch, N_nodes, N_neighbors, num_embeddings] class EdgeFeatures(nn.Module): def __init__(self, edge_features, num_positional_embeddings=16, num_rbf=16, top_k=30, augment_eps=0.): super(EdgeFeatures, self).__init__() self.top_k = top_k self.augment_eps = augment_eps self.num_rbf = num_rbf # Positional encoding self.PE = PositionalEncodings(num_positional_embeddings) # Embedding and normalization self.edge_embedding = nn.Linear(num_positional_embeddings + num_rbf + 7, edge_features, bias=True) self.norm_edges = Normalize(edge_features) def _dist(self, X, mask, eps=1E-6): """ Pairwise euclidean distances """ mask_2D = torch.unsqueeze(mask,1) * torch.unsqueeze(mask,2) # mask [N, L] => mask_2D [N, L, L] dX = torch.unsqueeze(X,1) - torch.unsqueeze(X,2) # X 坐标矩阵 [N, L, 3] dX 坐标差矩阵 [N, L, L, 3] D = mask_2D * torch.sqrt(torch.sum(dX**2, 3) + eps) # 距离矩阵 [N, L, L] # Identify k nearest neighbors (including self) D_max, _ = torch.max(D, -1, keepdim=True) D_adjust = D + (1. - mask_2D) * D_max D_neighbors, E_idx = torch.topk(D_adjust, self.top_k, dim=-1, largest=False) # [N, L, k] D_neighbors为具体距离值(从小到大),E_idx为对应邻居节点的编号 return D_neighbors, E_idx def _rbf(self, D): # Distance radial basis function D_min, D_max, D_count = 0., 20., self.num_rbf D_mu = torch.linspace(D_min, D_max, D_count).cuda() D_mu = D_mu.view([1,1,1,-1]) D_sigma = (D_max - D_min) / D_count D_expand = torch.unsqueeze(D, -1) RBF = torch.exp(-((D_expand - D_mu) / D_sigma)**2) return RBF # [B, L, K, self.num_rbf] def _quaternions(self, R): """ Convert a batch of 3D rotations [R] to quaternions [Q] R [...,3,3] Q [...,4] """ # Simple Wikipedia version # en.wikipedia.org/wiki/Rotation_matrix#Quaternion # For other options see math.stackexchange.com/questions/2074316/calculating-rotation-axis-from-rotation-matrix diag = torch.diagonal(R, dim1=-2, dim2=-1) Rxx, Ryy, Rzz = diag.unbind(-1) magnitudes = 0.5 * torch.sqrt(torch.abs(1 + torch.stack([ Rxx - Ryy - Rzz, - Rxx + Ryy - Rzz, - Rxx - Ryy + Rzz ], -1))) _R = lambda i,j: R[:,:,:,i,j] signs = torch.sign(torch.stack([ _R(2,1) - _R(1,2), _R(0,2) - _R(2,0), _R(1,0) - _R(0,1) ], -1)) xyz = signs * magnitudes # The relu enforces a non-negative trace w = torch.sqrt(F.relu(1 + diag.sum(-1, keepdim=True))) / 2. Q = torch.cat((xyz, w), -1) Q = F.normalize(Q, dim=-1) return Q def _orientations(self, X, E_idx, eps=1e-6): # Shifted slices of unit vectors dX = X[:,1:,:] - X[:,:-1,:] U = F.normalize(dX, dim=-1) # 少了第一个(u0) u_2 = U[:,:-2,:] # u 1~n-2 u_1 = U[:,1:-1,:] # u 2~n-1 # Backbone normals n_2 = F.normalize(torch.cross(u_2, u_1), dim=-1) # n 1~n-2 # Build relative orientations o_1 = F.normalize(u_2 - u_1, dim=-1) # b 角平分线向量 O = torch.stack((o_1, n_2, torch.cross(o_1, n_2)), 2) O = O.view(list(O.shape[:2]) + [9]) O = F.pad(O, (0,0,1,2), 'constant', 0) # [B, L, 9]
class PositionalEncodings(nn.Module): def __init__(self, num_embeddings): super(PositionalEncodings, self).__init__() self.num_embeddings = num_embeddings def forward(self, E_idx): # i-j N_batch = E_idx.size(0) N_nodes = E_idx.size(1) N_neighbors = E_idx.size(2) ii = torch.arange(N_nodes, dtype=torch.float32).view((1, -1, 1)).cuda() d = (E_idx.float() - ii).unsqueeze(-1) # Original Transformer frequencies frequency = torch.exp( torch.arange(0, self.num_embeddings, 2, dtype=torch.float32) * -(np.log(10000.0) / self.num_embeddings)).cuda() angles = d * frequency.view((1,1,1,-1)) E = torch.cat((torch.cos(angles), torch.sin(angles)), -1) return E # [N_batch, N_nodes, N_neighbors, num_embeddings] class EdgeFeatures(nn.Module): def __init__(self, edge_features, num_positional_embeddings=16, num_rbf=16, top_k=30, augment_eps=0.): super(EdgeFeatures, self).__init__() self.top_k = top_k self.augment_eps = augment_eps self.num_rbf = num_rbf # Positional encoding self.PE = PositionalEncodings(num_positional_embeddings) # Embedding and normalization self.edge_embedding = nn.Linear(num_positional_embeddings + num_rbf + 7, edge_features, bias=True) self.norm_edges = Normalize(edge_features) def _dist(self, X, mask, eps=1E-6): """ Pairwise euclidean distances """ mask_2D = torch.unsqueeze(mask,1) * torch.unsqueeze(mask,2) # mask [N, L] => mask_2D [N, L, L] dX = torch.unsqueeze(X,1) - torch.unsqueeze(X,2) # X 坐标矩阵 [N, L, 3] dX 坐标差矩阵 [N, L, L, 3] D = mask_2D * torch.sqrt(torch.sum(dX**2, 3) + eps) # 距离矩阵 [N, L, L] # Identify k nearest neighbors (including self) D_max, _ = torch.max(D, -1, keepdim=True) D_adjust = D + (1. - mask_2D) * D_max D_neighbors, E_idx = torch.topk(D_adjust, self.top_k, dim=-1, largest=False) # [N, L, k] D_neighbors为具体距离值(从小到大),E_idx为对应邻居节点的编号 return D_neighbors, E_idx def _rbf(self, D): # Distance radial basis function D_min, D_max, D_count = 0., 20., self.num_rbf D_mu = torch.linspace(D_min, D_max, D_count).cuda() D_mu = D_mu.view([1,1,1,-1]) D_sigma = (D_max - D_min) / D_count D_expand = torch.unsqueeze(D, -1) RBF = torch.exp(-((D_expand - D_mu) / D_sigma)**2) return RBF # [B, L, K, self.num_rbf] def _quaternions(self, R): """ Convert a batch of 3D rotations [R] to quaternions [Q] R [...,3,3] Q [...,4] """ # Simple Wikipedia version # en.wikipedia.org/wiki/Rotation_matrix#Quaternion # For other options see math.stackexchange.com/questions/2074316/calculating-rotation-axis-from-rotation-matrix diag = torch.diagonal(R, dim1=-2, dim2=-1) Rxx, Ryy, Rzz = diag.unbind(-1) magnitudes = 0.5 * torch.sqrt(torch.abs(1 + torch.stack([ Rxx - Ryy - Rzz, - Rxx + Ryy - Rzz, - Rxx - Ryy + Rzz ], -1))) _R = lambda i,j: R[:,:,:,i,j] signs = torch.sign(torch.stack([ _R(2,1) - _R(1,2), _R(0,2) - _R(2,0), _R(1,0) - _R(0,1) ], -1)) xyz = signs * magnitudes # The relu enforces a non-negative trace w = torch.sqrt(F.relu(1 + diag.sum(-1, keepdim=True))) / 2. Q = torch.cat((xyz, w), -1) Q = F.normalize(Q, dim=-1) return Q def _orientations(self, X, E_idx, eps=1e-6): # Shifted slices of unit vectors dX = X[:,1:,:] - X[:,:-1,:] U = F.normalize(dX, dim=-1) # 少了第一个(u0) u_2 = U[:,:-2,:] # u 1~n-2 u_1 = U[:,1:-1,:] # u 2~n-1 # Backbone normals n_2 = F.normalize(torch.cross(u_2, u_1), dim=-1) # n 1~n-2 # Build relative orientations o_1 = F.normalize(u_2 - u_1, dim=-1) # b 角平分线向量 O = torch.stack((o_1, n_2, torch.cross(o_1, n_2)), 2) O = O.view(list(O.shape[:2]) + [9]) O = F.pad(O, (0,0,1,2), 'constant', 0) # [B, L, 9]
O_neighbors = gather_nodes(O, E_idx) # [B, L, K, 9]
1
2023-11-04 15:32:31+00:00
4k
gchada/ROAM
real/rail_walker_gym/joystick_policy/reward_providers.py
[ { "identifier": "near_quadratic_bound", "path": "real/rail_walker_gym/joystick_policy/reward_util.py", "snippet": "def near_quadratic_bound(value, target, left_margin, right_margin, out_of_margin_activation : str | None = \"linear\", power = 2.0, value_at_margin = 0.0):\n delta = value-target\n fract = delta/right_margin if delta > 0 else delta/left_margin\n \n if out_of_margin_activation is None or out_of_margin_activation != \"near_quadratic\":\n clipped_fract = np.clip(fract, -1.0, 1.0)\n rew = 1 - (1-value_at_margin) * (np.abs(clipped_fract) ** power)\n oodfract = fract - clipped_fract\n if out_of_margin_activation == \"linear\":\n rew -= (1-value_at_margin) * np.abs(oodfract)\n elif out_of_margin_activation == \"quadratic\":\n rew -= (1-value_at_margin) * (oodfract ** 2)\n elif out_of_margin_activation == \"gaussian\":\n rew += value_at_margin * np.exp(-oodfract**2/0.25)\n elif out_of_margin_activation == \"near_quadratic\":\n rew = 1 - (1-value_at_margin) * (np.abs(fract) ** power)\n return rew" }, { "identifier": "calculate_gaussian_activation", "path": "real/rail_walker_gym/joystick_policy/reward_util.py", "snippet": "def calculate_gaussian_activation(x : float | np.ndarray):\n return np.exp(-np.linalg.norm(x)**2/0.25)" }, { "identifier": "calculate_torque", "path": "real/rail_walker_gym/joystick_policy/reward_util.py", "snippet": "def calculate_torque(current_qpos : np.ndarray, current_qvel : np.ndarray, target_qpos : np.ndarray, Kp : float, Kd : float):\n return Kp * (target_qpos - current_qpos) - Kd * current_qvel" } ]
import numpy as np import transforms3d as tr3d import typing from typing import Any,Optional from rail_walker_interface import JoystickPolicyRewardProvider, JoystickPolicyTerminationConditionProvider from rail_walker_interface import BaseWalker from rail_mujoco_walker import RailSimWalkerDMControl from dm_control.utils import rewards from collections import deque from rail_walker_interface.joystick_policy.joystick_interfaces import JoystickPolicyTerminationConditionProvider from rail_walker_interface.robot.robot import BaseWalker from .reward_util import near_quadratic_bound, calculate_gaussian_activation, calculate_torque
1,799
def get_reward(self) -> float: return self.rew def reset_reward( self, Robot: BaseWalker, info_dict: dict[str,Any], termination_provider_triggered: JoystickPolicyTerminationConditionProvider, randomState: np.random.RandomState ) -> None: self.rew = 0.0 def step_reward( self, Robot: BaseWalker, action_target_qpos: np.ndarray, target_goal_world_delta: np.ndarray, target_goal_local: np.ndarray, target_yaw : float, target_delta_yaw: float, target_velocity: float, velocity_to_goal: float, change_in_abs_target_delta_yaw : float, target_custom_data: Optional[Any], enable_target_custom_obs : bool, info_dict: dict[str,Any], randomState: np.random.RandomState ) -> None: roll, pitch, yaw = Robot.get_roll_pitch_yaw() velocity_local = Robot.get_3d_local_velocity() projected_x_velocity = np.cos(pitch) * velocity_local[0] reward_v = rewards.tolerance( projected_x_velocity, bounds=(target_velocity,2*target_velocity), margin=2*target_velocity, value_at_margin=0, sigmoid='linear' ) * (target_velocity / 0.5) * 10.0 penalty_drpy = 1.0 * np.abs(Robot.get_3d_angular_velocity()[-1]) reward_perstep = reward_v - penalty_drpy info_dict["reward_v"] = reward_v info_dict["penalty_drpy"] = penalty_drpy reward_perstep *= max(Robot.get_foot_contact()) if hasattr(Robot, "get_foot_contact") else 1.0 self.rew = reward_perstep class JoystickPolicyStrictRewardProvider(JoystickPolicyRewardProvider[BaseWalker]): def __init__( self, energy_penalty_weight: float = 0.0, smooth_torque_penalty_weight: float = 0.0, joint_diagonal_penalty_weight: float = 0.0, joint_shoulder_penalty_weight : float = 0.0, joint_acc_penalty_weight : float = 0.0, thigh_torque_penalty_weight: float = 0.0, joint_vel_penalty_weight : float = 0.0, pitch_rate_penalty_factor: float = 0.0, roll_rate_penalty_factor: float = 0.0, qpos_penalty_weight: float = 0.0, contact_reward_weight : float = 0.0 ) -> None: self.energy_penalty_weight = energy_penalty_weight self.smooth_torque_penalty_weight = smooth_torque_penalty_weight self.joint_diagonal_penalty_weight = joint_diagonal_penalty_weight self.joint_shoulder_penalty_weight = joint_shoulder_penalty_weight self.joint_acc_penalty_weight = joint_acc_penalty_weight self.joint_vel_penalty_weight = joint_vel_penalty_weight self.pitch_rate_penalty_factor = pitch_rate_penalty_factor self.roll_rate_penalty_factor = roll_rate_penalty_factor self.qpos_penalty_weight = qpos_penalty_weight self.thigh_torque_penalty_weight = thigh_torque_penalty_weight self.contact_reward_weight = contact_reward_weight self.rew = 0.0 self._last_torque = None def get_reward(self) -> float: return self.rew def reset_reward( self, Robot: BaseWalker, info_dict: dict[str,Any], termination_provider_triggered: JoystickPolicyTerminationConditionProvider, randomState: np.random.RandomState ) -> None: self.rew = 0.0 self._last_torque = Robot.get_joint_torques().copy() self._last_joint_qpos = Robot.get_joint_qpos().copy() self._last_contacts = Robot.get_foot_contact().copy() if hasattr(Robot, "get_foot_contact") else None self._last_foot_force_norm = Robot.get_foot_force_norm().copy() if hasattr(Robot, "get_foot_force_norm") else None def calculate_velocity_reward_norm( self, Robot: BaseWalker, action_target_qpos: np.ndarray, target_goal_world_delta: np.ndarray, target_goal_local: np.ndarray, target_yaw : float, target_delta_yaw: float, target_velocity: float, velocity_to_goal: float, change_in_abs_target_delta_yaw : float, target_custom_data: Optional[Any], enable_target_custom_obs : bool, info_dict: dict[str,Any], randomState: np.random.RandomState ) -> float: velocity_local = Robot.get_3d_local_velocity() roll, pitch, yaw = Robot.get_roll_pitch_yaw() # reward_v = rewards.tolerance( # (np.cos(pitch) * velocity_local[0]), # bounds=(target_velocity, # target_velocity + 0.1), # margin=target_velocity, # value_at_margin=0, # sigmoid='linear' # ) * target_velocity projected_x_velocity = np.cos(pitch) * velocity_local[0]
JOINT_WEIGHTS = np.array([1.0, 0.75, 0.5] * 4) CONTACT_DELTA_QPOS_THRESHOLD = -0.2 CONTACT_DELTA_FORCE_THRESHOLD = 0.4 class WalkInTheParkRewardProvider(JoystickPolicyRewardProvider[BaseWalker]): def __init__(self) -> None: super().__init__() self.rew = 0.0 def get_reward(self) -> float: return self.rew def reset_reward( self, Robot: BaseWalker, info_dict: dict[str,Any], termination_provider_triggered: JoystickPolicyTerminationConditionProvider, randomState: np.random.RandomState ) -> None: self.rew = 0.0 def step_reward( self, Robot: BaseWalker, action_target_qpos: np.ndarray, target_goal_world_delta: np.ndarray, target_goal_local: np.ndarray, target_yaw : float, target_delta_yaw: float, target_velocity: float, velocity_to_goal: float, change_in_abs_target_delta_yaw : float, target_custom_data: Optional[Any], enable_target_custom_obs : bool, info_dict: dict[str,Any], randomState: np.random.RandomState ) -> None: roll, pitch, yaw = Robot.get_roll_pitch_yaw() velocity_local = Robot.get_3d_local_velocity() projected_x_velocity = np.cos(pitch) * velocity_local[0] reward_v = rewards.tolerance( projected_x_velocity, bounds=(target_velocity,2*target_velocity), margin=2*target_velocity, value_at_margin=0, sigmoid='linear' ) * (target_velocity / 0.5) * 10.0 penalty_drpy = 1.0 * np.abs(Robot.get_3d_angular_velocity()[-1]) reward_perstep = reward_v - penalty_drpy info_dict["reward_v"] = reward_v info_dict["penalty_drpy"] = penalty_drpy reward_perstep *= max(Robot.get_foot_contact()) if hasattr(Robot, "get_foot_contact") else 1.0 self.rew = reward_perstep class JoystickPolicyStrictRewardProvider(JoystickPolicyRewardProvider[BaseWalker]): def __init__( self, energy_penalty_weight: float = 0.0, smooth_torque_penalty_weight: float = 0.0, joint_diagonal_penalty_weight: float = 0.0, joint_shoulder_penalty_weight : float = 0.0, joint_acc_penalty_weight : float = 0.0, thigh_torque_penalty_weight: float = 0.0, joint_vel_penalty_weight : float = 0.0, pitch_rate_penalty_factor: float = 0.0, roll_rate_penalty_factor: float = 0.0, qpos_penalty_weight: float = 0.0, contact_reward_weight : float = 0.0 ) -> None: self.energy_penalty_weight = energy_penalty_weight self.smooth_torque_penalty_weight = smooth_torque_penalty_weight self.joint_diagonal_penalty_weight = joint_diagonal_penalty_weight self.joint_shoulder_penalty_weight = joint_shoulder_penalty_weight self.joint_acc_penalty_weight = joint_acc_penalty_weight self.joint_vel_penalty_weight = joint_vel_penalty_weight self.pitch_rate_penalty_factor = pitch_rate_penalty_factor self.roll_rate_penalty_factor = roll_rate_penalty_factor self.qpos_penalty_weight = qpos_penalty_weight self.thigh_torque_penalty_weight = thigh_torque_penalty_weight self.contact_reward_weight = contact_reward_weight self.rew = 0.0 self._last_torque = None def get_reward(self) -> float: return self.rew def reset_reward( self, Robot: BaseWalker, info_dict: dict[str,Any], termination_provider_triggered: JoystickPolicyTerminationConditionProvider, randomState: np.random.RandomState ) -> None: self.rew = 0.0 self._last_torque = Robot.get_joint_torques().copy() self._last_joint_qpos = Robot.get_joint_qpos().copy() self._last_contacts = Robot.get_foot_contact().copy() if hasattr(Robot, "get_foot_contact") else None self._last_foot_force_norm = Robot.get_foot_force_norm().copy() if hasattr(Robot, "get_foot_force_norm") else None def calculate_velocity_reward_norm( self, Robot: BaseWalker, action_target_qpos: np.ndarray, target_goal_world_delta: np.ndarray, target_goal_local: np.ndarray, target_yaw : float, target_delta_yaw: float, target_velocity: float, velocity_to_goal: float, change_in_abs_target_delta_yaw : float, target_custom_data: Optional[Any], enable_target_custom_obs : bool, info_dict: dict[str,Any], randomState: np.random.RandomState ) -> float: velocity_local = Robot.get_3d_local_velocity() roll, pitch, yaw = Robot.get_roll_pitch_yaw() # reward_v = rewards.tolerance( # (np.cos(pitch) * velocity_local[0]), # bounds=(target_velocity, # target_velocity + 0.1), # margin=target_velocity, # value_at_margin=0, # sigmoid='linear' # ) * target_velocity projected_x_velocity = np.cos(pitch) * velocity_local[0]
reward_v = near_quadratic_bound(
0
2023-11-02 23:21:38+00:00
4k
NUCCASJNR/PaystackPyAPI
tests/test_transaction.py
[ { "identifier": "Transaction", "path": "paystackpyAPI/transaction.py", "snippet": "class Transaction(PaystackAPI):\n INITIALIZATION_OPTIONAL_PARAMS = [\n \"currency\",\n \"reference\",\n \"callback_url\",\n \"plan\",\n \"invoice_limit\",\n \"metadata\",\n \"channels\",\n \"split_code\",\n \"subaccount\",\n \"transaction_charge\",\n \"bearer\"\n ]\n\n TRANSACTION_LIST_OPTIONAL_PARAMS = [\n \"customer\",\n \"terminalid\",\n \"status\",\n \"from\",\n \"to\",\n \"amount\"\n ]\n CHARGE_AUTHORIZATION_OPTIONAL_PARAMS = [\n \"reference\",\n \"currency\",\n \"metadata\",\n \"channels\",\n \"subaccount\",\n \"transaction_charge\",\n \"bearer\",\n \"queue\"\n ]\n \n EXPORT_OPTIONAL_PARAMS = [\n 'from',\n 'to',\n 'customer',\n 'status',\n 'currency',\n 'amount',\n 'settled',\n 'settlement',\n 'payment_page'\n ]\n\n def __init__(self, api_key: str):\n super().__init__(api_key)\n self.paystack_initialization_url = \"https://api.paystack.co/transaction/initialize\"\n self.paystack_verification_url = \"https://api.paystack.co/transaction/verify\"\n self.list_transaction_url = \"https://api.paystack.co/transaction\"\n self.fetch_transaction_url = \"https://api.paystack.co/transaction\"\n self.charge_authorization_url = \"https://api.paystack.co/transaction/charge_authorization\"\n self.transaction_timeline_url = \"https://api.paystack.co/transaction/timeline\"\n self.transaction_totals_url = \"https://api.paystack.co/transaction/totals\"\n self.export_transactions_url = \"https://api.paystack.co/transaction/export\"\n \n\n def initialize_transaction(self, email: str, amount: int, **kwargs):\n \"\"\"\n Initialize a Paystack transaction.\n\n :param email: Customer's email address.\n :param amount: Transaction amount.\n :param kwargs: Optional parameters for the transaction.\n Example: `currency`, `callback_url`, etc.\n :return: JSON response from Paystack API.\n :raises APIError: If required parameters are missing or the API key is invalid.\n \"\"\"\n if not email or not amount:\n raise APIError(400, \"Missing required parameters: email and/or amount\")\n\n valid_kwargs = {key: value for key, value in kwargs.items() if key in self.INITIALIZATION_OPTIONAL_PARAMS}\n data = {\n \"email\": email,\n \"amount\": amount * 100,\n **valid_kwargs\n }\n\n if not self.api_key:\n raise APIError(401, \"Invalid API key\")\n\n headers = {\n 'Authorization': f'Bearer {self.api_key}',\n 'Content-Type': 'application/json',\n }\n response = requests.post(self.paystack_initialization_url, headers=headers, json=data)\n if response.status_code == 200:\n custom_response = {\n \"status_code\": response.status_code,\n \"message\": \"Transaction initialized successfully\",\n \"response_from_api\": response.json()\n }\n else:\n error_message = response.text\n raise APIError(response.status_code, error_message)\n return custom_response\n\n def verify_transaction(self, reference: Union[int, str]) -> Dict:\n \"\"\"\n Verify a Paystack transaction.\n\n :param reference: Reference id of the transaction (int or str).\n :return: Customized response from Paystack API.\n :raises APIError: If the reference is missing or the API key is invalid.\n \"\"\"\n if not reference:\n raise APIError(400, \"Missing required parameter: reference\")\n\n if not self.api_key:\n raise APIError(401, \"Invalid API key\")\n\n url = f\"{self.paystack_verification_url}/{reference}\"\n headers = {\n 'Authorization': f'Bearer {self.api_key}'\n }\n response = requests.get(url, headers=headers)\n\n if response.status_code == 200:\n custom_response = {\n \"status_code\": response.status_code,\n \"message\": \"Transaction details retrieved successfully\",\n \"response_from_api\": response.json()\n }\n else:\n error_message = response.text\n raise APIError(response.status_code, error_message)\n\n return custom_response\n\n def list_transactions(self, **kwargs: Dict) -> Dict:\n \"\"\"\n Retrieve a list of transactions based on optional parameters.\n\n :param kwargs: Optional parameters for filtering the list of transactions.\n Supported parameters:\n - `perPage`: Number of transactions to retrieve per page.\n - `page`: Page number for pagination.\n - `from`: Start date for transactions in the format 'YYYY-MM-DD'.\n - `to`: End date for transactions in the format 'YYYY-MM-DD'.\n - `customer`: Customer's email or identification.\n - `status`: Transaction status (e.g., 'success', 'failed').\n - `currency`: Currency code (e.g., 'NGN', 'USD').\n - `amount`: Transaction amount.\n - `reference`: Transaction reference.\n - `gateway`: Payment gateway used (e.g., 'card', 'bank').\n - `channel`: Transaction channel (e.g., 'card', 'bank').\n - `plan`: Plan code associated with the transaction.\n\n :return: Customized response with the list of transactions.\n Format: {\n \"status_code\": int,\n \"message\": str,\n \"data\": dict\n }\n\n :raises APIError: If the API key is invalid or if there's an issue with the request.\n \"\"\"\n if not self.api_key:\n raise APIError(401, \"Invalid API Key\")\n\n valid_kwargs = {key: value for key, value in kwargs.items() if key in self.TRANSACTION_LIST_OPTIONAL_PARAMS}\n\n headers = {\n 'Authorization': f'Bearer {self.api_key}'\n }\n\n data = {\n **valid_kwargs\n }\n\n response = requests.get(self.list_transaction_url, headers=headers, params=data)\n\n if response.status_code == 200:\n custom_response = {\n \"status_code\": response.status_code,\n \"message\": \"Transactions details below\",\n \"response_from_api\": response.json()\n }\n else:\n error_message = response.text\n raise APIError(response.status_code, error_message)\n\n return custom_response\n\n def fetch_transaction(self, id: int) -> Dict:\n \"\"\"\n Fetches the details of a transaction using the id provided\n :param id:\n Transaction Id\n \"\"\"\n if not self.api_key:\n raise APIError(401, \"Invalid Api Key\")\n url = f\"{self.fetch_transaction_url}/{id}\"\n headers = {\n 'Authorization': f'Bearer {self.api_key}'\n }\n response = requests.get(url, headers=headers)\n if response.status_code == 200:\n custom_response = {\n \"status_code\": response.status_code,\n \"message\": \"Transaction Successfully fetched\",\n \"response_from_api\": response.json()\n }\n else:\n error_message = response.text\n raise APIError(response.status_code, error_message)\n return custom_response\n\n def charge_authorization(self, email: str, amount: int, authorization_code: str, **kwargs: Dict) -> Dict:\n \"\"\"charge a transaction\"\"\"\n\n if not self.api_key:\n raise APIError(401, \"Invalid API Key\")\n valid_kwargs = {key: value for key, value in kwargs.items() if key in self.CHARGE_AUTHORIZATION_OPTIONAL_PARAMS}\n if not amount:\n raise APIError(400, \"Missing required parameter amount\")\n if not email:\n raise APIError(400, \"Missing required parameter email\")\n if not authorization_code:\n raise APIError(400, \"Missing required parameter authorization_code\")\n headers = {\n 'Authorization': f'Bearer {self.api_key}',\n 'Content-Type': 'application/json',\n }\n data = {\n \"amount\": amount * 100,\n \"email\": email,\n \"authorization_code\": f\"AUTH_{authorization_code}\",\n **valid_kwargs\n }\n response = requests.post(self.charge_authorization_url, headers=headers, json=data)\n if response.status_code == 200:\n custom_response = {\n \"status_code\": response.status_code,\n \"message\": \"Transaction initialized successfully\",\n \"response_from_api\": response.json()\n }\n else:\n error_message = response.text\n raise APIError(response.status_code, error_message)\n return custom_response\n\n def show_transaction_timeline(self, id_or_reference: str) -> Dict:\n \"\"\"\n SHow a transaction timeline\n \"\"\"\n headers = {\n 'Authorization': f'Bearer {self.api_key}'\n }\n url = f\"{self.transaction_timeline_url}/{id_or_reference}\"\n response = requests.get(url, headers=headers)\n if response.status_code == 200:\n custom_response = {\n \"status_code\": response.status_code,\n \"message\": \"Transaction timeline retrieved\",\n \"response_from_api\": response.json()\n }\n else:\n error_message = response.text\n raise APIError(response.status_code, error_message)\n return custom_response\n\n def get_total_transactions(self, per_page=50, page=1, from_date=None, to_date=None):\n \"\"\"\n Retrieve the total amount received on your account based on specified parameters.\n\n :param per_page: Number of records to retrieve per page (default is 50).\n :param page: Page number to retrieve (default is 1).\n :param from_date: Start date for listing transactions in the format 'YYYY-MM-DDTHH:mm:ss.SSSZ'.\n :param to_date: End date for listing transactions in the format 'YYYY-MM-DDTHH:mm:ss.SSSZ'.\n\n :return: Customized response with the total amount received.\n Format: {\n \"status_code\": int,\n \"message\": str,\n \"data\": {\n \"total_amount\": float\n }\n }\n\n :raises APIError: If the API key is invalid or if there's an issue with the request.\n \"\"\"\n if not self.api_key:\n raise APIError(401, \"Invalid API Key\")\n\n headers = {\n 'Authorization': f'Bearer {self.api_key}'\n }\n\n params = {\n 'perPage': per_page,\n 'page': page,\n 'from': from_date,\n 'to': to_date\n }\n\n response = requests.get(self.transaction_totals_url, headers=headers, params=params)\n\n if response.status_code == 200:\n custom_response = {\n \"status_code\": response.status_code,\n \"message\": \"Transaction totals retrieved successfully\",\n \"response_from_api\": response.json()\n }\n else:\n error_message = response.text\n raise APIError(response.status_code, error_message)\n\n return custom_response\n \n def download_csv(self, url, output_filename='exported_file.csv'):\n response = requests.get(url)\n response.raise_for_status()\n\n with open(output_filename, 'wb') as file:\n file.write(response.content)\n\n print(f'File downloaded successfully: {output_filename}')\n\n def export_transactions(self, per_page=50, page=1, filename=\"export.csv\", **kwargs):\n \"\"\"\n initiate the export, and download the CSV file.\n\n :param per_page: Number of records to retrieve per page (default is 50).\n :param page: Page number to retrieve (default is 1).\n :param filename: Optional filename for the exported CSV file.\n\n :return: Customized response indicating the success of the export.\n Format: {\n \"status_code\": int,\n \"message\": str,\n \"data\": {\n \"exported_file\": str # File path or URL\n }\n }\n\n :raises APIError: If the API key is invalid, export initiation fails, or if there's an issue with the request.\n \"\"\"\n optional_kwargs = {key: value for key, value in kwargs.items() if key in self.EXPORT_OPTIONAL_PARAMS}\n if not self.api_key:\n raise APIError(401, \"Invalid API key\")\n headers = {\n 'Authorization': f'Bearer {self.api_key}'\n }\n\n params = {\n 'perPage': per_page,\n 'page': page,\n **optional_kwargs\n }\n try:\n response = requests.get(self.export_transactions_url, headers=headers, params=params)\n if response.status_code == 200:\n data = response.json()\n url_to_visit = data['data']['path']\n # webbrowser.open(url_to_visit)\n self.download_csv(url_to_visit, output_filename=filename)\n\n custom_response = {\n \"status_code\": response.status_code,\n \"message\": f\"Transactions exported successfully to {filename or url_to_visit}\",\n \"data\": {\n \"exported_file\": filename or url_to_visit\n }\n }\n\n return custom_response\n \n\n except requests.exceptions.HTTPError as errh:\n raise APIError(errh.response.status_code, f\"HTTP Error: {errh}\")\n except requests.exceptions.ConnectionError as errc:\n raise APIError(500, f\"Error Connecting: {errc}\")\n except requests.exceptions.Timeout as errt:\n raise APIError(500, f\"Timeout Error: {errt}\")\n except requests.exceptions.RequestException as err:\n raise APIError(500, f\"An error occurred: {err}\")" }, { "identifier": "APIError", "path": "errors.py", "snippet": "class APIError(PaystackError):\n \"\"\"Exception raised for errors in the Paystack API.\n\n Attributes:\n status_code -- the HTTP status code indicating the error\n error_message -- a description of the error\n \"\"\"\n\n def __init__(self, status_code, error_message):\n self.status_code = status_code\n self.error_message = error_message\n super().__init__(self.error_message)" } ]
import tracemalloc import unittest import secrets import responses from unittest.mock import Mock, patch from paystackpyAPI.transaction import Transaction from errors import APIError from os import getenv
3,449
REFERENCE = secrets.token_hex(16) ID = '' print(ID) class TestPaystackAPI(unittest.TestCase): def setUp(self): # Set up any necessary test data or configurations
REFERENCE = secrets.token_hex(16) ID = '' print(ID) class TestPaystackAPI(unittest.TestCase): def setUp(self): # Set up any necessary test data or configurations
self.api = Transaction(api_key=getenv("PAYSTACK_KEY"))
0
2023-11-07 18:00:39+00:00
4k
Dataherald/Assistant
dataherald_assistant.py
[ { "identifier": "Function", "path": "function.py", "snippet": "class Function(BaseModel, ABC):\n name: str\n description: Optional[str] = None\n parameters: Optional[List[Property]] = None\n\n def to_dict(self):\n if self.parameters is None:\n return {\n \"name\": self.name,\n \"description\": self.description,\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {},\n \"required\": [],\n },\n }\n return {\n \"name\": self.name,\n \"description\": self.description,\n \"parameters\": {\n \"type\": \"object\",\n \"properties\": {\n p.name: {\"type\": p.type, \"description\": p.description}\n for p in self.parameters\n },\n \"required\": [p.name for p in self.parameters if p.required],\n },\n }\n\n def run(self, function_call: FunctionCall = None):\n if function_call.arguments == {} and self.parameters is not None:\n raise Exception(\"Missing parameters\")\n if function_call.arguments == {} and self.parameters is None:\n return self.function()\n if function_call.arguments != {} and self.parameters is None:\n raise Exception(\"Unexpected parameters\")\n if function_call is not None and self.parameters is not None:\n for p in self.parameters:\n if p.name not in function_call.arguments and p.required:\n raise Exception(f\"Missing parameter {p.name}\")\n return self.function(**function_call.arguments)\n\n def run_catch_exceptions(self, function_call: FunctionCall = None):\n try:\n return self.run(function_call=function_call)\n except Exception as e:\n return str(e)\n\n @abstractmethod\n def function(self, **kwargs):\n pass" }, { "identifier": "Property", "path": "function.py", "snippet": "class Property(BaseModel):\n name: str\n type: str\n required: bool = True\n description: Optional[str] = None" }, { "identifier": "AIAssistant", "path": "assistant.py", "snippet": "class AIAssistant:\n assistant: Assistant\n client: OpenAI\n assistant_name: str\n assistant_description: str\n instruction: str\n model: str\n use_retrieval: bool\n use_code_interpreter: bool\n functions: list[Function]\n threads: list[Thread]\n tools: list[dict]\n file_ids: list[str]\n conversation: Conversation\n verbose: bool\n auto_delete: bool = True\n\n def __init__(\n self,\n instruction: str,\n model: str,\n use_retrieval: bool = False,\n use_code_interpreter: bool = False,\n file_ids: list[str] = None,\n functions: list[Function] = None,\n assistant_name: str = \"AI Assistant\",\n assistant_description: str = \"An AI Assistant\",\n verbose: bool = False,\n auto_delete: bool = True,\n ):\n self.client = Client()\n self.instruction = instruction\n self.model = model\n self.use_retrieval = use_retrieval\n self.use_code_interpreter = use_code_interpreter\n self.file_ids = file_ids\n self.functions = functions\n self.assistant_name = assistant_name\n self.assistant_description = assistant_description\n self.tools = [\n {\"type\": \"function\", \"function\": f.to_dict()} for f in self.functions\n ] if self.functions else []\n if self.use_retrieval:\n self.tools.append({\"type\": \"retrieval\"})\n if self.use_code_interpreter:\n self.tools.append({\"type\": \"code_interpreter\"})\n self.assistant = self.client.beta.assistants.create(\n name=self.assistant_name,\n description=self.assistant_description,\n instructions=self.instruction,\n model=self.model,\n tools=self.tools,\n file_ids=self.file_ids if self.file_ids else [],\n )\n self.threads = []\n self.conversation = Conversation(messages=[])\n self.verbose = verbose\n self.auto_delete = auto_delete\n\n def delete_assistant_file_by_id(self, file_id: str):\n file_deletion_status = self.client.beta.assistants.files.delete(\n assistant_id=self.assistant.id, file_id=file_id\n )\n return file_deletion_status\n\n def create_thread(self) -> Thread:\n thread = self.client.beta.threads.create()\n self.threads.append(thread)\n return thread\n\n def create_tool_outputs(self, run: Run) -> list[dict]:\n tool_outputs = []\n for tool in run.required_action.submit_tool_outputs.tool_calls:\n tool_found = False\n function_name = tool.function.name\n if tool.function.arguments:\n function_arguments = json.loads(tool.function.arguments)\n else:\n function_arguments = {}\n call_id = tool.id\n function_call = FunctionCall(\n call_id=call_id, name=function_name, arguments=function_arguments\n )\n for function in self.functions:\n if function.name == function_name:\n tool_found = True\n if self.verbose:\n random_color = random.choice(PRINT_COLORS)\n print(f'\\n{random_color}{function_name} function has called by assistant with the following arguments: {function_arguments}')\n response = function.run_catch_exceptions(\n function_call=function_call\n )\n if self.verbose:\n random_color = random.choice(PRINT_COLORS)\n print(f\"{random_color}Function {function_name} responsed: {response}\")\n tool_outputs.append(\n {\n \"tool_call_id\": call_id,\n \"output\": response,\n }\n )\n if not tool_found:\n if self.verbose:\n random_color = random.choice(PRINT_COLORS)\n print(f\"{random_color}Function {function_name} alled by assistant not found\")\n tool_outputs.append(\n {\n \"tool_call_id\": call_id,\n \"output\": f\"Function {function_name} not found\",\n }\n )\n return tool_outputs\n\n def get_required_functions_names(self, run: Run) -> list[str]:\n function_names = []\n for tool in run.required_action.submit_tool_outputs.tool_calls:\n function_names.append(tool.function)\n return function_names\n\n def create_conversation(self, thread_id: str):\n messages = self.client.beta.threads.messages.list(thread_id=thread_id).data\n for message in messages:\n self.conversation.messages.append(\n Message(\n thread_id=thread_id,\n role=message.role,\n content=self.format_message(message=message),\n file_ids=message.file_ids,\n )\n )\n return self.conversation.print_conversation()\n \n def list_files(self):\n return self.client.files.list().data\n \n def create_file(self, filename: str, file_id: str):\n content = self.client.files.retrieve_content(file_id)\n with open(filename.split(\"/\")[-1], 'w') as file:\n file.write(content)\n\n def upload_file(self, filename: str) -> str:\n file = self.client.files.create(\n file=open(filename, \"rb\"),\n purpose='assistants'\n )\n return file.id\n \n def delete_file(self, file_id: str) -> bool:\n file_deletion_status = self.client.beta.assistants.files.delete(\n assistant_id=self.assistant.id,\n file_id=file_id\n )\n return file_deletion_status.deleted\n\n def format_message(self, message: ThreadMessage) -> str:\n if getattr(message.content[0], \"text\", None) is not None:\n message_content = message.content[0].text\n else:\n message_content = message.content[0]\n annotations = message_content.annotations\n citations = []\n for index, annotation in enumerate(annotations):\n message_content.value = message_content.value.replace(\n annotation.text, f\" [{index}]\"\n )\n if file_citation := getattr(annotation, \"file_citation\", None):\n cited_file = self.client.files.retrieve(file_citation.file_id)\n citations.append(\n f\"[{index}] {file_citation.quote} from {cited_file.filename}\"\n )\n elif file_path := getattr(annotation, \"file_path\", None):\n cited_file = self.client.files.retrieve(file_path.file_id)\n citations.append(\n f\"[{index}] file: {cited_file.filename} is downloaded\"\n )\n self.create_file(filename=cited_file.filename, file_id=cited_file.id)\n\n message_content.value += \"\\n\" + \"\\n\".join(citations)\n return message_content.value\n\n def extract_run_message(self, run: Run, thread_id: str) -> str:\n messages = self.client.beta.threads.messages.list(\n thread_id=thread_id,\n ).data\n for message in messages:\n if message.run_id == run.id:\n return f\"{message.role}: \" + self.format_message(message=message)\n return \"Assistant: No message found\"\n\n def create_response(\n self,\n thread_id: str,\n content: str,\n message_files: list[str] = None,\n run_instructions: str = None,\n ) -> str:\n self.client.beta.threads.messages.create(\n thread_id=thread_id,\n role=\"user\",\n content=content,\n file_ids=message_files if message_files else [],\n )\n run = self.client.beta.threads.runs.create(\n thread_id=thread_id,\n assistant_id=self.assistant.id,\n instructions=run_instructions,\n )\n with yaspin(text=\"Loading\", color=\"yellow\"):\n while run.status != \"completed\":\n run = self.client.beta.threads.runs.retrieve(\n thread_id=thread_id, run_id=run.id\n )\n if run.status == \"failed\":\n raise Exception(f\"Run failed with the following error {run.last_error}\")\n if run.status == \"expired\":\n raise Exception(\n f\"Run expired when calling {self.get_required_functions_names(run=run)}\"\n )\n if run.status == \"requires_action\":\n tool_outputs = self.create_tool_outputs(run=run)\n run = self.client.beta.threads.runs.submit_tool_outputs(\n thread_id=thread_id,\n run_id=run.id,\n tool_outputs=tool_outputs,\n )\n if self.verbose:\n random_color = random.choice(PRINT_COLORS)\n print(f\"\\n{random_color}Run status: {run.status}\")\n time.sleep(0.5)\n return \"\\n\" + self.extract_run_message(run=run, thread_id=thread_id)\n \n def chat(self, file_ids: list[str] = None):\n thread = self.create_thread()\n user_input = \"\"\n while user_input != \"bye\" and user_input != \"exit\":\n user_input = input(\"\\033[32mYou (type bye to quit): \")\n message = self.create_response(\n thread_id=thread.id, content=user_input, message_files=file_ids\n )\n print(f\"\\033[33m{message}\")\n if self.auto_delete:\n if file_ids:\n for file in file_ids:\n self.delete_file(file_id=file)\n self.client.beta.threads.delete(thread_id=thread.id)\n self.client.beta.assistants.delete(assistant_id=self.assistant.id)" }, { "identifier": "answer_question", "path": "dataherald.py", "snippet": "def answer_question(\n question: str,\n db_name: enumerate(DATABASES_IDS.keys()) = 'RealEstate'\n) -> str:\n payload = {\n \"db_connection_id\": DATABASES_IDS[db_name],\n \"question\": question,\n }\n json_data = json.dumps(payload)\n response = requests.post(HOST, data=json_data) \n if response.status_code == 201:\n engine_response = response.json()['response'] + '\\n' + json.dumps(response.json()['sql_query_result'])\n return engine_response\n else:\n return \"Sorry, I don't know the answer to that question.\"" } ]
from function import Function, Property from dotenv import load_dotenv from assistant import AIAssistant from dataherald import answer_question
2,974
load_dotenv() class DataheraldFunction(Function): def __init__(self): super().__init__( name="dataherald", description="Answer questions on a given database", parameters=[ Property( name="db_name", description="The database to query, possible values are: RealEstate, SenateStock", type="string", required=False, ), Property( name="question", description="The question to answer", type="string", required=True, ), ] ) def function(self, db_name, question): return answer_question(question, db_name) if __name__ == "__main__":
load_dotenv() class DataheraldFunction(Function): def __init__(self): super().__init__( name="dataherald", description="Answer questions on a given database", parameters=[ Property( name="db_name", description="The database to query, possible values are: RealEstate, SenateStock", type="string", required=False, ), Property( name="question", description="The question to answer", type="string", required=True, ), ] ) def function(self, db_name, question): return answer_question(question, db_name) if __name__ == "__main__":
assistant = AIAssistant(
2
2023-11-09 01:58:07+00:00
4k
Skytliang/SpyGame
spygame.py
[ { "identifier": "TurboPlayer", "path": "utils/agent.py", "snippet": "class TurboPlayer(Agent):\n def __init__(self, model_name: str, name: str, secret_word: str, temperature:float, sleep_time: float) -> None:\n \"\"\"Create a player in the spy game\n\n Args:\n model_name(str): model name\n name (str): name of this player\n secret_word (str): the secret word that this player holds\n temperature (float): higher values make the output more random, while lower values make it more focused and deterministic\n sleep_time (float): sleep against rate limiting\n \"\"\"\n super().__init__(model_name, temperature, sleep_time)\n self.name = name\n self.secret_word = secret_word\n if cycle_all_keys:\n self.openai_api_keys = gpt4_api_keys if 'gpt-4' in self.model_name else gpt3_api_keys\n \n # if 'gpt-4' in model_name:\n # self.api_base = \"https://aigptx.top/v1\"\n # else:\n # self.api_base = \"https://api.openai.com/v1\"\n self.api_base = \"https://api.openai.com/v1\"" }, { "identifier": "DavinciPlayer", "path": "utils/agent.py", "snippet": "class DavinciPlayer(TurboPlayer):\n @backoff.on_exception(backoff.expo, (RateLimitError, APIError, ServiceUnavailableError, APIConnectionError, AuthenticationError), max_tries=5)\n def query(self, prompt: str, max_tokens: int, api_key: str, temperature: float) -> str:\n try:\n openai.api_base = self.api_base\n response = openai.Completion.create(\n model=self.model_name,\n prompt=prompt,\n api_key=api_key,\n temperature=temperature,\n max_tokens=max_tokens,\n presence_penalty=0.75\n )\n gen = response.choices[0].text.strip()\n gen = f\"{self.name}: {gen}\"\n return gen\n \n except RateLimitError as e:\n if \"You exceeded your current quota, please check your plan and billing details\" in e.user_message:\n self.openai_api_keys.remove(api_key)\n print(f'Out Of Quota: {api_key}')\n raise OutOfQuotaException(api_key)\n elif \"Your access was terminated due to violation of our policies\" in e.user_message:\n self.openai_api_keys.remove(api_key)\n print(f'Access Terminated: {api_key}')\n raise AccessTerminatedException(api_key)\n else:\n raise e\n\n\n def ask(self, temperature: float=None):\n time.sleep(self.sleep_time)\n \n memory_lst = copy.deepcopy(self.memory_lst)\n memory_lst = np.array(memory_lst)\n memory_lst = memory_lst[self.memory_lst_idx > 0]\n self.memory_lst_idx -= 1\n memory_lst = list(memory_lst)\n\n contents = [m[\"content\"] for m in memory_lst]\n prompt = '\\n\\n'.join(contents) + f\"\\n\\n{self.name}: \"\n\n num_context_token = num_tokens_from_string(prompt, self.model_name)\n max_tokens = model2max_context[self.model_name] - num_context_token\n\n now = datetime.now()\n current_time = now.strftime(\"%H:%M:%S\").split(':')\n start_time = 60 * int(current_time[0]) + int(current_time[1])\n while True:\n # print(self.openai_api_keys)\n now = datetime.now()\n current_time = now.strftime(\"%H:%M:%S\").split(':')\n end_time = 60 * int(current_time[0]) + int(current_time[1])\n step_time = end_time - start_time\n if step_time // 2 > 0 and step_time % 2 == 0:\n print(f'Out of Time: {step_time} mins')\n if step_time > 5:\n raise TimeOutException\n try:\n if cycle_all_keys:\n return self.query(prompt, max_tokens, api_key=next(key_generator(self.model_name)), temperature=temperature if temperature else self.temperature)\n else:\n return self.query(prompt, max_tokens, api_key=random.choice(self.openai_api_keys), temperature=temperature if temperature else self.temperature)\n except:\n time.sleep(5)\n\n\n def ask2(self, temperature: float=None):\n time.sleep(self.sleep_time)\n openai.api_base = self.api_base\n\n memory_lst = copy.deepcopy(self.memory_lst)\n memory_lst = np.array(memory_lst)\n memory_lst = memory_lst[self.memory_lst_idx > 0]\n self.memory_lst_idx -= 1\n memory_lst = list(memory_lst)\n\n try:\n contents = [m[\"content\"] for m in memory_lst]\n prompt = '\\n\\n'.join(contents) + f\"\\n\\n{self.name}: \"\n\n num_context_token = num_tokens_from_string(prompt, self.model_name)\n max_tokens = model2max_context[self.model_name] - num_context_token\n api_key = random.choice(self.openai_api_keys)\n # api_key=next(key_generator)\n\n response = openai.Completion.create(\n model=self.model_name,\n prompt=prompt,\n api_key=api_key,\n temperature=temperature if temperature else self.temperature,\n max_tokens=max_tokens,\n presence_penalty=0.75\n )\n gen = response.choices[0].text.strip()\n gen = f\"{self.name}: {gen}\"\n return gen\n \n except RateLimitError as e:\n if \"You exceeded your current quota, please check your plan and billing details\" in e.user_message:\n raise OutOfQuotaException(api_key)\n elif \"Your access was terminated due to violation of our policies\" in e.user_message:\n raise AccessTerminatedException(api_key)\n else:\n raise e" }, { "identifier": "BardPlayer", "path": "utils/agent.py", "snippet": "class BardPlayer(TurboPlayer):\n def __init__(self, model_name: str, name: str, secret_word: str, temperature:float, sleep_time: float) -> None:\n\n super.__init__(model_name, name, secret_word, temperature, openai_api_keys, sleep_time)\n\n self.bard_token = random.choice(openai_api_keys)\n\n session = requests.Session()\n session.headers = {\n \"Host\": \"bard.google.com\",\n \"X-Same-Domain\": \"1\",\n \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36\",\n \"Content-Type\": \"application/x-www-form-urlencoded;charset=UTF-8\",\n \"Origin\": \"https://bard.google.com\",\n \"Referer\": \"https://bard.google.com/\",\n }\n \n session.cookies.set(\"__Secure-1PSID\", self.bard_token)\n self.bard = Bard(token=self.bard_token, session=session, timeout=30)\n self.start_id = 0\n\n def ask(self, temperature: float=None):\n time.sleep(self.sleep_time)\n\n contents_all = [m[\"content\"] for m in self.memory_lst]\n contents = contents_all[self.start_id: ]\n self.start_id = len(contents_all)\n content = '\\n\\n'.join(contents)\n bard_ans = self.bard.get_answer(content)['content']\n return bard_ans" }, { "identifier": "VicunaPlayer", "path": "utils/agent.py", "snippet": "class VicunaPlayer(Agent):\n def __init__(self, model_name: str, name: str, secret_word: str, temperature:float, sleep_time: float) -> None:\n\n support_models = ['vicuna', 'fastchat-t5', 'longchat']\n super().__init__(model_name, temperature, sleep_time)\n self.name = name\n self.secret_word = secret_word\n self.openai_api_keys = openai_api_keys\n\n assert self.model_name in support_models, f\"Not support {self.model_name}. Choices: {support_models}\"\n\n magic_path = os.path.abspath(__file__).rsplit('/', 1)[0]\n self.repetition_penalty = 1.0\n if model_name == \"vicuna\":\n model_path = f\"{magic_path}/FastChat/vicuna-13b-v1.3\"\n elif model_name == 'fastchat-t5':\n model_path = f\"{magic_path}/FastChat/fastchat-t5-3b-v1.0\"\n self.repetition_penalty = 1.2\n elif model_name == 'longchat':\n model_path = f\"{magic_path}/FastChat/longchat-13b-16k\"\n self.model, self.tokenizer = load_model(\n model_path,\n \"cuda\",\n 1,\n 1,\n False,\n False,\n revision=\"main\",\n debug=False,\n )\n self.conv = get_conversation_template(model_path)\n\n def set_meta_prompt(self, meta_prompt: str):\n \"\"\"Set the meta_prompt\n\n Args:\n meta_prompt (str): the meta prompt\n \"\"\"\n self.memory_lst.append({\"role\": \"system\", \"content\": f\"{meta_prompt}\"})\n self.conv.append_message(self.conv.roles[0], str(meta_prompt))\n\n def add_event(self, event: str):\n \"\"\"Add an new event in the memory\n\n Args:\n event (str): string that describe the event.\n \"\"\"\n # if not self.memory_lst[-1]['role'] == 'user':\n self.memory_lst.append({\"role\": \"user\", \"content\": f\"{event}\"})\n self.conv.append_message(self.conv.roles[0], str(event))\n # else:\n # self.memory_lst[-1]['content'] += f\"\\n\\n{event}\"\n\n def add_memory(self, memory: str):\n \"\"\"Monologue in the memory\n\n Args:\n memory (str): string that generated by the model in the last round.\n \"\"\"\n self.memory_lst.append({\"role\": \"assistant\", \"content\": f\"{memory}\"})\n self.conv.replace_message(self.conv.roles[1], str(memory))\n\n def ask(self, temperature: float=None):\n time.sleep(self.sleep_time)\n \n self.conv.append_message(self.conv.roles[1], None)\n prompt = self.conv.get_prompt()\n input_ids = self.tokenizer([prompt]).input_ids\n input_ids = [input_ids[0]]\n\n output_ids = self.model.generate(\n torch.as_tensor(input_ids).cuda(),\n do_sample=True,\n temperature=temperature if temperature else self.temperature,\n repetition_penalty=self.repetition_penalty,\n max_new_tokens=200,\n )\n if self.model.config.is_encoder_decoder:\n output_ids = output_ids[0]\n else:\n output_ids = output_ids[0][len(input_ids[0]) :]\n outputs = self.tokenizer.decode(\n output_ids, skip_special_tokens=True, spaces_between_special_tokens=False\n )\n return outputs" } ]
import re import os import json import argparse import itertools import random from utils.agent import TurboPlayer, DavinciPlayer, BardPlayer, VicunaPlayer from datetime import datetime from tqdm import tqdm
3,058
# random.seed(0) PRINT_LOG = True NAME_LIST_ANONYMOUS = ["Player 1", "Player 2", "Player 3", "Player 4", "Player 5", "Player 6", "Player 7", "Player 8", "Player 9", "Player 10"] SUPPORT_MODELS = ["gpt-4", "gpt-4-0314", "gpt-4-0613", "gpt-3.5-turbo", "gpt-3.5-turbo-0301", "gpt-3.5-turbo-0613", "text-davinci-003", "text-davinci-002", "bard", "vicuna", "fastchat-t5", "longchat"] SUPPORT_MODELS_WITH_MEMORY_LIST = ["gpt-4", "gpt-4-0314", "gpt-4-0613", "gpt-3.5-turbo", "gpt-3.5-turbo-0301", "gpt-3.5-turbo-0613", "text-davinci-003", "text-davinci-002", "bard"] def SpyPlayer(model_name: str = None, name: str = None, secret_word: str = None, temperature:float = 0, sleep_time: float = 0): assert model_name in SUPPORT_MODELS, f"Not support {model_name}. Choices: {SUPPORT_MODELS}" if model_name in ["gpt-4", "gpt-4-0314", "gpt-4-0613", "gpt-3.5-turbo", "gpt-3.5-turbo-0301", "gpt-3.5-turbo-0613"]: return TurboPlayer(model_name, name, secret_word, temperature, sleep_time) elif model_name in ["text-davinci-003", "text-davinci-002"]:
# random.seed(0) PRINT_LOG = True NAME_LIST_ANONYMOUS = ["Player 1", "Player 2", "Player 3", "Player 4", "Player 5", "Player 6", "Player 7", "Player 8", "Player 9", "Player 10"] SUPPORT_MODELS = ["gpt-4", "gpt-4-0314", "gpt-4-0613", "gpt-3.5-turbo", "gpt-3.5-turbo-0301", "gpt-3.5-turbo-0613", "text-davinci-003", "text-davinci-002", "bard", "vicuna", "fastchat-t5", "longchat"] SUPPORT_MODELS_WITH_MEMORY_LIST = ["gpt-4", "gpt-4-0314", "gpt-4-0613", "gpt-3.5-turbo", "gpt-3.5-turbo-0301", "gpt-3.5-turbo-0613", "text-davinci-003", "text-davinci-002", "bard"] def SpyPlayer(model_name: str = None, name: str = None, secret_word: str = None, temperature:float = 0, sleep_time: float = 0): assert model_name in SUPPORT_MODELS, f"Not support {model_name}. Choices: {SUPPORT_MODELS}" if model_name in ["gpt-4", "gpt-4-0314", "gpt-4-0613", "gpt-3.5-turbo", "gpt-3.5-turbo-0301", "gpt-3.5-turbo-0613"]: return TurboPlayer(model_name, name, secret_word, temperature, sleep_time) elif model_name in ["text-davinci-003", "text-davinci-002"]:
return DavinciPlayer(model_name, name, secret_word, temperature, sleep_time)
1
2023-11-01 03:42:10+00:00
4k
jdelahayes/ha-voltalis
custom_components/voltalis/climate.py
[ { "identifier": "DEFAULT_MAX_TEMP", "path": "custom_components/voltalis/const.py", "snippet": "DEFAULT_MAX_TEMP = 24" }, { "identifier": "DEFAULT_MIN_TEMP", "path": "custom_components/voltalis/const.py", "snippet": "DEFAULT_MIN_TEMP = 7" }, { "identifier": "DOMAIN", "path": "custom_components/voltalis/const.py", "snippet": "DOMAIN = \"voltalis\"" }, { "identifier": "HA_PRESET_MODES", "path": "custom_components/voltalis/const.py", "snippet": "HA_PRESET_MODES = {\n \"ECO\": PRESET_ECO,\n \"CONFORT\": PRESET_COMFORT,\n \"TEMPERATURE\": PRESET_HOME,\n \"HORS_GEL\": PRESET_AWAY,\n}" }, { "identifier": "VOLTALIS_CONTROLLER", "path": "custom_components/voltalis/const.py", "snippet": "VOLTALIS_CONTROLLER = \"voltalis_controller\"" }, { "identifier": "VOLTALIS_PRESET_MODES", "path": "custom_components/voltalis/const.py", "snippet": "VOLTALIS_PRESET_MODES = {\n PRESET_ECO: \"ECO\",\n PRESET_COMFORT: \"CONFORT\",\n PRESET_HOME: \"TEMPERATURE\",\n PRESET_AWAY: \"HORS_GEL\",\n}" }, { "identifier": "VOLTALIS_HEATER_TYPE", "path": "custom_components/voltalis/const.py", "snippet": "VOLTALIS_HEATER_TYPE = \"HEATER\"" }, { "identifier": "VoltalisEntity", "path": "custom_components/voltalis/entity.py", "snippet": "class VoltalisEntity(CoordinatorEntity):\n \"\"\"Base class for Voltalis entities.\"\"\"\n\n def __init__(\n self,\n coordinator: DataUpdateCoordinator,\n appliance: VoltalisAppliance,\n entity_name,\n ) -> None:\n \"\"\"Initialize the entity.\n\n Given a appliance id and a short name for the entity, we provide basic device\n info, name, unique id, etc. for all derived entities.\n \"\"\"\n super().__init__(coordinator)\n self.appliance = appliance\n self._attr_unique_id = str(appliance.id)\n self._attr_device_info = DeviceInfo(\n identifiers={(DOMAIN, str(appliance.id))},\n name=appliance.name.capitalize(),\n manufacturer=appliance.modulatorType,\n model=appliance.applianceType,\n )" } ]
import logging from typing import Any from homeassistant.components.climate import ( ClimateEntity, ClimateEntityFeature, HVACAction, HVACMode, ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.util.unit_conversion import TemperatureConverter from .const import ( DEFAULT_MAX_TEMP, DEFAULT_MIN_TEMP, DOMAIN, HA_PRESET_MODES, VOLTALIS_CONTROLLER, VOLTALIS_PRESET_MODES, VOLTALIS_HEATER_TYPE, ) from .entity import VoltalisEntity
1,944
def hvac_action(self) -> HVACAction | None: """Return the current running hvac operation.""" if self.appliance.programming.isOn: return HVACAction.HEATING return HVACAction.OFF @property def hvac_mode(self) -> HVACMode | None: """Return hvac operation ie. heat, cool mode.""" if self.appliance.programming.progType == "MANUAL": if not self.appliance.programming.isOn: return HVACMode.OFF return HVACMode.HEAT if self.appliance.programming.progType == "USER": return HVACMode.AUTO return self._attr_hvac_mode async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None: """Set new target hvac mode.""" _LOGGER.debug( "Set Voltalis appliance %s HVAC Mode to %s", self.appliance.id, hvac_mode ) curjson = { "id": self.appliance.idManualSetting, "enabled": True, "idAppliance": self.appliance.id, "applianceName": self.appliance.name, "applianceType": self.appliance.applianceType, "untilFurtherNotice": self.appliance.programming.untilFurtherNotice, "mode": self.appliance.programming.mode, "heatingLevel": self.appliance.heatingLevel, "endDate": self.appliance.programming.endDate, "temperatureTarget": self.appliance.programming.temperatureTarget, "isOn": self.appliance.programming.isOn, } if hvac_mode == HVACMode.HEAT: # HVACMode.HEAT -> Manual setting enable: off, untilFurtherNotice: true curjson["enabled"] = True curjson["mode"] = "TEMPERATURE" curjson["untilFurtherNotice"] = True await self.appliance.api.async_set_manualsetting( json=curjson, programming_id=self.appliance.idManualSetting ) await self.coordinator.async_request_refresh() elif hvac_mode == HVACMode.OFF: # HVACMode.OFF -> Manual setting enable: off, isOn: false curjson["enabled"] = True curjson["isOn"] = False curjson["untilFurtherNotice"] = True await self.appliance.api.async_set_manualsetting( json=curjson, programming_id=self.appliance.idManualSetting ) await self.coordinator.async_request_refresh() elif hvac_mode == HVACMode.AUTO: # HVACMode.AUTO -> Manual setting enable: False curjson["enabled"] = False await self.appliance.api.async_set_manualsetting( json=curjson, programming_id=self.appliance.idManualSetting ) await self.coordinator.async_request_refresh() @property def min_temp(self) -> float: """Return the minimum temperature.""" min_temp = DEFAULT_MIN_TEMP return TemperatureConverter.convert( min_temp, UnitOfTemperature.CELSIUS, self.temperature_unit ) @property def max_temp(self) -> float: """Return the maximum temperature.""" max_temp = DEFAULT_MAX_TEMP return TemperatureConverter.convert( max_temp, UnitOfTemperature.CELSIUS, self.temperature_unit ) @property def preset_mode(self) -> str: """Return the current preset mode.""" return HA_PRESET_MODES[self.appliance.programming.mode] @property def target_temperature(self) -> float: """Return the target temperature.""" return self.appliance.programming.temperatureTarget async def async_set_temperature(self, **kwargs: Any) -> None: """Set new target temperature.""" temperature = kwargs[ATTR_TEMPERATURE] request_body = { "id": self.appliance.idManualSetting, "enabled": True, "idAppliance": self.appliance.id, "applianceName": self.appliance.name, "applianceType": self.appliance.applianceType, "untilFurtherNotice": True, "mode": "TEMPERATURE", "heatingLevel": self.appliance.heatingLevel, "endDate": None, "temperatureTarget": temperature, "isOn": True, } await self.appliance.api.async_set_manualsetting( json=request_body, programming_id=self.appliance.idManualSetting ) await self.coordinator.async_request_refresh() async def async_set_preset_mode(self, preset_mode: str) -> None: """Activate the specified preset mode.""" request_body = { "id": self.appliance.idManualSetting, "enabled": True, "idAppliance": self.appliance.id, "applianceName": self.appliance.name, "applianceType": self.appliance.applianceType, "untilFurtherNotice": True,
"""Platform for climate integration.""" from __future__ import annotations _LOGGER = logging.getLogger(__name__) async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: """Set up climate entity for Voltalis Appliance.""" controller = hass.data[DOMAIN][entry.entry_id][VOLTALIS_CONTROLLER] entities = [] for appliance in controller.appliances: if appliance.applianceType == VOLTALIS_HEATER_TYPE: entities.append(VoltalisClimate(controller.coordinator, appliance)) async_add_entities(entities) class VoltalisClimate(VoltalisEntity, ClimateEntity): """Voltalis climate.""" _attr_has_entity_name = True _attr_hvac_mode = HVACMode.HEAT _attr_hvac_modes = [HVACMode.AUTO, HVACMode.HEAT, HVACMode.OFF] _attr_preset_modes = list(HA_PRESET_MODES.values()) _attr_max_temp = DEFAULT_MAX_TEMP _attr_min_temp = DEFAULT_MIN_TEMP _attr_supported_features = ( ClimateEntityFeature.PRESET_MODE | ClimateEntityFeature.TARGET_TEMPERATURE ) _attr_temperature_unit = UnitOfTemperature.CELSIUS def __init__(self, coordinator, appliance): """Initialize the entity.""" super().__init__(coordinator, appliance, "Appliance") @property def hvac_action(self) -> HVACAction | None: """Return the current running hvac operation.""" if self.appliance.programming.isOn: return HVACAction.HEATING return HVACAction.OFF @property def hvac_mode(self) -> HVACMode | None: """Return hvac operation ie. heat, cool mode.""" if self.appliance.programming.progType == "MANUAL": if not self.appliance.programming.isOn: return HVACMode.OFF return HVACMode.HEAT if self.appliance.programming.progType == "USER": return HVACMode.AUTO return self._attr_hvac_mode async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None: """Set new target hvac mode.""" _LOGGER.debug( "Set Voltalis appliance %s HVAC Mode to %s", self.appliance.id, hvac_mode ) curjson = { "id": self.appliance.idManualSetting, "enabled": True, "idAppliance": self.appliance.id, "applianceName": self.appliance.name, "applianceType": self.appliance.applianceType, "untilFurtherNotice": self.appliance.programming.untilFurtherNotice, "mode": self.appliance.programming.mode, "heatingLevel": self.appliance.heatingLevel, "endDate": self.appliance.programming.endDate, "temperatureTarget": self.appliance.programming.temperatureTarget, "isOn": self.appliance.programming.isOn, } if hvac_mode == HVACMode.HEAT: # HVACMode.HEAT -> Manual setting enable: off, untilFurtherNotice: true curjson["enabled"] = True curjson["mode"] = "TEMPERATURE" curjson["untilFurtherNotice"] = True await self.appliance.api.async_set_manualsetting( json=curjson, programming_id=self.appliance.idManualSetting ) await self.coordinator.async_request_refresh() elif hvac_mode == HVACMode.OFF: # HVACMode.OFF -> Manual setting enable: off, isOn: false curjson["enabled"] = True curjson["isOn"] = False curjson["untilFurtherNotice"] = True await self.appliance.api.async_set_manualsetting( json=curjson, programming_id=self.appliance.idManualSetting ) await self.coordinator.async_request_refresh() elif hvac_mode == HVACMode.AUTO: # HVACMode.AUTO -> Manual setting enable: False curjson["enabled"] = False await self.appliance.api.async_set_manualsetting( json=curjson, programming_id=self.appliance.idManualSetting ) await self.coordinator.async_request_refresh() @property def min_temp(self) -> float: """Return the minimum temperature.""" min_temp = DEFAULT_MIN_TEMP return TemperatureConverter.convert( min_temp, UnitOfTemperature.CELSIUS, self.temperature_unit ) @property def max_temp(self) -> float: """Return the maximum temperature.""" max_temp = DEFAULT_MAX_TEMP return TemperatureConverter.convert( max_temp, UnitOfTemperature.CELSIUS, self.temperature_unit ) @property def preset_mode(self) -> str: """Return the current preset mode.""" return HA_PRESET_MODES[self.appliance.programming.mode] @property def target_temperature(self) -> float: """Return the target temperature.""" return self.appliance.programming.temperatureTarget async def async_set_temperature(self, **kwargs: Any) -> None: """Set new target temperature.""" temperature = kwargs[ATTR_TEMPERATURE] request_body = { "id": self.appliance.idManualSetting, "enabled": True, "idAppliance": self.appliance.id, "applianceName": self.appliance.name, "applianceType": self.appliance.applianceType, "untilFurtherNotice": True, "mode": "TEMPERATURE", "heatingLevel": self.appliance.heatingLevel, "endDate": None, "temperatureTarget": temperature, "isOn": True, } await self.appliance.api.async_set_manualsetting( json=request_body, programming_id=self.appliance.idManualSetting ) await self.coordinator.async_request_refresh() async def async_set_preset_mode(self, preset_mode: str) -> None: """Activate the specified preset mode.""" request_body = { "id": self.appliance.idManualSetting, "enabled": True, "idAppliance": self.appliance.id, "applianceName": self.appliance.name, "applianceType": self.appliance.applianceType, "untilFurtherNotice": True,
"mode": VOLTALIS_PRESET_MODES[preset_mode],
5
2023-11-01 09:05:17+00:00
4k
r-three/licensed-pile
stackexchange/preprocess.py
[ { "identifier": "PermissiveLicenses", "path": "licensed_pile/licenses.py", "snippet": "class PermissiveLicenses(StringEnum):\n PD = \"Public Domain\"\n CC0 = \"Creative Commons Zero - Public Domain - https://creativecommons.org/publicdomain/zero/1.0/\"\n CC_BY = (\n \"Creative Commons - Attribution - https://creativecommons.org/licenses/by/4.0/\"\n )\n CC_BY_3 = (\n \"Creative Commons - Attribution - https://creativecommons.org/licenses/by/3.0/\"\n )\n CC_BY_SA = \"Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/4.0/\"\n CC_BY_SA_3 = \"Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/3.0/\"\n CC_BY_SA_2_5 = \"Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/2.5/\"\n GFDL = \"GNU Free Documentation License\"\n APACHE_2 = \"Apache 2 License - https://www.apache.org/licenses/LICENSE-2.0\"\n MIT = \"MIT License\"\n BSD = \"BSD License\"\n\n # TODO: Fill out this function to match in more cases.\n # Note: This kind of function will always be messy and probably require\n # multiple checks that are common across branches. Instead of trying to\n # clean on the implementation, which would get complex (like the compositional\n # solution to fizzbuzz https://themonadreader.files.wordpress.com/2014/04/fizzbuzz.pdf)\n # we should just have a bit of a mess and lots of unittests.\n @classmethod\n def from_string(cls, s: str) -> \"PermissiveLicenses\":\n s = s.lower().strip()\n if re.match(r\".*/publicdomain/zero/1.0/?$\", s):\n return cls.CC0\n if m := re.match(r\".*/licenses/by(?P<share>-sa)?/(?P<version>\\d).0/?$\", s):\n if m.group(\"version\") == \"4\":\n if m.group(\"share\") is None:\n return cls.CC_BY_SA\n return cls.CC_BY\n elif m.group(1) == \"3\":\n if m.group(\"share\") is None:\n return cls.CC_BY_SA_3\n return cls.CC_BY_3\n else:\n raise ValueError(f\"Unable to understand license {s}\")\n raise ValueError(f\"Unable to understand license {s}\")" }, { "identifier": "to_dolma", "path": "licensed_pile/write.py", "snippet": "def to_dolma(\n examples: Sequence[Dict],\n path: str,\n filename: str,\n shard_size: int = 1,\n quiet: bool = False,\n):\n \"\"\"Write `examples` to `path` in the dolma format with `shard_size`GB shards.\"\"\"\n os.makedirs(path, exist_ok=True)\n shard_idx = 0\n size = 0\n # Gigabytes, not Gibibytes\n max_bytes = shard_size * 1000 * 1000 * 1000\n with ExitStack() as stack:\n wf = stack.enter_context(\n smart_open.open(os.path.join(path, shard_name(filename, shard_idx)), \"w\")\n )\n for example in tqdm.tqdm(examples, disable=quiet):\n data = json.dumps(example)\n # Assume one character is about 1 bytes, good enough as we use utf-8\n size += len(data)\n if size >= max_bytes:\n wf.close()\n shard_idx += 1\n wf = stack.enter_context(\n smart_open.open(\n os.path.join(path, shard_name(filename, shard_idx)), \"w\"\n )\n )\n size = 0\n wf.write(data + \"\\n\")" } ]
import argparse import collections import dataclasses import datetime import functools import itertools import logging import multiprocessing as mp import operator as op import os import shelve import urllib.parse import bs4 import tqdm import licensed_pile.xml as xml from dataclasses import dataclass from typing import List from markdown_it import MarkdownIt from licensed_pile.licenses import PermissiveLicenses from licensed_pile.write import to_dolma
3,499
for user_id, user_names in pool.imap_unordered( functools.partial(process_user, site=site), user_xml, chunksize=100 ): if user_id is None: continue author_display[user_id].update(user_names) print("Building Lookup from post id -> authors") history_xml = xml.iterate_xml( os.path.join(args.input, "PostHistory.xml"), "row" ) # It would probably be better/faster to use a database to store these # intermediate lookups instead of a shelve (which requires multiple # pickle serialization/deserialization) but I didn't want to implement # a database based key-value store that supports list values, set values # and scalar values. if args.shelve: post_authors = shelve.open(os.path.join(args.output, "authors.shelve")) else: post_authors = {} for post_id, user_id in pool.imap_unordered( process_revision, history_xml, chunksize=100 ): if post_id is None: continue authors = post_authors.get(post_id, set()) authors.update(author_display[user_id]) # Get and assign so that values are written back to the shelve. post_authors[post_id] = authors print("Building Lookup from post/answer id -> comments") if args.shelve: comments = shelve.open(os.path.join(args.output, "comments.shelve")) else: comments = {} comment_xml = xml.iterate_xml(os.path.join(args.input, "Comments.xml"), "row") for post_id, user_id, text, date in pool.imap_unordered( process_comment, comment_xml, chunksize=100 ): if post_id is None: continue comment = comments.get(post_id, []) comment.append( Comment( text=text, author=author_display[user_id], date=date, ) ) # Get and assign so that values are written back to the shelve. comments[post_id] = comment # Sort comments based on creation date, then when we add them to the text # we know that they will be in the correct order, even if they are out # of order in the dump/from multiprocessing. # Explicit loop instead of a comprehension because it might be a shelve :( for cid, cs in comments.items(): comments[cid] = sorted(cs, key=op.attrgetter("date")) if args.shelve: parsed_dump = shelve.open(os.path.join(args.output, "questions.shelve")) else: parsed_dump = {} # Questions are the "document" level for this dataset, therefore we do # no need to sort them. print("Parsing Questions") post_xml = xml.iterate_xml(os.path.join(args.input, "Posts.xml"), "row") for post_id, text, date, license in pool.imap_unordered( process_question, post_xml, chunksize=100 ): if post_id is None: continue parsed_dump[post_id] = Question( text=text, id=post_id, authors=post_authors[post_id], # Comments are sorted in chronological order. comments=comments.get(post_id, []), date=date, license=license, ) print("Parsing Answers") # Reinitialize the iterator over the Posts as it was consumed when # looking for questions. We do this as a second pass so we know that # there will always be a question we can attach this answer to. post_xml = xml.iterate_xml(os.path.join(args.input, "Posts.xml"), "row") for question_id, answer_id, answer, date in pool.imap_unordered( process_answer, post_xml, chunksize=100 ): if question_id is None: continue question = parsed_dump[question_id] question.answers.append( Answer( text=answer, authors=post_authors[answer_id], # Comments are sorted in chronological order. comments=comments.get(answer_id, []), date=date, ) ) # Get and assign so that values are written back to the shelve. parsed_dump[question_id] = question # Sort answers to questions based on creation date, when when they are # added to the question text we know they will be in the correct order, # even if they are out of order in the dump/from multiprocessing. # Explicit loop instead of a compreshension because it might be a shelve :( for qid, q in parsed_dump.items(): q.answers = sorted(q.answers, key=op.attrgetter("date")) parsed_dump[qid] = q # Use iterators so we don't need to have the full dataset loaded at once. print("Formatting Questions as Dolma Documents") # Even on rather large datasets, such as askubuntu.com, it faster to # do the comment/answer sorting and run format dolma in the main process # I assume the cost to serialize and decerialize the question is large # and especially when the main process is the only writer. examples = map(functools.partial(format_dolma, site=site), parsed_dump.values())
"""Preprocess stack exchange data.""" parser = argparse.ArgumentParser(description="Parse a stack exchange dump.") parser.add_argument("--input", help="Path to the dump, data/dump/${site}") parser.add_argument( "--output", help="Path to the output, data/stack-exchange/v0/${site}/documents" ) parser.add_argument( "--processes", default=mp.cpu_count(), help="The number of multicore processors to use.", ) parser.add_argument( "--shelve", action="store_true", help="Save lookup tables as shelves so we don't need to keep them all in memory.", ) # Use over commonmark library as that is deprecated and has errors parsing stack overflow. MD = MarkdownIt("commonmark", {"breaks": True, "html": True}) LICENSES = { "CC BY-SA 2.5": PermissiveLicenses.CC_BY_SA_2_5, "CC BY-SA 3.0": PermissiveLicenses.CC_BY_SA_3, "CC BY-SA 4.0": PermissiveLicenses.CC_BY_SA, } @dataclass class Post: text: str date: datetime.datetime @dataclass class Comment(Post): author: str @dataclass class Answer(Post): authors: List[str] comments: List[Comment] @dataclass class Question(Post): id: str authors: List[str] comments: List[Comment] license: PermissiveLicenses answers: List[Answer] = dataclasses.field(default_factory=list) def get_attr(xml_obj, key): if key in xml_obj.attrib: return xml_obj.attrib[key] return None def get_html_text(html): soup = bs4.BeautifulSoup(html, "html.parser") return soup.get_text() def get_body_text(xml_obj): return get_html_text(get_attr(xml_obj, "Body")) def get_markdown_text(xml_obj): return get_html_text(MD.render(get_attr(xml_obj, "Text"))) # The original commonmark library used is not maintained anymore and has # issues with some of the data. # return get_html_text(commonmark.commonmark(get_attr(xml_obj, "Text"))) def process_user(user, site): """Extract user information from xml. Returns: The url to the user's page on stack exchange, the username. """ user_id = get_attr(user, "Id") if user_id == -1: return None, None return user_id, { stackexchange_url(site, user_id, "users"), get_attr(user, "DisplayName"), } def process_revision(revision): """Extract post revision information from xml. Returns: The id of the post and the id of the user who made the post. """ user_id = get_attr(revision, "Id") if user_id in (-1, None): return None, None return get_attr(revision, "PostId"), user_id def process_comment(comment): """Extract comment information from xml. Returns: The id for the comment The id for the user who made the comment The text of the comment The date the comment as created """ return ( get_attr(comment, "PostId"), get_attr(comment, "UserId"), get_markdown_text(comment), get_date(get_attr(comment, "CreationDate")), ) def get_date(ts: str) -> datetime.datetime: # TODO: Add better error handling? return datetime.datetime.fromisoformat(ts.split(".")[0]) def process_question(question): """Extract question information from xml. Returns: The id of the question The text of the question (title + content) The date the question was posted The license that applies to the question """ if get_attr(question, "PostTypeId") != "1": return None, None, None, None post_id = get_attr(question, "Id") text = f"{get_attr(question, 'Title')}\n{get_body_text(question)}" date = get_date(get_attr(question, "CreationDate")) license = stackexchange_license(get_attr(question, "ContentLicense")) return post_id, text, date, license def process_answer(answer): """Extract answer information from xml. Returns: The id of the question this answer is for The id of the answer The text of the answer The date the answer was given """ if get_attr(answer, "PostTypeId") != "2": return None, None, None, None question_id = get_attr(answer, "ParentId") answer_id = get_attr(answer, "Id") text = get_body_text(answer) date = get_date(get_attr(answer, "CreationDate")) return question_id, answer_id, text, date def stackexchange_license(license): """For a rough idea of date based licenses see https://stackoverflow.com/help/licensing. Note: Each comment, answer, and question have an attached ContentLicense, but we are currently just using the Question License for the document license. TODO: Add filtering based on license type (do any answer/comment/question have licenses that aren't permissive?) """ return LICENSES.get(license, license) def stackexchange_url(site, id, collection: str = "questions"): return urllib.parse.quote(f"https://{site}/{collection}/{id}", safe=":/") def format_dolma(question, site): all_authors = set( itertools.chain( # Authors of the questions question.authors, # Authors for each answer *(ans.authors for ans in question.answers), # Authors for each comment on the question *(c.author for c in question.comments if c.author is not None), # Authors for each comment on answers for the questions *(c.author for a in question.answers for c in a.comments), ) ) text = "\n".join( itertools.chain( # Question text (question.text,), # Text for each comment on the question (c.text for c in question.comments), # Answer text + comment on answer text for each answer *( itertools.chain((a.text,), (c.text for c in a.comments)) for a in question.answers ), ) ) return { "id": question.id, "text": text, # Source is more than just "Stack Exchange" as we want to use the question # id as the id which needs to be unique *per* source*. "source": "Stack Exchange", "added": datetime.datetime.utcnow().isoformat(), "created": question.date.isoformat(), "metadata": { "license": str(question.license), "site": site, "url": stackexchange_url(site, question.id), "authors": sorted(all_authors), }, } def main(args): # Note: The Stack Exchage data doesn't lend itself to being shared into the # dolma format before the preprocessing is done, therefore we manually use # multiprocessing as we go to generate examples in parallel which are # eventually stored in the dolma format. site = os.path.basename(args.input) os.makedirs(args.output, exist_ok=True) # TODO: Does setting the start method to `spawn` help reduce memory usage? # Note: We use iterables through out this to reduce memory usage, however, # we need to be sure that we *consume* the iterable output of the # multiprocessing pool *within* the pool context manager, otherwise the # pool will be "finalized" (deleted) before all the data is processed and # the program will hang. with mp.Pool(processes=args.processes) as pool: print("Building Lookup from user id -> user names") user_xml = xml.iterate_xml(os.path.join(args.input, "Users.xml"), "row") # This table is fairly small so we don't need to create a shelve for it. author_display = collections.defaultdict(set) for user_id, user_names in pool.imap_unordered( functools.partial(process_user, site=site), user_xml, chunksize=100 ): if user_id is None: continue author_display[user_id].update(user_names) print("Building Lookup from post id -> authors") history_xml = xml.iterate_xml( os.path.join(args.input, "PostHistory.xml"), "row" ) # It would probably be better/faster to use a database to store these # intermediate lookups instead of a shelve (which requires multiple # pickle serialization/deserialization) but I didn't want to implement # a database based key-value store that supports list values, set values # and scalar values. if args.shelve: post_authors = shelve.open(os.path.join(args.output, "authors.shelve")) else: post_authors = {} for post_id, user_id in pool.imap_unordered( process_revision, history_xml, chunksize=100 ): if post_id is None: continue authors = post_authors.get(post_id, set()) authors.update(author_display[user_id]) # Get and assign so that values are written back to the shelve. post_authors[post_id] = authors print("Building Lookup from post/answer id -> comments") if args.shelve: comments = shelve.open(os.path.join(args.output, "comments.shelve")) else: comments = {} comment_xml = xml.iterate_xml(os.path.join(args.input, "Comments.xml"), "row") for post_id, user_id, text, date in pool.imap_unordered( process_comment, comment_xml, chunksize=100 ): if post_id is None: continue comment = comments.get(post_id, []) comment.append( Comment( text=text, author=author_display[user_id], date=date, ) ) # Get and assign so that values are written back to the shelve. comments[post_id] = comment # Sort comments based on creation date, then when we add them to the text # we know that they will be in the correct order, even if they are out # of order in the dump/from multiprocessing. # Explicit loop instead of a comprehension because it might be a shelve :( for cid, cs in comments.items(): comments[cid] = sorted(cs, key=op.attrgetter("date")) if args.shelve: parsed_dump = shelve.open(os.path.join(args.output, "questions.shelve")) else: parsed_dump = {} # Questions are the "document" level for this dataset, therefore we do # no need to sort them. print("Parsing Questions") post_xml = xml.iterate_xml(os.path.join(args.input, "Posts.xml"), "row") for post_id, text, date, license in pool.imap_unordered( process_question, post_xml, chunksize=100 ): if post_id is None: continue parsed_dump[post_id] = Question( text=text, id=post_id, authors=post_authors[post_id], # Comments are sorted in chronological order. comments=comments.get(post_id, []), date=date, license=license, ) print("Parsing Answers") # Reinitialize the iterator over the Posts as it was consumed when # looking for questions. We do this as a second pass so we know that # there will always be a question we can attach this answer to. post_xml = xml.iterate_xml(os.path.join(args.input, "Posts.xml"), "row") for question_id, answer_id, answer, date in pool.imap_unordered( process_answer, post_xml, chunksize=100 ): if question_id is None: continue question = parsed_dump[question_id] question.answers.append( Answer( text=answer, authors=post_authors[answer_id], # Comments are sorted in chronological order. comments=comments.get(answer_id, []), date=date, ) ) # Get and assign so that values are written back to the shelve. parsed_dump[question_id] = question # Sort answers to questions based on creation date, when when they are # added to the question text we know they will be in the correct order, # even if they are out of order in the dump/from multiprocessing. # Explicit loop instead of a compreshension because it might be a shelve :( for qid, q in parsed_dump.items(): q.answers = sorted(q.answers, key=op.attrgetter("date")) parsed_dump[qid] = q # Use iterators so we don't need to have the full dataset loaded at once. print("Formatting Questions as Dolma Documents") # Even on rather large datasets, such as askubuntu.com, it faster to # do the comment/answer sorting and run format dolma in the main process # I assume the cost to serialize and decerialize the question is large # and especially when the main process is the only writer. examples = map(functools.partial(format_dolma, site=site), parsed_dump.values())
to_dolma(examples, os.path.join(args.output, "documents"), "se.jsonl.gz")
1
2023-11-06 16:04:10+00:00
4k
UMass-Foundation-Model/genome
engine/utils.py
[ { "identifier": "parse_step", "path": "engine/step_interpreters.py", "snippet": "def parse_step(step_str,partial=False): # ANSWER1=EVAL(image=IMAGE,expr=f\"'top' if {ANSWER0} > 0 else 'bottom'\",object='vehicle')\n tokens = list(tokenize.generate_tokens(io.StringIO(step_str).readline))\n # print(tokens)\n output_var = tokens[0].string # ANSWER1\n step_name = tokens[2].string # EVAL\n parsed_result = dict(\n output_var=output_var,\n step_name=step_name)\n if partial:\n return parsed_result\n\n arg_tokens = [token for token in tokens[4:-3] if token.string not in [',','=']] # image IMAGE ...\n num_tokens = len(arg_tokens) // 2\n args = dict()\n for i in range(num_tokens):\n args[arg_tokens[2*i].string] = arg_tokens[2*i+1].string # dict: image -> IMAGE\n parsed_result['args'] = args\n return parsed_result" }, { "identifier": "Wizardlm", "path": "engine/llm.py", "snippet": "class Wizardlm():\n @classmethod\n def init(cls, base_model=\"WizardLM/WizardCoder-Python-34B-V1.0\", n_gpus=4, max_input_tokens=16384):\n cls.llm = LLM(model=base_model, tensor_parallel_size=n_gpus, max_num_batched_tokens=max_input_tokens)\n\n @classmethod\n def generate(cls, prompt, stop_token=None, temperature=0, top_p=1, max_new_tokens=2048):\n problem_instruction = [prompt]\n stop_tokens = ['</s>']\n if stop_token:\n stop_tokens.append(stop_token)\n sampling_params = SamplingParams(temperature=temperature, top_p=top_p, max_tokens=max_new_tokens, stop=stop_tokens)\n completions = cls.llm.generate(problem_instruction, sampling_params)\n return completions[0].outputs[0].text" }, { "identifier": "Codellama", "path": "engine/llm.py", "snippet": "class Codellama():\n @classmethod\n def init(cls, base_model=\"codellama/CodeLlama-34b-Python-hf\", n_gpus=4, max_input_tokens=8192):\n cls.llm = LLM(\n model=base_model,\n dtype=\"float16\",\n trust_remote_code=True,\n tensor_parallel_size=n_gpus,\n tokenizer=\"hf-internal-testing/llama-tokenizer\",\n max_num_batched_tokens=max_input_tokens)\n\n @classmethod\n def generate(cls, prompt, stop_token=None, temperature=0, top_p=1, max_new_tokens=2048):\n problem_instruction = [prompt]\n stop_tokens = ['</s>']\n if stop_token:\n stop_tokens.append(stop_token)\n sampling_params = SamplingParams(temperature=temperature, top_p=top_p, max_tokens=max_new_tokens, stop=stop_tokens)\n completions = cls.llm.generate(problem_instruction, sampling_params)\n return completions[0].outputs[0].text" }, { "identifier": "parse_opt", "path": "param.py", "snippet": "def parse_opt():\n\n parser = argparse.ArgumentParser()\n # Data input settings\n\n # Dataset and Image\n parser.add_argument('--dataset', type=str, default=\"gqa\", help='') # Pending\n\n parser.add_argument('--ann_path', type=str, default=\"\", help='')\n parser.add_argument('--image_path', type=str, default=\"\", help='')\n parser.add_argument('--dataset_dir', type=str, default=\"\", help='')\n parser.add_argument('--output_dir', type=str, default=\"\", help='')\n parser.add_argument('--reuse_dir', type=str, default=\"\", help='')\n parser.add_argument('--split', type=str, default=\"test\", help='')\n\n parser.add_argument('--last_stage_output_dir', type=str, default=\"\", help='')\n parser.add_argument('--threshold', type=float, default=0.5, help='')\n\n parser.add_argument('--coco_dir', type=str, default=\"\", help='')\n\n parser.add_argument('--temperature', type=float, default=0, help='')\n parser.add_argument('--begin', type=int, default=0, help='')\n\n # Bool\n parser.add_argument('--use_new_module', action='store_true', default=False)\n parser.add_argument('--save_output', action='store_true', default=False)\n parser.add_argument('--add_cases', action='store_true', default=False)\n parser.add_argument('--split_cases', action='store_true', default=False)\n parser.add_argument('--save_all_module', action='store_true', default=False)\n parser.add_argument('--save_case_result', action='store_true', default=False)\n parser.add_argument('--save_prog_state', action='store_true', default=False)\n\n # Prompt\n parser.add_argument('--learning_prompt_path', type=str, help=\"\", default='./prompts/learning_prompt_debug.txt')\n parser.add_argument('--module_make_prompt_path', type=str, help=\"\", default='./prompts/module_make_prompt.txt')\n parser.add_argument('--online_prompt_path', type=str, help=\"\", default='./prompts/learning_prompt_online.txt')\n parser.add_argument('--offline_prompt_path', type=str, help=\"\", default='./prompts/learning_prompt_offlinev2.txt')\n parser.add_argument('--inference_prompt_path', type=str, help=\"\", default='./prompts/learning_prompt_inference.txt')\n parser.add_argument('--training_prompt_path', type=str, help=\"\", default='./prompts/module_debug_train_prompt.txt')\n\n parser.add_argument('--module_debug_init_prompt_path', type=str, help=\"\", default='./prompts/module_debug_init_prompt.txt')\n parser.add_argument('--module_debug_execute_error_prompt_path', type=str, help=\"\", default='./prompts/module_debug_execute_error_prompt.txt')\n parser.add_argument('--module_debug_execute_wrong_prompt_path', type=str, help=\"\", default='./prompts/module_debug_execute_wrong_prompt.txt')\n parser.add_argument('--merge_prompt_path', type=str, help=\"\", default='./prompts/merge_prompt.txt')\n\n # Save\n parser.add_argument('--module_save_dir', type=str, help=\"\", default='output/gqa_train_eval1') # Pending need to specify\n # Debug\n parser.add_argument('--test_num', type=int, help=\"\", default=3) # test 100 samples or 105\n\n # Model and Key Hyperparameter\n parser.add_argument('--stop_token', type=str, default=\"\", help='')\n parser.add_argument('--model', type=str, help=\"GPT Model\", default='gpt-3.5-turbo-16k') # Pending \"gpt-3.5-turbo-16k-0613\" or text-davinci-003\n parser.add_argument('--stage', type=float, help=\"\", default=0) # Pending\n\n # parse\n args = parser.parse_args()\n opt = vars(args)\n pprint('parsed input parameters:')\n pprint(opt)\n return args, opt" } ]
import os import openai import numpy as np import copy import io, tokenize import math import time import pdb; pdb.set_trace() from PIL import Image from .step_interpreters import parse_step from engine.llm import Wizardlm, Codellama from tenacity import ( retry, stop_after_attempt, wait_random_exponential, ) # for exponential backoff from param import parse_opt
2,394
args, opt = parse_opt() class Program: def __init__(self,prog_str,init_state=None): self.prog_str = prog_str self.state = init_state if init_state is not None else dict() self.instructions = self.prog_str.split('\n') # 每一行的代码 class ProgramInterpreter: def __init__(self, step_interpreters): self.step_interpreters = step_interpreters def add_step_interpreter(self, step_name, interpreter): self.step_interpreters[step_name] = interpreter def execute_step(self,prog_step,inspect): parse_result = parse_step(prog_step.prog_str) step_name = parse_result['step_name'] print(step_name) args = parse_result['args'] print(args) for key in args.keys(): arg_str = args[key] if arg_str[-1] in ("'", '"'): if arg_str[0] == 'f': arg_str = eval(arg_str[1:]) print(arg_str) args[key] = arg_str.format(**prog_step.state) else: args[key] = eval(arg_str) else: try: args[key] = prog_step.state[arg_str] except Exception as e: args[key] = eval(arg_str) #print(args) execute_result = self.step_interpreters[step_name].execute(*args.values()) output_var = parse_result['output_var'] prog_step.state[output_var] = execute_result return execute_result def execute(self,prog,init_state,inspect=False): if isinstance(prog,str): prog = Program(prog,init_state) else: assert(isinstance(prog,Program)) prog_steps = [Program(instruction,init_state=prog.state) \ for instruction in prog.instructions] # html_str = '<hr>' for prog_step in prog_steps: # if inspect: step_output, step_html = self.execute_step(prog_step,inspect) html_str += step_html + '<hr>' else: step_output = self.execute_step(prog_step,inspect) if inspect: return step_output, prog.state, html_str return step_output, prog.state # step_output class ProgramGenerator(): def __init__(self,args=None, temperature=0.0, top_p=0.5,prob_agg='mean'): with open('api.key') as f: openai.api_key = f.read().strip() self.temperature = args.temperature self.top_p = top_p self.prob_agg = prob_agg self.args = args self.model = args.model self.stop_token = args.stop_token def compute_prob(self,response): eos = '<|endoftext|>' for i,token in enumerate(response.choices[0]['logprobs']['tokens']): if token==eos: break if self.prob_agg=='mean': agg_fn = np.mean elif self.prob_agg=='sum': agg_fn = np.sum else: raise NotImplementedError return np.exp(agg_fn( response.choices[0]['logprobs']['token_logprobs'][:i])) @retry(wait=wait_random_exponential(min=0.2, max=0.5), stop=stop_after_attempt(10)) def generate(self,inputs): if args.model == 'wizardlm':
args, opt = parse_opt() class Program: def __init__(self,prog_str,init_state=None): self.prog_str = prog_str self.state = init_state if init_state is not None else dict() self.instructions = self.prog_str.split('\n') # 每一行的代码 class ProgramInterpreter: def __init__(self, step_interpreters): self.step_interpreters = step_interpreters def add_step_interpreter(self, step_name, interpreter): self.step_interpreters[step_name] = interpreter def execute_step(self,prog_step,inspect): parse_result = parse_step(prog_step.prog_str) step_name = parse_result['step_name'] print(step_name) args = parse_result['args'] print(args) for key in args.keys(): arg_str = args[key] if arg_str[-1] in ("'", '"'): if arg_str[0] == 'f': arg_str = eval(arg_str[1:]) print(arg_str) args[key] = arg_str.format(**prog_step.state) else: args[key] = eval(arg_str) else: try: args[key] = prog_step.state[arg_str] except Exception as e: args[key] = eval(arg_str) #print(args) execute_result = self.step_interpreters[step_name].execute(*args.values()) output_var = parse_result['output_var'] prog_step.state[output_var] = execute_result return execute_result def execute(self,prog,init_state,inspect=False): if isinstance(prog,str): prog = Program(prog,init_state) else: assert(isinstance(prog,Program)) prog_steps = [Program(instruction,init_state=prog.state) \ for instruction in prog.instructions] # html_str = '<hr>' for prog_step in prog_steps: # if inspect: step_output, step_html = self.execute_step(prog_step,inspect) html_str += step_html + '<hr>' else: step_output = self.execute_step(prog_step,inspect) if inspect: return step_output, prog.state, html_str return step_output, prog.state # step_output class ProgramGenerator(): def __init__(self,args=None, temperature=0.0, top_p=0.5,prob_agg='mean'): with open('api.key') as f: openai.api_key = f.read().strip() self.temperature = args.temperature self.top_p = top_p self.prob_agg = prob_agg self.args = args self.model = args.model self.stop_token = args.stop_token def compute_prob(self,response): eos = '<|endoftext|>' for i,token in enumerate(response.choices[0]['logprobs']['tokens']): if token==eos: break if self.prob_agg=='mean': agg_fn = np.mean elif self.prob_agg=='sum': agg_fn = np.sum else: raise NotImplementedError return np.exp(agg_fn( response.choices[0]['logprobs']['token_logprobs'][:i])) @retry(wait=wait_random_exponential(min=0.2, max=0.5), stop=stop_after_attempt(10)) def generate(self,inputs): if args.model == 'wizardlm':
return Wizardlm.generate(inputs,self.stop_token), None
1
2023-11-01 16:39:33+00:00
4k
ml4bio/RhoFold
rhofold/model/pair.py
[ { "identifier": "Linear", "path": "rhofold/model/primitives.py", "snippet": "class Linear(nn.Linear):\n \"\"\"\n A Linear layer with built-in nonstandard initializations. Called just\n like torch.nn.Linear.\n\n Implements the initializers in 1.11.4, plus some additional ones found\n in the code.\n \"\"\"\n\n def __init__(\n self,\n in_dim: int,\n out_dim: int,\n bias: bool = True,\n ):\n \"\"\"\n Args:\n in_dim:\n The final dimension of inputs to the layer\n out_dim:\n The final dimension of layer outputs\n bias:\n Whether to learn an additive bias. True by default\n \"\"\"\n super(Linear, self).__init__(in_dim, out_dim, bias=bias)\n\n if bias:\n with torch.no_grad():\n self.bias.fill_(0)" }, { "identifier": "LayerNorm", "path": "rhofold/model/primitives.py", "snippet": "class LayerNorm(nn.Module):\n def __init__(self, c_in, eps=1e-5):\n super(LayerNorm, self).__init__()\n \n self.c_in = (c_in,)\n self.eps = eps\n\n self.weight = nn.Parameter(torch.ones(c_in))\n self.bias = nn.Parameter(torch.zeros(c_in))\n\n def forward(self, x): \n\n out = nn.functional.layer_norm(\n x,\n self.c_in,\n self.weight,\n self.bias,\n self.eps,\n )\n\n return out" }, { "identifier": "chunk_layer", "path": "rhofold/utils/chunk_utils.py", "snippet": "def chunk_layer(\n layer: Callable,\n inputs: Dict[str, Any],\n chunk_size: int,\n no_batch_dims: int,\n low_mem: bool = False,\n _out: Any = None,\n _add_into_out: bool = False,\n) -> Any:\n \"\"\"\n Implements the \"chunking\" procedure described in section 1.11.8.\n\n Layer outputs and inputs are assumed to be simple \"pytrees,\"\n consisting only of (arbitrarily nested) lists, tuples, and dicts with\n torch.Tensor leaves.\n\n Args:\n layer:\n The layer to be applied chunk-wise\n inputs:\n A (non-nested) dictionary of keyworded inputs. All leaves must\n be tensors and must share the same batch dimensions.\n chunk_size:\n The number of sub-batches per chunk. If multiple batch\n dimensions are specified, a \"sub-batch\" is defined as a single\n indexing of all batch dimensions simultaneously (s.t. the\n number of sub-batches is the product of the batch dimensions).\n no_batch_dims:\n How many of the initial dimensions of each input tensor can\n be considered batch dimensions.\n low_mem:\n Avoids flattening potentially large input tensors. Unnecessary\n in most cases, and is ever so slightly slower than the default\n setting.\n Returns:\n The reassembled output of the layer on the inputs.\n \"\"\"\n if not (len(inputs) > 0):\n raise ValueError(\"Must provide at least one input\")\n\n initial_dims = [shape[:no_batch_dims] for shape in _fetch_dims(inputs)]\n orig_batch_dims = tuple([max(s) for s in zip(*initial_dims)])\n\n def _prep_inputs(t):\n if(not low_mem):\n if not sum(t.shape[:no_batch_dims]) == no_batch_dims:\n t = t.expand(orig_batch_dims + t.shape[no_batch_dims:])\n t = t.reshape(-1, *t.shape[no_batch_dims:])\n else:\n t = t.expand(orig_batch_dims + t.shape[no_batch_dims:])\n return t\n\n prepped_inputs = tensor_tree_map(_prep_inputs, inputs)\n prepped_outputs = None\n if(_out is not None):\n reshape_fn = lambda t: t.view([-1] + list(t.shape[no_batch_dims:]))\n prepped_outputs = tensor_tree_map(reshape_fn, _out)\n\n flat_batch_dim = 1\n for d in orig_batch_dims:\n flat_batch_dim *= d\n\n no_chunks = flat_batch_dim // chunk_size + (\n flat_batch_dim % chunk_size != 0\n )\n\n i = 0\n out = prepped_outputs\n for _ in range(no_chunks):\n # Chunk the input\n if(not low_mem):\n select_chunk = (\n lambda t: t[i : i + chunk_size] if t.shape[0] != 1 else t\n )\n else:\n select_chunk = (\n partial(\n _chunk_slice, \n flat_start=i, \n flat_end=min(flat_batch_dim, i + chunk_size), \n no_batch_dims=len(orig_batch_dims)\n )\n )\n\n chunks = tensor_tree_map(select_chunk, prepped_inputs)\n\n # Run the layer on the chunk\n output_chunk = layer(**chunks)\n\n # Allocate space for the output\n if out is None:\n allocate = lambda t: t.new_zeros((flat_batch_dim,) + t.shape[1:])\n out = tensor_tree_map(allocate, output_chunk)\n\n # Put the chunk in its pre-allocated space\n out_type = type(output_chunk)\n if out_type is dict:\n def assign(d1, d2):\n for k, v in d1.items():\n if type(v) is dict:\n assign(v, d2[k])\n else:\n if(_add_into_out):\n v[i: i + chunk_size] += d2[k]\n else:\n v[i: i + chunk_size] = d2[k]\n\n assign(out, output_chunk)\n elif out_type is tuple:\n for x1, x2 in zip(out, output_chunk):\n if(_add_into_out):\n x1[i: i + chunk_size] += x2\n else:\n x1[i : i + chunk_size] = x2\n elif out_type is torch.Tensor:\n if(_add_into_out):\n out[i: i + chunk_size] += output_chunk\n else:\n out[i: i + chunk_size] = output_chunk\n else:\n raise ValueError(\"Not supported\")\n\n i += chunk_size\n\n reshape = lambda t: t.view(orig_batch_dims + t.shape[1:])\n out = tensor_tree_map(reshape, out)\n\n return out" } ]
from typing import Optional from rhofold.model.primitives import Linear, LayerNorm from rhofold.utils.chunk_utils import chunk_layer import torch import torch.nn as nn import math
2,647
d_msa = 21, p_drop = 0., is_pos_emb = True, ): super(PairNet, self).__init__() self.pair_emb = PairEmbNet(d_model= d_model, p_drop = p_drop, d_seq = d_msa, is_pos_emb = is_pos_emb) def forward(self, msa_tokens, **unused): seq_tokens = msa_tokens[:, 0, :] B, L = seq_tokens.shape idx = torch.cat([torch.arange(L).long().unsqueeze(0) for i in range(B)], dim=0) if idx.device != seq_tokens.device: idx = idx.to(seq_tokens.device) return self.pair_emb(seq_tokens, idx) class PositionalEncoding2D(nn.Module): def __init__(self, d_model, p_drop=0.1): super(PositionalEncoding2D, self).__init__() self.drop = nn.Dropout(p_drop) d_model_half = d_model // 2 div_term = torch.exp(torch.arange(0., d_model_half, 2) * -(math.log(10000.0) / d_model_half)) self.register_buffer('div_term', div_term) def forward(self, x, idx_s): B, L, _, K = x.shape K_half = K // 2 pe = torch.zeros_like(x) i_batch = -1 for idx in idx_s: i_batch += 1 if idx.device != self.div_term.device: idx = idx.to(self.div_term.device) sin_inp = idx.unsqueeze(1) * self.div_term emb = torch.cat((sin_inp.sin(), sin_inp.cos()), dim=-1) pe[i_batch, :, :, :K_half] = emb.unsqueeze(1) pe[i_batch, :, :, K_half:] = emb.unsqueeze(0) x = x + torch.autograd.Variable(pe, requires_grad=False) return self.drop(x) class PairEmbNet(nn.Module): def __init__(self, d_model=128, d_seq=21, p_drop=0.1, is_pos_emb = True): super(PairEmbNet, self).__init__() self.d_model = d_model self.d_emb = d_model // 2 self.emb = nn.Embedding(d_seq, self.d_emb) self.projection = nn.Linear(d_model, d_model) self.is_pos_emb = is_pos_emb if self.is_pos_emb: self.pos = PositionalEncoding2D(d_model, p_drop=p_drop) def forward(self, seq, idx): L = seq.shape[1] seq = self.emb(seq) left = seq.unsqueeze(2).expand(-1,-1,L,-1) right = seq.unsqueeze(1).expand(-1,L,-1,-1) pair = torch.cat((left, right), dim=-1) pair = self.projection(pair) pair = self.pos(pair, idx) if self.is_pos_emb else pair return pair class PairTransition(nn.Module): """ Implements Algorithm 15. """ def __init__(self, c_z, n): """ Args: c_z: Pair transition channel dimension n: Factor by which c_z is multiplied to obtain hidden channel dimension """ super(PairTransition, self).__init__() self.c_z = c_z self.n = n self.layer_norm = LayerNorm(self.c_z) self.linear_1 = Linear(self.c_z, self.n * self.c_z) self.relu = nn.ReLU() self.linear_2 = Linear(self.n * self.c_z, c_z) def _transition(self, z, mask): # [*, N_res, N_res, C_z] z = self.layer_norm(z) # [*, N_res, N_res, C_hidden] z = self.linear_1(z) z = self.relu(z) # [*, N_res, N_res, C_z] z = self.linear_2(z) * mask return z @torch.jit.ignore def _chunk(self, z: torch.Tensor, mask: torch.Tensor, chunk_size: int, ) -> torch.Tensor:
# 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. class PairNet(nn.Module): def __init__(self, d_model = 64, d_msa = 21, p_drop = 0., is_pos_emb = True, ): super(PairNet, self).__init__() self.pair_emb = PairEmbNet(d_model= d_model, p_drop = p_drop, d_seq = d_msa, is_pos_emb = is_pos_emb) def forward(self, msa_tokens, **unused): seq_tokens = msa_tokens[:, 0, :] B, L = seq_tokens.shape idx = torch.cat([torch.arange(L).long().unsqueeze(0) for i in range(B)], dim=0) if idx.device != seq_tokens.device: idx = idx.to(seq_tokens.device) return self.pair_emb(seq_tokens, idx) class PositionalEncoding2D(nn.Module): def __init__(self, d_model, p_drop=0.1): super(PositionalEncoding2D, self).__init__() self.drop = nn.Dropout(p_drop) d_model_half = d_model // 2 div_term = torch.exp(torch.arange(0., d_model_half, 2) * -(math.log(10000.0) / d_model_half)) self.register_buffer('div_term', div_term) def forward(self, x, idx_s): B, L, _, K = x.shape K_half = K // 2 pe = torch.zeros_like(x) i_batch = -1 for idx in idx_s: i_batch += 1 if idx.device != self.div_term.device: idx = idx.to(self.div_term.device) sin_inp = idx.unsqueeze(1) * self.div_term emb = torch.cat((sin_inp.sin(), sin_inp.cos()), dim=-1) pe[i_batch, :, :, :K_half] = emb.unsqueeze(1) pe[i_batch, :, :, K_half:] = emb.unsqueeze(0) x = x + torch.autograd.Variable(pe, requires_grad=False) return self.drop(x) class PairEmbNet(nn.Module): def __init__(self, d_model=128, d_seq=21, p_drop=0.1, is_pos_emb = True): super(PairEmbNet, self).__init__() self.d_model = d_model self.d_emb = d_model // 2 self.emb = nn.Embedding(d_seq, self.d_emb) self.projection = nn.Linear(d_model, d_model) self.is_pos_emb = is_pos_emb if self.is_pos_emb: self.pos = PositionalEncoding2D(d_model, p_drop=p_drop) def forward(self, seq, idx): L = seq.shape[1] seq = self.emb(seq) left = seq.unsqueeze(2).expand(-1,-1,L,-1) right = seq.unsqueeze(1).expand(-1,L,-1,-1) pair = torch.cat((left, right), dim=-1) pair = self.projection(pair) pair = self.pos(pair, idx) if self.is_pos_emb else pair return pair class PairTransition(nn.Module): """ Implements Algorithm 15. """ def __init__(self, c_z, n): """ Args: c_z: Pair transition channel dimension n: Factor by which c_z is multiplied to obtain hidden channel dimension """ super(PairTransition, self).__init__() self.c_z = c_z self.n = n self.layer_norm = LayerNorm(self.c_z) self.linear_1 = Linear(self.c_z, self.n * self.c_z) self.relu = nn.ReLU() self.linear_2 = Linear(self.n * self.c_z, c_z) def _transition(self, z, mask): # [*, N_res, N_res, C_z] z = self.layer_norm(z) # [*, N_res, N_res, C_hidden] z = self.linear_1(z) z = self.relu(z) # [*, N_res, N_res, C_z] z = self.linear_2(z) * mask return z @torch.jit.ignore def _chunk(self, z: torch.Tensor, mask: torch.Tensor, chunk_size: int, ) -> torch.Tensor:
return chunk_layer(
2
2023-11-01 10:29:08+00:00
4k
trangdata/askalex
app.py
[ { "identifier": "answer_question", "path": "askalex.py", "snippet": "def answer_question(\n question,\n df,\n engine=\"T-Cell-Phenotype\", # \"GPT-4-32k\",\n max_len=4097,\n size=\"ada\",\n debug=False,\n stop_sequence=None,\n):\n \"\"\"\n Answer a question based on the most similar context from the dataframe texts\n \"\"\"\n if question is None:\n return \"\"\n\n template = (\n \"You are an intelligent assistant helping users with their questions. \"\n + \"Use 'you' to refer to the individual asking the questions even if they ask with 'I'. \"\n + \"Answer the following question using only the data provided in the sources below. \"\n + \"For tabular information return it as an html table. Do not return markdown format. \"\n + \"Each source has a name followed by colon and the actual information, always include the source name for each fact you use in the response. \"\n + \"If you cannot answer using the sources below, say you don't know. \"\n + \"\\n\\nContext: {context}\\n\\n---\\n\\nQuestion: {question}\\nAnswer: \"\n )\n\n context = create_context(\n question,\n df,\n max_len=max_len,\n size=size,\n )\n # If debug, print the raw model response\n if debug:\n print(\"Context:\\n\" + context)\n print(\"\\n\\n\")\n\n prompt = template.format(context=context, question=question)\n try:\n return trim_incomplete_sentence(complete_model(prompt, engine, stop_sequence))\n except Exception as e:\n print(e)\n return \"\"" }, { "identifier": "find_abs", "path": "openalex.py", "snippet": "def find_abs(keywords, per_page=100):\n print(\"Finding pubs...\")\n woi = pyalex.Works().search_filter(abstract=keywords).get(per_page=per_page)\n abs_df = pd.DataFrame(\n [\n {\n \"title\": e[\"title\"],\n \"abstract\": e[\"abstract\"],\n \"url\": e[\"doi\"],\n }\n for e in woi\n ]\n )\n abs_df[\"abstract\"] = abs_df[\"abstract\"].apply(shorten_abstract)\n print(\"Done!\")\n return abs_df" }, { "identifier": "get_embed", "path": "openalex.py", "snippet": "def get_embed(\n df,\n embedding_model=\"tcell_ada_embeddings\",\n embedding_encoding=\"cl100k_base\", # this the encoding for text-embedding-ada-002\n max_tokens=8000, # the maximum for text-embedding-ada-002 is 8191\n top_n=1000,\n):\n print(\"Finding embeddings...\")\n # omit reviews that are too long to embed\n encoding = tiktoken.get_encoding(embedding_encoding)\n df[\"n_tokens\"] = df.abstract.apply(lambda x: len(encoding.encode(x)))\n df = df[df.n_tokens <= max_tokens].tail(top_n)\n\n df[\"embedding\"] = df.abstract.apply(\n lambda x: get_embedding(x, engine=embedding_model)\n )\n print(\"Done!\")\n return df" }, { "identifier": "search_docs", "path": "openalex.py", "snippet": "def search_docs(\n df,\n user_query,\n embedding_model=\"tcell_ada_embeddings\",\n top_n=10,\n):\n # perform semantic search on these abstracts and find\n # the top 10 relevant abstracts\n embedding = get_embedding(user_query, engine=embedding_model)\n df[\"similarities\"] = df.embedding.apply(lambda x: cosine_similarity(x, embedding))\n res = df.sort_values(\"similarities\", ascending=False).head(top_n)\n return res" }, { "identifier": "style_dataframe", "path": "openalex.py", "snippet": "def style_dataframe(df):\n # check that the input DataFrame has the correct columns\n expected_columns = [\"similarities\", \"title\", \"abstract\", \"url\"]\n missing_columns = set(expected_columns) - set(df.columns)\n if missing_columns:\n raise ValueError(f\"Missing columns in input DataFrame: {missing_columns}\")\n\n styled_df = pd.DataFrame()\n styled_df[\"Publication\"] = df.apply(\n lambda row: f'<p style=\"font-weight: bold; font-size: larger\"><a href=\"{row[\"url\"]}\">{row[\"title\"]}</a></p><p>{row[\"abstract\"]}</p>',\n axis=1,\n )\n styled_df[\"Similarity\"] = df[\"similarities\"].apply(lambda x: f\"{x:.3f}\")\n\n return styled_df" } ]
from shiny import App, render, ui, reactive from dotenv import load_dotenv from askalex import answer_question from openalex import find_abs, get_embed, search_docs, style_dataframe import os import openai import pyalex import random
2,148
ui.column( 4, ui.input_text( "oa_quick_key", "", placeholder=random.choice(sample_keys), width="100%", ), ), ui.column( 4, ui.input_action_button( "oa_quick_submit", "Submit", ), ), ), ui.br(), ui.output_text("quick_sum"), ui.output_ui("refs"), ui.output_table("oa_quick_articles_tab"), ), ), ui.nav( "Ask your question", ui.layout_sidebar( ui.panel_sidebar( ui.input_text( "oa_keyword", "Keyword(s) to OpenAlex", placeholder="TYK2", width="100%", ), ui.input_select( "oa_engine", "LLM model", model_engine_dict, ), ui.input_slider( "n_articles", "Number of articles to index:", min=5, max=30, value=10, ), ), ui.panel_main( ui.row( ui.column( 5, ui.p("Question:"), ), ui.column( 5, ui.input_switch("oa_sample", "Use an example", False), ), ui.column( 2, ui.input_action_button( "oa_submit", "Submit", style="margin-top: -6px;margin-bottom: 12px;", width="100%", ), ), ), ui.output_ui("oa_question"), ui.output_text("oa_txt"), ), ), ui.output_table("oa_articles_tab"), ), ui.nav_spacer(), ui.nav_menu( "Other links", ui.nav_control( ui.a( "Source code", href="https://github.com/trangdata/askalex", target="_blank", ), ), align="right", ), title="🦙 AskAlex", inverse=True, id="navbar_id", ) def server(input, output, session): ids: list[str] = [] @output @render.ui @reactive.event( input.oa_quick_submit, input.oa_submit, input.ps_submit, ) def refs(): return ui.h4("References") def embedded_abs(abs): nonlocal ids id = ui.notification_show("Computing embeddings...", duration=None) ids.append(id) emb = get_embed(abs) return emb ## OpenAlex tab: Quick summary: oa_ @reactive.Calc @reactive.event(input.oa_quick_submit) def oa_quick_question(): return "Give me a quick summary of " + input.oa_quick_key() @reactive.Calc @reactive.event(input.oa_quick_submit) def oa_quick_articles():
# %% model_engine_dict = { "Text-Davinci-003": "text-davinci-003 (faster)", "GPT-4": "gpt-4", "GPT-4-32k": "gpt-4-32k (slower)", } sample_keys = ["TYK2", "DLBCL", "ProTiler", "atopic dermatitis"] oa_sample_questions = { "On a scale from 0—10, what score would you give the gene BRCA1 for its association with breast cancer?": "BRCA1 breast cancer", "What are some key points about TYK2?": "TYK2", } # %% # %% load_dotenv() openai.api_type = os.getenv("OPENAI_API_TYPE") openai.api_base = os.getenv("OPENAI_API_BASE") openai.api_version = os.getenv("OPENAI_API_VERSION") openai.api_key = os.getenv("OPENAI_API_KEY") openai.proxy = os.getenv("OPENAI_PROXY") pyalex.config.api_key = os.getenv("OPENALEX_API_KEY") pyalex.config.email = "[email protected]" # client = openai.AzureOpenAI( # api_key=openai.api_key, # api_version=openai.api_version, # # azure_endpoint=openai.api_base, # base_url=openai.api_base, # ) if os.getenv("APP_RUN") == "local": company_proxy = os.getenv("COMPANY_PROXY") os.environ["http_proxy"] = company_proxy os.environ["https_proxy"] = company_proxy os.environ["ftp_proxy"] = company_proxy os.environ["no_proxy"] = os.getenv("COMPANY_NO_PROXY") app_ui = ui.page_navbar( ui.nav( "Quick summary", ui.div( {"style": "width:70%;margin: 0 auto"}, ui.p("\n"), ui.row( ui.column( 4, ui.p( "Give me a quick summary of", style="margin-top: 6px;", ), ), ui.column( 4, ui.input_text( "oa_quick_key", "", placeholder=random.choice(sample_keys), width="100%", ), ), ui.column( 4, ui.input_action_button( "oa_quick_submit", "Submit", ), ), ), ui.br(), ui.output_text("quick_sum"), ui.output_ui("refs"), ui.output_table("oa_quick_articles_tab"), ), ), ui.nav( "Ask your question", ui.layout_sidebar( ui.panel_sidebar( ui.input_text( "oa_keyword", "Keyword(s) to OpenAlex", placeholder="TYK2", width="100%", ), ui.input_select( "oa_engine", "LLM model", model_engine_dict, ), ui.input_slider( "n_articles", "Number of articles to index:", min=5, max=30, value=10, ), ), ui.panel_main( ui.row( ui.column( 5, ui.p("Question:"), ), ui.column( 5, ui.input_switch("oa_sample", "Use an example", False), ), ui.column( 2, ui.input_action_button( "oa_submit", "Submit", style="margin-top: -6px;margin-bottom: 12px;", width="100%", ), ), ), ui.output_ui("oa_question"), ui.output_text("oa_txt"), ), ), ui.output_table("oa_articles_tab"), ), ui.nav_spacer(), ui.nav_menu( "Other links", ui.nav_control( ui.a( "Source code", href="https://github.com/trangdata/askalex", target="_blank", ), ), align="right", ), title="🦙 AskAlex", inverse=True, id="navbar_id", ) def server(input, output, session): ids: list[str] = [] @output @render.ui @reactive.event( input.oa_quick_submit, input.oa_submit, input.ps_submit, ) def refs(): return ui.h4("References") def embedded_abs(abs): nonlocal ids id = ui.notification_show("Computing embeddings...", duration=None) ids.append(id) emb = get_embed(abs) return emb ## OpenAlex tab: Quick summary: oa_ @reactive.Calc @reactive.event(input.oa_quick_submit) def oa_quick_question(): return "Give me a quick summary of " + input.oa_quick_key() @reactive.Calc @reactive.event(input.oa_quick_submit) def oa_quick_articles():
df = search_docs(
3
2023-11-08 16:29:27+00:00
4k
dcermak/rpm-spec-language-server
rpm_spec_language_server/server.py
[ { "identifier": "SpecSections", "path": "rpm_spec_language_server/document_symbols.py", "snippet": "class SpecSections:\n sections: list[SpecSection]\n spec: Specfile\n\n def section_under_cursor(self, position: Position) -> SpecSection | None:\n for sect in self.sections:\n if position.line >= sect.starting_line and position.line < sect.ending_line:\n return sect\n return None\n\n @staticmethod\n def parse(spec: Specfile) -> SpecSections:\n sections = []\n\n with spec.sections() as sects:\n current_line = 0\n\n for section in sects:\n name = section.name\n\n if opt := str(section.options).strip():\n if not opt.startswith(\"-\"):\n name = f\"{name} {spec.name}-{opt.split()[0]}\"\n\n if \"-n\" in opt:\n name = f\"{name} {(o := opt.split())[o.index('-n') + 1]}\"\n\n section_length = len(section.data)\n\n if name != \"package\":\n section_length += 1\n\n sections.append(\n SpecSection(\n name,\n starting_line=current_line,\n ending_line=current_line + section_length,\n _section=section,\n )\n )\n\n current_line += section_length\n\n return SpecSections(sections, spec)\n\n def to_document_symbols(self) -> list[DocumentSymbol]:\n return [\n DocumentSymbol(\n name=section.name,\n kind=SymbolKind.Namespace,\n range=(\n section_range := Range(\n start=Position(line=section.starting_line, character=0),\n end=Position(line=section.ending_line, character=0),\n )\n ),\n selection_range=section_range\n if section.name == \"package\"\n else Range(\n start=Position(line=section.starting_line, character=0),\n end=Position(line=section.starting_line + 1, character=0),\n ),\n )\n for section in self.sections\n ]" }, { "identifier": "create_autocompletion_documentation_from_spec_md", "path": "rpm_spec_language_server/extract_docs.py", "snippet": "def create_autocompletion_documentation_from_spec_md(spec_md: str) -> AutoCompleteDoc:\n spec = split_document(spec_md.splitlines())\n\n preamble_keywords = get_preamble_or_dependencies_keywords(spec.preamble)\n dependencies_keywords = get_preamble_or_dependencies_keywords(spec.dependencies)\n build_scriptlets_keywords = get_build_scriptlets_keywords(spec.build_scriptlets)\n\n preamble = {}\n dependencies = {}\n build_scriptlets = {}\n\n for keyword in preamble_keywords:\n preamble[keyword] = get_preamble_or_dependencies_doc(keyword, spec.preamble)\n\n for keyword in dependencies_keywords:\n dependencies[keyword] = get_preamble_or_dependencies_doc(\n keyword, spec.dependencies\n )\n\n for keyword in build_scriptlets_keywords:\n build_scriptlets[keyword] = get_build_scriptlets_doc(\n keyword, spec.build_scriptlets\n )\n\n return AutoCompleteDoc(preamble, dependencies, build_scriptlets)" }, { "identifier": "spec_md_from_rpm_db", "path": "rpm_spec_language_server/extract_docs.py", "snippet": "def spec_md_from_rpm_db() -> str | None:\n path = os.path.expanduser(\"~/.cache/rpm/spec.md\")\n if os.path.exists(path):\n with open(path) as spec_md_f:\n return spec_md_f.read(-1)\n else:\n ts = rpm.TransactionSet()\n for pkg in ts.dbMatch(\"name\", \"rpm\"):\n for f in rpm.files(pkg):\n if (path := f.name).endswith(\"spec.md\"):\n with open(path) as spec_md_f:\n return spec_md_f.read(-1)\n\n return None" }, { "identifier": "LOGGER", "path": "rpm_spec_language_server/logging.py", "snippet": "LOGGER = logging.getLogger()" }, { "identifier": "get_macro_under_cursor", "path": "rpm_spec_language_server/macros.py", "snippet": "@overload\ndef get_macro_under_cursor(\n *,\n spec: Specfile,\n position: Position,\n macros_dump: list[Macro] | None = None,\n) -> Macro | str | None:\n ..." }, { "identifier": "position_from_match", "path": "rpm_spec_language_server/util.py", "snippet": "def position_from_match(re_match: Match[str]) -> Position:\n \"\"\"Calculate the position of a regex search/match in a string.\"\"\"\n\n line_count_before_match = re_match.string[: re_match.start()].count(\"\\n\")\n lines_before_match = re_match.string.splitlines()[:line_count_before_match]\n\n # length of all the lines *before* the match\n length_of_lines = (\n # summed up length of all lines before the match\n # add 0 as the initial value in case the match is on the first line\n reduce(lambda a, b: a + b, (len(line) for line in lines_before_match), 0)\n # don't forget to consider the line separators\n + len(lines_before_match)\n )\n\n character_pos = re_match.start() - length_of_lines\n\n return Position(line=line_count_before_match, character=character_pos)" }, { "identifier": "spec_from_text", "path": "rpm_spec_language_server/util.py", "snippet": "def spec_from_text(spec_contents: str, file_name: str | None = None) -> Specfile | None:\n \"\"\"Load a specfile with the supplied contents and return a ``Specfile``\n instance or ``None`` if the spec cannot be parsed.\n\n The optional ``file_name`` parameter can be used to set the file name of the\n temporary spec that is used for parsing.\n\n \"\"\"\n with TemporaryDirectory() as tmp_dir:\n with open(\n path := (f\"{tmp_dir}/{file_name or 'unnamed.spec'}\"), \"w\"\n ) as tmp_spec:\n tmp_spec.write(spec_contents)\n\n try:\n return Specfile(path)\n except RPMException as rpm_exc:\n LOGGER.debug(\"Failed to parse spec, got %s\", rpm_exc)\n return None" }, { "identifier": "spec_from_text_document", "path": "rpm_spec_language_server/util.py", "snippet": "def spec_from_text_document(\n text_document: TextDocumentIdentifier | TextDocumentItem,\n) -> Specfile | None:\n \"\"\"Load a Specfile from a ``TextDocumentIdentifier`` or ``TextDocumentItem``.\n\n For ``TextDocumentIdentifier``s, load the file from disk and create the\n ``Specfile`` instance. For ``TextDocumentItem``s, load the spec from the\n in-memory representation.\n\n Returns ``None`` if the spec cannot be parsed.\n\n \"\"\"\n url = urlparse(text_document.uri)\n\n if url.scheme != \"file\" or not url.path.endswith(\".spec\"):\n return None\n\n if not (text := getattr(text_document, \"text\", None)):\n try:\n return Specfile(url.path)\n except RPMException as rpm_exc:\n LOGGER.debug(\"Failed to parse spec %s, got %s\", url.path, rpm_exc)\n return None\n\n return spec_from_text(text, os.path.basename(url.path))" } ]
import rpm import re import os.path from importlib import metadata from specfile.exceptions import RPMException from specfile.macros import MacroLevel, Macros from lsprotocol.types import ( TEXT_DOCUMENT_COMPLETION, TEXT_DOCUMENT_DEFINITION, TEXT_DOCUMENT_DID_CHANGE, TEXT_DOCUMENT_DID_CLOSE, TEXT_DOCUMENT_DID_OPEN, TEXT_DOCUMENT_DID_SAVE, TEXT_DOCUMENT_DOCUMENT_SYMBOL, TEXT_DOCUMENT_HOVER, CompletionItem, CompletionList, CompletionOptions, CompletionParams, DefinitionParams, DidChangeTextDocumentParams, DidCloseTextDocumentParams, DidOpenTextDocumentParams, DidSaveTextDocumentParams, DocumentSymbol, DocumentSymbolParams, Hover, HoverParams, Location, LocationLink, MarkupContent, MarkupKind, Position, Range, SymbolInformation, TextDocumentIdentifier, TextDocumentItem, ) from pygls.server import LanguageServer from rpm_spec_language_server.document_symbols import SpecSections from rpm_spec_language_server.extract_docs import ( create_autocompletion_documentation_from_spec_md, spec_md_from_rpm_db, ) from rpm_spec_language_server.logging import LOGGER from rpm_spec_language_server.macros import get_macro_under_cursor from rpm_spec_language_server.util import ( position_from_match, spec_from_text, spec_from_text_document, )
2,070
class RpmSpecLanguageServer(LanguageServer): _CONDITION_KEYWORDS = [ # from https://github.com/rpm-software-management/rpm/blob/7d3d9041af2d75c4709cf7a721daf5d1787cce14/build/rpmbuild_internal.h#L58 "%endif", "%else", "%if", "%ifarch", "%ifnarch", "%ifos", "%ifnos", "%include", "%elifarch", "%elifos", "%elif", ] def __init__(self) -> None: super().__init__(name := "rpm_spec_language_server", metadata.version(name)) self.spec_files: dict[str, SpecSections] = {} self.macros = Macros.dump()
class RpmSpecLanguageServer(LanguageServer): _CONDITION_KEYWORDS = [ # from https://github.com/rpm-software-management/rpm/blob/7d3d9041af2d75c4709cf7a721daf5d1787cce14/build/rpmbuild_internal.h#L58 "%endif", "%else", "%if", "%ifarch", "%ifnarch", "%ifos", "%ifnos", "%include", "%elifarch", "%elifos", "%elif", ] def __init__(self) -> None: super().__init__(name := "rpm_spec_language_server", metadata.version(name)) self.spec_files: dict[str, SpecSections] = {} self.macros = Macros.dump()
self.auto_complete_data = create_autocompletion_documentation_from_spec_md(
1
2023-11-02 10:52:17+00:00
4k
ziqi-zhang/TAOISM
python/layers/quant_relu.py
[ { "identifier": "SecretActivationLayer", "path": "python/layers/activation.py", "snippet": "class SecretActivationLayer(SecretNonlinearLayer):\n def __init__(\n self, sid, LayerName, EnclaveMode, link_prev=True, link_next=True,\n manually_register_prev=False, manually_register_next=False, merge_own_tensors=False\n ):\n super().__init__(sid, LayerName, EnclaveMode, link_prev, link_next, manually_register_prev, manually_register_next)\n self.Shapefortranspose = None\n self.link_prev = link_prev\n self.link_next = link_next\n self.manual_register_prev = manually_register_prev\n self.manual_register_next = manually_register_next\n self.merge_own_tensors = merge_own_tensors\n\n def init_shape(self):\n self.InputShape = self.PrevLayer.get_output_shape()\n self.OutputShape = self.InputShape\n self.HandleShape = self.InputShape\n\n def init(self, start_enclave=True):\n TensorLoader.init(self, start_enclave)\n\n def link_tensors(self):\n if self.merge_own_tensors:\n self.manually_link_owned_two_tensors(\"input\", \"output\")\n super().link_tensors()\n\n\n def get_output_shape(self):\n return self.OutputShape\n\n def generate_tensor_name_list(self, force=False):\n if not force and self.tensor_name_list:\n return\n if self.sid == 2:\n self.tensor_name_list = {}\n return\n if len(self.InputShape) == 4:\n # self.Shapefortranspose = [int(round(((self.InputShape[0] * self.InputShape[1] * self.InputShape[2] * self.InputShape[3])/262144+1/2))), 262144, 1, 1]\n self.Shapefortranspose = [int(round(((self.InputShape[0] * self.InputShape[1] * self.InputShape[2] * self.InputShape[3])/602112+1/2))), 602112, 1, 1]\n \n else:\n self.Shapefortranspose = self.InputShape\n NeededTensorNames = [(\"output\", self.OutputShape, None),\n (\"handle\", self.HandleShape, None),\n # (\"DerInput\", self.InputShape, None),\n (\"input\", self.InputShape, None),\n # (\"DerOutput\", self.OutputShape, None),\n (\"inputtrans\", self.Shapefortranspose, None),\n (\"outputtrans\", self.Shapefortranspose, None),\n ]\n\n self.tensor_name_list = NeededTensorNames\n\n def forward(self):\n with NamedTimerInstance(f\"S{self.sid}: {self.LayerName} Forward\", verbose_level=VerboseLevel.LAYER):\n with NamedTimerInstance(f\" S{self.sid}: {self.LayerName} Input Preprocess\", verbose_level=VerboseLevel.LAYER):\n self.forward_tensor_transfer()\n # self.requires_grad_on_cpu(\"input\")\n if self.EnclaveMode == ExecutionModeOptions.Enclave:\n # if self.PrevLayer.EnclaveMode is not ExecutionModeOptions.Enclave:\n # with NamedTimerInstance(f\" S{self.sid}: {self.LayerName} Input Preprocess\", verbose_level=VerboseLevel.LAYER):\n # self.transfer_enclave_to_cpu(\"input\")\n # if torch.sum(self.get_cpu(\"input\").abs()) == 0:\n # raise RuntimeError(f\"{self.LayerName}: SGX input not load\")\n # self.transfer_cpu_to_enclave(\"input\")\n with NamedTimerInstance(f\" S{self.sid}: {self.LayerName} ForwardFunc\", verbose_level=VerboseLevel.LAYER):\n self.ForwardFunc(\"input\", \"output\")\n elif self.EnclaveMode == ExecutionModeOptions.CPU:\n if self.PrevLayer.EnclaveMode is not ExecutionModeOptions.CPU and torch.sum(self.get_cpu(\"input\").abs()) == 0:\n raise RuntimeError(f\"{self.LayerName}: SGX input not load\")\n self.set_cpu(\"output\", self.ForwardFunc(self.get_cpu(\"input\")))\n elif self.EnclaveMode == ExecutionModeOptions.GPU:\n if self.PrevLayer.EnclaveMode is not ExecutionModeOptions.GPU and torch.sum(self.get_gpu(\"input\").abs()) == 0:\n raise RuntimeError(f\"{self.LayerName}: SGX input not load\")\n self.set_gpu(\"output\", self.ForwardFunc(self.get_gpu(\"input\")))\n else:\n raise RuntimeError\n\n def backward(self):\n with NamedTimerInstance(f\"S{self.sid}: {self.LayerName} Backward\", verbose_level=VerboseLevel.LAYER):\n self.backward_tensor_transfer()\n if self.is_enclave_mode:\n self.BackwardFunc(\"output\", \"DerOutput\", \"DerInput\")\n else:\n self.set_cpu(\"DerInput\", self.get_cpu(\"output\").grad_fn(self.get_cpu(\"DerOutput\")))" }, { "identifier": "ExecutionModeOptions", "path": "python/utils/basic_utils.py", "snippet": "class ExecutionModeOptions(Enum):\n Enclave = 1\n CPU = 2\n GPU = 3" }, { "identifier": "compare_expected_actual", "path": "python/utils/torch_utils.py", "snippet": "def compare_expected_actual(expected, actual, show_where_err=False, get_relative=False, verbose=False, show_values=False):\n def purify(x):\n # return torch.tensor(x)\n res = x\n # if not (isinstance(x, torch.Tensor) or isinstance(x, torch.Variable)):\n if not (isinstance(x, torch.Tensor) ):\n res = torch.tensor(x)\n # return x.detach().numpy()\n return res.type(torch.float).to(\"cpu\")\n expected = purify(expected)\n actual = purify(actual)\n\n if show_values:\n print(\"expected:\", expected[0, 0])\n print(\"actual:\", actual[0, 0])\n\n avg_abs_diff = torch.mean(torch.abs(expected - actual)).item()\n res = avg_abs_diff\n\n if show_where_err:\n show_indices = torch.abs(expected - actual) / torch.abs(expected) > 0.5\n # show_indices = (expected != actual)\n print(\"error indices: \", np.where(show_indices.cpu()))\n print(\"expected values:\", expected[show_indices])\n print(\"difference:\", (expected - actual)[show_indices])\n\n if get_relative:\n tmp_expected, tmp_actual = expected[expected != 0], actual[expected != 0]\n relative_diff = torch.abs(tmp_expected - tmp_actual) / torch.abs(tmp_expected)\n relative_avg_diff = torch.mean(torch.abs(tmp_actual - tmp_expected)) / torch.mean(torch.abs(tmp_expected))\n Error = namedtuple(\"Error\", (\"AvgAbsDiff\", \"RelAvgDiff\", \"AvgRelDiff\", \"StdRelDiff\"))\n res = Error(avg_abs_diff, relative_avg_diff.item(), torch.mean(relative_diff).item(), torch.std(relative_diff).item())\n\n if verbose:\n print(res)\n\n return res" }, { "identifier": "NamedTimerInstance", "path": "python/utils/timer_utils.py", "snippet": "class NamedTimerInstance(object):\n def __init__(self, name, verbose_level=VerboseLevel.EVERY):\n self.name = name\n self.verbose_level = verbose_level\n\n def __enter__(self):\n return NamedTimer.start(self.name, verbose_level=self.verbose_level)\n ...\n\n def __exit__(self, *args):\n NamedTimer.end(self.name)\n ..." }, { "identifier": "VerboseLevel", "path": "python/utils/timer_utils.py", "snippet": "class VerboseLevel(IntEnum):\n EVERY = 1\n LAYER = 2\n RUN = 3\n EPOCH = 4" } ]
import torch import ctypes as C import numpy as np from pdb import set_trace as st from python.layers.activation import SecretActivationLayer from python.utils.basic_utils import ExecutionModeOptions from python.utils.torch_utils import compare_expected_actual from python.utils.timer_utils import NamedTimerInstance, VerboseLevel from ctypes.util import find_library
2,465
class SecretEnclaveQuantReLULayer(SecretActivationLayer): def __init__( self, sid, LayerName, EnclaveMode, link_prev=True, link_next=True, manually_register_prev=False, manually_register_next=False, merge_own_tensors=False ): super().__init__( sid, LayerName, EnclaveMode, link_prev, link_next, manually_register_prev, manually_register_next, merge_own_tensors ) self.ForwardFuncName = "ReLU" self.BackwardFuncName = "DerReLU" self.PlainFunc = torch.nn.ReLU # if self.EnclaveMode is ExecutionModeOptions.Enclave: # self.ForwardFunc = self.relufunc # self.BackwardFunc = self.relubackfunc # elif self.EnclaveMode is ExecutionModeOptions.CPU: # self.ForwardFunc = torch.nn.ReLU # elif self.EnclaveMode is ExecutionModeOptions.GPU: # self.ForwardFunc = torch.nn.ReLU # self.ForwardFunc = self.quant_relufunc self.BackwardFunc = self.relubackfunc self.EnclaveMode = ExecutionModeOptions.GPU def init(self, start_enclave=True): super().init(start_enclave) self.PlainFunc = self.PlainFunc() def init_shape(self): self.InputShape = self.PrevLayer.get_output_shape() self.OutputShape = self.InputShape self.HandleShape = self.InputShape assert self.InputShape[1]%4 == 0 self.QuantizedInputShape = [self.InputShape[0], self.InputShape[1]//4, self.InputShape[2], self.InputShape[3]] def generate_tensor_name_list(self, force=False): if not force and self.tensor_name_list: return if self.sid == 2: self.tensor_name_list = {} return if len(self.InputShape) == 4: # self.Shapefortranspose = [int(round(((self.InputShape[0] * self.InputShape[1] * self.InputShape[2] * self.InputShape[3])/262144+1/2))), 262144, 1, 1] self.Shapefortranspose = [int(round(((self.InputShape[0] * self.InputShape[1] * self.InputShape[2] * self.InputShape[3])/602112+1/2))), 602112, 1, 1] else: self.Shapefortranspose = self.InputShape NeededTensorNames = [("output", self.OutputShape, None), ("handle", self.HandleShape, None), # ("DerInput", self.InputShape, None), ("input", self.InputShape, None), ("quant_input", self.QuantizedInputShape, None), ("quant_output", self.QuantizedInputShape, None), ("inputtrans", self.Shapefortranspose, None), ("outputtrans", self.Shapefortranspose, None), ] self.tensor_name_list = NeededTensorNames def forward(self):
class SecretEnclaveQuantReLULayer(SecretActivationLayer): def __init__( self, sid, LayerName, EnclaveMode, link_prev=True, link_next=True, manually_register_prev=False, manually_register_next=False, merge_own_tensors=False ): super().__init__( sid, LayerName, EnclaveMode, link_prev, link_next, manually_register_prev, manually_register_next, merge_own_tensors ) self.ForwardFuncName = "ReLU" self.BackwardFuncName = "DerReLU" self.PlainFunc = torch.nn.ReLU # if self.EnclaveMode is ExecutionModeOptions.Enclave: # self.ForwardFunc = self.relufunc # self.BackwardFunc = self.relubackfunc # elif self.EnclaveMode is ExecutionModeOptions.CPU: # self.ForwardFunc = torch.nn.ReLU # elif self.EnclaveMode is ExecutionModeOptions.GPU: # self.ForwardFunc = torch.nn.ReLU # self.ForwardFunc = self.quant_relufunc self.BackwardFunc = self.relubackfunc self.EnclaveMode = ExecutionModeOptions.GPU def init(self, start_enclave=True): super().init(start_enclave) self.PlainFunc = self.PlainFunc() def init_shape(self): self.InputShape = self.PrevLayer.get_output_shape() self.OutputShape = self.InputShape self.HandleShape = self.InputShape assert self.InputShape[1]%4 == 0 self.QuantizedInputShape = [self.InputShape[0], self.InputShape[1]//4, self.InputShape[2], self.InputShape[3]] def generate_tensor_name_list(self, force=False): if not force and self.tensor_name_list: return if self.sid == 2: self.tensor_name_list = {} return if len(self.InputShape) == 4: # self.Shapefortranspose = [int(round(((self.InputShape[0] * self.InputShape[1] * self.InputShape[2] * self.InputShape[3])/262144+1/2))), 262144, 1, 1] self.Shapefortranspose = [int(round(((self.InputShape[0] * self.InputShape[1] * self.InputShape[2] * self.InputShape[3])/602112+1/2))), 602112, 1, 1] else: self.Shapefortranspose = self.InputShape NeededTensorNames = [("output", self.OutputShape, None), ("handle", self.HandleShape, None), # ("DerInput", self.InputShape, None), ("input", self.InputShape, None), ("quant_input", self.QuantizedInputShape, None), ("quant_output", self.QuantizedInputShape, None), ("inputtrans", self.Shapefortranspose, None), ("outputtrans", self.Shapefortranspose, None), ] self.tensor_name_list = NeededTensorNames def forward(self):
with NamedTimerInstance(f"S{self.sid}: {self.LayerName} Forward", verbose_level=VerboseLevel.LAYER):
4
2023-11-01 10:37:37+00:00
4k
rafaelleinio/biar
biar/services.py
[ { "identifier": "ContentCallbackError", "path": "biar/errors.py", "snippet": "class ContentCallbackError(Exception):\n \"\"\"Base Exception for content callback errors.\"\"\"" }, { "identifier": "PollError", "path": "biar/errors.py", "snippet": "class PollError(Exception):\n \"\"\"Base Exception for poll errors.\"\"\"" }, { "identifier": "ResponseEvaluationError", "path": "biar/errors.py", "snippet": "class ResponseEvaluationError(Exception):\n \"\"\"Base Exception for non-OK responses.\"\"\"" }, { "identifier": "PollConfig", "path": "biar/model.py", "snippet": "class PollConfig(BaseModel):\n \"\"\"Poll configuration model.\n\n Args:\n timeout: timeout in seconds.\n interval: interval in seconds between polls.\n success_condition: callback to be called after each poll.\n The callback should return True if the polling should stop.\n\n \"\"\"\n\n timeout: float = 60 * 5\n interval: float = 5\n success_condition: Callable[[BaseModel], bool]" }, { "identifier": "RateLimiter", "path": "biar/model.py", "snippet": "class RateLimiter(BaseModel):\n \"\"\"Limit the number of requests in a given time frame.\n\n Attributes:\n rate: number of requests allowed in the given time frame.\n time_frame: number of seconds for the time frame.\n identity: identification for the rate-limiting bucket.\n Same identity can be used universally for all endpoints in a given host, if\n the API have a global limit. If the API have different limits for each\n endpoint, different identities can be used as well.\n\n \"\"\"\n\n model_config = ConfigDict(arbitrary_types_allowed=True)\n\n rate: int = 10\n time_frame: int = 1\n identity: str = \"default\"\n\n @computed_field # type: ignore[misc]\n @cached_property\n def limiter(self) -> Limiter:\n \"\"\"In memory bucket to limit the number of requests.\"\"\"\n return Limiter(\n InMemoryBucket(\n rates=[\n Rate(\n limit=self.rate,\n interval=self.time_frame * Duration.SECOND.value,\n )\n ]\n ),\n raise_when_fail=False,\n max_delay=Duration.MINUTE.value,\n )" }, { "identifier": "RequestConfig", "path": "biar/model.py", "snippet": "class RequestConfig(BaseModel):\n \"\"\"Base configuration for a request.\n\n Attributes:\n method: http method to be used.\n download_json_content: if true will await for json content download.\n download_text_content: if true will await for text content download.\n proxy_config: proxy configuration.\n rate_limiter: rate limiting configuration.\n retryer: retry logic configuration.\n timeout: maximum number of seconds for timeout.\n By default, is 300 seconds (5 minutes).\n use_random_user_agent: if true will use a random user agent.\n user_agent_list: list of user agents to be randomly selected.\n By default, it uses a sample from `biar.user_agents` module.\n bearer_token: bearer token to be used in the request.\n headers: headers dictionary to use in request.\n params: parameters dictionary to use in request.\n session: aiohttp session to be used in request.\n If the user wants to use a custom session and handle its lifecycle, it can\n be passed here.\n acceptable_codes: list of acceptable status codes.\n If the response status code is not in this list, an exception will be\n raised. By default, it only accepts 200.\n\n \"\"\"\n\n model_config = ConfigDict(arbitrary_types_allowed=True)\n\n method: Literal[\"GET\", \"POST\", \"PUT\", \"DELETE\"] = \"GET\"\n download_json_content: bool = True\n download_text_content: bool = True\n proxy_config: Optional[ProxyConfig] = None\n rate_limiter: RateLimiter = RateLimiter()\n retryer: Retryer = Retryer()\n timeout: int = 300\n use_random_user_agent: bool = True\n user_agent_list: Optional[List[str]] = None\n bearer_token: Optional[str] = None\n headers: Optional[Dict[str, str]] = None\n params: Optional[Dict[str, Any]] = None\n session: Optional[aiohttp.ClientSession] = None\n acceptable_codes: Optional[List[int]] = None" }, { "identifier": "Response", "path": "biar/model.py", "snippet": "class Response(BaseModel):\n \"\"\"Attributes from the http request response.\n\n Attributes:\n url: final url after (possible) redirects.\n status_code: HTTP status code.\n headers: headers in the response.\n json_content: response content as json dict.\n text_content: raw response content as a string.\n\n \"\"\"\n\n model_config = ConfigDict(arbitrary_types_allowed=True)\n\n url: URL\n status_code: int\n headers: Dict[str, Any] = Field(default_factory=dict)\n json_content: Dict[str, JsonValue] = Field(default_factory=dict)\n text_content: str = \"\"" }, { "identifier": "StructuredResponse", "path": "biar/model.py", "snippet": "class StructuredResponse(Response):\n \"\"\"Attributes from the http request response.\n\n Attributes:\n url: final url after (possible) redirects.\n status_code: HTTP status code.\n headers: headers in the response.\n json_content: response content as json dict.\n text_content: raw response content as a string.\n structured_content: response content as a pydantic model.\n\n \"\"\"\n\n structured_content: Any" }, { "identifier": "get_user_agent", "path": "biar/user_agents.py", "snippet": "def get_user_agent(user_agent_list: Optional[List[str]] = None) -> str:\n \"\"\"Get a random User-Agent.\n\n The list needs to be updated from time to time from this database:\n https://www.whatismybrowser.com/guides/the-latest-user-agent/\n\n Returns:\n User-Agent string.\n\n \"\"\"\n return random.choice(user_agent_list or USER_AGENTS)" } ]
import asyncio import datetime import ssl import aiodns import aiohttp import certifi import tenacity from typing import Any, Callable, Dict, List, Optional, Type, Union from loguru import logger from pydantic import BaseModel from yarl import URL from biar import ( ContentCallbackError, PollConfig, PollError, RateLimiter, RequestConfig, Response, ResponseEvaluationError, StructuredResponse, ) from biar.user_agents import get_user_agent
3,363
async def request_structured( model: Type[BaseModel], url: Union[str, URL], config: RequestConfig = RequestConfig(), payload: Optional[BaseModel] = None, ) -> StructuredResponse: """Make a request and structure the response. This function forces the download of the json content to be deserialized as a pydantic model. Args: model: pydantic model to be used to structure the response content. url: url to send request. config: request configuration. payload: payload to be sent in the request as a structured pydantic model. Returns: Structured response content deserialized as a pydantic model. """ new_config = config.model_copy(update=dict(download_json_content=True)) logger.debug(f"Request started, {new_config.method} method to {url}...") rc = new_config.retryer.retrying_config new_callable = _request_structured.retry_with(**rc) # type: ignore async with aiohttp.ClientSession() as new_session: structured_response: StructuredResponse = await new_callable( model=model, retry_based_on_content_callback=( new_config.retryer.retry_based_on_content_callback ), download_json_content=new_config.download_json_content, download_text_content=new_config.download_text_content, rate_limiter=new_config.rate_limiter, session=new_config.session or new_session, acceptable_codes=new_config.acceptable_codes, **_build_kwargs(url=url, config=new_config, payload=payload), ) logger.debug("Request finished!") return structured_response async def request_structured_many( model: Type[BaseModel], urls: List[Union[str, URL]], config: RequestConfig = RequestConfig(), payloads: Optional[List[BaseModel]] = None, ) -> List[StructuredResponse]: """Make many requests and structure the responses. Args: model: pydantic model to be used to structure the response. urls: list of urls to send requests. config: request configuration. payloads: list of payloads as structured pydantic models. Returns: List of structured response content deserialized as a pydantic model. """ payloads = _normalize_payloads(urls=urls, payloads=payloads) coroutines = ( [ request_structured( model=model, url=url, config=config, payload=payload, ) for url, payload in zip(urls, payloads) ] if payloads else [ request_structured( model=model, url=url, config=config, ) for url in urls ] ) results: List[StructuredResponse] = await asyncio.gather(*coroutines) return results async def poll( model: Type[BaseModel], poll_config: PollConfig, url: Union[str, URL], config: RequestConfig = RequestConfig(), ) -> StructuredResponse: """Poll a url until a condition is met. Args: url: url to be polled. config: request configuration. model: pydantic model to be used to structure the response. poll_config: poll configuration. Returns: Structured response. """ logger.debug(f"Polling {url}...") start_time = datetime.datetime.utcnow() elapsed_time = datetime.timedelta(seconds=0) while elapsed_time.total_seconds() < poll_config.timeout: response = await request_structured(model=model, url=url, config=config) if poll_config.success_condition(response.structured_content): logger.debug("Condition met, polling finished!") return response await asyncio.sleep(poll_config.interval) elapsed_time = datetime.datetime.utcnow() - start_time logger.debug(f"Condition not met yet. Elapsed time: {elapsed_time} seconds...")
async def is_host_reachable(host: str) -> bool: """Async check if a host is reachable. Args: host: url to check if is reachable. Returns: True if the host is reachable. """ dns_solver = aiodns.DNSResolver() try: _ = await dns_solver.query(host, qtype="A") return True except aiodns.error.DNSError: return False def get_ssl_context(extra_certificate: Optional[str] = None) -> ssl.SSLContext: """Create a ssl context. It uses the collection of certificates provided by certifi package. Besides, the user can give an additional certificate to be appended to the final collection. Args: extra_certificate: extra string certificate to be used alongside default ones. Returns: new ssl context. """ with open(certifi.where()) as f: certificate = f.read() if extra_certificate: certificate = certificate + "\n" + extra_certificate return ssl.create_default_context(cadata=certificate) async def _request_base( download_json_content: bool, download_text_content: bool, rate_limiter: RateLimiter, session: aiohttp.ClientSession, acceptable_codes: Optional[List[int]] = None, **request_kwargs: Any, ) -> Response: rate_limiter.limiter.try_acquire(name=rate_limiter.identity) async with session.request(**request_kwargs) as response: text_content = await response.text() if download_text_content else "" if response.status not in (acceptable_codes or [200]): formated_text_content = text_content.replace("{", "{{").replace("}", "}}") raise ResponseEvaluationError( f"Error: status={response.status}, " f"Text content (if loaded): {formated_text_content}" ) json_content = ( await response.json(content_type=None) if download_json_content else None ) normalized_json_content = ( json_content if isinstance(json_content, dict) else {"content": json_content} ) http_response = Response( url=response.url, status_code=response.status, headers={k: v for k, v in response.headers.items()}, json_content=normalized_json_content, text_content=text_content, ) return http_response @tenacity.retry async def _request( download_json_content: bool, download_text_content: bool, rate_limiter: RateLimiter, session: aiohttp.ClientSession, acceptable_codes: Optional[List[int]] = None, **request_kwargs: Any, ) -> Response: return await _request_base( download_json_content=download_json_content, download_text_content=download_text_content, rate_limiter=rate_limiter, session=session, acceptable_codes=acceptable_codes, **request_kwargs, ) def _build_kwargs( url: Union[str, URL], config: RequestConfig, payload: Optional[BaseModel] = None, ) -> Dict[str, Any]: headers = { **(config.headers or {}), **( {"User-Agent": get_user_agent(user_agent_list=config.user_agent_list)} if config.use_random_user_agent else {} ), **( {"Authorization": f"Bearer {config.bearer_token}"} if config.bearer_token else {} ), } proxy_kwargs = ( { "proxy": config.proxy_config.host, "proxy_headers": config.proxy_config.headers, "ssl_context": get_ssl_context( extra_certificate=config.proxy_config.ssl_cadata ), } if config.proxy_config else {} ) return { "url": url, "method": config.method, "headers": headers, "params": config.params or None, "timeout": config.timeout, "json": payload.model_dump(mode="json") if payload else None, **proxy_kwargs, } async def request( url: Union[str, URL], config: RequestConfig = RequestConfig(), payload: Optional[BaseModel] = None, ) -> Response: """Make a request. Args: url: url to send request. config: request configuration. payload: payload to be sent in the request as a structured pydantic model. Returns: Response object from the request. """ logger.debug(f"Request started, {config.method} method to {url}...") new_callable = _request.retry_with(**config.retryer.retrying_config) # type: ignore async with aiohttp.ClientSession() as new_session: response: Response = await new_callable( download_json_content=config.download_json_content, download_text_content=config.download_text_content, rate_limiter=config.rate_limiter, session=config.session or new_session, acceptable_codes=config.acceptable_codes, **_build_kwargs(url=url, config=config, payload=payload), ) logger.debug("Request finished!") return response def _normalize_payloads( urls: List[Union[str, URL]], payloads: Optional[List[BaseModel]] = None, ) -> Optional[List[BaseModel]]: payloads = payloads or [] if payloads and len(urls) != len(payloads): raise ValueError( f"Number of urls ({len(urls)}) and payloads ({len(payloads or [])}) " f"must be the same." ) return payloads async def request_many( urls: List[Union[str, URL]], config: RequestConfig = RequestConfig(), payloads: Optional[List[BaseModel]] = None, ) -> List[Response]: """Make many requests. Args: urls: list of urls to send requests. config: request configuration. payloads: list of payloads as structured pydantic models. Returns: List of response objects from the requests. """ payloads = _normalize_payloads(urls=urls, payloads=payloads) coroutines = ( [ request( url=url, config=config, payload=payload, ) for url, payload in zip(urls, payloads) ] if payloads else [ request( url=url, config=config, ) for url in urls ] ) results: List[Response] = await asyncio.gather(*coroutines) return results @tenacity.retry async def _request_structured( model: Type[BaseModel], retry_based_on_content_callback: Optional[Callable[[StructuredResponse], bool]], download_json_content: bool, download_text_content: bool, rate_limiter: RateLimiter, session: aiohttp.ClientSession, acceptable_codes: Optional[List[int]] = None, **request_kwargs: Any, ) -> StructuredResponse: response = await _request_base( download_json_content=download_json_content, download_text_content=download_text_content, rate_limiter=rate_limiter, session=session, acceptable_codes=acceptable_codes, **request_kwargs, ) structured_response = StructuredResponse( url=response.url, status_code=response.status_code, headers=response.headers, json_content=response.json_content, text_content=response.text_content, structured_content=model(**response.json_content), ) if retry_based_on_content_callback and retry_based_on_content_callback( structured_response.structured_content ): raise ContentCallbackError("Structured content retry callback returned True") return structured_response async def request_structured( model: Type[BaseModel], url: Union[str, URL], config: RequestConfig = RequestConfig(), payload: Optional[BaseModel] = None, ) -> StructuredResponse: """Make a request and structure the response. This function forces the download of the json content to be deserialized as a pydantic model. Args: model: pydantic model to be used to structure the response content. url: url to send request. config: request configuration. payload: payload to be sent in the request as a structured pydantic model. Returns: Structured response content deserialized as a pydantic model. """ new_config = config.model_copy(update=dict(download_json_content=True)) logger.debug(f"Request started, {new_config.method} method to {url}...") rc = new_config.retryer.retrying_config new_callable = _request_structured.retry_with(**rc) # type: ignore async with aiohttp.ClientSession() as new_session: structured_response: StructuredResponse = await new_callable( model=model, retry_based_on_content_callback=( new_config.retryer.retry_based_on_content_callback ), download_json_content=new_config.download_json_content, download_text_content=new_config.download_text_content, rate_limiter=new_config.rate_limiter, session=new_config.session or new_session, acceptable_codes=new_config.acceptable_codes, **_build_kwargs(url=url, config=new_config, payload=payload), ) logger.debug("Request finished!") return structured_response async def request_structured_many( model: Type[BaseModel], urls: List[Union[str, URL]], config: RequestConfig = RequestConfig(), payloads: Optional[List[BaseModel]] = None, ) -> List[StructuredResponse]: """Make many requests and structure the responses. Args: model: pydantic model to be used to structure the response. urls: list of urls to send requests. config: request configuration. payloads: list of payloads as structured pydantic models. Returns: List of structured response content deserialized as a pydantic model. """ payloads = _normalize_payloads(urls=urls, payloads=payloads) coroutines = ( [ request_structured( model=model, url=url, config=config, payload=payload, ) for url, payload in zip(urls, payloads) ] if payloads else [ request_structured( model=model, url=url, config=config, ) for url in urls ] ) results: List[StructuredResponse] = await asyncio.gather(*coroutines) return results async def poll( model: Type[BaseModel], poll_config: PollConfig, url: Union[str, URL], config: RequestConfig = RequestConfig(), ) -> StructuredResponse: """Poll a url until a condition is met. Args: url: url to be polled. config: request configuration. model: pydantic model to be used to structure the response. poll_config: poll configuration. Returns: Structured response. """ logger.debug(f"Polling {url}...") start_time = datetime.datetime.utcnow() elapsed_time = datetime.timedelta(seconds=0) while elapsed_time.total_seconds() < poll_config.timeout: response = await request_structured(model=model, url=url, config=config) if poll_config.success_condition(response.structured_content): logger.debug("Condition met, polling finished!") return response await asyncio.sleep(poll_config.interval) elapsed_time = datetime.datetime.utcnow() - start_time logger.debug(f"Condition not met yet. Elapsed time: {elapsed_time} seconds...")
raise PollError("Timeout reached")
1
2023-11-03 00:03:59+00:00
4k
NVlabs/M2T2
demo_rlbench.py
[ { "identifier": "collate", "path": "m2t2/dataset.py", "snippet": "def collate(batch):\n batch = [data for data in batch if not data.get('invalid', False)]\n batch = {key: [data[key] for data in batch] for key in batch[0]}\n if 'task' in batch:\n task = batch.pop('task')\n batch['task_is_pick'] = torch.stack([\n torch.tensor(t == 'pick') for t in task\n ])\n batch['task_is_place'] = torch.stack([\n torch.tensor(t == 'place') for t in task\n ])\n for key in batch:\n if key in [\n 'inputs', 'points', 'seg', 'object_inputs', 'bottom_center',\n 'cam_pose', 'ee_pose', 'placement_masks', 'placement_region',\n 'lang_tokens'\n ]:\n batch[key] = torch.stack(batch[key])\n if key in [\n 'contact_dirs', 'approach_dirs', 'offsets'\n ]:\n batch[key] = torch.cat(batch[key])\n return batch" }, { "identifier": "normalize_rgb", "path": "m2t2/dataset_utils.py", "snippet": "class NormalizeInverse(transforms.Normalize):\n def __init__(self, mean, std):\n def __call__(self, tensor):\ndef depth_to_xyz(depth, intrinsics):\ndef jitter_gaussian(xyz, std, clip):\ndef sample_points(xyz, num_points):\n Z = depth\n X = (u - cx) * (Z / fx)\n Y = (v - cy) * (Z / fy)" }, { "identifier": "create_visualizer", "path": "m2t2/meshcat_utils.py", "snippet": "def create_visualizer(clear=True):\n print(\n \"Waiting for meshcat server... have you started a server? Run `meshcat-server` to start a server\"\n )\n vis = meshcat.Visualizer(zmq_url=\"tcp://127.0.0.1:6000\")\n if clear:\n vis.delete()\n return vis" }, { "identifier": "visualize_grasp", "path": "m2t2/meshcat_utils.py", "snippet": "def visualize_grasp(vis, name, transform, color=[255, 0, 0], **kwargs):\n grasp_vertices = load_grasp_points()\n vis[name].set_object(\n g.Line(\n g.PointsGeometry(grasp_vertices),\n g.MeshBasicMaterial(color=rgb2hex(tuple(color)), **kwargs),\n )\n )\n vis[name].set_transform(transform.astype(np.float64))" }, { "identifier": "visualize_pointcloud", "path": "m2t2/meshcat_utils.py", "snippet": "def visualize_pointcloud(vis, name, pc, color=None, transform=None, **kwargs):\n \"\"\"\n Args:\n vis: meshcat visualizer object\n name: str\n pc: Nx3 or HxWx3\n color: (optional) same shape as pc[0 - 255] scale or just rgb tuple\n transform: (optional) 4x4 homogeneous transform\n \"\"\"\n if pc.ndim == 3:\n pc = pc.reshape(-1, pc.shape[-1])\n\n if color is not None:\n if isinstance(color, list):\n color = np.array(color)\n color = np.array(color)\n # Resize the color np array if needed.\n if color.ndim == 3:\n color = color.reshape(-1, color.shape[-1])\n if color.ndim == 1:\n color = np.ones_like(pc) * np.array(color)\n\n # Divide it by 255 to make sure the range is between 0 and 1,\n color = color.astype(np.float32) / 255\n else:\n color = np.ones_like(pc)\n\n vis[name].set_object(\n meshcat.geometry.PointCloud(position=pc.T, color=color.T, **kwargs)\n )\n\n if transform is not None:\n vis[name].set_transform(transform)" }, { "identifier": "M2T2", "path": "m2t2/m2t2.py", "snippet": "class M2T2(nn.Module):\n def __init__(\n self,\n backbone: nn.Module,\n transformer: nn.Module,\n object_encoder: nn.Module = None,\n grasp_mlp: nn.Module = None,\n set_criterion: nn.Module = None,\n grasp_criterion: nn.Module = None,\n place_criterion: nn.Module = None\n ):\n super(M2T2, self).__init__()\n self.backbone = backbone\n self.object_encoder = object_encoder\n self.transformer = transformer\n self.grasp_mlp = grasp_mlp\n self.set_criterion = set_criterion\n self.grasp_criterion = grasp_criterion\n self.place_criterion = place_criterion\n\n @classmethod\n def from_config(cls, cfg):\n args = {}\n args['backbone'] = PointNet2MSG.from_config(cfg.scene_encoder)\n channels = args['backbone'].out_channels\n obj_channels = None\n if cfg.contact_decoder.num_place_queries > 0:\n args['object_encoder'] = PointNet2MSGCls.from_config(\n cfg.object_encoder\n )\n obj_channels = args['object_encoder'].out_channels\n args['place_criterion'] = PlaceCriterion.from_config(\n cfg.place_loss\n )\n args['transformer'] = ContactDecoder.from_config(\n cfg.contact_decoder, channels, obj_channels\n )\n if cfg.contact_decoder.num_grasp_queries > 0:\n args['grasp_mlp'] = ActionDecoder.from_config(\n cfg.action_decoder, args['transformer']\n )\n matcher = HungarianMatcher.from_config(cfg.matcher)\n args['set_criterion'] = SetCriterion.from_config(\n cfg.grasp_loss, matcher\n )\n args['grasp_criterion'] = GraspCriterion.from_config(\n cfg.grasp_loss\n )\n return cls(**args)\n\n def forward(self, data, cfg):\n scene_feat = self.backbone(data['inputs'])\n object_inputs = data['object_inputs']\n object_feat = {}\n if self.object_encoder is not None:\n object_feat = self.object_encoder(object_inputs)\n if 'task_is_place' in data:\n for key, val in object_feat['features'].items():\n object_feat['features'][key] = (\n val * data['task_is_place'].view(\n data['task_is_place'].shape[0], 1, 1\n )\n )\n lang_tokens = data.get('lang_tokens')\n embedding, outputs = self.transformer(\n scene_feat, object_feat, lang_tokens\n )\n\n losses = {}\n if self.place_criterion is not None:\n losses, stats = self.place_criterion(outputs, data)\n outputs[-1].update(stats)\n\n if self.set_criterion is not None:\n set_losses, outputs = self.set_criterion(outputs, data)\n losses.update(set_losses)\n else:\n outputs = outputs[-1]\n\n if self.grasp_mlp is not None:\n mask_features = scene_feat['features'][\n self.transformer.mask_feature\n ]\n obj_embedding = [emb[idx] for emb, idx in zip(\n embedding['grasp'], outputs['matched_idx']\n )]\n confidence = [\n mask.sigmoid() for mask in outputs['matched_grasping_masks']\n ]\n grasp_outputs = self.grasp_mlp(\n data['points'], mask_features, confidence,\n cfg.mask_thresh, obj_embedding, data['grasping_masks']\n )\n outputs.update(grasp_outputs)\n contact_losses = self.grasp_criterion(outputs, data)\n losses.update(contact_losses)\n\n return outputs, losses\n\n def infer(self, data, cfg):\n scene_feat = self.backbone(data['inputs'])\n object_feat = self.object_encoder(data['object_inputs'])\n if 'task_is_place' in data:\n for key in object_feat['features']:\n object_feat['features'][key] = (\n object_feat['features'][key] * data['task_is_place'].view(\n data['task_is_place'].shape[0], 1, 1\n )\n )\n lang_tokens = data.get('lang_tokens')\n embedding, outputs = self.transformer(\n scene_feat, object_feat, lang_tokens\n )\n outputs = outputs[-1]\n\n if 'place' in embedding and embedding['place'].shape[1] > 0:\n cam_pose = None if cfg.world_coord else data['cam_pose']\n placement_outputs = infer_placements(\n data['points'], outputs['placement_masks'],\n data['bottom_center'], data['ee_pose'],\n cam_pose, cfg.mask_thresh, cfg.placement_height\n )\n outputs.update(placement_outputs)\n outputs['placement_masks'] = (\n outputs['placement_masks'].sigmoid() > cfg.mask_thresh\n )\n\n if 'grasp' in embedding and embedding['grasp'].shape[1] > 0:\n masks = outputs['grasping_masks'].sigmoid() > cfg.mask_thresh\n mask_features = scene_feat['features'][\n self.transformer.mask_feature\n ]\n if 'objectness' in outputs:\n objectness = outputs['objectness'].sigmoid()\n object_ids = [\n torch.where(\n (score > cfg.object_thresh) & mask.sum(dim=1) > 0\n )[0]\n for score, mask in zip(objectness, masks)\n ]\n outputs['objectness'] = [\n score[idx] for score, idx in zip(objectness, object_ids)\n ]\n confidence = [\n logits.sigmoid()[idx]\n for logits, idx in zip(outputs['grasping_masks'], object_ids)\n ]\n outputs['grasping_masks'] = [\n mask[idx] for mask, idx in zip(masks, object_ids)\n ]\n obj_embedding = [emb[idx] for emb, idx in zip(\n embedding['grasp'], object_ids\n )]\n else:\n obj_embedding = embedding['grasp']\n confidence = [\n logits.sigmoid() for logits in outputs['grasping_masks']\n ]\n grasp_outputs = self.grasp_mlp(\n data['points'], mask_features, confidence,\n cfg.mask_thresh, obj_embedding\n )\n outputs.update(grasp_outputs)\n\n return outputs" }, { "identifier": "load_image", "path": "m2t2/rlbench_utils.py", "snippet": "def load_image(episode_dir, camera, meta_data, frame_id):\n rgb = np.array(\n Image.open(f\"{episode_dir}/{camera}_rgb/{frame_id}.png\")\n )\n seg = np.array(\n Image.open(f\"{episode_dir}/{camera}_mask/{frame_id}.png\")\n )[..., 0]\n depth = np.array(\n Image.open(f\"{episode_dir}/{camera}_depth/{frame_id}.png\")\n )\n depth = np.sum(depth * [65536, 256, 1], axis=2)\n near = meta_data[f'{camera}_camera_near']\n far = meta_data[f'{camera}_camera_far']\n depth = near + depth / (2**24 - 1) * (far - near)\n pcd = depth_to_xyz(depth, meta_data[f'{camera}_camera_intrinsics'])\n cam_pose = meta_data[f'{camera}_camera_extrinsics'][frame_id]\n pcd = pcd @ cam_pose[:3, :3].T + cam_pose[:3, 3]\n return rgb, pcd, seg" }, { "identifier": "within_bound", "path": "m2t2/rlbench_utils.py", "snippet": "def within_bound(demo, cameras, bounds):\n pcds, rgbs, masks = [], [], []\n for camera in cameras:\n pcd = demo[f'{camera}_point_cloud']\n rgb = demo[f'{camera}_rgb']\n pcds.append(pcd.reshape(-1, 3))\n rgbs.append(rgb.reshape(-1, 3))\n masks.append(demo[f'{camera}_mask'].reshape(-1))\n pcd = np.concatenate(pcds)\n rgb = np.concatenate(rgbs)\n mask = np.concatenate(masks)\n within = (pcd[:, 0] > bounds[0]) & (pcd[:, 0] < bounds[3]) \\\n & (pcd[:, 1] > bounds[1]) & (pcd[:, 1] < bounds[4]) \\\n & (pcd[:, 2] > bounds[2]) & (pcd[:, 2] < bounds[5])\n return pcd[within], rgb[within], mask[within]" }, { "identifier": "gripper_pose_from_rlbench", "path": "m2t2/rlbench_utils.py", "snippet": "def gripper_pose_from_rlbench(pose, gripper_depth=0.1034):\n pose = pose @ tra.euler_matrix(0, 0, np.pi / 2)\n pose[:3, 3] -= gripper_depth * pose[:3, 2]\n return pose" }, { "identifier": "to_cpu", "path": "m2t2/train_utils.py", "snippet": "def to_cpu(dic):\n for key in dic:\n if isinstance(dic[key], torch.Tensor):\n dic[key] = dic[key].detach().cpu()\n elif isinstance(dic[key], list):\n if isinstance(dic[key][0], torch.Tensor):\n for i in range(len(dic[key])):\n dic[key][i] = dic[key][i].detach().cpu()\n elif isinstance(dic[key][0], list):\n for i in range(len(dic[key])):\n for j in range(len(dic[key][i])):\n if isinstance(dic[key][i][j], torch.Tensor):\n dic[key][i][j] = dic[key][i][j].detach().cpu()" }, { "identifier": "to_gpu", "path": "m2t2/train_utils.py", "snippet": "def to_gpu(dic):\n for key in dic:\n if isinstance(dic[key], torch.Tensor):\n dic[key] = dic[key].cuda()\n elif isinstance(dic[key], list):\n if isinstance(dic[key][0], torch.Tensor):\n for i in range(len(dic[key])):\n dic[key][i] = dic[key][i].cuda()\n elif isinstance(dic[key][0], list):\n for i in range(len(dic[key])):\n for j in range(len(dic[key][i])):\n if isinstance(dic[key][i][j], torch.Tensor):\n dic[key][i][j] = dic[key][i][j].detach().cuda()" } ]
import hydra import pickle import torch from m2t2.dataset import collate from m2t2.dataset_utils import normalize_rgb, sample_points from m2t2.meshcat_utils import ( create_visualizer, visualize_grasp, visualize_pointcloud ) from m2t2.m2t2 import M2T2 from m2t2.rlbench_utils import ( load_image, within_bound, gripper_pose_from_rlbench ) from m2t2.train_utils import to_cpu, to_gpu
3,566
# Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved. # # NVIDIA CORPORATION and its licensors retain all intellectual property # and proprietary rights in and to this software, related documentation # and any modifications thereto. Any use, reproduction, disclosure or # distribution of this software and related documentation without an express # license agreement from NVIDIA CORPORATION is strictly prohibited. # # Author: Wentao Yuan ''' Demo script showing prediction for language-conditioned tasks. ''' def load_data(episode_dir, cfg): with open(f"{episode_dir}/meta_data.pkl", 'rb') as f: meta_data = pickle.load(f) data = {} for camera in cfg.rlbench.cameras:
# Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved. # # NVIDIA CORPORATION and its licensors retain all intellectual property # and proprietary rights in and to this software, related documentation # and any modifications thereto. Any use, reproduction, disclosure or # distribution of this software and related documentation without an express # license agreement from NVIDIA CORPORATION is strictly prohibited. # # Author: Wentao Yuan ''' Demo script showing prediction for language-conditioned tasks. ''' def load_data(episode_dir, cfg): with open(f"{episode_dir}/meta_data.pkl", 'rb') as f: meta_data = pickle.load(f) data = {} for camera in cfg.rlbench.cameras:
rgb, xyz, mask = load_image(
6
2023-11-03 22:32:05+00:00
4k
Codra-Ingenierie-Informatique/DataLab
cdl/widgets/signalpeakdialog.py
[ { "identifier": "peak_indexes", "path": "cdl/algorithms/signal.py", "snippet": "def peak_indexes(\n y, thres: float = 0.3, min_dist: int = 1, thres_abs: bool = False\n) -> np.ndarray:\n # Copyright (c) 2014 Lucas Hermann Negri\n # Unmodified code snippet from PeakUtils 1.3.0\n \"\"\"Peak detection routine.\n\n Finds the numeric index of the peaks in *y* by taking its first order\n difference. By using *thres* and *min_dist* parameters, it is possible\n to reduce the number of detected peaks. *y* must be signed.\n\n Parameters\n ----------\n y : ndarray (signed)\n 1D amplitude data to search for peaks.\n thres : float between [0., 1.]\n Normalized threshold. Only the peaks with amplitude higher than the\n threshold will be detected.\n min_dist : int\n Minimum distance between each detected peak. The peak with the highest\n amplitude is preferred to satisfy this constraint.\n thres_abs: boolean\n If True, the thres value will be interpreted as an absolute value,\n instead of a normalized threshold.\n\n Returns\n -------\n ndarray\n Array containing the numeric indexes of the peaks that were detected\n \"\"\"\n if isinstance(y, np.ndarray) and np.issubdtype(y.dtype, np.unsignedinteger):\n raise ValueError(\"y must be signed\")\n\n if not thres_abs:\n thres = thres * (np.max(y) - np.min(y)) + np.min(y)\n\n # compute first order difference\n dy = np.diff(y)\n\n # propagate left and right values successively to fill all plateau pixels\n # (0-value)\n (zeros,) = np.where(dy == 0)\n\n # check if the signal is totally flat\n if len(zeros) == len(y) - 1:\n return np.array([])\n\n if len(zeros):\n # compute first order difference of zero indexes\n zeros_diff = np.diff(zeros)\n # check when zeros are not chained together\n (zeros_diff_not_one,) = np.add(np.where(zeros_diff != 1), 1)\n # make an array of the chained zero indexes\n zero_plateaus = np.split(zeros, zeros_diff_not_one)\n\n # fix if leftmost value in dy is zero\n if zero_plateaus[0][0] == 0:\n dy[zero_plateaus[0]] = dy[zero_plateaus[0][-1] + 1]\n zero_plateaus.pop(0)\n\n # fix if rightmost value of dy is zero\n if len(zero_plateaus) > 0 and zero_plateaus[-1][-1] == len(dy) - 1:\n dy[zero_plateaus[-1]] = dy[zero_plateaus[-1][0] - 1]\n zero_plateaus.pop(-1)\n\n # for each chain of zero indexes\n for plateau in zero_plateaus:\n median = np.median(plateau)\n # set leftmost values to leftmost non zero values\n dy[plateau[plateau < median]] = dy[plateau[0] - 1]\n # set rightmost and middle values to rightmost non zero values\n dy[plateau[plateau >= median]] = dy[plateau[-1] + 1]\n\n # find the peaks by using the first order difference\n peaks = np.where(\n (np.hstack([dy, 0.0]) < 0.0)\n & (np.hstack([0.0, dy]) > 0.0)\n & (np.greater(y, thres))\n )[0]\n\n # handle multiple peaks, respecting the minimum distance\n if peaks.size > 1 and min_dist > 1:\n highest = peaks[np.argsort(y[peaks])][::-1]\n rem = np.ones(y.size, dtype=bool)\n rem[peaks] = False\n\n for peak in highest:\n if not rem[peak]:\n sl = slice(max(0, peak - min_dist), peak + min_dist + 1)\n rem[sl] = True\n rem[peak] = False\n\n peaks = np.arange(y.size)[~rem]\n\n return peaks" }, { "identifier": "_", "path": "cdl/config.py", "snippet": "CONF_VERSION = \"1.0.0\"\nAPP_NAME = \"DataLab\"\nMOD_NAME = \"cdl\"\nAPP_DESC = _(\"\"\"DataLab is a generic signal and image processing platform\"\"\")\nAPP_PATH = osp.dirname(__file__)\nDEBUG = os.environ.get(\"DEBUG\", \"\").lower() in (\"1\", \"true\")\nTEST_SEGFAULT_ERROR = len(os.environ.get(\"TEST_SEGFAULT_ERROR\", \"\")) > 0\nDATETIME_FORMAT = \"%d/%m/%Y - %H:%M:%S\"\nDATAPATH = configtools.get_module_data_path(MOD_NAME, \"data\")\nSHOTPATH = osp.join(\n configtools.get_module_data_path(MOD_NAME), os.pardir, \"doc\", \"images\", \"shots\"\n)\nOTHER_PLUGINS_PATHLIST = [configtools.get_module_data_path(MOD_NAME, \"plugins\")]\nIS_FROZEN = is_frozen(MOD_NAME)\nPLOTPY_DEFAULTS = {\n \"plot\": {\n # \"antialiasing\": False,\n # \"title/font/size\": 12,\n # \"title/font/bold\": False,\n # \"marker/curve/text/font/size\": 8,\n # \"marker/curve/text/font/family\": \"default\",\n # \"marker/curve/text/font/bold\": False,\n # \"marker/curve/text/font/italic\": False,\n \"marker/curve/text/textcolor\": \"black\",\n # \"marker/curve/text/background_color\": \"#ffffff\",\n # \"marker/curve/text/background_alpha\": 0.8,\n # \"marker/cross/text/font/family\": \"default\",\n # \"marker/cross/text/font/size\": 8,\n # \"marker/cross/text/font/bold\": False,\n # \"marker/cross/text/font/italic\": False,\n \"marker/cross/text/textcolor\": \"black\",\n # \"marker/cross/text/background_color\": \"#ffffff\",\n \"marker/cross/text/background_alpha\": 0.7,\n # \"marker/cross/line/style\": \"DashLine\",\n # \"marker/cross/line/color\": \"yellow\",\n # \"marker/cross/line/width\": 1,\n # \"marker/cursor/text/font/size\": 8,\n # \"marker/cursor/text/font/family\": \"default\",\n # \"marker/cursor/text/font/bold\": False,\n # \"marker/cursor/text/font/italic\": False,\n # \"marker/cursor/text/textcolor\": \"#ff9393\",\n # \"marker/cursor/text/background_color\": \"#ffffff\",\n # \"marker/cursor/text/background_alpha\": 0.8,\n \"shape/drag/symbol/marker\": \"NoSymbol\",\n \"shape/mask/symbol/size\": 5,\n \"shape/mask/sel_symbol/size\": 8,\n # -----------------------------------------------------------------------------\n # Annotated shape style for annotations:\n \"shape/annotation/line/style\": \"SolidLine\",\n \"shape/annotation/line/color\": \"#ffff00\",\n \"shape/annotation/line/width\": 1,\n \"shape/annotation/fill/style\": \"SolidPattern\",\n \"shape/annotation/fill/color\": MAIN_BG_COLOR,\n \"shape/annotation/fill/alpha\": 0.1,\n \"shape/annotation/symbol/marker\": \"Rect\",\n \"shape/annotation/symbol/size\": 3,\n \"shape/annotation/symbol/edgecolor\": \"#ffff00\",\n \"shape/annotation/symbol/facecolor\": \"#ffff00\",\n \"shape/annotation/symbol/alpha\": 1.0,\n \"shape/annotation/sel_line/style\": \"SolidLine\",\n \"shape/annotation/sel_line/color\": \"#00ff00\",\n \"shape/annotation/sel_line/width\": 1,\n \"shape/annotation/sel_fill/style\": \"SolidPattern\",\n \"shape/annotation/sel_fill/color\": MAIN_BG_COLOR,\n \"shape/annotation/sel_fill/alpha\": 0.1,\n \"shape/annotation/sel_symbol/marker\": \"Rect\",\n \"shape/annotation/sel_symbol/size\": 9,\n \"shape/annotation/sel_symbol/edgecolor\": \"#00aa00\",\n \"shape/annotation/sel_symbol/facecolor\": \"#00ff00\",\n \"shape/annotation/sel_symbol/alpha\": 0.7,\n # -----------------------------------------------------------------------------\n # Annotated shape style for result shapes / signals:\n \"shape/result/s/line/style\": \"SolidLine\",\n \"shape/result/s/line/color\": MAIN_FG_COLOR,\n \"shape/result/s/line/width\": 1,\n \"shape/result/s/fill/style\": \"SolidPattern\",\n \"shape/result/s/fill/color\": MAIN_BG_COLOR,\n \"shape/result/s/fill/alpha\": 0.1,\n \"shape/result/s/symbol/marker\": \"XCross\",\n \"shape/result/s/symbol/size\": 7,\n \"shape/result/s/symbol/edgecolor\": MAIN_FG_COLOR,\n \"shape/result/s/symbol/facecolor\": MAIN_FG_COLOR,\n \"shape/result/s/symbol/alpha\": 1.0,\n \"shape/result/s/sel_line/style\": \"SolidLine\",\n \"shape/result/s/sel_line/color\": \"#00ff00\",\n \"shape/result/s/sel_line/width\": 1,\n \"shape/result/s/sel_fill/style\": \"SolidPattern\",\n \"shape/result/s/sel_fill/color\": MAIN_BG_COLOR,\n \"shape/result/s/sel_fill/alpha\": 0.1,\n \"shape/result/s/sel_symbol/marker\": \"Rect\",\n \"shape/result/s/sel_symbol/size\": 9,\n \"shape/result/s/sel_symbol/edgecolor\": \"#00aa00\",\n \"shape/result/s/sel_symbol/facecolor\": \"#00ff00\",\n \"shape/result/s/sel_symbol/alpha\": 0.7,\n # -----------------------------------------------------------------------------\n # Annotated shape style for result shapes / images:\n \"shape/result/i/line/style\": \"SolidLine\",\n \"shape/result/i/line/color\": \"#ffff00\",\n \"shape/result/i/line/width\": 1,\n \"shape/result/i/fill/style\": \"SolidPattern\",\n \"shape/result/i/fill/color\": MAIN_BG_COLOR,\n \"shape/result/i/fill/alpha\": 0.1,\n \"shape/result/i/symbol/marker\": \"Rect\",\n \"shape/result/i/symbol/size\": 3,\n \"shape/result/i/symbol/edgecolor\": \"#ffff00\",\n \"shape/result/i/symbol/facecolor\": \"#ffff00\",\n \"shape/result/i/symbol/alpha\": 1.0,\n \"shape/result/i/sel_line/style\": \"SolidLine\",\n \"shape/result/i/sel_line/color\": \"#00ff00\",\n \"shape/result/i/sel_line/width\": 1,\n \"shape/result/i/sel_fill/style\": \"SolidPattern\",\n \"shape/result/i/sel_fill/color\": MAIN_BG_COLOR,\n \"shape/result/i/sel_fill/alpha\": 0.1,\n \"shape/result/i/sel_symbol/marker\": \"Rect\",\n \"shape/result/i/sel_symbol/size\": 9,\n \"shape/result/i/sel_symbol/edgecolor\": \"#00aa00\",\n \"shape/result/i/sel_symbol/facecolor\": \"#00ff00\",\n \"shape/result/i/sel_symbol/alpha\": 0.7,\n # -----------------------------------------------------------------------------\n },\n}\ndef is_frozen(module_name: str) -> bool:\ndef get_mod_source_dir() -> str | None:\n def get_def_dict(cls, category: str) -> dict:\n def set_def_dict(cls, category: str, def_dict: dict) -> None:\ndef get_old_log_fname(fname):\ndef initialize():\ndef reset():\nclass MainSection(conf.Section, metaclass=conf.SectionMeta):\nclass ConsoleSection(conf.Section, metaclass=conf.SectionMeta):\nclass IOSection(conf.Section, metaclass=conf.SectionMeta):\nclass ProcSection(conf.Section, metaclass=conf.SectionMeta):\nclass ViewSection(conf.Section, metaclass=conf.SectionMeta):\nclass Conf(conf.Configuration, metaclass=conf.ConfMeta):" } ]
import numpy as np from guidata.configtools import get_icon from plotpy.builder import make from plotpy.plot import PlotDialog from qtpy import QtCore as QC from qtpy import QtWidgets as QW from cdl.algorithms.signal import peak_indexes from cdl.config import _
3,295
# -*- coding: utf-8 -*- # # Licensed under the terms of the BSD 3-Clause # (see cdl/LICENSE for details) """Signal peak detection feature""" # pylint: disable=invalid-name # Allows short reference names like x, y, ... class DistanceSlider(QW.QWidget): """Minimum distance slider""" TITLE = _("Minimum distance:") SIG_VALUE_CHANGED = QC.Signal(int) def __init__(self, parent): super().__init__(parent) self.slider = QW.QSlider(QC.Qt.Horizontal) self.label = QW.QLabel() layout = QW.QHBoxLayout() layout.addWidget(self.label) layout.addWidget(self.slider) self.setLayout(layout) def value_changed(self, value): """Slider value has changed""" plural = "s" if value > 1 else "" self.label.setText(f"{self.TITLE} {value} point{plural}") self.SIG_VALUE_CHANGED.emit(value) def setup_slider(self, value, maxval): """Setup slider""" self.slider.setMinimum(1) self.slider.setMaximum(maxval) self.slider.setValue(value) self.slider.setTickPosition(QW.QSlider.TicksBothSides) self.value_changed(value) self.slider.valueChanged.connect(self.value_changed) class SignalPeakDetectionDialog(PlotDialog): """Signal Peak detection dialog""" def __init__(self, parent=None): self.peaks = None
# -*- coding: utf-8 -*- # # Licensed under the terms of the BSD 3-Clause # (see cdl/LICENSE for details) """Signal peak detection feature""" # pylint: disable=invalid-name # Allows short reference names like x, y, ... class DistanceSlider(QW.QWidget): """Minimum distance slider""" TITLE = _("Minimum distance:") SIG_VALUE_CHANGED = QC.Signal(int) def __init__(self, parent): super().__init__(parent) self.slider = QW.QSlider(QC.Qt.Horizontal) self.label = QW.QLabel() layout = QW.QHBoxLayout() layout.addWidget(self.label) layout.addWidget(self.slider) self.setLayout(layout) def value_changed(self, value): """Slider value has changed""" plural = "s" if value > 1 else "" self.label.setText(f"{self.TITLE} {value} point{plural}") self.SIG_VALUE_CHANGED.emit(value) def setup_slider(self, value, maxval): """Setup slider""" self.slider.setMinimum(1) self.slider.setMaximum(maxval) self.slider.setValue(value) self.slider.setTickPosition(QW.QSlider.TicksBothSides) self.value_changed(value) self.slider.valueChanged.connect(self.value_changed) class SignalPeakDetectionDialog(PlotDialog): """Signal Peak detection dialog""" def __init__(self, parent=None): self.peaks = None
self.peak_indexes = None
0
2023-11-09 16:56:03+00:00
4k
sxwyh/pytradecn
src/pytradecn/control/wrappersa.py
[ { "identifier": "BaseUIAWrapper", "path": "src/pytradecn/control/baseuiawrapper.py", "snippet": "class BaseUIAWrapper(UIAWrapper):\n\n _control_types = ['BaseUIA']\n\n def __init__(self, element_info):\n super(BaseUIAWrapper, self).__init__(element_info)\n self._client = get_client(process=element_info.process_id)\n self._prompt = self._client.prompt\n self._win32structure = win32structure\n\n def _get_control(self, control_define):\n # control_define 为Client格式的字符串或字典,或者pywinauto格式的字典\n return get_control_specification(self._client.window(), control_define)\n\n def config(self, key): # 弹出框无法使用\n return self.element_info.config.get(key, None)\n\n def top_level_parent(self):\n # NOTE 官方top_level_parent()效率低且易出错,重写\n # return self._client.window().wrapper_object() # 注意:集成环境下仍然指向客户端主窗口\n return self._client.root_window().wrapper_object()\n\n def standard(self):\n \"\"\"返回此控件的pywinauto官方标准控件\"\"\"\n # NOTE 不要在条件中添加type和class,有可能失效\n return get_control_specification(self.element_info.parent, {'handle': self.element_info.handle})\n\n def own(self): # 弹出框无法使用\n \"\"\"返回此控件的另一个副本\"\"\"\n return get_control_specification(self.element_info.current_parent, self.element_info.control_define)\n\n def child(self, control_define):\n \"\"\"返回此控件的后代规范\"\"\"\n # control_define 为Client格式的字符串或字典,或者pywinauto格式的字典\n return get_control_specification(self.element_info, control_define)\n\n def texts(self):\n \"\"\"重写texts()\"\"\"\n rtn = [c.window_text() for c in self.descendants() if c.window_text() != '']\n return [self.window_text()] + list(map(lambda s: s.replace('\\r', '').replace('\\n', ''), rtn))\n\n def image_text(self, box=None, whitelist=None):\n \"\"\"返回控件的表面可见文本\"\"\"\n if whitelist is None:\n whitelist = ''\n return image_to_string(self.capture_as_image(box), tessedit_char_whitelist=whitelist)\n\n def exists(self, timeout=None):\n \"\"\"判断控件是否还存在\"\"\"\n return self._get_control({'handle': self.handle}).exists(timeout=timeout)" }, { "identifier": "RecordNotFoundError", "path": "src/pytradecn/error.py", "snippet": "class ElementAmbiguousError(Exception):\nclass ElementNotFoundError(Exception):\nclass ItemKeyError(Exception):\nclass ClientConfigError(Exception):\nclass TradeFailFError(Exception):\nclass StockCountError(Exception):\nclass StockPriceError(Exception):\nclass StockCodeError(Exception):\nclass ScreenLockedError(Exception):\nclass LoginError(Exception):\nclass RecordNotFoundError(Exception):\nclass RecordAmbiguousError(Exception):" } ]
from os import remove from csv import DictReader from decimal import Decimal from tempfile import NamedTemporaryFile from os.path import exists from .baseuiawrapper import BaseUIAWrapper from ..error import RecordNotFoundError, RecordAmbiguousError, ItemKeyError, TimeoutError
3,220
if item not in ['pop', 'popitem', 'update', 'setdefault', 'clear', 'fromkeys']: return getattr(self.__data, item) else: raise AttributeError(f'GridItem对象没有{item}属性') def click(self, x=None, double=False): self.__grid.click_input( coords=(x, self.__headHeight + int(self.__lineHeight >> 1) + (self.__lineHeight * self.__data['index'])), double=double ) def double_click(self): self.click(double=True) def select(self): self.click(x=self.__offset) class GridWrapper(BaseUIAWrapper): _control_types = ['GridCSV'] def __init__(self, elem): super(GridWrapper, self).__init__(elem) def __getitem__(self, item): return self.__data[item] def __getattribute__(self, attr): return object.__getattribute__(self, attr) def __getattr__(self, item): if item in ['count', 'index', 'copy']: return getattr(self.__data, item) else: raise AttributeError(f'GridWrapper对象没有{item}属性') def __iter__(self): return iter(self.__data) def __len__(self): return len(self.__data) def __repr__(self): rtn = '[' for item in self.__data: rtn += '\n\t' + str(item) + ',' return rtn + '\n]' def __str__(self): rtn = '[' for item in self.__data: rtn += '\n\t' + str(item) + ',' return rtn + '\n]' def __saveto(self, file): # 关闭可能存在的弹窗 self._prompt.close() self.set_focus().type_keys('^s') saveto = self._get_control(self.config('saveto')) # saveto.child(self.config('savetofile')).set_text(file) savetofile = saveto.child(self.config('savetofile')) # 将鼠标移动到输入框,否则微软UIA的接口会找不到主窗口,不知何故 savetofile.click_input() savetofile.set_text(file) saveto.ok() def __save_csv_and_parse(self): """使用另存为方式保存数据""" with NamedTemporaryFile(mode='w+', prefix='WYH_', suffix='.csv', newline='', delete=True) as f: file = f.name self.__saveto(file) while not exists(file): # 等待保存完成 pass with open(file, newline='') as csvfile: reader = DictReader(csvfile) self.__data = [GridItem(self, dict(index=reader.line_num-2, **row)) for row in reader] # row为何是str? if exists(file): remove(file) def items(self, **kwargs): """ 依据给定的条件过滤列表,返回过滤后的列表(行,即GridItem对象) kwargs关键字可以是表格标头的任何一个字段,value是一个字符串或由字符串组成的元组, 即使像成交价格、成交数量等在GridWrapper中仍然以字符串格式保存,这样做的好处是 便于使用Decimal类进行浮点数运算,而不会因计算机浮点数危机使价格计算错误。 items()方法是GridWrapper对象的核心方法,使用场景可能如下: 1、获得全部委托单 grid.items() 2、使用一个关键字参数过滤列表 grid.items(证券名称='农业银行') # 所有证券名称为‘农业银行’的委托单 3、使用多个关键字参数过滤列表 grid.items(证券名称='农业银行', 操作='买入') # 将农业银行的买入单过滤出来 4、使用一个关键字参数,多值过滤列表 grid.items(证券名称=('农业银行', '平安银行')) # 所有证券名称为‘农业银行’和‘平安银行’的委托单 grid.items(合同编号=('123456', '654321')) # 合同编号为‘123456’和‘654321’的委托单 5、使用多关键字参数,多值过滤列表 grid.items(证券名称=('农业银行', '平安银行'), 操作='买入') # 农业银行和平安银行的买入单 """ table = self.__data.copy() for key, value in kwargs.items(): values = (str(value),) if isinstance(value, (str, int, float, Decimal)) else value table = [row for row in table if row[key] in values] return table def item(self, **kwargs): """依据给定的条件,返回一个匹配的项目""" table = self.items(**kwargs) if not table: raise RecordNotFoundError(kwargs) if len(table) > 1:
# # 券商客户端自动化测试库 # Copyright (C) 2023 谁的谁([email protected]) All rights reserved. # # 模块功能:各种自定义控件 # 建立日期:2023.07.20 # 联系方式:谁的谁([email protected]) # # 开源软件声明: # 本软件遵守“MIT License”开源协议开源,仅供学习和参考。您可以自由使用或修改源代码或二进制文件,但必须保留上述版权声明。 # 该软件旨在深度学习和挖掘python pywinauto库的功能和潜力,由于环境的不确定性和该软件的不可靠性,请不要将该软件应用于 # 实盘交易。如您确需量化交易实盘功能,请使用券商提供的量化交易平台,否则由于您使用该软件实盘交易所造成的账户损失或政策风 # 险,开源软件提供者或插件提供者均不承担任何责任。同时,无论是直接的、间接的、偶然的、潜在的因使用该软件所造成的账号安全 # 损失、数据安全损失、账户资产损失或其他任何责任事故,开源软件提供者或插件提供者均不承担任何责任。请不要将该软件应用于商 # 业活动,否则由于把该软件应用于商业活动所造成的一切损失或法律责任,开源软件提供者或插件提供者均不承担任何责任。 # # 修改日志: # 2022-07-20 第一次编写 # class PromptWrapper(BaseUIAWrapper): _control_types = ['Prompt'] def __init__(self, elem): super(PromptWrapper, self).__init__(elem) def __wait_prompt_close(self): try: # NOTE 使用_get_control从顶层窗口查找 self._get_control({'handle': self.handle}).wait_not('exists') except TimeoutError: # 超时因存在关闭确认框或其他已知的原因 pass @property def title(self): title_spec = self.child(self._client.PROMPT_TITLE_ID) return title_spec.window_text() if title_spec.exists() else '' def content(self): text_spec = self.child(self._client.PROMPT_CONTENT_ID) return text_spec.window_text() if text_spec.exists() else '' def ok(self): ok_btn = self.child({ 'title_re': self._client.PROMPT_OKBUTTON_TITLE, 'control_type': 'Button' }) if ok_btn.exists(): ok_btn.click() self.__wait_prompt_close() def cancel(self): cancel_btn = self.child({ 'title_re': self._client.PROMPT_CANCELBUTTON_TITLE, 'control_type': 'Button' }) if cancel_btn.exists(): cancel_btn.click() self.__wait_prompt_close() def close(self): # FIXME 有弹框关闭时会弹出确认对话框 criterias = list(self._client.PROMPT_CLOSE_BUTTON) criterias.extend([ {'title_re': self._client.PROMPT_CANCELBUTTON_TITLE, 'control_type': 'Button'}, {'title_re': self._client.PROMPT_OKBUTTON_TITLE, 'control_type': 'Button'} ]) for criteria in criterias: cls_btn = self.child(criteria) if cls_btn.exists(): # 非捕捉模式 cls_btn.click() self.__wait_prompt_close() break class GridItem(object): """表格中的项,非控件""" def __init__(self, grid, data): self.__grid = grid self.__data = data config = self.__grid.config self.__headHeight = 24 if config('headHeight') is None else config('headHeight') self.__lineHeight = 24 if config('lineHeight') is None else config('lineHeight') self.__offset = 6 if config('offset') is None else config('offset') def __getitem__(self, item): try: return self.__data[item] except KeyError: raise ItemKeyError(f'表格中没有<{item}>字段') def __iter__(self): return iter(self.__data) def __len__(self): return len(self.__data) def __str__(self): return str(self.__data) def __repr__(self): return str(self.__data) def __getattribute__(self, attr): return object.__getattribute__(self, attr) def __getattr__(self, item): if item not in ['pop', 'popitem', 'update', 'setdefault', 'clear', 'fromkeys']: return getattr(self.__data, item) else: raise AttributeError(f'GridItem对象没有{item}属性') def click(self, x=None, double=False): self.__grid.click_input( coords=(x, self.__headHeight + int(self.__lineHeight >> 1) + (self.__lineHeight * self.__data['index'])), double=double ) def double_click(self): self.click(double=True) def select(self): self.click(x=self.__offset) class GridWrapper(BaseUIAWrapper): _control_types = ['GridCSV'] def __init__(self, elem): super(GridWrapper, self).__init__(elem) def __getitem__(self, item): return self.__data[item] def __getattribute__(self, attr): return object.__getattribute__(self, attr) def __getattr__(self, item): if item in ['count', 'index', 'copy']: return getattr(self.__data, item) else: raise AttributeError(f'GridWrapper对象没有{item}属性') def __iter__(self): return iter(self.__data) def __len__(self): return len(self.__data) def __repr__(self): rtn = '[' for item in self.__data: rtn += '\n\t' + str(item) + ',' return rtn + '\n]' def __str__(self): rtn = '[' for item in self.__data: rtn += '\n\t' + str(item) + ',' return rtn + '\n]' def __saveto(self, file): # 关闭可能存在的弹窗 self._prompt.close() self.set_focus().type_keys('^s') saveto = self._get_control(self.config('saveto')) # saveto.child(self.config('savetofile')).set_text(file) savetofile = saveto.child(self.config('savetofile')) # 将鼠标移动到输入框,否则微软UIA的接口会找不到主窗口,不知何故 savetofile.click_input() savetofile.set_text(file) saveto.ok() def __save_csv_and_parse(self): """使用另存为方式保存数据""" with NamedTemporaryFile(mode='w+', prefix='WYH_', suffix='.csv', newline='', delete=True) as f: file = f.name self.__saveto(file) while not exists(file): # 等待保存完成 pass with open(file, newline='') as csvfile: reader = DictReader(csvfile) self.__data = [GridItem(self, dict(index=reader.line_num-2, **row)) for row in reader] # row为何是str? if exists(file): remove(file) def items(self, **kwargs): """ 依据给定的条件过滤列表,返回过滤后的列表(行,即GridItem对象) kwargs关键字可以是表格标头的任何一个字段,value是一个字符串或由字符串组成的元组, 即使像成交价格、成交数量等在GridWrapper中仍然以字符串格式保存,这样做的好处是 便于使用Decimal类进行浮点数运算,而不会因计算机浮点数危机使价格计算错误。 items()方法是GridWrapper对象的核心方法,使用场景可能如下: 1、获得全部委托单 grid.items() 2、使用一个关键字参数过滤列表 grid.items(证券名称='农业银行') # 所有证券名称为‘农业银行’的委托单 3、使用多个关键字参数过滤列表 grid.items(证券名称='农业银行', 操作='买入') # 将农业银行的买入单过滤出来 4、使用一个关键字参数,多值过滤列表 grid.items(证券名称=('农业银行', '平安银行')) # 所有证券名称为‘农业银行’和‘平安银行’的委托单 grid.items(合同编号=('123456', '654321')) # 合同编号为‘123456’和‘654321’的委托单 5、使用多关键字参数,多值过滤列表 grid.items(证券名称=('农业银行', '平安银行'), 操作='买入') # 农业银行和平安银行的买入单 """ table = self.__data.copy() for key, value in kwargs.items(): values = (str(value),) if isinstance(value, (str, int, float, Decimal)) else value table = [row for row in table if row[key] in values] return table def item(self, **kwargs): """依据给定的条件,返回一个匹配的项目""" table = self.items(**kwargs) if not table: raise RecordNotFoundError(kwargs) if len(table) > 1:
exception = RecordAmbiguousError('有{0}条记录, 在此条件下{1}'.format(len(table), str(kwargs),))
1
2023-11-03 02:22:34+00:00
4k
humemarx/CPG-LCF
datasets/nusc/nusc_cam_data.py
[ { "identifier": "data_aug", "path": "datasets/data_aug.py", "snippet": "def points_in_convex_polygon_jit(points, polygon, clockwise=True):\ndef in_range_3d(pcds, point_range):\ndef in_range_bev(boxs, box_range):\ndef limit_period(val, offset=0.5, period=np.pi):\ndef corners_nd(dims, origin=0.5):\ndef rotation_2d(points, angles):\ndef center_to_corner_box2d(centers, dims, angles=None, origin=0.5):\ndef minmax_to_corner_2d(minmax_box):\n def __init__(self, limit_range):\n def __call__(self, pcds, gt_boxes=None):\n def __init__(self, probability=0.5):\n def __call__(self, pcds, gt_boxes=None):\n def __init__(self, rot_rad=np.pi):\n def __call__(self, pcds, gt_boxes=None):\n def __init__(self, min_scale=0.95, max_scale=1.05):\n def __call__(self, pcds, gt_boxes=None):\n def __init__(self, shift_range=((-3, 3), (-3, 3), (-0.4, 0.4))):\n def __call__(self, pcds, gt_boxes=None):\n def __init__(self, noise_mean=0.0, noise_std=0.0):\n def __call__(self, pcds, gt_boxes=None):\n def __init__(self,\n range_x = (-100.0, 100.0), \n range_y = (-100.0, 100.0),\n range_z = (-5.0, 3.0)):\n def __call__(self, pcds, gt_boxes=None):\n def __init__(self,\n range_x = (-100.0, 100.0), \n range_y = (-100.0, 100.0),\n range_z = (-5.0, 3.0)):\n def __call__(self, gt_boxes, gt_labels):\n def __init__(self, transforms):\n def __call__(self, pcds, gt_boxes=None):\n def __init__(self, transforms):\n def __call__(self, res, info):\n def __init__(self, \n instance_class: List[int], \n swap_ratio: float = 0.5,\n rotate_paste_ratio: float = 1.0,\n omega_factor: float = 0.667):\n def swap(self, pt1, pt2, start_angle, end_angle, label1, label2):\n def rotate_copy(self, pts, labels):\n def __call__(self, pcds1, labels1, pcds2, labels2):\nclass ObjFilterRange:\nclass RandomFlip:\nclass GlobalRotation:\nclass GlobalScale:\nclass GlobalShift:\nclass GlobalNoise:\nclass PointsRangeFilter:\nclass ObjectRangeFilter:\nclass PointAugCompose(object):\nclass Compose(object):\nclass PolarMix(object):" }, { "identifier": "utils", "path": "datasets/utils.py", "snippet": "def in_hull(p, hull):\r\ndef gaussian_radius(det_size, min_overlap=0.5):\r\ndef gaussian2D(shape, sigma=1):\r\ndef draw_umich_gaussian(heatmap, center, radius, k=1):\r\ndef draw_ellip_gaussian_2D(heatmap,\r\n center,\r\n radius_x,\r\n radius_y,\r\n k=1):\r\ndef ellip_gaussian2D(shape,\r\n sigma_x,\r\n sigma_y):\r\ndef make_point_feat(pcds_xyzi, pcds_coord, pcds_sphere_coord, feat_index=4, with_time=False):\r\ndef compute_box_3d(center, size, yaw):\r\ndef random_float(v_range):\r\ndef in_range(v, r):\r\ndef filter_pcds(pcds, range_x=(-40, 60), range_y=(-40, 40), range_z=(-3, 5)):\r\ndef filter_pcds_mask(pcds, range_x=(-40, 60), range_y=(-40, 40), range_z=(-3, 5)):\r\ndef Trans(pcds, mat):\r\ndef relabel(pcds_labels, label_map):\r\ndef recolor(pcds_labels, color_map):\r\ndef Quantize(pcds, pcds_label=None, range_x=(-40, 62.4), range_y=(-40, 40), range_z=(-3, 5), size=(512, 512, 20)):\r\ndef SphereQuantize(pcds, pcds_label=None, phi_range=(-180.0, 180.0), theta_range=(-16.0, 10.0), size=(64, 2048)):\r\ndef CylinderQuantize(pcds, phi_range=(-180.0, 180.0), range_z=(-3, 5), size=(64, 2048)):\r\n def __init__(self, noise_mean=0, noise_std=0.01, theta_range=(-45, 45), shift_range=(0, 0), size_range=(0.95, 1.05)):\r\n def __call__(self, pcds, gt_boxes=None):\r\n R = np.array([[c, -s, 0],\r\n [s, c, 0],\r\n [0, 0, 1]])\r\n H = size[0]\r\n W = size[1]\r\n H = size[0]\r\n W = size[1]\r\nclass DataAugment:\r" }, { "identifier": "copy_paste", "path": "datasets/copy_paste.py", "snippet": "def in_range(v, r):\ndef in_hull(p, hull):\ndef compute_box_3d(center, size, yaw):\ndef csr2corners(center, size, yaw):\ndef csr2corners_batch(gt_3d_box):\ndef corners2csr_batch(gt_3d_box_corners):\ndef rotate_along_z(pcds, theta):\ndef random_f(r):\n def __init__(self, config):\n def get_random_rotate_along_z_obj(self, pcds_obj, bbox_corners, theta):\n def get_fov(self, pcds_obj):\n def no_occlusion_check(self, pcds, pcds_label, phi_fov, theta_fov):\n def no_collision_check(self, pcds, pcds_label, bbox_corners):\n def paste_single_obj(self, pcds, pcds_road, pcds_label, idx_mask, gt_3d_box=None):\n def __call__(self, pcds, pcds_label, pcds_road_label=None, gt_3d_box=None):\n def relabel(pcds_labels, label_map):\n R = np.array([[c, -s, 0],\n [s, c, 0],\n [0, 0, 1]])\n R = np.array([[c, -s, 0],\n [s, c, 0],\n [0, 0, 1]])\nclass CutPaste:\n class CopyPasteAug:" }, { "identifier": "camera_aug", "path": "datasets/camera_aug.py", "snippet": "class ImgNormalize(object):\nclass BottomCrop(object):\nclass CenterCrop(object):\nclass RandomCrop(object):\nclass HorizontalFlip(object):\nclass VerticalFlip(object):\nclass RandomScale(object):\nclass ColorJitter(object):\nclass GaussBlur(object):\nclass RotateImage(object):\nclass MultiScale(object):\nclass ImageAugCompose(object):\n def __init__(self, mean, std, *args, **kwargs):\n def __call__(self, im_dict):\n def __init__(self, cropsize, *args, **kwargs):\n def __call__(self, im_dict):\n def __init__(self, cropsize, *args, **kwargs):\n def __call__(self, im_dict):\n def __init__(self, cropsize, *args, **kwargs):\n def __call__(self, im_dict):\n def __init__(self, p=0.5, *args, **kwargs):\n def __call__(self, im_dict):\n def __init__(self, p=0.5, *arg, **kwargs):\n def __call__(self, im_dict):\n def __init__(self, scale=1.0, *args, **kwargs):\n def __call__(self, im_dict):\n def __init__(self, brightness=None, contrast=None, saturation=None, *args, **kwargs):\n def __call__(self, im_dict):\n def __init__(self, radius=(0,)):\n def __call__(self, im_dict):\n def __init__(self, rotate_boundary=[1,1]):\n def __call__(self, im_dict):\n def __init__(self, scales):\n def __call__(self, img):\n def __init__(self, do_list):\n def __call__(self, im_lb):\n W, H = self.size # new\n W, H = self.size # new\n W, H = self.size # new\n A = np.asarray([[-1, 0], [0, 1]])\n A = np.asarray([[1, 0], [0, -1]])\n W, H = im.size\n A = np.asarray(\n [\n [np.cos(theta), np.sin(theta)],\n [-np.sin(theta), np.cos(theta)],\n ]\n )\n W, H = img.size" } ]
import pickle as pkl import yaml import json import numpy as np import os import os.path as osp import copy import random import math import torch from torch.utils.data import Dataset from datasets import data_aug, utils, copy_paste, camera_aug from nuscenes.utils.geometry_utils import view_points from PIL import Image
3,296
''' Author: husserl License: Apache Licence Software: VSCode Date: 2023-03-01 03:40:26 LastEditors: husserl LastEditTime: 2023-11-02 09:18:48 ''' nus_categories = ('car', 'truck', 'trailer', 'bus', 'construction_vehicle', 'bicycle', 'motorcycle', 'pedestrian', 'traffic_cone', 'barrier') class DataloadTrain(Dataset): def __init__(self,config): self.config = config self.mode = config.mode self.fname_pkl = config.fname_pkl self.data_root = config.SeqDir self.frame_point_num = random.choice(self.config.frame_point_num) with open('datasets/nusc/nuscenes.yaml', 'r') as f: self.task_cfg = yaml.load(f, Loader=yaml.Loader) # prob resample if hasattr(self.config, 'use_prob_resample'): self.use_prob_resample = self.config.use_prob_resample else: self.use_prob_resample = False self.use_camera = 'none' self.rand_level = 0 self.point_aug = None self.image_aug = None self.init_lidar_aug() self.init_cp_aug() self.init_cam_anno() self.load_infos(self.fname_pkl) def init_cp_aug(self): print('init copy paste aug!') self.cp_aug = None if hasattr(self.config, 'CopyPasteAug') and self.config.CopyPasteAug.is_use: self.cp_aug = copy_paste.CutPaste(self.config.CopyPasteAug) def init_cam_anno(self): if hasattr(self.config, 'rand_level'): self.rand_level = self.config.rand_level print('init cam anno!') self.img_feat_num = 0 # load image data if 'camera_raw' in self.config.SensorParam.modal_list: self.use_camera = 'camera_raw' self.img_feat_num = self.config.SensorParam.camera_feat_num transforms = [] if hasattr(self.config, 'CameraAug'): for aug_dic in self.config.CameraAug.transforms: aug_func = eval('camera_aug.{}'.format(aug_dic['type']))(**aug_dic['params']) transforms.append(aug_func) self.image_aug = camera_aug.ImageAugCompose(transforms) else: pass def init_lidar_aug(self): print('init lidar aug!') if hasattr(self.config, 'PointAug'): transforms = [] for aug_dic in self.config.PointAug.transforms: aug_func = eval('data_aug.{}'.format(aug_dic['type']))(**aug_dic['params']) transforms.append(aug_func) self.point_aug = data_aug.PointAugCompose(transforms) def load_infos(self, info_path): print('load data infos!') with open(info_path, 'rb') as f: self.data_infos = pkl.load(f)['infos'] self.sample_length = len(self.data_infos) print('{} Samples: '.format(self.mode), self.sample_length) if hasattr(self.config, 'obj_sample') and self.config.obj_sample: # get object class dist _cls_infos = {name: [] for name in nus_categories} for info in self.data_infos: for name in set(info["gt_names"]): if name in nus_categories: _cls_infos[name].append(info) duplicated_samples = sum([len(v) for _, v in _cls_infos.items()]) _cls_dist = {k: len(v) / max(duplicated_samples, 1) for k, v in _cls_infos.items()} self._nusc_infos_all = [] frac = 1.0 / len(nus_categories) ratios = [frac / v for v in _cls_dist.values()] for cls_infos, ratio in zip(list(_cls_infos.values()), ratios): self._nusc_infos_all += np.random.choice( cls_infos, int(len(cls_infos) * ratio) ).tolist() self.sample_length = len(self._nusc_infos_all) print('{} RE Samples: '.format(self.mode), self.sample_length) else: self._nusc_infos_all = self.data_infos # random.shuffle(self._nusc_infos_all) # self.data_infos = self._nusc_infos_all[:self.sample_length] self.data_infos = self._nusc_infos_all def load_pcd_from_file(self, file_path): file_path = os.path.join('data', file_path) points = np.fromfile(file_path, dtype=np.float32).reshape(-1, 5)[:, :4] return points def load_pcdlabel_from_file(self, file_path): file_path = os.path.join('data', file_path) pcds_label_use = np.fromfile(file_path, dtype=np.uint8).reshape((-1))
# coding=utf-8 ''' Author: husserl License: Apache Licence Software: VSCode Date: 2023-03-01 03:40:26 LastEditors: husserl LastEditTime: 2023-11-02 09:18:48 ''' nus_categories = ('car', 'truck', 'trailer', 'bus', 'construction_vehicle', 'bicycle', 'motorcycle', 'pedestrian', 'traffic_cone', 'barrier') class DataloadTrain(Dataset): def __init__(self,config): self.config = config self.mode = config.mode self.fname_pkl = config.fname_pkl self.data_root = config.SeqDir self.frame_point_num = random.choice(self.config.frame_point_num) with open('datasets/nusc/nuscenes.yaml', 'r') as f: self.task_cfg = yaml.load(f, Loader=yaml.Loader) # prob resample if hasattr(self.config, 'use_prob_resample'): self.use_prob_resample = self.config.use_prob_resample else: self.use_prob_resample = False self.use_camera = 'none' self.rand_level = 0 self.point_aug = None self.image_aug = None self.init_lidar_aug() self.init_cp_aug() self.init_cam_anno() self.load_infos(self.fname_pkl) def init_cp_aug(self): print('init copy paste aug!') self.cp_aug = None if hasattr(self.config, 'CopyPasteAug') and self.config.CopyPasteAug.is_use: self.cp_aug = copy_paste.CutPaste(self.config.CopyPasteAug) def init_cam_anno(self): if hasattr(self.config, 'rand_level'): self.rand_level = self.config.rand_level print('init cam anno!') self.img_feat_num = 0 # load image data if 'camera_raw' in self.config.SensorParam.modal_list: self.use_camera = 'camera_raw' self.img_feat_num = self.config.SensorParam.camera_feat_num transforms = [] if hasattr(self.config, 'CameraAug'): for aug_dic in self.config.CameraAug.transforms: aug_func = eval('camera_aug.{}'.format(aug_dic['type']))(**aug_dic['params']) transforms.append(aug_func) self.image_aug = camera_aug.ImageAugCompose(transforms) else: pass def init_lidar_aug(self): print('init lidar aug!') if hasattr(self.config, 'PointAug'): transforms = [] for aug_dic in self.config.PointAug.transforms: aug_func = eval('data_aug.{}'.format(aug_dic['type']))(**aug_dic['params']) transforms.append(aug_func) self.point_aug = data_aug.PointAugCompose(transforms) def load_infos(self, info_path): print('load data infos!') with open(info_path, 'rb') as f: self.data_infos = pkl.load(f)['infos'] self.sample_length = len(self.data_infos) print('{} Samples: '.format(self.mode), self.sample_length) if hasattr(self.config, 'obj_sample') and self.config.obj_sample: # get object class dist _cls_infos = {name: [] for name in nus_categories} for info in self.data_infos: for name in set(info["gt_names"]): if name in nus_categories: _cls_infos[name].append(info) duplicated_samples = sum([len(v) for _, v in _cls_infos.items()]) _cls_dist = {k: len(v) / max(duplicated_samples, 1) for k, v in _cls_infos.items()} self._nusc_infos_all = [] frac = 1.0 / len(nus_categories) ratios = [frac / v for v in _cls_dist.values()] for cls_infos, ratio in zip(list(_cls_infos.values()), ratios): self._nusc_infos_all += np.random.choice( cls_infos, int(len(cls_infos) * ratio) ).tolist() self.sample_length = len(self._nusc_infos_all) print('{} RE Samples: '.format(self.mode), self.sample_length) else: self._nusc_infos_all = self.data_infos # random.shuffle(self._nusc_infos_all) # self.data_infos = self._nusc_infos_all[:self.sample_length] self.data_infos = self._nusc_infos_all def load_pcd_from_file(self, file_path): file_path = os.path.join('data', file_path) points = np.fromfile(file_path, dtype=np.float32).reshape(-1, 5)[:, :4] return points def load_pcdlabel_from_file(self, file_path): file_path = os.path.join('data', file_path) pcds_label_use = np.fromfile(file_path, dtype=np.uint8).reshape((-1))
pcds_label_use = utils.relabel(pcds_label_use, self.task_cfg['learning_map'])
1
2023-11-02 09:50:13+00:00
4k
JaeBinCHA7/DEMUCS-for-Speech-Enhancement
models/DEMUCS.py
[ { "identifier": "downsample2", "path": "models/tools.py", "snippet": "def downsample2(x, zeros=56):\n \"\"\"\n Downsampling the input by 2 using sinc interpolation.\n Smith, Julius, and Phil Gossett. \"A flexible sampling-rate conversion method.\"\n ICASSP'84. IEEE International Conference on Acoustics, Speech, and Signal Processing.\n Vol. 9. IEEE, 1984.\n \"\"\"\n if x.shape[-1] % 2 != 0:\n x = F.pad(x, (0, 1))\n xeven = x[..., ::2]\n xodd = x[..., 1::2]\n *other, time = xodd.shape\n kernel = kernel_downsample2(zeros).to(x)\n out = xeven + F.conv1d(xodd.view(-1, 1, time), kernel, padding=zeros)[..., :-1].view(\n *other, time)\n return out.view(*other, -1).mul(0.5)" }, { "identifier": "upsample2", "path": "models/tools.py", "snippet": "def upsample2(x, zeros=56):\n \"\"\"\n Upsampling the input by 2 using sinc interpolation.\n Smith, Julius, and Phil Gossett. \"A flexible sampling-rate conversion method.\"\n ICASSP'84. IEEE International Conference on Acoustics, Speech, and Signal Processing.\n Vol. 9. IEEE, 1984.\n \"\"\"\n *other, time = x.shape # [32, 1, 32085]\n kernel = kernel_upsample2(zeros).to(x) # [1, 1, 112]\n out = F.conv1d(x.view(-1, 1, time), kernel, padding=zeros)[..., 1:].view(*other, time) # [32, 1, 32085]\n y = th.stack([x, out], dim=-1) # [32, 1, 32085, 2]\n return y.view(*other, -1)" }, { "identifier": "capture_init", "path": "models/tools.py", "snippet": "def capture_init(init):\n \"\"\"capture_init.\n Decorate `__init__` with this, and you can then\n recover the *args and **kwargs passed to it in `self._init_args_kwargs`\n \"\"\"\n @functools.wraps(init)\n def __init__(self, *args, **kwargs):\n self._init_args_kwargs = (args, kwargs)\n init(self, *args, **kwargs)\n\n return __init__" } ]
import math import torch as th from torch import nn from torch.nn import functional as F from .tools import downsample2, upsample2, capture_init
1,918
- chout (int): number of output channels. - hidden (int): number of initial hidden channels. - depth (int): number of layers. - kernel_size (int): kernel size for each layer. - stride (int): stride for each layer. - causal (bool): if false, uses BiLSTM instead of LSTM. - resample (int): amount of resampling to apply to the input/output. Can be one of 1, 2 or 4. - growth (float): number of channels is multiplied by this for every layer. - max_hidden (int): maximum number of channels. Can be useful to control the size/speed of the model. - normalize (bool): if true, normalize the input. - glu (bool): if true uses GLU instead of ReLU in 1x1 convolutions. - rescale (float): controls custom weight initialization. See https://arxiv.org/abs/1911.13254. - floor (float): stability flooring when normalizing. - sample_rate (float): sample_rate used for training the model. """ @capture_init def __init__(self, chin=1, chout=1, hidden=48, depth=5, kernel_size=8, stride=4, causal=True, resample=4, growth=2, max_hidden=10_000, normalize=True, glu=True, rescale=0.1, floor=1e-3, sample_rate=16_000): super().__init__() if resample not in [1, 2, 4]: raise ValueError("Resample should be 1, 2 or 4.") self.chin = chin self.chout = chout self.hidden = hidden self.depth = depth self.kernel_size = kernel_size self.stride = stride self.causal = causal self.floor = floor self.resample = resample self.normalize = normalize self.sample_rate = sample_rate self.encoder = nn.ModuleList() self.decoder = nn.ModuleList() activation = nn.GLU(1) if glu else nn.ReLU() ch_scale = 2 if glu else 1 for index in range(depth): encode = [] encode += [ nn.Conv1d(chin, hidden, kernel_size, stride), nn.ReLU(), nn.Conv1d(hidden, hidden * ch_scale, 1), activation, ] self.encoder.append(nn.Sequential(*encode)) decode = [] decode += [ nn.Conv1d(hidden, ch_scale * hidden, 1), activation, nn.ConvTranspose1d(hidden, chout, kernel_size, stride), ] if index > 0: decode.append(nn.ReLU()) self.decoder.insert(0, nn.Sequential(*decode)) chout = hidden chin = hidden hidden = min(int(growth * hidden), max_hidden) self.lstm = BLSTM(chin, bi=not causal) if rescale: rescale_module(self, reference=rescale) def valid_length(self, length): """ Return the nearest valid length to use with the model so that there is no time steps left over in a convolutions, e.g. for all layers, size of the input - kernel_size % stride = 0. If the mixture has a valid length, the estimated sources will have exactly the same length. """ length = math.ceil(length * self.resample) # 128000 for idx in range(self.depth): length = math.ceil((length - self.kernel_size) / self.stride) + 1 length = max(length, 1) for idx in range(self.depth): length = (length - 1) * self.stride + self.kernel_size length = int(math.ceil(length / self.resample)) return int(length) @property def total_stride(self): return self.stride ** self.depth // self.resample def forward(self, mix): if mix.dim() == 2: mix = mix.unsqueeze(1) if self.normalize: mono = mix.mean(dim=1, keepdim=True) std = mono.std(dim=-1, keepdim=True) mix = mix / (self.floor + std) else: std = 1 length = mix.shape[-1] x = mix x = F.pad(x, (0, self.valid_length(length) - length)) if self.resample == 2:
""" Reference: https://github.com/facebookresearch/denoiser/blob/main/denoiser/demucs.py Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. This source code is licensed under the license found in the LICENSE file in the root directory of this source tree. author: adefossez """ class BLSTM(nn.Module): def __init__(self, dim, layers=2, bi=True): super().__init__() klass = nn.LSTM self.lstm = klass(bidirectional=bi, num_layers=layers, hidden_size=dim, input_size=dim) self.linear = None if bi: self.linear = nn.Linear(2 * dim, dim) def forward(self, x, hidden=None): x, hidden = self.lstm(x, hidden) if self.linear: x = self.linear(x) return x, hidden def rescale_conv(conv, reference): std = conv.weight.std().detach() scale = (std / reference) ** 0.5 conv.weight.data /= scale if conv.bias is not None: conv.bias.data /= scale def rescale_module(module, reference): for sub in module.modules(): if isinstance(sub, (nn.Conv1d, nn.ConvTranspose1d)): rescale_conv(sub, reference) class DEMUCS(nn.Module): """ Demucs speech enhancement model. Args: - chin (int): number of input channels. - chout (int): number of output channels. - hidden (int): number of initial hidden channels. - depth (int): number of layers. - kernel_size (int): kernel size for each layer. - stride (int): stride for each layer. - causal (bool): if false, uses BiLSTM instead of LSTM. - resample (int): amount of resampling to apply to the input/output. Can be one of 1, 2 or 4. - growth (float): number of channels is multiplied by this for every layer. - max_hidden (int): maximum number of channels. Can be useful to control the size/speed of the model. - normalize (bool): if true, normalize the input. - glu (bool): if true uses GLU instead of ReLU in 1x1 convolutions. - rescale (float): controls custom weight initialization. See https://arxiv.org/abs/1911.13254. - floor (float): stability flooring when normalizing. - sample_rate (float): sample_rate used for training the model. """ @capture_init def __init__(self, chin=1, chout=1, hidden=48, depth=5, kernel_size=8, stride=4, causal=True, resample=4, growth=2, max_hidden=10_000, normalize=True, glu=True, rescale=0.1, floor=1e-3, sample_rate=16_000): super().__init__() if resample not in [1, 2, 4]: raise ValueError("Resample should be 1, 2 or 4.") self.chin = chin self.chout = chout self.hidden = hidden self.depth = depth self.kernel_size = kernel_size self.stride = stride self.causal = causal self.floor = floor self.resample = resample self.normalize = normalize self.sample_rate = sample_rate self.encoder = nn.ModuleList() self.decoder = nn.ModuleList() activation = nn.GLU(1) if glu else nn.ReLU() ch_scale = 2 if glu else 1 for index in range(depth): encode = [] encode += [ nn.Conv1d(chin, hidden, kernel_size, stride), nn.ReLU(), nn.Conv1d(hidden, hidden * ch_scale, 1), activation, ] self.encoder.append(nn.Sequential(*encode)) decode = [] decode += [ nn.Conv1d(hidden, ch_scale * hidden, 1), activation, nn.ConvTranspose1d(hidden, chout, kernel_size, stride), ] if index > 0: decode.append(nn.ReLU()) self.decoder.insert(0, nn.Sequential(*decode)) chout = hidden chin = hidden hidden = min(int(growth * hidden), max_hidden) self.lstm = BLSTM(chin, bi=not causal) if rescale: rescale_module(self, reference=rescale) def valid_length(self, length): """ Return the nearest valid length to use with the model so that there is no time steps left over in a convolutions, e.g. for all layers, size of the input - kernel_size % stride = 0. If the mixture has a valid length, the estimated sources will have exactly the same length. """ length = math.ceil(length * self.resample) # 128000 for idx in range(self.depth): length = math.ceil((length - self.kernel_size) / self.stride) + 1 length = max(length, 1) for idx in range(self.depth): length = (length - 1) * self.stride + self.kernel_size length = int(math.ceil(length / self.resample)) return int(length) @property def total_stride(self): return self.stride ** self.depth // self.resample def forward(self, mix): if mix.dim() == 2: mix = mix.unsqueeze(1) if self.normalize: mono = mix.mean(dim=1, keepdim=True) std = mono.std(dim=-1, keepdim=True) mix = mix / (self.floor + std) else: std = 1 length = mix.shape[-1] x = mix x = F.pad(x, (0, self.valid_length(length) - length)) if self.resample == 2:
x = upsample2(x)
1
2023-11-06 08:16:24+00:00
4k
yongchanghao/MLoRAx
examples/eval.py
[ { "identifier": "LoRASpec", "path": "mlorax.py", "snippet": "class LoRASpec:\n rank: int\n rules: Iterable[str]\n alpha: Optional[float] = None # default to rank\n dropout: float = 0.0\n tune_vectors: bool = False\n seed: int = 0\n disabled: bool = False" }, { "identifier": "lora_init", "path": "mlorax.py", "snippet": "def lora_init(\n lora_spec: LoRASpec,\n model: Any,\n params: Optional[flax.core.FrozenDict] = None,\n apply_fn: Optional[Callable] = None,\n):\n \"\"\"\n Initialize a model with LoRA parameters.\n Return a tuple of (trainable_params, apply_fn, merge_fn),\n where apply_fn: (trainable_params, *args, **kwargs) -> model_output\n and merge_fn: (trainable_params) -> full_params after merging.\n \"\"\"\n if params is None:\n params = model.params\n if apply_fn is None:\n apply_fn = model.__call__\n\n if lora_spec.disabled:\n return params, apply_fn, lambda params: params\n\n rank = lora_spec.rank\n init_rng = jax.random.PRNGKey(lora_spec.seed)\n\n trainable = {}\n freezed = {}\n for path, weight in flax.traverse_util.flatten_dict(\n params, sep=PATH_SEP\n ).items():\n weight_state = _decision_fn(lora_spec, path, weight)\n if weight_state == WeightState.FULL:\n trainable[path] = weight\n elif weight_state == WeightState.FREEZED:\n freezed[path] = weight\n elif weight_state == WeightState.FACTORIZED:\n trainable[f\"{path}{LORA_A_SUFFIX}\"] = jax.random.normal(\n init_rng, (weight.shape[0], rank), dtype=weight.dtype\n ) / jnp.sqrt(weight.shape[0] / 2)\n trainable[f\"{path}{LORA_B_SUFFIX}\"] = jnp.zeros(\n (rank, weight.shape[1]), dtype=weight.dtype\n )\n freezed[path] = weight\n init_rng = jax.random.split(init_rng)[0]\n else:\n raise ValueError(f\"Unknown weight state: {weight_state}\")\n\n trainable = flax.traverse_util.unflatten_dict(trainable, sep=PATH_SEP)\n\n def wrapped_apply_fn(\n params,\n lora_rng=None,\n lora_rng_detection=True,\n *args,\n **kwargs,\n ):\n \"\"\"\n Apply the model with trainable parameters.\n Dropout is applied if\n - lora_rng is not None, or\n - kwargs[kw] is detected for kw in RNG_KEYWORDS\n when lora_rng_detection=True (default).\n \"\"\"\n\n if lora_rng is None and lora_rng_detection:\n for kw in RNG_KEYWORDS:\n if isinstance(kwargs.get(kw, None), chex.PRNGKey):\n lora_rng = jax.random.split(kwargs[kw])[0]\n break\n\n return apply_fn(\n params=_lora_merge(\n lora_spec=lora_spec,\n trainable=params,\n freezed=freezed,\n rng=lora_rng,\n ),\n *args,\n **kwargs,\n )\n\n def wrapped_merge_fn(\n params,\n lora_rng=None,\n ):\n \"\"\"\n Merge trainable and freezed parameters into a full parameter set.\n Dropout is applied if lora_rng is not None.\n \"\"\"\n return _lora_merge(\n lora_spec=lora_spec,\n trainable=params,\n freezed=freezed,\n rng=lora_rng,\n )\n\n return trainable, wrapped_apply_fn, wrapped_merge_fn" } ]
import argparse import math import multiprocessing as mp import evaluate import jax import nltk import numpy as np import orbax.checkpoint as ocp import tqdm import transformers from pathlib import Path from datasets import Dataset, load_dataset from mlorax import LoRASpec, lora_init
2,058
parser.add_argument("--seed", type=int, default=0) parser.add_argument("--lora-disabled", action="store_true") parser.add_argument("--max-source-length", type=int, default=512) parser.add_argument("--max-target-length", type=int, default=64) parser.add_argument("--batch-size", type=int, default=8) metric = evaluate.load("rouge") def shift_tokens_right( input_ids: np.array, pad_token_id: int, decoder_start_token_id: int ) -> np.ndarray: """ Shift input ids one token to the right. """ shifted_input_ids = np.zeros_like(input_ids) shifted_input_ids[:, 1:] = input_ids[:, :-1] shifted_input_ids[:, 0] = decoder_start_token_id shifted_input_ids = np.where( shifted_input_ids == -100, pad_token_id, shifted_input_ids ) return shifted_input_ids def preprocess_function(examples): inputs = examples["document"] targets = examples["summary"] inputs = ["summarize: " + inp for inp in inputs] model_inputs = tokenizer( inputs, max_length=args.max_source_length, padding="max_length", truncation=True, return_tensors="np", ) # Setup the tokenizer for targets labels = tokenizer( text_target=targets, max_length=args.max_target_length, padding="max_length", truncation=True, return_tensors="np", ) model_inputs["labels"] = labels["input_ids"] decoder_input_ids = shift_tokens_right( labels["input_ids"], config.pad_token_id, config.decoder_start_token_id, ) model_inputs["decoder_input_ids"] = np.asarray(decoder_input_ids) # We need decoder_attention_mask so we can ignore pad tokens from loss model_inputs["decoder_attention_mask"] = labels["attention_mask"] return model_inputs def postprocess_text(preds, labels): preds = [pred.strip() for pred in preds] labels = [label.strip() for label in labels] # rougeLSum expects newline after each sentence preds = ["\n".join(nltk.sent_tokenize(pred)) for pred in preds] labels = ["\n".join(nltk.sent_tokenize(label)) for label in labels] return preds, labels def compute_metrics(preds, labels): decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True) decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True) # Some simple post-processing decoded_preds, decoded_labels = postprocess_text( decoded_preds, decoded_labels ) result = metric.compute( predictions=decoded_preds, references=decoded_labels, use_stemmer=True, ) result = {k: round(v * 100, 4) for k, v in result.items()} prediction_lens = [ np.count_nonzero(pred != tokenizer.pad_token_id) for pred in preds ] result["gen_len"] = np.mean(prediction_lens) return result args = parser.parse_args() ckptr = ocp.PyTreeCheckpointer() model = transformers.FlaxAutoModelForSeq2SeqLM.from_pretrained(args.model) tokenizer = transformers.AutoTokenizer.from_pretrained(args.model) dataset = load_dataset(args.data, split=args.split) config = transformers.AutoConfig.from_pretrained(args.model) # dataset = dataset["test"] dataset = dataset.map( preprocess_function, batched=True, num_proc=mp.cpu_count(), remove_columns=dataset.column_names, desc="Running tokenizer on prediction dataset", ) if args.lora is not None and not args.lora_disabled: lora_spec = LoRASpec( rank=args.rank, rules=args.rules, alpha=args.alpha, tune_vectors=args.tune_vectors, seed=args.seed, disabled=args.lora_disabled, )
# Copyright (C) 2023 Yongchang Hao. 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. parser = argparse.ArgumentParser() parser.add_argument("--model", type=str, default="t5-small") parser.add_argument("--data", type=str, default="xsum") parser.add_argument("--split", type=str, default="test") parser.add_argument("--lora", type=str) parser.add_argument("--rank", type=int, default=8) parser.add_argument( "--rules", type=str, nargs="+", default=["Attention.q", "Attention.k", "Attention.v", "Attention.o"], ) parser.add_argument("--alpha", type=float, default=None) parser.add_argument("--tune-vectors", action="store_true") parser.add_argument("--seed", type=int, default=0) parser.add_argument("--lora-disabled", action="store_true") parser.add_argument("--max-source-length", type=int, default=512) parser.add_argument("--max-target-length", type=int, default=64) parser.add_argument("--batch-size", type=int, default=8) metric = evaluate.load("rouge") def shift_tokens_right( input_ids: np.array, pad_token_id: int, decoder_start_token_id: int ) -> np.ndarray: """ Shift input ids one token to the right. """ shifted_input_ids = np.zeros_like(input_ids) shifted_input_ids[:, 1:] = input_ids[:, :-1] shifted_input_ids[:, 0] = decoder_start_token_id shifted_input_ids = np.where( shifted_input_ids == -100, pad_token_id, shifted_input_ids ) return shifted_input_ids def preprocess_function(examples): inputs = examples["document"] targets = examples["summary"] inputs = ["summarize: " + inp for inp in inputs] model_inputs = tokenizer( inputs, max_length=args.max_source_length, padding="max_length", truncation=True, return_tensors="np", ) # Setup the tokenizer for targets labels = tokenizer( text_target=targets, max_length=args.max_target_length, padding="max_length", truncation=True, return_tensors="np", ) model_inputs["labels"] = labels["input_ids"] decoder_input_ids = shift_tokens_right( labels["input_ids"], config.pad_token_id, config.decoder_start_token_id, ) model_inputs["decoder_input_ids"] = np.asarray(decoder_input_ids) # We need decoder_attention_mask so we can ignore pad tokens from loss model_inputs["decoder_attention_mask"] = labels["attention_mask"] return model_inputs def postprocess_text(preds, labels): preds = [pred.strip() for pred in preds] labels = [label.strip() for label in labels] # rougeLSum expects newline after each sentence preds = ["\n".join(nltk.sent_tokenize(pred)) for pred in preds] labels = ["\n".join(nltk.sent_tokenize(label)) for label in labels] return preds, labels def compute_metrics(preds, labels): decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True) decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True) # Some simple post-processing decoded_preds, decoded_labels = postprocess_text( decoded_preds, decoded_labels ) result = metric.compute( predictions=decoded_preds, references=decoded_labels, use_stemmer=True, ) result = {k: round(v * 100, 4) for k, v in result.items()} prediction_lens = [ np.count_nonzero(pred != tokenizer.pad_token_id) for pred in preds ] result["gen_len"] = np.mean(prediction_lens) return result args = parser.parse_args() ckptr = ocp.PyTreeCheckpointer() model = transformers.FlaxAutoModelForSeq2SeqLM.from_pretrained(args.model) tokenizer = transformers.AutoTokenizer.from_pretrained(args.model) dataset = load_dataset(args.data, split=args.split) config = transformers.AutoConfig.from_pretrained(args.model) # dataset = dataset["test"] dataset = dataset.map( preprocess_function, batched=True, num_proc=mp.cpu_count(), remove_columns=dataset.column_names, desc="Running tokenizer on prediction dataset", ) if args.lora is not None and not args.lora_disabled: lora_spec = LoRASpec( rank=args.rank, rules=args.rules, alpha=args.alpha, tune_vectors=args.tune_vectors, seed=args.seed, disabled=args.lora_disabled, )
trainable, _, merge_fn = lora_init(lora_spec, model)
1
2023-11-07 14:13:49+00:00
4k
pauloxnet/generatedfields
samples/tests.py
[ { "identifier": "Circle", "path": "samples/models.py", "snippet": "class Circle(models.Model):\n radius = models.FloatField()\n area = models.GeneratedField(\n expression=Round(\n Power(\"radius\", 2) * Pi(),\n precision=2,\n ),\n output_field=models.FloatField(),\n db_persist=True,\n )\n\n def __str__(self):\n return f\"{self.radius}²×π={self.area}\"" }, { "identifier": "Event", "path": "samples/models.py", "snippet": "class Event(models.Model):\n start = models.DateTimeField()\n start_date = models.GeneratedField(\n expression=TruncDate(\"start\"),\n output_field=models.DateField(),\n db_persist=True,\n )\n end = models.DateTimeField(null=True)\n end_date = models.GeneratedField(\n expression=TruncDate(\"end\"),\n output_field=models.DateField(),\n db_persist=True,\n )\n duration = models.GeneratedField(\n expression=F(\"end\") - F(\"start\"),\n output_field=models.DurationField(),\n db_persist=True,\n )\n\n def __str__(self):\n return f\"[{self.duration or '∞'}] {self.start_date}…{self.end_date or ''}\"" }, { "identifier": "Item", "path": "samples/models.py", "snippet": "class Item(models.Model):\n price = models.DecimalField(max_digits=6, decimal_places=2)\n quantity = models.PositiveSmallIntegerField(db_default=Value(1))\n total_price = models.GeneratedField(\n expression=F(\"price\") * F(\"quantity\"),\n output_field=models.DecimalField(max_digits=11, decimal_places=2),\n db_persist=True,\n )\n\n def __str__(self):\n return f\"{self.price}×{self.quantity}={self.total_price}\"" }, { "identifier": "Order", "path": "samples/models.py", "snippet": "class Order(models.Model):\n creation = models.DateTimeField()\n payment = models.DateTimeField(null=True)\n status = models.GeneratedField(\n expression=Case(\n When(\n payment__isnull=False,\n then=Value(\"paid\"),\n ),\n default=Value(\"created\"),\n ),\n output_field=models.TextField(),\n db_persist=True,\n )\n\n def __str__(self):\n return f\"[{self.status}] {self.payment or self.creation}\"" }, { "identifier": "Package", "path": "samples/models.py", "snippet": "class Package(models.Model):\n slug = models.SlugField()\n data = models.JSONField()\n version = models.GeneratedField(\n expression=F(\"data__info__version\"),\n output_field=models.TextField(),\n db_persist=True,\n )\n\n def __str__(self):\n return f\"{self.slug} {self.version}\"" }, { "identifier": "Rectangle", "path": "samples/models.py", "snippet": "class Rectangle(models.Model):\n base = models.FloatField()\n height = models.FloatField()\n area = models.GeneratedField(\n expression=F(\"base\") * F(\"height\"),\n output_field=models.FloatField(),\n db_persist=True,\n )\n\n def __str__(self):\n return f\"{self.base}×{self.height}={self.area}\"" }, { "identifier": "RightTriangle", "path": "samples/models.py", "snippet": "class RightTriangle(models.Model):\n hypotenuse = models.FloatField()\n angle = models.FloatField()\n area = models.GeneratedField(\n expression=Round(\n (Power(\"hypotenuse\", 2) * Sin(Radians(\"angle\")) * Cos(Radians(\"angle\")))\n / 2,\n precision=2,\n ),\n output_field=models.FloatField(),\n db_persist=True,\n )\n\n def __str__(self):\n return f\"{self.hypotenuse}²×sin({self.angle}°)×cos({self.angle}°)÷2={self.area}\"" }, { "identifier": "Square", "path": "samples/models.py", "snippet": "class Square(models.Model):\n side = models.FloatField()\n area = models.GeneratedField(\n expression=Power(\"side\", 2),\n output_field=models.FloatField(),\n db_persist=True,\n )\n\n def __str__(self):\n return f\"{self.side}²={self.area}\"" }, { "identifier": "User", "path": "samples/models.py", "snippet": "class User(models.Model):\n first_name = models.CharField(max_length=150)\n last_name = models.CharField(max_length=150)\n full_name = models.GeneratedField(\n expression=Concat(\"first_name\", Value(\" \"), \"last_name\"),\n output_field=models.TextField(),\n db_persist=True,\n )\n\n def __str__(self):\n return self.full_name" } ]
from django.test import TestCase from samples.models import ( Circle, Event, Item, Order, Package, Rectangle, RightTriangle, Square, User, )
1,731
class RectangleTestCase(TestCase): @classmethod def setUpTestData(cls): cls.rectangle = Rectangle.objects.create(base=6, height=7) def test_str(self): self.assertEqual(str(self.rectangle), "6×7=42.0") class SquareTestCase(TestCase): @classmethod def setUpTestData(cls): cls.square = Square.objects.create(side=3) def test_str(self): self.assertEqual(str(self.square), "3²=9.0") class CircleTestCase(TestCase): @classmethod def setUpTestData(cls): cls.circle = Circle.objects.create(radius=3.1415) def test_str(self): self.assertEqual(str(self.circle), "3.1415²×π=31.0") class RightTriangleTestCase(TestCase): @classmethod def setUpTestData(cls): cls.righttriangle = RightTriangle.objects.create(hypotenuse=5, angle=45) def test_str(self): self.assertEqual(str(self.righttriangle), "5²×sin(45°)×cos(45°)÷2=6.25") class ItemTestCase(TestCase): @classmethod def setUpTestData(cls): cls.single_item = Item.objects.create(price=9.99) cls.multiple_item = Item.objects.create(price=4.99, quantity=2) def test_str(self): self.assertEqual(str(self.single_item), "9.99×1=9.99") self.assertEqual(str(self.multiple_item), "4.99×2=9.98") class OrderTestCase(TestCase): @classmethod def setUpTestData(cls): cls.createdorder = Order.objects.create(creation="2023-01-01 12:00Z") cls.paidorder = Order.objects.create( creation="2023-01-02 00:00Z", payment="2023-01-03 06:30Z", ) def test_str(self): self.assertEqual(str(self.createdorder), "[created] 2023-01-01 12:00Z") self.assertEqual(str(self.paidorder), "[paid] 2023-01-03 06:30Z") class EventTestCase(TestCase): @classmethod def setUpTestData(cls): cls.startevent = Event.objects.create(start="2023-1-1 12:00Z") cls.endevent = Event.objects.create( start="2023-1-1 11:45Z", end="2023-1-9 00:00Z" ) def test_str(self): self.assertEqual(str(self.startevent), "[∞] 2023-01-01…") self.assertEqual(str(self.endevent), "[7 days, 12:15:00] 2023-01-01…2023-01-09") class PackageTestCase(TestCase): @classmethod def setUpTestData(cls): cls.package = Package.objects.create( slug="django", data={"info": {"version": "4.2.7"}} ) def test_str(self): self.assertEqual(str(self.package), "django 4.2.7") class UserTestCase(TestCase): @classmethod def setUpTestData(cls):
class RectangleTestCase(TestCase): @classmethod def setUpTestData(cls): cls.rectangle = Rectangle.objects.create(base=6, height=7) def test_str(self): self.assertEqual(str(self.rectangle), "6×7=42.0") class SquareTestCase(TestCase): @classmethod def setUpTestData(cls): cls.square = Square.objects.create(side=3) def test_str(self): self.assertEqual(str(self.square), "3²=9.0") class CircleTestCase(TestCase): @classmethod def setUpTestData(cls): cls.circle = Circle.objects.create(radius=3.1415) def test_str(self): self.assertEqual(str(self.circle), "3.1415²×π=31.0") class RightTriangleTestCase(TestCase): @classmethod def setUpTestData(cls): cls.righttriangle = RightTriangle.objects.create(hypotenuse=5, angle=45) def test_str(self): self.assertEqual(str(self.righttriangle), "5²×sin(45°)×cos(45°)÷2=6.25") class ItemTestCase(TestCase): @classmethod def setUpTestData(cls): cls.single_item = Item.objects.create(price=9.99) cls.multiple_item = Item.objects.create(price=4.99, quantity=2) def test_str(self): self.assertEqual(str(self.single_item), "9.99×1=9.99") self.assertEqual(str(self.multiple_item), "4.99×2=9.98") class OrderTestCase(TestCase): @classmethod def setUpTestData(cls): cls.createdorder = Order.objects.create(creation="2023-01-01 12:00Z") cls.paidorder = Order.objects.create( creation="2023-01-02 00:00Z", payment="2023-01-03 06:30Z", ) def test_str(self): self.assertEqual(str(self.createdorder), "[created] 2023-01-01 12:00Z") self.assertEqual(str(self.paidorder), "[paid] 2023-01-03 06:30Z") class EventTestCase(TestCase): @classmethod def setUpTestData(cls): cls.startevent = Event.objects.create(start="2023-1-1 12:00Z") cls.endevent = Event.objects.create( start="2023-1-1 11:45Z", end="2023-1-9 00:00Z" ) def test_str(self): self.assertEqual(str(self.startevent), "[∞] 2023-01-01…") self.assertEqual(str(self.endevent), "[7 days, 12:15:00] 2023-01-01…2023-01-09") class PackageTestCase(TestCase): @classmethod def setUpTestData(cls): cls.package = Package.objects.create( slug="django", data={"info": {"version": "4.2.7"}} ) def test_str(self): self.assertEqual(str(self.package), "django 4.2.7") class UserTestCase(TestCase): @classmethod def setUpTestData(cls):
cls.user = User.objects.create(first_name="Jane", last_name="Doe")
8
2023-11-07 17:06:11+00:00
4k
akhilravidas/stack-sparrow
sparrow/assistant/run.py
[ { "identifier": "actions", "path": "sparrow/assistant/actions.py", "snippet": "class FileReviewComments(BaseModel):\nclass FileReviewResult(BaseModel):\n def new(cls, json_input: str) -> FileReviewResult:" }, { "identifier": "BaseReview", "path": "sparrow/assistant/review.py", "snippet": "class BaseReview(Protocol):\n \"\"\"\n Base Review Protocol\n \"\"\"\n\n def current_file_contents(self, path: str) -> Optional[str]:\n \"\"\"Read the current file contents which includes the changes made by this review\"\"\"\n ...\n\n def previous_file_contents(self, path: str) -> Optional[str]:\n \"\"\"Read a file contents before the changes made by this review\"\"\"\n ...\n\n @property\n def diff_by_file(\n self,\n ) -> Iterator[Tuple[str, Optional[str], List[Tuple[int, int]]]]:\n \"\"\"\n Returns a generator of (file_path, unified_diff, list of (hunk_start_line and hunk_end_line)) tuples\n \"\"\"\n ...\n\n @property\n def root_dir(self) -> str:\n \"\"\"Returns the root directory of the review\"\"\"\n ..." }, { "identifier": "ReviewFile", "path": "sparrow/assistant/review.py", "snippet": "class ReviewFile:\n \"\"\"\n Wrapper for a single LLM call\n \"\"\"\n\n file_path: str\n message: str\n status: Literal[\"needs_review\", \"skipped\"]\n input_tokens: int\n skipped_reason: Optional[str] = None" }, { "identifier": "ReviewPlan", "path": "sparrow/assistant/review.py", "snippet": "class ReviewPlan:\n \"\"\"\n Review broken down into individual review steps.\n\n Includes other metrics like estimated cost and input/output token counts computed during\n `plan` in case user confirmation is needed.\n \"\"\"\n\n files: List[ReviewFile] = field(default_factory=list)\n estimated_cost: float = 0\n input_tokens: int = 0\n estimated_output_tokens: int = 0\n\n def add_file(self, file: ReviewFile, in_tokens: int, est_out_tokens: int) -> None:\n \"\"\"\n Add a review step to the plan.\n \"\"\"\n self.files.append(file)\n self.input_tokens += in_tokens\n self.estimated_output_tokens += est_out_tokens" }, { "identifier": "config", "path": "sparrow/libs/config.py", "snippet": "EXCLUDED_EXTENSIONS = (\".lock\", \".yaml\", \".toml\", \".json\", \".md\", \".txt\")\ndef is_excluded(path: str):\ndef app_data_root() -> Path:\ndef config_path() -> Path:\n def instance(cls) -> AppConfig:\n def save(self):\n def model_name(self) -> str:\ndef get() -> AppConfig:\nclass AppConfig:" }, { "identifier": "constants", "path": "sparrow/libs/constants.py", "snippet": "PACKAGE_NAME = \"stack-sparrow\"\nMODEL_COSTS: Dict[str, ModelCost] = {\n \"gpt-4-1106-preview\": ModelCost(\n block_size=1000, input_cost_per_block=0.01, output_cost_per_block=0.03\n ),\n}\nMAX_TOKENS_PER_REVIEW = 20 * 1000 # 20K for high signal\nSENTRY_DSN = \"https://d57c1dcbafc96c6c28e233af853ac991@o4506171527266304.ingest.sentry.io/4506171531132928\"" }, { "identifier": "llm", "path": "sparrow/libs/llm.py", "snippet": "def num_tokens(prompt: str) -> int:\ndef cost(input_tokens: int, estimated_output_tokens: int) -> float:" }, { "identifier": "scm", "path": "sparrow/libs/scm.py", "snippet": "def get_repo(repo_path: Optional[str] = None) -> Optional[git.Repo]:\ndef maybe_commit_rev(\n commit_hash: str, repo_path: Optional[str]\n) -> Optional[git.Commit]:\ndef patch_set(\n repo: git.Repo, head_commit_rev: str, base_commit_rev: Optional[str]\n) -> unidiff.PatchSet:\ndef file_is_binary(file_path, check_bytes=8000):" }, { "identifier": "strings", "path": "sparrow/libs/strings.py", "snippet": "MAX_PADDING = 5\ndef annotated_file_contents(\n content: str, changed_line_ranges: List[Tuple[int, int]], start: int = 1\n) -> str:\n def is_changed_line(line_number):" } ]
import json import logging import os import time import pydantic from functools import lru_cache from typing import List, Optional, Tuple from openai import OpenAI from openai.types.beta.threads import Run from rich import print # pylint: disable=redefined-builtin from rich.progress import Progress, SpinnerColumn, TextColumn from sparrow.assistant import actions from sparrow.assistant.review import BaseReview, ReviewFile, ReviewPlan from sparrow.libs import config, constants, llm, scm, strings
2,000
def _client() -> OpenAI: return OpenAI(api_key=config.AppConfig.instance().openai_token) @lru_cache(maxsize=None) def _assistant_id() -> str: cfg = config.AppConfig.instance() if not cfg.assistant_id: client = _client() # TODO: Should this be a different assistant / repo? # (11/6): No - use threads / review request instead. assistant = client.beta.assistants.create( name="Stack Sparrow", model=config.AppConfig.instance().model_name, instructions=ASSISTANT_INSTRUCTIONS, tools=[actions.review_tool], ) cfg.assistant_id = assistant.id cfg.save() return cfg.assistant_id SINGLE_MESSAGE = """ File Path: {file_path} File Contents (annotated): ``` {file_contents_with_line_numbers} ``` """ MAX_WAIT_SECONDS = 120 SLEEP_DURATION_SECONDS = 5 MAX_RETRIES = int(MAX_WAIT_SECONDS / SLEEP_DURATION_SECONDS) # approx def wait_for_run_completion(client: OpenAI, run: Run) -> Optional[Run]: """ Wait for a single review thread to complete. """ with Progress( SpinnerColumn(), TextColumn("[progress.description]{task.description}"), transient=True, ) as progress: progress.add_task(description="Reviewing...", total=None) for _ in range(0, MAX_RETRIES): time.sleep(SLEEP_DURATION_SECONDS) run = client.beta.threads.runs.retrieve( thread_id=run.thread_id, run_id=run.id ) if run.status not in ("queued", "in_progress"): return run print("Timed out waiting for review chunk to complete") def execute_code_review(plan: ReviewPlan) -> List[actions.FileReviewResult]: """ Run code review """ client = _client() review_chunks = [] current_chunk = [] review_tokens = 0 for step in plan.files: if step.status == "skipped": continue if review_tokens + step.input_tokens > constants.MAX_TOKENS_PER_REVIEW: review_chunks.append(current_chunk) current_chunk = [] review_tokens = 0 else: review_tokens += step.input_tokens current_chunk.append(step.message) if current_chunk: review_chunks.append(current_chunk) total_chunks = len(review_chunks) results = [] for idx, chunk in enumerate(review_chunks): print(f"Starting review... [{idx + 1}/{total_chunks}]") run = client.beta.threads.create_and_run( assistant_id=_assistant_id(), thread={ "messages": [ { "role": "user", "content": REVIEW_THREAD_INSTRUCTIONS, "file_ids": [], }, *[{"role": "user", "content": msg} for msg in chunk], ], }, ) chunk_result = wait_for_run_completion(client, run) if chunk_result: results.extend(_deserialize_review_response(chunk_result)) return results def _deserialize_review_response(response: Run) -> List[actions.FileReviewResult]: res = [] if response.status in ("requires_action", "completed") and response.required_action: tool_calls = response.required_action.submit_tool_outputs.tool_calls for call in tool_calls: try: res.append( actions.FileReviewResult.model_validate_json( call.function.arguments ) ) except (json.JSONDecodeError, pydantic.ValidationError): print("Failed to deserialize response") print(response) return res
""" OpenAI assistant """ ASSISTANT_INSTRUCTIONS = """ You an an assistant that helps with DevOps tasks. You review code, help with adding documentation etc.. """.strip() REVIEW_THREAD_INSTRUCTIONS = """ Each message in this thread represents changes made to a file in the patch set. The first line is the file path. The subsequent lines contains the file contents annotated with line numbers. Only the lines that start with an asterisk were updated. IMPORTANT: - Review code and flag substantive issues for updated code (lines marked with an asterisk). - Only reject if you are sure that there is an underlying issue with the code. - Do not flag formatting or style issues. """.strip() @lru_cache(maxsize=None) def _client() -> OpenAI: return OpenAI(api_key=config.AppConfig.instance().openai_token) @lru_cache(maxsize=None) def _assistant_id() -> str: cfg = config.AppConfig.instance() if not cfg.assistant_id: client = _client() # TODO: Should this be a different assistant / repo? # (11/6): No - use threads / review request instead. assistant = client.beta.assistants.create( name="Stack Sparrow", model=config.AppConfig.instance().model_name, instructions=ASSISTANT_INSTRUCTIONS, tools=[actions.review_tool], ) cfg.assistant_id = assistant.id cfg.save() return cfg.assistant_id SINGLE_MESSAGE = """ File Path: {file_path} File Contents (annotated): ``` {file_contents_with_line_numbers} ``` """ MAX_WAIT_SECONDS = 120 SLEEP_DURATION_SECONDS = 5 MAX_RETRIES = int(MAX_WAIT_SECONDS / SLEEP_DURATION_SECONDS) # approx def wait_for_run_completion(client: OpenAI, run: Run) -> Optional[Run]: """ Wait for a single review thread to complete. """ with Progress( SpinnerColumn(), TextColumn("[progress.description]{task.description}"), transient=True, ) as progress: progress.add_task(description="Reviewing...", total=None) for _ in range(0, MAX_RETRIES): time.sleep(SLEEP_DURATION_SECONDS) run = client.beta.threads.runs.retrieve( thread_id=run.thread_id, run_id=run.id ) if run.status not in ("queued", "in_progress"): return run print("Timed out waiting for review chunk to complete") def execute_code_review(plan: ReviewPlan) -> List[actions.FileReviewResult]: """ Run code review """ client = _client() review_chunks = [] current_chunk = [] review_tokens = 0 for step in plan.files: if step.status == "skipped": continue if review_tokens + step.input_tokens > constants.MAX_TOKENS_PER_REVIEW: review_chunks.append(current_chunk) current_chunk = [] review_tokens = 0 else: review_tokens += step.input_tokens current_chunk.append(step.message) if current_chunk: review_chunks.append(current_chunk) total_chunks = len(review_chunks) results = [] for idx, chunk in enumerate(review_chunks): print(f"Starting review... [{idx + 1}/{total_chunks}]") run = client.beta.threads.create_and_run( assistant_id=_assistant_id(), thread={ "messages": [ { "role": "user", "content": REVIEW_THREAD_INSTRUCTIONS, "file_ids": [], }, *[{"role": "user", "content": msg} for msg in chunk], ], }, ) chunk_result = wait_for_run_completion(client, run) if chunk_result: results.extend(_deserialize_review_response(chunk_result)) return results def _deserialize_review_response(response: Run) -> List[actions.FileReviewResult]: res = [] if response.status in ("requires_action", "completed") and response.required_action: tool_calls = response.required_action.submit_tool_outputs.tool_calls for call in tool_calls: try: res.append( actions.FileReviewResult.model_validate_json( call.function.arguments ) ) except (json.JSONDecodeError, pydantic.ValidationError): print("Failed to deserialize response") print(response) return res
def plan_code_review(revu: BaseReview) -> ReviewPlan:
1
2023-11-07 00:55:26+00:00
4k
som-shahlab/INSPECT_public
image/radfusion3/data/dataset_2d.py
[ { "identifier": "DatasetBase", "path": "image/radfusion3/data/dataset_base.py", "snippet": "class DatasetBase(Dataset):\n def __init__(self, cfg, split=\"train\", transform=None):\n self.cfg = cfg\n self.transform = transform\n self.split = split\n self.hdf5_dataset = None\n\n path = \"/share/pi/nigam/projects/zphuo/data/omop_extract_PHI/som-nero-phi-nigam-starr.frazier/dict_slice_thickness.pkl\"\n self.dict_slice_thickness = pickle.load(open(path, \"rb\"))\n\n def __getitem__(self, index):\n raise NotImplementedError\n\n def __len__(self):\n raise NotImplementedError\n\n def read_from_hdf5(self, key, hdf5_path, slice_idx=None):\n if self.hdf5_dataset is None:\n self.hdf5_dataset = h5py.File(hdf5_path, \"r\")\n\n if slice_idx is None:\n arr = self.hdf5_dataset[key][:]\n else:\n arr = self.hdf5_dataset[key][slice_idx]\n\n # df_dicom_headers[\"patient_datetime\"] = df_dicom_headers.apply(\n # lambda x: f\"{x.PatientID}_{x.StudyTime}\", axis=1\n # )\n\n # only add slice thickness to stanford data\n if \"rsna\" not in self.cfg.dataset.csv_path:\n thickness_ls = []\n for idx_th in range(arr.shape[0]):\n try:\n thickness_ls.append(self.dict_slice_thickness[key] * idx_th)\n except:\n print(\n key,\n idx_th,\n \"=========no thickness info=============================\",\n )\n thickness_ls.append(0)\n thickness_ls = np.array(thickness_ls)\n arr = np.concatenate([arr, thickness_ls[:, None]], axis=1)\n elif \"rsna\" in self.cfg.dataset.csv_path:\n thickness_ls = []\n for idx_th in range(arr.shape[0]):\n try:\n thickness_ls.append(self.dict_slice_thickness[key] * idx_th)\n except:\n thickness_ls.append(0)\n thickness_ls = np.array(thickness_ls)\n arr = np.concatenate([arr, thickness_ls[:, None]], axis=1)\n\n return arr\n\n def read_dicom(self, file_path: str, resize_size=None, channels=None):\n if resize_size is None:\n resize_size = self.cfg.dataset.transform.resize_size\n if channels is None:\n channels = self.cfg.dataset.transform.channels\n\n # read dicom\n if \"rsna\" in self.cfg.dataset.csv_path:\n dcm = pydicom.dcmread(file_path)\n else:\n patient_id = file_path.split(\"/\")[-1].split(\"_\")[0]\n tar_content = read_tar_dicom(\n os.path.join(self.cfg.dataset.dicom_dir, patient_id + \".tar\")\n )\n dcm = pydicom.dcmread(io.BytesIO(tar_content[file_path]))\n\n try:\n pixel_array = dcm.pixel_array\n except:\n print(file_path)\n if channels == \"repeat\":\n pixel_array = np.zeros((resize_size, resize_size))\n else:\n pixel_array = np.zeros((3, resize_size, resize_size))\n\n # rescale\n try:\n intercept = dcm.RescaleIntercept\n slope = dcm.RescaleSlope\n except:\n intercept = 0\n slope = 1\n\n pixel_array = pixel_array * slope + intercept\n\n # resize\n if resize_size != pixel_array.shape[-1]:\n pixel_array = cv2.resize(\n pixel_array, (resize_size, resize_size), interpolation=cv2.INTER_AREA\n )\n\n return pixel_array\n\n def windowing(self, pixel_array: np.array, window_center: int, window_width: int):\n lower = window_center - window_width // 2\n upper = window_center + window_width // 2\n pixel_array = np.clip(pixel_array.copy(), lower, upper)\n pixel_array = (pixel_array - lower) / (upper - lower)\n\n return pixel_array\n\n def process_numpy(self, numpy_path, idx):\n slice_array = np.load(numpy_path)[idx]\n\n resize_size = self.cfg.dataset.transform.resize_size\n channels = self.cfg.dataset.transform.channels\n\n if resize_size != slice_array.shape[-1]:\n slice_array = cv2.resize(\n slice_array, (resize_size, resize_size), interpolation=cv2.INTER_AREA\n )\n\n # window\n if self.cfg.dataset.transform.channels == \"repeat\":\n ct_slice = self.windowing(\n slice_array, 400, 1000\n ) # use PE window by default\n # create 3 channels after converting to Tensor\n # using torch.repeat won't take up 3x memory\n else:\n ct_slice = [\n self.windowing(slice_array, -600, 1500), # LUNG window\n self.windowing(slice_array, 400, 1000), # PE window\n self.windowing(slice_array, 40, 400), # MEDIASTINAL window\n ]\n ct_slice = np.stack(ct_slice)\n\n return ct_slice\n\n def process_slice(\n self,\n slice_info: pd.Series = None,\n dicom_dir: Path = None,\n slice_path: str = None,\n ):\n \"\"\"process slice with windowing, resize and tranforms\"\"\"\n\n if slice_path is None:\n slice_path = dicom_dir / slice_info[INSTANCE_PATH_COL]\n slice_array = self.read_dicom(slice_path)\n\n # window\n if self.cfg.dataset.transform.channels == \"repeat\":\n ct_slice = self.windowing(\n slice_array, 400, 1000\n ) # use PE window by default\n # create 3 channels after converting to Tensor\n # using torch.repeat won't take up 3x memory\n else:\n ct_slice = [\n self.windowing(slice_array, -600, 1500), # LUNG window\n self.windowing(slice_array, 400, 1000), # PE window\n self.windowing(slice_array, 40, 400), # MEDIASTINAL window\n ]\n ct_slice = np.stack(ct_slice)\n\n return ct_slice\n\n def fix_slice_number(self, df: pd.DataFrame):\n num_slices = min(self.cfg.dataset.num_slices, df.shape[0])\n if self.cfg.dataset.sample_strategy == \"random\":\n slice_idx = np.random.choice(\n np.arange(df.shape[0]), replace=False, size=num_slices\n )\n slice_idx = list(np.sort(slice_idx))\n df = df.iloc[slice_idx, :]\n elif self.cfg.dataset.sample_strategy == \"fix\":\n df = df.iloc[:num_slices, :]\n else:\n raise Exception(\"Sampling strategy either 'random' or 'fix'\")\n return df\n\n def fix_series_slice_number(self, series):\n num_slices = min(self.cfg.dataset.num_slices, series.shape[0])\n if num_slices == self.cfg.dataset.num_slices:\n if self.cfg.dataset.sample_strategy == \"random\":\n slice_idx = np.random.choice(\n np.arange(series.shape[0]), replace=False, size=num_slices\n )\n slice_idx = list(np.sort(slice_idx))\n features = series[slice_idx, :]\n elif self.cfg.dataset.sample_strategy == \"fix\":\n pad = int((series.shape[0] - num_slices) / 2) # select middle slices\n start = pad\n end = pad + num_slices\n features = series[start:end, :]\n else:\n raise Exception(\"Sampling strategy either 'random' or 'fix'\")\n mask = np.ones(num_slices)\n else:\n mask = np.zeros(self.cfg.dataset.num_slices)\n mask[:num_slices] = 1\n shape = [self.cfg.dataset.num_slices] + list(series.shape[1:])\n features = np.zeros(shape)\n\n features[:num_slices] = series\n\n return features, mask\n\n def fill_series_to_num_slicess(self, series, num_slices):\n x = torch.zeros(()).new_full((num_slices, *series.shape[1:]), 0.0)\n x[: series.shape[0]] = series\n return x" }, { "identifier": "read_tar_dicom", "path": "image/radfusion3/utils.py", "snippet": "def read_tar_dicom(tar_file_path):\n tar_contents = {}\n try:\n # Open the tar file as a binary stream\n with tarfile.open(tar_file_path, \"r\") as tar:\n # Iterate through the files in the tar archive\n for tar_info in tar:\n # Check if the tar entry is a regular file (not a directory or a symlink)\n if tar_info.isfile():\n # Read the content of the file into a variable\n content = tar.extractfile(tar_info).read()\n\n # Store the content in the dictionary with the file name as the key\n tar_contents[tar_info.name] = content\n\n except tarfile.TarError as e:\n print(f\"Error while processing the tar file: {e}\")\n\n return tar_contents" } ]
import torch import numpy as np import pandas as pd import tqdm import os from PIL import Image from pathlib import Path from ..constants import * from .dataset_base import DatasetBase from ..utils import read_tar_dicom
2,595
class Dataset2D(DatasetBase): def __init__(self, cfg, split="train", transform=None): super().__init__(cfg, split) self.transform = transform self.cfg = cfg self.df = pd.read_csv(cfg.dataset.csv_path) # match dicom datetime format self.df["procedure_time"] = self.df["procedure_time"].apply( lambda x: x.replace("T", " ") ) # get unique patient_datetime id by combining patient id and datetime self.df["patient_datetime"] = self.df.apply( lambda x: f"{x.patient_id}_{x.procedure_time}", axis=1 ) if self.split != "all": self.df = self.df[self.df["split"] == self.split] if self.split == "train": if cfg.dataset.sample_frac < 1.0: num_pdt = list(self.df["patient_datetime"].unique()) num_sample = int(num_pdt * cfg.dataset.sample_frac) sampled_pdt = np.random.choice(num_pdt, num_sample, replace=False) self.df = self.df[self.df["patient_datetime"].isin(sampled_pdt)] # get all dicom files for a study self.all_instances = [] for idx, row in tqdm.tqdm(self.df.iterrows(), total=len(self.df)): # # glob all paths # study_path = ( # Path(self.cfg.dataset.dicom_dir) # / str(row["patient_id"]) # / str(row["procedure_time"]) # ) # slice_paths = study_path.glob("*.dcm")
class Dataset2D(DatasetBase): def __init__(self, cfg, split="train", transform=None): super().__init__(cfg, split) self.transform = transform self.cfg = cfg self.df = pd.read_csv(cfg.dataset.csv_path) # match dicom datetime format self.df["procedure_time"] = self.df["procedure_time"].apply( lambda x: x.replace("T", " ") ) # get unique patient_datetime id by combining patient id and datetime self.df["patient_datetime"] = self.df.apply( lambda x: f"{x.patient_id}_{x.procedure_time}", axis=1 ) if self.split != "all": self.df = self.df[self.df["split"] == self.split] if self.split == "train": if cfg.dataset.sample_frac < 1.0: num_pdt = list(self.df["patient_datetime"].unique()) num_sample = int(num_pdt * cfg.dataset.sample_frac) sampled_pdt = np.random.choice(num_pdt, num_sample, replace=False) self.df = self.df[self.df["patient_datetime"].isin(sampled_pdt)] # get all dicom files for a study self.all_instances = [] for idx, row in tqdm.tqdm(self.df.iterrows(), total=len(self.df)): # # glob all paths # study_path = ( # Path(self.cfg.dataset.dicom_dir) # / str(row["patient_id"]) # / str(row["procedure_time"]) # ) # slice_paths = study_path.glob("*.dcm")
tar_content = read_tar_dicom(
1
2023-11-06 21:17:03+00:00
4k
JakubPluta/gymhero
tests/integration/conftest.py
[ { "identifier": "get_settings", "path": "gymhero/config.py", "snippet": "def get_settings(env: str = \"dev\") -> Settings:\n \"\"\"\n Return the settings object based on the environment.\n\n Parameters:\n env (str): The environment to retrieve the settings for. Defaults to \"dev\".\n\n Returns:\n Settings: The settings object based on the environment.\n\n Raises:\n ValueError: If the environment is invalid.\n \"\"\"\n log.debug(\"getting settings for env: %s\", env)\n\n if env.lower() in [\"dev\", \"d\", \"development\"]:\n return ContainerDevSettings()\n if env.lower() in [\"test\", \"t\", \"testing\"]:\n return ContainerTestSettings()\n if env.lower() in [\"local\", \"l\"]:\n return LocalDevSettings()\n\n raise ValueError(\"Invalid environment. Must be 'dev' or 'test' ,'local'.\")" }, { "identifier": "get_logger", "path": "gymhero/log.py", "snippet": "def get_logger(\n name: Optional[str] = None, level: DebugLevelType = \"DEBUG\"\n) -> logging.Logger:\n \"\"\"\n Creates and configures a logger for logging messages.\n\n Parameters:\n name (Optional[str]): The name of the logger. Defaults to None.\n level (DebugLevel): The logging level. Defaults to DebugLevel.DEBUG.\n\n Returns:\n logging.Logger: The configured logger object.\n \"\"\"\n logger = logging.getLogger(name=name)\n handler = logging.StreamHandler(sys.stdout)\n formatter = logging.Formatter(LOGGING_FORMATTER)\n handler.setFormatter(formatter)\n logger.addHandler(handler)\n\n if not level or level not in DebugLevels:\n logger.warning(\n \"Invalid logging level %s. Setting logging level to DEBUG.\", level\n )\n level = \"DEBUG\"\n\n logger.setLevel(level=level)\n return logger" }, { "identifier": "app", "path": "gymhero/main.py", "snippet": "def root():" }, { "identifier": "Base", "path": "gymhero/models/exercise.py", "snippet": "class Exercise(Base):\nclass ExerciseType(Base):\n def __repr__(self):\n def __repr__(self):" }, { "identifier": "create_access_token", "path": "gymhero/security.py", "snippet": "def create_access_token(\n subject: Union[str, Any], expires_delta: timedelta = None\n) -> str:\n \"\"\"\n Creates an access token.\n\n Parameters:\n subject (Union[str, Any]): The subject for which the access token is created.\n expires_delta (timedelta, optional): The expiration time for the access token. Defaults to None.\n\n Returns:\n str: The encoded access token.\n \"\"\"\n if expires_delta:\n expire = datetime.utcnow() + expires_delta\n else:\n expire = datetime.utcnow() + timedelta(\n minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES\n )\n to_encode = {\"exp\": expire, \"sub\": str(subject)}\n encoded_jwt = jwt.encode(\n to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM\n )\n return encoded_jwt" }, { "identifier": "seed_database", "path": "scripts/core/_initdb.py", "snippet": "def seed_database(env, limit=None):\n \"\"\"\n Seed the database with initial data.\n\n Parameters:\n env (str): The environment in which the database is being seeded.\n \"\"\"\n\n settings = get_settings(env)\n database_url = build_sqlalchemy_database_url_from_settings(settings)\n get_db = partial(get_ctx_db, database_url=database_url)\n log.info(\"Seeding database %s\", str(database_url.split(\"@\")[-1]))\n log.info(\"Seeding database %s\", str(database_url))\n exercise_path = os.path.join(RESOURCE_DIR_PATH, \"exercises.csv\")\n df = pd.read_csv(exercise_path, header=0, index_col=0)\n df.replace({\"\": None, \"nan\": None, \"N/A\": None, np.nan: None}, inplace=True)\n df.drop_duplicates(subset=[\"Title\"], keep=\"first\", inplace=True)\n\n exercise_types: list[str] = _get_unique_values(df, \"Type\")\n body_parts: list[str] = _get_unique_values(df, \"BodyPart\")\n levels: list[str] = _get_unique_values(df, \"Level\")\n\n log.debug(\"Exercise types: %s\", exercise_types)\n log.debug(\"Body parts: %s\", body_parts)\n log.debug(\"Levels: %s\", levels)\n\n with get_db() as session:\n superuser = create_first_superuser(session)\n superuser_id = superuser.id\n\n with get_db() as session:\n levels_dict = {\n level.name: level.id for level in create_initial_levels(session, levels)\n }\n\n with get_db() as session:\n bodyparts_dict = {\n bodypart.name: bodypart.id\n for bodypart in create_initial_body_parts(session, body_parts)\n }\n\n with get_db() as session:\n exercise_types_dict = {\n exercise_type.name: exercise_type.id\n for exercise_type in create_initial_exercise_types(session, exercise_types)\n }\n\n log.debug(\"Levels: %s\", levels_dict)\n log.debug(\"BodyParts: %s\", bodyparts_dict)\n log.debug(\"ExerciseTypes: %s\", exercise_types_dict)\n\n if limit:\n df = df.head(limit)\n\n with get_db() as session:\n create_initial_exercises(\n session, df, bodyparts_dict, levels_dict, exercise_types_dict, superuser_id\n )\n log.debug(\"Exercises seeded\")" }, { "identifier": "_create_first_user", "path": "scripts/core/utils.py", "snippet": "def _create_first_user(\n db: Session,\n email: str,\n password: str,\n username: Optional[str] = None,\n is_superuser: bool = True,\n is_active: bool = True,\n) -> User:\n \"\"\"Create first user\"\"\"\n user = user_crud.get_user_by_email(db, email=email)\n if user:\n log.debug(\"First user already exists\")\n return user\n\n user_in = UserInDB(\n email=email,\n hashed_password=get_password_hash(password),\n full_name=username,\n is_superuser=is_superuser,\n is_active=is_active,\n )\n user = user_crud.create(db, obj_create=user_in)\n log.debug(\"Created first user: %s\", user)\n return user" }, { "identifier": "_get_unique_values", "path": "scripts/core/utils.py", "snippet": "def _get_unique_values(dataframe: pd.DataFrame, col: str) -> list:\n \"\"\"\n Get the unique values from a specific column in a pandas DataFrame.\n\n Parameters:\n dataframe (pd.DataFrame): The pandas DataFrame from which\n to retrieve the unique values.\n col (str): The name of the column from which to retrieve the unique values.\n\n Returns:\n list: A list of unique values from the specified column.\n \"\"\"\n return dataframe[col].unique().tolist()" }, { "identifier": "create_initial_body_parts", "path": "scripts/core/utils.py", "snippet": "def create_initial_body_parts(session: Session, unique_body_parts: list) -> None:\n \"\"\"\n Create initial body parts in the database.\n\n Parameters:\n session (Session): The database session object.\n unique_body_parts (list): A list of unique body parts.\n\n Returns:\n None\n \"\"\"\n\n body_parts = [BodyPart(name=body_part) for body_part in unique_body_parts]\n session.add_all(body_parts)\n session.commit()\n log.debug(\"Created %d initial body parts\", len(body_parts))\n return body_parts" }, { "identifier": "create_initial_exercise_types", "path": "scripts/core/utils.py", "snippet": "def create_initial_exercise_types(\n session: Session, unique_exercise_types: list\n) -> None:\n \"\"\"\n Create initial exercise types in the database.\n\n Parameters:\n session (Session): The database session object.\n unique_exercise_types (list): A list of unique exercise types.\n\n Returns:\n None\n \"\"\"\n\n exercise_types = [\n ExerciseType(name=exercise_type) for exercise_type in unique_exercise_types\n ]\n\n session.add_all(exercise_types)\n session.commit()\n log.debug(\"Created %d initial exercise types\", len(exercise_types))\n return exercise_types" }, { "identifier": "create_initial_levels", "path": "scripts/core/utils.py", "snippet": "def create_initial_levels(session: Session, unique_levels: list) -> None:\n \"\"\"Create initial levels in the database.\n\n Parameters:\n session (Session): The database session.\n unique_levels (list): A list of unique level names.\n\n Returns:\n None\n \"\"\"\n levels = [Level(name=level) for level in unique_levels]\n session.add_all(levels)\n session.commit()\n log.debug(\"Created %d initial levels\", len(levels))\n return levels" }, { "identifier": "load_exercise_resource", "path": "scripts/core/utils.py", "snippet": "def load_exercise_resource() -> DataFrame:\n \"\"\"\n Load exercise resource data from the exercises.csv file.\n\n Returns:\n DataFrame: The loaded exercise resource data.\n \"\"\"\n resource_dir_path: Union[Path, str] = os.path.join(\n Path(os.path.abspath(__file__)).parent.parent.parent, \"resources\"\n )\n df: DataFrame = pd.read_csv(\n os.path.join(resource_dir_path, \"exercises.csv\"),\n header=0,\n index_col=0,\n )\n df.replace(\n {\"\": None, \"nan\": None, \"N/A\": None, np.nan: None},\n inplace=True,\n )\n df.drop_duplicates(subset=[\"Title\"], keep=\"first\", inplace=True)\n return df" }, { "identifier": "engine", "path": "tests/conftest.py", "snippet": "TEST_SQLALCHEMY_DATABASE_URL = (\n f\"postgresql://{_test_settings.POSTGRES_USER}:{_test_settings.POSTGRES_PASSWORD}@\"\n f\"{_test_settings.POSTGRES_HOST}:{_test_settings.POSTGRES_PORT}/{_test_settings.POSTGRES_DB}\"\n)\ndef test_sqlalchemy_database_url():\ndef test_settings():\ndef _engine():\ndef _test_session(_engine):\ndef override_get_db():" } ]
from datetime import timedelta from fastapi.testclient import TestClient from sqlalchemy.orm import sessionmaker from gymhero.config import get_settings from gymhero.log import get_logger from gymhero.main import app from gymhero.models import Base from gymhero.security import create_access_token from scripts.core._initdb import seed_database from scripts.core.utils import ( _create_first_user, _get_unique_values, create_initial_body_parts, create_initial_exercise_types, create_initial_levels, load_exercise_resource, ) from tests.conftest import engine import pytest
2,660
log = get_logger("conftest") @pytest.fixture(scope="function", autouse=True) def setup_and_teardown(): try: Base.metadata.drop_all(bind=engine) log.debug("database dropped") except Exception: pass log.debug("engine url %s", str(engine.url)) log.debug(" gymhero test started ".center(70, "*")) Base.metadata.create_all(bind=engine) yield Base.metadata.drop_all(bind=engine) log.debug(" gymhero test ended ".center(70, "*")) @pytest.fixture def seed_test_database(): seed_database("test", limit=10) @pytest.fixture def test_settings(): return get_settings("test") @pytest.fixture def subject(): return "user123" @pytest.fixture def expires_delta(): return timedelta(minutes=30) @pytest.fixture def get_test_db(_test_session): db = _test_session() try: yield db finally: db.close() @pytest.fixture def test_client(): return TestClient(app) @pytest.fixture def exercises_df(): return load_exercise_resource() @pytest.fixture def initial_levels(exercises_df):
log = get_logger("conftest") @pytest.fixture(scope="function", autouse=True) def setup_and_teardown(): try: Base.metadata.drop_all(bind=engine) log.debug("database dropped") except Exception: pass log.debug("engine url %s", str(engine.url)) log.debug(" gymhero test started ".center(70, "*")) Base.metadata.create_all(bind=engine) yield Base.metadata.drop_all(bind=engine) log.debug(" gymhero test ended ".center(70, "*")) @pytest.fixture def seed_test_database(): seed_database("test", limit=10) @pytest.fixture def test_settings(): return get_settings("test") @pytest.fixture def subject(): return "user123" @pytest.fixture def expires_delta(): return timedelta(minutes=30) @pytest.fixture def get_test_db(_test_session): db = _test_session() try: yield db finally: db.close() @pytest.fixture def test_client(): return TestClient(app) @pytest.fixture def exercises_df(): return load_exercise_resource() @pytest.fixture def initial_levels(exercises_df):
return _get_unique_values(exercises_df, "Level")
7
2023-11-05 14:37:46+00:00
4k
choderalab/chiron
chiron/integrators.py
[ { "identifier": "SamplerState", "path": "chiron/states.py", "snippet": "class SamplerState:\n \"\"\"\n Represents the state of the system that is updated during integration.\n\n Parameters\n ----------\n x0 : unit.Quantity\n The current positions of the particles in the simulation.\n velocities : unit.Quantity, optional\n The velocities of the particles in the simulation.\n box_vectors : unit.Quantity, optional\n The box vectors defining the simulation's periodic boundary conditions.\n\n \"\"\"\n\n def __init__(\n self,\n x0: unit.Quantity,\n velocities: Optional[unit.Quantity] = None,\n box_vectors: Optional[unit.Quantity] = None,\n ) -> None:\n # NOTE: all units are internally in the openMM units system as documented here:\n # http://docs.openmm.org/latest/userguide/theory/01_introduction.html#units\n if not isinstance(x0, unit.Quantity):\n raise TypeError(f\"x0 must be a unit.Quantity, got {type(x0)} instead.\")\n if velocities is not None and not isinstance(velocities, unit.Quantity):\n raise TypeError(\n f\"velocities must be a unit.Quantity, got {type(velocities)} instead.\"\n )\n if box_vectors is not None and not isinstance(box_vectors, unit.Quantity):\n if isinstance(box_vectors, List):\n try:\n box_vectors = self._convert_from_openmm_box(box_vectors)\n except:\n raise TypeError(f\"Unable to parse box_vectors {box_vectors}.\")\n else:\n raise TypeError(\n f\"box_vectors must be a unit.Quantity or openMM box, got {type(box_vectors)} instead.\"\n )\n if not x0.unit.is_compatible(unit.nanometer):\n raise ValueError(f\"x0 must have units of distance, got {x0.unit} instead.\")\n if velocities is not None and not velocities.unit.is_compatible(\n unit.nanometer / unit.picosecond\n ):\n raise ValueError(\n f\"velocities must have units of distance/time, got {velocities.unit} instead.\"\n )\n if box_vectors is not None and not box_vectors.unit.is_compatible(\n unit.nanometer\n ):\n raise ValueError(\n f\"box_vectors must have units of distance, got {box_vectors.unit} instead.\"\n )\n if box_vectors is not None and box_vectors.shape != (3, 3):\n raise ValueError(\n f\"box_vectors must be a 3x3 array, got {box_vectors.shape} instead.\"\n )\n\n self._x0 = x0\n self._velocities = velocities\n self._box_vectors = box_vectors\n self._distance_unit = unit.nanometer\n\n @property\n def x0(self) -> jnp.array:\n return self._convert_to_jnp(self._x0)\n\n @property\n def velocities(self) -> jnp.array:\n if self._velocities is None:\n return None\n return self._convert_to_jnp(self._velocities)\n\n @property\n def box_vectors(self) -> jnp.array:\n if self._box_vectors is None:\n return None\n return self._convert_to_jnp(self._box_vectors)\n\n @x0.setter\n def x0(self, x0: Union[jnp.array, unit.Quantity]) -> None:\n if isinstance(x0, unit.Quantity):\n self._x0 = x0\n else:\n self._x0 = unit.Quantity(x0, self._distance_unit)\n\n @property\n def distance_unit(self) -> unit.Unit:\n return self._distance_unit\n\n def _convert_to_jnp(self, array: unit.Quantity) -> jnp.array:\n \"\"\"\n Convert the sampler state to jnp arrays.\n \"\"\"\n import jax.numpy as jnp\n\n array_ = array.value_in_unit_system(unit.md_unit_system)\n return jnp.array(array_)\n\n def _convert_from_openmm_box(self, openmm_box_vectors: List) -> unit.Quantity:\n box_vec = []\n for i in range(0, 3):\n layer = []\n for j in range(0, 3):\n layer.append(\n openmm_box_vectors[i][j].value_in_unit(openmm_box_vectors[0].unit)\n )\n box_vec.append(layer)\n return unit.Quantity(jnp.array(box_vec), openmm_box_vectors[0].unit)" }, { "identifier": "ThermodynamicState", "path": "chiron/states.py", "snippet": "class ThermodynamicState:\n \"\"\"\n Represents the thermodynamic state of the system.\n\n Parameters\n ----------\n potential : NeuralNetworkPotential\n The potential energy function of the system.\n temperature : unit.Quantity, optional\n The temperature of the simulation.\n volume : unit.Quantity, optional\n The volume of the simulation.\n pressure : unit.Quantity, optional\n The pressure of the simulation.\n\n \"\"\"\n\n def __init__(\n self,\n potential: Optional[NeuralNetworkPotential],\n temperature: Optional[unit.Quantity] = None,\n volume: Optional[unit.Quantity] = None,\n pressure: Optional[unit.Quantity] = None,\n ):\n self.potential = potential\n\n if temperature is not None and not isinstance(temperature, unit.Quantity):\n raise TypeError(\n f\"temperature must be a unit.Quantity, got {type(temperature)} instead.\"\n )\n elif temperature is not None:\n if not temperature.unit.is_compatible(unit.kelvin):\n raise ValueError(\n f\"temperature must have units of temperature, got {temperature.unit} instead.\"\n )\n\n if volume is not None and not isinstance(volume, unit.Quantity):\n raise TypeError(\n f\"volume must be a unit.Quantity, got {type(volume)} instead.\"\n )\n elif volume is not None:\n if not volume.unit.is_compatible(unit.nanometer**3):\n raise ValueError(\n f\"volume must have units of distance**3, got {volume.unit} instead.\"\n )\n if pressure is not None and not isinstance(pressure, unit.Quantity):\n raise TypeError(\n f\"pressure must be a unit.Quantity, got {type(pressure)} instead.\"\n )\n elif pressure is not None:\n if not pressure.unit.is_compatible(unit.atmosphere):\n raise ValueError(\n f\"pressure must have units of pressure, got {pressure.unit} instead.\"\n )\n\n self.temperature = temperature\n if temperature is not None:\n self.beta = 1.0 / (unit.BOLTZMANN_CONSTANT_kB * (self.temperature))\n else:\n self.beta = None\n\n self.volume = volume\n self.pressure = pressure\n\n from .utils import get_nr_of_particles\n\n self.nr_of_particles = get_nr_of_particles(self.potential.topology)\n self._check_completness()\n\n def check_variables(self) -> None:\n \"\"\"\n Check if all necessary variables are set and log the simulation ensemble.\n \"\"\"\n variables = [\n \"temperature\",\n \"volume\",\n \"pressure\",\n ]\n set_variables = [var for var in variables if getattr(self, var) is not None]\n return set_variables\n\n def _check_completness(self):\n # check which variables are set\n set_variables = self.check_variables()\n\n if len(set_variables) == 0:\n log.info(\"No variables are set.\")\n\n # print all set variables\n for var in set_variables:\n log.info(f\"{var} is set.\")\n\n if self.temperature and self.volume and self.nr_of_particles:\n log.info(\"NVT ensemble simulated.\")\n if self.temperature and self.pressure and self.nr_of_particles:\n log.info(\"NpT ensemble is simulated.\")\n\n @classmethod\n def are_states_compatible(cls, state1, state2):\n \"\"\"\n Check if two simulation states are compatible.\n\n This method should define the criteria for compatibility,\n such as matching number of particles, etc.\n\n Parameters\n ----------\n state1 : SimulationState\n The first simulation state to compare.\n state2 : SimulationState\n The second simulation state to compare.\n\n Returns\n -------\n bool\n True if states are compatible, False otherwise.\n \"\"\"\n pass\n\n def get_reduced_potential(\n self, sampler_state: SamplerState, nbr_list=None\n ) -> float:\n \"\"\"\n Compute the reduced potential for the given sampler state.\n\n Parameters\n ----------\n sampler_state : SamplerState\n The sampler state for which to compute the reduced potential.\n nbr_list : NeighborList or PairList, optional\n The neighbor list or pair list routine to use for calculating the reduced potential.\n\n Returns\n -------\n float\n The reduced potential of the system.\n\n Notes\n -----\n The reduced potential is computed as:\n u = \\beta [U(x) + p V(x) + \\mu N(x)],\n where \\beta is the inverse temperature, p is the pressure,\n \\mu is the chemical potential, x are the atomic positions,\n U(x) is the potential energy, V(x) is the box volume,\n and N(x) is the number of particles.\n \"\"\"\n if self.beta is None:\n self.beta = 1.0 / (\n unit.BOLTZMANN_CONSTANT_kB * (self.temperature * unit.kelvin)\n )\n log.debug(f\"sample state: {sampler_state.x0}\")\n reduced_potential = (\n unit.Quantity(\n self.potential.compute_energy(sampler_state.x0, nbr_list),\n unit.kilojoule_per_mole,\n )\n ) / unit.AVOGADRO_CONSTANT_NA\n log.debug(f\"reduced potential: {reduced_potential}\")\n if self.pressure is not None:\n reduced_potential += self.pressure * self.volume\n\n return self.beta * reduced_potential\n\n def kT_to_kJ_per_mol(self, energy):\n energy = energy * unit.AVOGADRO_CONSTANT_NA\n return energy / self.beta" }, { "identifier": "SimulationReporter", "path": "chiron/reporters.py", "snippet": "class SimulationReporter:\n def __init__(self, filename: str, topology: Topology, buffer_size: int = 1):\n \"\"\"\n Initialize the SimulationReporter.\n\n Parameters\n ----------\n filename : str\n Name of the HDF5 file to write the simulation data.\n topology: openmm.Topology\n buffer_size : int, optional\n Number of data points to buffer before writing to disk (default is 1).\n\n \"\"\"\n import mdtraj as md\n\n self.filename = filename\n self.buffer_size = buffer_size\n self.topology = topology\n self.buffer = {}\n self.h5file = h5py.File(filename, \"a\")\n log.info(f\"Writing simulation data to {filename}\")\n\n def get_available_keys(self):\n return self.h5file.keys()\n\n def report(self, data_dict):\n \"\"\"\n Add new data to the buffer and write the buffer to disk if it's full.\n\n Parameters\n ----------\n data_dict : dict\n Dictionary containing data to report. Keys are data labels (e.g., 'energy'),\n and values are the data points (usually numpy arrays).\n\n \"\"\"\n for key, value in data_dict.items():\n if key not in self.buffer:\n self.buffer[key] = []\n self.buffer[key].append(value)\n\n if len(self.buffer[key]) >= self.buffer_size:\n self._write_to_disk(key)\n\n def _write_to_disk(self, key):\n \"\"\"\n Write buffered data of a given key to the HDF5 file.\n\n Parameters\n ----------\n key : str\n The key of the data to write to disk.\n\n \"\"\"\n data = np.array(self.buffer[key])\n if key in self.h5file:\n dset = self.h5file[key]\n dset.resize((dset.shape[0] + data.shape[0],) + data.shape[1:])\n dset[-data.shape[0] :] = data\n else:\n log.debug(f\"Creating {key} in {self.filename}\")\n self.h5file.create_dataset(\n key, data=data, maxshape=(None,) + data.shape[1:], chunks=True\n )\n\n self.buffer[key] = []\n\n def close(self):\n \"\"\"\n Write any remaining data in the buffer to disk and close the HDF5 file.\n\n \"\"\"\n for key in self.buffer:\n if self.buffer[key]:\n self._write_to_disk(key)\n self.h5file.close()\n\n def get_property(self, name: str):\n \"\"\"\n Get the property from the HDF5 file.\n\n Parameters\n ----------\n name : str\n Name of the property to get.\n\n Returns\n -------\n np.ndarray\n The property.\n\n \"\"\"\n if name not in self.h5file:\n log.debug(f\"{name} not in HDF5 file\")\n return None\n else:\n return np.array(self.h5file[name])\n\n def get_mdtraj_trajectory(self):\n import mdtraj as md\n\n return md.Trajectory(\n xyz=self.get_property(\"traj\"),\n topology=md.Topology.from_openmm(self.topology),\n unitcell_lengths=self.get_property(\"box_vectors\"),\n unitcell_angles=self.get_property(\"box_angles\"),\n )" } ]
import jax.numpy as jnp from jax import random from tqdm import tqdm from openmm import unit from .states import SamplerState, ThermodynamicState from typing import Dict from loguru import logger as log from .reporters import SimulationReporter from typing import Optional from .utils import get_list_of_mass
3,453
# This file contains the integrator class for the Langevin dynamics simulation class LangevinIntegrator: """ Langevin dynamics integrator for molecular dynamics simulation using the BAOAB splitting scheme [1]. References: [1] Benedict Leimkuhler, Charles Matthews; Robust and efficient configurational molecular sampling via Langevin dynamics. J. Chem. Phys. 7 May 2013; 138 (17): 174102. https://doi.org/10.1063/1.4802990 """ def __init__( self, stepsize=1.0 * unit.femtoseconds, collision_rate=1.0 / unit.picoseconds, save_frequency: int = 100,
# This file contains the integrator class for the Langevin dynamics simulation class LangevinIntegrator: """ Langevin dynamics integrator for molecular dynamics simulation using the BAOAB splitting scheme [1]. References: [1] Benedict Leimkuhler, Charles Matthews; Robust and efficient configurational molecular sampling via Langevin dynamics. J. Chem. Phys. 7 May 2013; 138 (17): 174102. https://doi.org/10.1063/1.4802990 """ def __init__( self, stepsize=1.0 * unit.femtoseconds, collision_rate=1.0 / unit.picoseconds, save_frequency: int = 100,
reporter: Optional[SimulationReporter] = None,
2
2023-11-07 18:17:43+00:00
4k
IIMunchII/restllm
src/restllm/models/prompts.py
[ { "identifier": "MetaModel", "path": "src/restllm/models/base.py", "snippet": "class MetaModel(BaseModel):\n id: int = Field(gt=0, examples=[1, 2, 3])\n class_name: str\n owner: int\n object: Any\n created_at: Datetime = Field(default_factory=Datetime)\n updated_at: Datetime = Field(default_factory=Datetime)" }, { "identifier": "is_valid_jinja2_template", "path": "src/restllm/models/validators.py", "snippet": "def is_valid_jinja2_template(template: str) -> bool:\n env = Environment()\n try:\n env.parse(template)\n return True\n except TemplateSyntaxError:\n return False" }, { "identifier": "names_and_variables_match", "path": "src/restllm/models/validators.py", "snippet": "def names_and_variables_match(template: str, parameters: list[str]) -> bool:\n env = Environment()\n parsed_content = env.parse(template)\n required_keys = meta.find_undeclared_variables(parsed_content)\n return set(required_keys) == set(parameters)" } ]
import re import iso639 import iso639.exceptions from enum import Enum, auto, UNIQUE, verify, StrEnum from jinja2 import Template from pydantic import ( BaseModel, Field, computed_field, create_model, model_validator, field_validator, ) from .base import MetaModel from .validators import is_valid_jinja2_template, names_and_variables_match
1,981
class PromptTag(BaseModel): name: PromptTagName @property def description(self) -> str: return PromptTagDescriptionMapping.get_description(self.name) class BasePrompt(BaseModel): name: str = Field( description="Name of the prompt", pattern=get_name_pattern(), examples=[ "EditPythonCodePrompt", "SummariseArticlePrompt", ], ) description: str = Field( description="Description of the prompt and what it does.", examples=["Prompt to edit python code according to Clean Code principles."], ) language: Language = Field(description="Language of the text in the prompt") tags: list[PromptTagName] | None = Field( description="List of prompt tags descripting the type of prompt" ) class PromptMessage(BaseModel): role: PromptRole = Field( description="User or System role for prompt", examples=[PromptRole.SYSTEM] ) content: str = Field( description="Text based prompt for user or system role.'", examples=[ "You are an expert Python programmer that values Clean Code and simplicity." ], ) class Prompt(BasePrompt): messages: list[PromptMessage] = Field( description="List of prompt messages. Role System must preceed user", max_length=2, min_length=1, ) @field_validator("messages", mode="before") def validate_messages(cls, value): if len(value) == 2: if value[0].role == PromptRole.USER: raise ValueError("First role must be system when two messages is used") if value[0].role == value[1].role: raise ValueError("Consecutive roles cannot be the same") return value class PromptTemplateArgument(BaseModel): name: str = Field( pattern=get_name_pattern(), examples=["python_code", "article_body"], ) type: VariableType class TemplateMessage(BaseModel): role: PromptRole content: str = Field( description="Valid Jinja2 template for the prompt", examples=[ 'Please edit this python code to follow Clean Code best pratices: "{{ python_code }}"' ], ) class PromptTemplate(BasePrompt): arguments: list[PromptTemplateArgument] = Field( description="Parameter name and type for the Jinja2 template. Keys must match the template" ) messages: list[TemplateMessage] = Field( description="List of template messages containing valid Jinja2 template strings." ) @model_validator(mode="after") def check_valid_template(self) -> "PromptTemplate": template = self._get_template_text() if not is_valid_jinja2_template(template): raise ValueError(f"String is invalid Jinja2 template: {template}") if not names_and_variables_match(template, self._get_variable_names()): raise ValueError(f"Parameter keys and template variables must match.") return self def _get_template_text(self): return "\n".join([message.content for message in self.messages]) def _get_variable_names(self) -> list[str]: return [item.name for item in self.arguments] def _get_pydantic_types(self) -> dict[str, tuple[type, ...]]: return {item.name: (item.type.type, ...) for item in self.arguments} def create_model(self) -> BaseModel: return create_model(self.name, **self._get_pydantic_types()) def render(self, parameters: dict) -> dict: template_model = self.create_model() parameter_instance = template_model.model_validate(parameters, strict=True) messages = [ { "role": message.role, "content": Template(message.content).render( parameter_instance.model_dump() ), } for message in self.messages ] prompt_dict = self.model_dump() prompt_dict.update({"messages": messages}) return prompt_dict
class LanguageProperties(BaseModel): name: str = Field(description="Langauge name", examples=["English"]) pt1: str = Field(description="ISO 639-1 language code", examples=["en"]) pt2b: str = Field(description="ISO 639-2/B language code", examples=["eng"]) pt2t: str = Field(description="ISO 639-2/B language code", examples=["eng"]) pt3: str = Field(description="ISO 639-3 language code", examples=["eng"]) pt5: str = Field(description="ISO 639-5 language code", examples=["cpe"]) class Language(BaseModel): iso639_3: str = Field( max_length=3, min_length=3, description="iso639-3 language code.", examples=["eng"], ) @field_validator("iso639_3") def validate_language_code(cls, value): try: iso639.Lang(value) except iso639.exceptions.InvalidLanguageValue as exec: raise ValueError(f"Invalid ISO 639-3 language code: {value}") from exec return value @computed_field(return_type=LanguageProperties) @property def properties(self) -> LanguageProperties: return LanguageProperties(**iso639.Lang(self.iso639_3).asdict()) def get_name_pattern() -> re.Pattern: return r"^[a-zA-Z_][a-zA-Z0-9_]*$" @verify(UNIQUE) class PromptRole(StrEnum): USER = auto() SYSTEM = auto() class VariableType(Enum): STRING = "str" INTEGER = "int" FLOAT = "float" BOOLEAN = "bool" LIST = "list" DICT = "dict" @property def type(self): return eval(self._value_) class PromptTagName(StrEnum): ZEROSHOT = "Zero-shot Prompting" FEWSHOT = "Few-shot Prompting" MANYSHOT = "Many-shot Prompting" CURRICULUMLEARNING = "Curriculum Learning Prompting" META = "Meta-Prompting" CONTINUOUS = "Continuous Prompting" ADAPTIVE = "Adaptive Prompting" COMPARATIVE = "Comparative Prompting" CHAIN = "Chain Prompting" HIERARCHICAL = "Hierarchical Prompting" class PromptTagDescriptionMapping: _mapping = { PromptTagName.ZEROSHOT: "The model is provided with a prompt and is expected to generate a relevant response without any prior examples.", PromptTagName.FEWSHOT: "Providing a few examples along with the prompt to guide the model towards the desired output.", PromptTagName.MANYSHOT: "Providing a larger number of examples along with the prompt to further guide the model.", PromptTagName.CURRICULUMLEARNING: "Arranging prompts in an order of increasing complexity, training the model progressively.", PromptTagName.META: "Designing prompts that instruct the model to consider certain variables or conditions while generating a response.", PromptTagName.CONTINUOUS: "Employing a sequence of prompts in a continuous manner, where the model’s response to one prompt serves as a part of the prompt for the next task.", PromptTagName.ADAPTIVE: "Dynamically adjusting the prompt based on the model’s previous responses to better guide it towards the desired output.", PromptTagName.COMPARATIVE: "Providing comparisons within the prompt to guide the model towards generating more accurate or nuanced responses.", PromptTagName.CHAIN: "Creating a chain of interlinked prompts where the output of one task serves as the prompt for the subsequent task.", PromptTagName.HIERARCHICAL: "Structuring prompts in a hierarchical manner, where higher-level prompts guide the overall narrative and lower-level prompts guide the details.", } @classmethod def get_description(cls, prompt_tag: PromptTagName): return cls._mapping.get(prompt_tag, "Technique not found") class PromptTag(BaseModel): name: PromptTagName @property def description(self) -> str: return PromptTagDescriptionMapping.get_description(self.name) class BasePrompt(BaseModel): name: str = Field( description="Name of the prompt", pattern=get_name_pattern(), examples=[ "EditPythonCodePrompt", "SummariseArticlePrompt", ], ) description: str = Field( description="Description of the prompt and what it does.", examples=["Prompt to edit python code according to Clean Code principles."], ) language: Language = Field(description="Language of the text in the prompt") tags: list[PromptTagName] | None = Field( description="List of prompt tags descripting the type of prompt" ) class PromptMessage(BaseModel): role: PromptRole = Field( description="User or System role for prompt", examples=[PromptRole.SYSTEM] ) content: str = Field( description="Text based prompt for user or system role.'", examples=[ "You are an expert Python programmer that values Clean Code and simplicity." ], ) class Prompt(BasePrompt): messages: list[PromptMessage] = Field( description="List of prompt messages. Role System must preceed user", max_length=2, min_length=1, ) @field_validator("messages", mode="before") def validate_messages(cls, value): if len(value) == 2: if value[0].role == PromptRole.USER: raise ValueError("First role must be system when two messages is used") if value[0].role == value[1].role: raise ValueError("Consecutive roles cannot be the same") return value class PromptTemplateArgument(BaseModel): name: str = Field( pattern=get_name_pattern(), examples=["python_code", "article_body"], ) type: VariableType class TemplateMessage(BaseModel): role: PromptRole content: str = Field( description="Valid Jinja2 template for the prompt", examples=[ 'Please edit this python code to follow Clean Code best pratices: "{{ python_code }}"' ], ) class PromptTemplate(BasePrompt): arguments: list[PromptTemplateArgument] = Field( description="Parameter name and type for the Jinja2 template. Keys must match the template" ) messages: list[TemplateMessage] = Field( description="List of template messages containing valid Jinja2 template strings." ) @model_validator(mode="after") def check_valid_template(self) -> "PromptTemplate": template = self._get_template_text() if not is_valid_jinja2_template(template): raise ValueError(f"String is invalid Jinja2 template: {template}") if not names_and_variables_match(template, self._get_variable_names()): raise ValueError(f"Parameter keys and template variables must match.") return self def _get_template_text(self): return "\n".join([message.content for message in self.messages]) def _get_variable_names(self) -> list[str]: return [item.name for item in self.arguments] def _get_pydantic_types(self) -> dict[str, tuple[type, ...]]: return {item.name: (item.type.type, ...) for item in self.arguments} def create_model(self) -> BaseModel: return create_model(self.name, **self._get_pydantic_types()) def render(self, parameters: dict) -> dict: template_model = self.create_model() parameter_instance = template_model.model_validate(parameters, strict=True) messages = [ { "role": message.role, "content": Template(message.content).render( parameter_instance.model_dump() ), } for message in self.messages ] prompt_dict = self.model_dump() prompt_dict.update({"messages": messages}) return prompt_dict
class PromptTemplateWithMeta(MetaModel):
0
2023-11-05 19:16:00+00:00
4k
rabilrbl/deepseek-api
deepseek_api/deepseek_api.py
[ { "identifier": "API_URL", "path": "deepseek_api/constants.py", "snippet": "class API_URL:\n \"\"\"Deepseek API URL constants\"\"\"\n\n BASE_URL = \"https://coder.deepseek.com/api/v0\"\n LOGIN = BASE_URL + \"/users/login\"\n CLEAR_CONTEXT = BASE_URL + \"/chat/clear_context\"\n CHAT = BASE_URL + \"/chat/completions\"" }, { "identifier": "DeepseekConstants", "path": "deepseek_api/constants.py", "snippet": "class DeepseekConstants:\n \"\"\"Deepseek constants\"\"\"\n\n BASE_HEADERS = {\n \"Accept-Language\": \"en-IN,en;q=0.9\",\n \"Cache-Control\": \"no-cache\",\n \"Connection\": \"keep-alive\",\n \"DNT\": \"1\",\n \"Origin\": \"https://coder.deepseek.com\",\n \"Pragma\": \"no-cache\",\n \"Referer\": \"https://coder.deepseek.com/\",\n \"Sec-Fetch-Dest\": \"empty\",\n \"Sec-Fetch-Mode\": \"cors\",\n \"Sec-Fetch-Site\": \"same-origin\",\n \"User-Agent\": \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome\",\n \"accept\": \"*/*\",\n \"content-type\": \"application/json\",\n \"sec-ch-ua\": '\"Google Chrome\";v=\"119\", \"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"',\n \"sec-ch-ua-mobile\": \"?0\",\n \"sec-ch-ua-platform\": '\"Linux\"',\n \"x-app-version\": \"20240105.0\",\n }" }, { "identifier": "EmptyEmailOrPasswordError", "path": "deepseek_api/errors.py", "snippet": "class EmptyEmailOrPasswordError(Exception):\n \"\"\"Exception raised when the email or password is empty.\"\"\"\n def __init__(self, message=\"Email or password cannot be empty\"):\n self.message = message\n super().__init__(self.message)" }, { "identifier": "NotLoggedInError", "path": "deepseek_api/errors.py", "snippet": "class NotLoggedInError(Exception):\n \"\"\"Exception raised when the user is not logged in.\"\"\"\n def __init__(self, message=\"You are not logged in. Please login first\"):\n self.message = message\n super().__init__(self.message)" } ]
import requests import aiohttp import aiofiles import threading import json import jwt import datetime from abc import ABC, abstractmethod from deepseek_api.constants import API_URL, DeepseekConstants from deepseek_api.errors import EmptyEmailOrPasswordError, NotLoggedInError
2,014
If file not found, calls _login() to login via API. If save_login is False, calls _login() to always login via API. Schedules an update token callback to refresh the token periodically. """ pass @abstractmethod def close(self): """Call destructor method""" pass @abstractmethod def new_chat(self): """Start a new chat""" pass @abstractmethod def chat(self, message: str): """Chat with the Deepseek API. Sends a chat message to the Deepseek API and yields the response. Args: message (str): The chat message to send. Yields: dict: The JSON response from the API for each chat message. """ pass @abstractmethod def _login(self): """Logs in the user by sending a POST request to the login API endpoint. Sends the login request with email, password and other required fields. Saves the credentials to a file if save_login is True. Returns the JSON response from the API. Raises: EmptyEmailOrPasswordError: If the email or password is not provided. HTTP Error: If the login request fails. Returns: dict: Credentials JSON data from login response """ pass class DeepseekAPI(DeepseekBase): """ An asynchronous class to interact with the Deepseek API. """ async def __aenter__(self): """Initializes an aiohttp ClientSession and logs in. This method is called when entering an async context manager. It creates the aiohttp ClientSession used for making requests. It also calls the login() method to authenticate with Deepseek. Returns: Self - Returns itself to enable use as an async context manager. """ self.session = aiohttp.ClientSession() await self.login() return self async def __aexit__(self, exc_type, exc, tb): """Closes the aiohttp ClientSession and cancels the scheduled token update. This method is called when exiting the async context manager. It closes the aiohttp ClientSession that was used for making requests to the API. It also cancels the scheduled token update that was created in __schedule_update_token() to periodically refresh the auth token. """ await self.session.close() if self._thread_timer: self._thread_timer.cancel() @staticmethod async def create(*args, **kwargs): """Creates a new DeepseekAPI instance and enters the context manager. This static method initializes a new DeepseekAPI instance with the given arguments and enters the async context manager by calling __aenter__(). Args: *args: Positional arguments to pass to DeepseekAPI constructor. **kwargs: Keyword arguments to pass to DeepseekAPI constructor. Returns: DeepseekAPI instance that has entered the context manager. """ self = DeepseekAPI(*args, **kwargs) await self.__aenter__() return self async def close(self): """Closes the DeepseekAPI instance by exiting the context manager. Calls __aexit__ to close the aiohttp session and cancel the token update. """ await self.__aexit__(None, None, None) async def _login(self): if self.email == "" or self.password == "": raise EmptyEmailOrPasswordError json_data = { "email": self.email, "mobile": "", "password": self.password, "area_code": "", } async with self.session.post(
class DeepseekBase(ABC): """ A base class to create DeepseekAPI instances. """ def __init__( self, email: str, password: str, model_class: str = "deepseek_code", save_login: bool = False, ): """ Constructor method for DeepseekAPI class. Initializes a DeepseekAPI instance with provided credentials and settings. Parameters: email (str): User's email for Deepseek account password (str): Password for user's Deepseek account model_class (str): Deepseek model to use, either 'deepseek_chat' or 'deepseek_code' save_login (bool): Whether to save credentials to login.json to avoid re-login """ self.email = email self.password = password self.model_class = model_class self.save_login = save_login self.headers = DeepseekConstants.BASE_HEADERS self.credentials = {} self._thread_timer = None # Initialized in the _schedule_update_token method self.session = None def set_authorization_header(self): """Sets the authorization header to a JWT token. Gets the JWT token by calling get_token() and prepends 'Bearer ' to set the authorization header. """ self.headers["authorization"] = "Bearer " + self.get_token() def get_token(self): """Get token Returns: str: JWT Authorization token """ return self.get_credentials()["data"]["user"]["token"] def get_credentials(self): """Get credentials Returns: dict: Credentials JSON data from login response """ return self.credentials def _schedule_update_token(self): """Schedules a timer to refresh the JWT token before it expires. Decodes the current JWT token to get the 'exp' expiration time. Subtracts 1 hour from the 'exp' time to refresh the token early. Starts a Timer thread to call the _login() method when the expiration time is reached. This will refresh the token and update the authorization header with the new token. """ # Decode the JWT token token = self.get_token() decoded_token = jwt.decode(token, options={"verify_signature": False}) # Fetch the 'exp' value and subtract 1 hour (to be safe) exp_time = datetime.datetime.fromtimestamp( decoded_token["exp"] ) - datetime.timedelta(hours=1) self._thread_timer = threading.Timer( (exp_time - datetime.datetime.now()).total_seconds(), self._login ) self._thread_timer.start() def is_logged_in(self): """Check if user is logged in Returns: bool: True if logged in, False otherwise """ if self.credentials: return True else: return False def raise_for_not_logged_in(self): """Raise NotLoggedInError if user is not logged in Raises: NotLoggedInError: If user is not logged in """ if not self.is_logged_in(): raise NotLoggedInError @abstractmethod def login(self): """Logs the user in by loading credentials from file or calling login API. If save_login is True, tries to load credentials from the login.json file. If file not found, calls _login() to login via API. If save_login is False, calls _login() to always login via API. Schedules an update token callback to refresh the token periodically. """ pass @abstractmethod def close(self): """Call destructor method""" pass @abstractmethod def new_chat(self): """Start a new chat""" pass @abstractmethod def chat(self, message: str): """Chat with the Deepseek API. Sends a chat message to the Deepseek API and yields the response. Args: message (str): The chat message to send. Yields: dict: The JSON response from the API for each chat message. """ pass @abstractmethod def _login(self): """Logs in the user by sending a POST request to the login API endpoint. Sends the login request with email, password and other required fields. Saves the credentials to a file if save_login is True. Returns the JSON response from the API. Raises: EmptyEmailOrPasswordError: If the email or password is not provided. HTTP Error: If the login request fails. Returns: dict: Credentials JSON data from login response """ pass class DeepseekAPI(DeepseekBase): """ An asynchronous class to interact with the Deepseek API. """ async def __aenter__(self): """Initializes an aiohttp ClientSession and logs in. This method is called when entering an async context manager. It creates the aiohttp ClientSession used for making requests. It also calls the login() method to authenticate with Deepseek. Returns: Self - Returns itself to enable use as an async context manager. """ self.session = aiohttp.ClientSession() await self.login() return self async def __aexit__(self, exc_type, exc, tb): """Closes the aiohttp ClientSession and cancels the scheduled token update. This method is called when exiting the async context manager. It closes the aiohttp ClientSession that was used for making requests to the API. It also cancels the scheduled token update that was created in __schedule_update_token() to periodically refresh the auth token. """ await self.session.close() if self._thread_timer: self._thread_timer.cancel() @staticmethod async def create(*args, **kwargs): """Creates a new DeepseekAPI instance and enters the context manager. This static method initializes a new DeepseekAPI instance with the given arguments and enters the async context manager by calling __aenter__(). Args: *args: Positional arguments to pass to DeepseekAPI constructor. **kwargs: Keyword arguments to pass to DeepseekAPI constructor. Returns: DeepseekAPI instance that has entered the context manager. """ self = DeepseekAPI(*args, **kwargs) await self.__aenter__() return self async def close(self): """Closes the DeepseekAPI instance by exiting the context manager. Calls __aexit__ to close the aiohttp session and cancel the token update. """ await self.__aexit__(None, None, None) async def _login(self): if self.email == "" or self.password == "": raise EmptyEmailOrPasswordError json_data = { "email": self.email, "mobile": "", "password": self.password, "area_code": "", } async with self.session.post(
API_URL.LOGIN, headers=self.headers, json=json_data
0
2023-11-09 18:42:43+00:00
4k
HealthSciTech/E2E-PPG
ppg_sqa.py
[ { "identifier": "normalize_data", "path": "utils.py", "snippet": "def normalize_data(sig: np.ndarray) -> np.ndarray:\n \"\"\"\n Normalize the input signal between zero and one\n \n Args:\n sig (np.ndarray): PPG signal.\n \n Return:\n np.ndarray: Normalized signal\n \"\"\"\n return (sig - np.min(sig)) / (np.max(sig) - np.min(sig))" }, { "identifier": "get_data", "path": "utils.py", "snippet": "def get_data(\n file_name: str,\n local_directory: str = \"data\",\n usecols: List[str] = ['ppg'],\n) -> np.ndarray:\n \"\"\"\n Import data (e.g., PPG signals)\n \n Args:\n file_name (str): Name of the input file\n local_directory (str): Data directory\n usecols (List[str]): The columns to read from the input file\n \n Return:\n sig (np.ndarray): the input signal (e.g., PPG)\n \"\"\"\n try:\n # Construct the file path\n file_path = os.path.join(local_directory, file_name)\n # Load data from the specified CSV file\n input_data = pd.read_csv(\n file_path,\n delim_whitespace=True,\n usecols=usecols)\n # Extract signal\n sig = input_data[usecols[0]].values\n return sig\n except FileNotFoundError:\n print(f\"File not found: {file_name}\")\n except pd.errors.EmptyDataError:\n print(f\"Empty data in file: {file_name}\")\n except Exception as e:\n print(f\"An unexpected error occurred: {e}\")\n # Return None in case of an error\n return None" }, { "identifier": "bandpass_filter", "path": "utils.py", "snippet": "def bandpass_filter(\n sig: np.ndarray,\n fs: int,\n lowcut: float,\n highcut: float,\n order: int=2\n) -> np.ndarray:\n \"\"\"\n Apply a bandpass filter to the input signal.\n\n Args:\n sig (np.ndarray): The input signal.\n fs (int): The sampling frequency of the input signal.\n lowcut (float): The low cutoff frequency of the bandpass filter.\n highcut (float): The high cutoff frequency of the bandpass filter.\n\n Return:\n sig_filtered (np.ndarray): The filtered signal using a Butterworth bandpass filter.\n \"\"\"\n nyquist = 0.5 * fs\n low = lowcut / nyquist\n high = highcut / nyquist\n b, a = butter(order, [low, high], btype='band')\n sig_filtered = filtfilt(b, a, sig)\n return sig_filtered" }, { "identifier": "find_peaks", "path": "utils.py", "snippet": "def find_peaks(\n ppg: np.ndarray,\n sampling_rate: int,\n return_sig: bool = False\n) -> Tuple[np.ndarray, np.ndarray]:\n \"\"\"\n Find peaks in PPG.\n\n Args:\n ppg (np.ndarray): The input PPG signal.\n sampling_rate (int): The sampling rate of the signal.\n return_sig (bool): If True, return the cleaned PPG\n signal along with the peak indices (default is False).\n\n Return:\n peaks (np.ndarray): An array containing the indices of\n the detected peaks in the PPG signal.\n ppg_cleaned (np.ndarray): The cleaned PPG signal, return if return_sig is True.\n\n \"\"\"\n\n # Clean the PPG signal and prepare it for peak detection\n ppg_cleaned = nk.ppg_clean(ppg, sampling_rate=sampling_rate)\n\n # Peak detection\n info = nk.ppg_findpeaks(ppg_cleaned, sampling_rate=sampling_rate)\n peaks = info[\"PPG_Peaks\"]\n\n # Return either just the peaks or both the cleaned signal and peaks\n if return_sig:\n return peaks, ppg_cleaned\n else:\n return peaks, None" }, { "identifier": "resample_signal", "path": "utils.py", "snippet": "def resample_signal(\n sig: np.ndarray,\n fs_origin: int,\n fs_target: int = 20,\n) -> np.ndarray:\n \"\"\"\n Resample the signal\n\n Args:\n sig (np.ndarray): The input signal.\n fs_origin (int): The sampling frequency of the input signal.\n fs_target (int): The sampling frequency of the output signal.\n\n Return:\n sig_resampled (np.ndarray): The resampled signal.\n \"\"\"\n # Exit if the sampling frequency already is 20 Hz (return the original signal)\n if fs_origin == fs_target:\n return sig\n # Calculate the resampling rate\n resampling_rate = fs_target/fs_origin\n # Resample the signal\n sig_resampled = resample(sig, int(len(sig)*resampling_rate))\n # Update the sampling frequency\n return sig_resampled" } ]
import pickle import os import more_itertools as mit import joblib import warnings import numpy as np from typing import Tuple, List from scipy import stats, signal from utils import normalize_data, get_data, bandpass_filter, find_peaks, resample_signal
3,130
energy.append(np.sum(beat*beat)) if not energy: var_energy = 0 else: # Calculate variation var_energy = max(energy) - min(energy) return var_energy def template_matching_features(hc: list) -> Tuple[float, float]: """ Extract template matching features from heart cycles Args: hc: List of heart cycles Return: tm_ave_eu: Average of Euclidean distance with the template tm_ave_corr: Average of correlation with the template """ hc = np.array([np.array(xi) for xi in hc if len(xi) != 0]) # Calculate the template by averaging all heart cycles template = np.mean(hc, axis=0) # Euclidean distance and correlation distances = [] corrs = [] for beat in hc: distances.append(np.linalg.norm(template-beat)) corr_matrix = np.corrcoef(template, beat) corrs.append(corr_matrix[0, 1]) tm_ave_eu = np.mean(distances) tm_ave_corr = np.mean(corrs) return tm_ave_eu, tm_ave_corr def feature_extraction( ppg: np.ndarray, sampling_rate: int, ) -> List[float]: """ Extract features from PPG signal Args: ppg: Input PPG signal. sampling_rate: Sampling rate of the PPG signal. Return: features: List of features """ # feature 1: Interquartile range iqr_rate = stats.iqr(ppg, interpolation='midpoint') # feature 2: STD of power spectral density _, pxx_den = signal.periodogram(ppg, sampling_rate) std_p_spec = np.std(pxx_den) # Heart cycle detection hc = heart_cycle_detection(ppg=ppg, sampling_rate=sampling_rate) if hc: # feature 3: variation in energy of heart cycles var_energy = energy_hc(hc) # features 4, 5: average Euclidean and Correlation in template matching tm_ave_eu, tm_ave_corr = template_matching_features(hc) else: var_energy = np.nan tm_ave_eu = np.nan tm_ave_corr = np.nan features = [iqr_rate, std_p_spec, var_energy, tm_ave_eu, tm_ave_corr] return features def sqa( sig: np.ndarray, sampling_rate: int, filter_signal: bool = True, ) -> Tuple[list, list]: """ Perform PPG Signal Quality Assessment (SQA). This function assesses the quality of a PPG signal by classifying its segments as reliable (clean) or unrelaible (noisy) using a pre-trained model. The clean indices represent parts of the PPG signal that are deemed reliable, while the noisy indices indicate parts that may be affected by noise or artifacts. Args: sig (np.ndarray): PPG signal. sampling_rate (int): Sampling rate of the PPG signal. filter_signal (bool): True if the signal has not filtered using a bandpass filter. Return: clean_indices: A list of clean indices. noisy_indices: A list of noisy indices. Reference: Feli, M., Azimi, I., Anzanpour, A., Rahmani, A. M., & Liljeberg, P. (2023). An energy-efficient semi-supervised approach for on-device photoplethysmogram signal quality assessment. Smart Health, 28, 100390. """ # Load pre-trained model and normalization scaler scaler = joblib.load(os.path.join(MODEL_PATH, SCALER_FILE_NAME)) model = pickle.load( open(os.path.join(MODEL_PATH, SQA_MODEL_FILE_NAME), 'rb')) resampling_flag = False # Check if resampling is needed and perform resampling if necessary if sampling_rate != SQA_MODEL_SAMPLING_FREQUENCY: sig = resample_signal( sig=sig, fs_origin=sampling_rate, fs_target=SQA_MODEL_SAMPLING_FREQUENCY) resampling_flag = True resampling_rate = sampling_rate/SQA_MODEL_SAMPLING_FREQUENCY sampling_rate = SQA_MODEL_SAMPLING_FREQUENCY # Apply bandpass filter if needed if filter_signal:
# -*- coding: utf-8 -*- warnings.filterwarnings("ignore") MODEL_PATH = "models" SCALER_FILE_NAME = "Train_data_scaler.save" SQA_MODEL_FILE_NAME = 'OneClassSVM_model.sav' SQA_MODEL_SAMPLING_FREQUENCY = 20 SEGMENT_SIZE = 30 SHIFTING_SIZE = 2 def segmentation( sig: np.ndarray, sig_indices: np.ndarray, sampling_rate: int, method: str = 'shifting', segment_size: int = 30, shift_size: int = 2, ) -> Tuple[List[np.ndarray], List[np.ndarray]]: """ Segments the signals (PPG) and their indices into fixed-size segments. Args: sig: Input signal (e.g., PPG). sig_indices: Corresponding indices for the input signal. sampling_rate: Sampling rate of the PPG signal. method: Segmentation method. Options: 'standard' or 'shifting'. Segments do not overlap for 'standard' and overlap with the size of (segment_size - shift_size) for 'shifting'. segment_size: Size of the segment (in second). shift_size: Size of the shift (in seconds) in segmentation in case method is 'shifting'. Return: segments_sig: List of segments (PPG). segments_indices: List of segments (indices). """ signal_length = len(sig) segment_length = int(segment_size*sampling_rate) shift_length = int(shift_size*sampling_rate) if method == 'standard': # Non-overlapping segments segments_sig = [sig[i:i+segment_length] for i in range( 0, signal_length, segment_length ) if i + segment_length <= signal_length] segments_indices = [sig_indices[i:i+segment_length] for i in range( 0, signal_length, segment_length ) if i + segment_length <= signal_length] elif method == 'shifting': # Overlapping segments segments_sig = [sig[i:i+segment_length] for i in range( 0, signal_length - segment_length + 1, shift_length ) if i + segment_length <= signal_length] segments_indices = [sig_indices[i:i+segment_length] for i in range( 0, signal_length - segment_length + 1, shift_length ) if i + segment_length <= signal_length] else: raise ValueError("Invalid method. Use 'standard' or 'shifting'.") return segments_sig, segments_indices def heart_cycle_detection( ppg: np.ndarray, sampling_rate: int, ) -> list: """ Extract heart cycles from the PPG signal Args: ppg: Input PPG signal. sampling_rate: Sampling rate of the PPG signal. Return: hc: List of heart cycles """ # Normalization ppg_normalized = normalize_data(ppg) # Upsampling signal by 2 sampling_rate = sampling_rate*2 ppg_upsampled = signal.resample(ppg_normalized, len(ppg_normalized)*2) # Systolic peak detection peaks, ppg_cleaned = find_peaks( ppg=ppg_upsampled, sampling_rate=sampling_rate, return_sig=True) # Heart cycle detection based on the peaks and fixed intervals hc = [] if len(peaks) < 2: return hc # Define a fixed interval in PPG signal to detect heart cycles beat_bound = round((len(ppg_upsampled)/len(peaks))/2) # Ignore the first and last beat to prevent boundary error for i in range(1, len(peaks) - 1): # Select beat from the signal and add it to the list beat_start = peaks[i] - beat_bound beat_end = peaks[i] + beat_bound if beat_start >= 0 and beat_end < len(ppg_cleaned): beat = ppg_cleaned[beat_start:beat_end] if len(beat) >= beat_bound*2: hc.append(beat) return hc def energy_hc(hc: list) -> float: """ Extract energy of heart cycle Args: hc: List of heart cycles Return: var_energy: Variation of heart cycles energy """ energy = [] for beat in hc: energy.append(np.sum(beat*beat)) if not energy: var_energy = 0 else: # Calculate variation var_energy = max(energy) - min(energy) return var_energy def template_matching_features(hc: list) -> Tuple[float, float]: """ Extract template matching features from heart cycles Args: hc: List of heart cycles Return: tm_ave_eu: Average of Euclidean distance with the template tm_ave_corr: Average of correlation with the template """ hc = np.array([np.array(xi) for xi in hc if len(xi) != 0]) # Calculate the template by averaging all heart cycles template = np.mean(hc, axis=0) # Euclidean distance and correlation distances = [] corrs = [] for beat in hc: distances.append(np.linalg.norm(template-beat)) corr_matrix = np.corrcoef(template, beat) corrs.append(corr_matrix[0, 1]) tm_ave_eu = np.mean(distances) tm_ave_corr = np.mean(corrs) return tm_ave_eu, tm_ave_corr def feature_extraction( ppg: np.ndarray, sampling_rate: int, ) -> List[float]: """ Extract features from PPG signal Args: ppg: Input PPG signal. sampling_rate: Sampling rate of the PPG signal. Return: features: List of features """ # feature 1: Interquartile range iqr_rate = stats.iqr(ppg, interpolation='midpoint') # feature 2: STD of power spectral density _, pxx_den = signal.periodogram(ppg, sampling_rate) std_p_spec = np.std(pxx_den) # Heart cycle detection hc = heart_cycle_detection(ppg=ppg, sampling_rate=sampling_rate) if hc: # feature 3: variation in energy of heart cycles var_energy = energy_hc(hc) # features 4, 5: average Euclidean and Correlation in template matching tm_ave_eu, tm_ave_corr = template_matching_features(hc) else: var_energy = np.nan tm_ave_eu = np.nan tm_ave_corr = np.nan features = [iqr_rate, std_p_spec, var_energy, tm_ave_eu, tm_ave_corr] return features def sqa( sig: np.ndarray, sampling_rate: int, filter_signal: bool = True, ) -> Tuple[list, list]: """ Perform PPG Signal Quality Assessment (SQA). This function assesses the quality of a PPG signal by classifying its segments as reliable (clean) or unrelaible (noisy) using a pre-trained model. The clean indices represent parts of the PPG signal that are deemed reliable, while the noisy indices indicate parts that may be affected by noise or artifacts. Args: sig (np.ndarray): PPG signal. sampling_rate (int): Sampling rate of the PPG signal. filter_signal (bool): True if the signal has not filtered using a bandpass filter. Return: clean_indices: A list of clean indices. noisy_indices: A list of noisy indices. Reference: Feli, M., Azimi, I., Anzanpour, A., Rahmani, A. M., & Liljeberg, P. (2023). An energy-efficient semi-supervised approach for on-device photoplethysmogram signal quality assessment. Smart Health, 28, 100390. """ # Load pre-trained model and normalization scaler scaler = joblib.load(os.path.join(MODEL_PATH, SCALER_FILE_NAME)) model = pickle.load( open(os.path.join(MODEL_PATH, SQA_MODEL_FILE_NAME), 'rb')) resampling_flag = False # Check if resampling is needed and perform resampling if necessary if sampling_rate != SQA_MODEL_SAMPLING_FREQUENCY: sig = resample_signal( sig=sig, fs_origin=sampling_rate, fs_target=SQA_MODEL_SAMPLING_FREQUENCY) resampling_flag = True resampling_rate = sampling_rate/SQA_MODEL_SAMPLING_FREQUENCY sampling_rate = SQA_MODEL_SAMPLING_FREQUENCY # Apply bandpass filter if needed if filter_signal:
sig = bandpass_filter(
2
2023-11-07 22:52:14+00:00
4k
WSH032/fastapi-proxy-lib
tests/test_http.py
[ { "identifier": "AppFactoryFixture", "path": "tests/conftest.py", "snippet": "_P = ParamSpec(\"_P\")\nclass LifeAppDataclass4Test(AppDataclass4Test):\nclass UvicornServerFixture(Protocol): # noqa: D101\n def __call__( # noqa: D102\n self, config: uvicorn.Config, contx_exit_timeout: Union[int, float, None] = None\n ) -> Coroutine[None, None, UvicornServer]:\ndef anyio_backend() -> Literal[\"asyncio\"]:\nasync def lifespan_manager() -> AsyncIterator[LifespanManagerFixture]:\n async def _lifespan_manager(app: ASGIApp) -> ASGIApp:\nasync def echo_http_test_model(\n lifespan_manager: LifespanManagerFixture,\n) -> LifeAppDataclass4Test:\nasync def echo_ws_test_model(\n lifespan_manager: LifespanManagerFixture,\n) -> LifeAppDataclass4Test:\ndef _app_fct_life_wapper( # noqa: D417\n app_fct: Callable[_P, ASGIApp], lifespan_manager_fixture: LifespanManagerFixture\n) -> Callable[_P, Coroutine[None, None, ASGIApp]]:\n async def wappered_app_fct(*args: _P.args, **kwargs: _P.kwargs) -> ASGIApp:\ndef forward_http_app_fct(\n lifespan_manager: LifespanManagerFixture,\n): # -> AppFactoryFixture\ndef reverse_http_app_fct(\n lifespan_manager: LifespanManagerFixture,\n): # -> AppFactoryFixture\ndef reverse_ws_app_fct(\n lifespan_manager: LifespanManagerFixture,\n): # -> AppFactoryFixture\nasync def uvicorn_server_fixture() -> AsyncIterator[UvicornServerFixture]:\n async def uvicorn_server_fct(\n config: uvicorn.Config, contx_exit_timeout: Union[int, float, None] = None\n ) -> UvicornServer:" }, { "identifier": "DEFAULT_URL", "path": "tests/tool.py", "snippet": "DEFAULT_URL = \"http://www.example.com/\"" }, { "identifier": "PRIVATE_IP_URL", "path": "tests/tool.py", "snippet": "PRIVATE_IP_URL = \"http://127.0.0.1/\"" }, { "identifier": "WRONG_PROTO_URL", "path": "tests/tool.py", "snippet": "WRONG_PROTO_URL = \"wrong://wrong.fastapi_proxy_test.wrong/\"" }, { "identifier": "AbstractTestProxy", "path": "tests/tool.py", "snippet": "class AbstractTestProxy(abc.ABC):\n \"\"\"Abstract class for testing proxy.\"\"\"\n\n @abc.abstractmethod\n def tool_4_test_fixture(self) -> Tool4TestFixture:\n \"\"\"Get the tool for test server.\"\"\"" }, { "identifier": "Tool4TestFixture", "path": "tests/tool.py", "snippet": "class Tool4TestFixture:\n \"\"\"Tool for test server.\n\n Attributes:\n client_for_conn_to_target_server: The client for connecting to target server.\n client_for_conn_to_proxy_server: The client for connecting to proxy server.\n get_request: Get the latest original http/websocket request from the client.\n target_server_base_url: The base url of target server.\n proxy_server_base_url: The base url of proxy server.\n \"\"\"\n\n client_for_conn_to_target_server: httpx.AsyncClient\n client_for_conn_to_proxy_server: httpx.AsyncClient\n get_request: Callable[[], ServerRecvRequestsTypes]\n target_server_base_url: str\n proxy_server_base_url: str" }, { "identifier": "check_if_err_resp_is_from_px_serv", "path": "tests/tool.py", "snippet": "def check_if_err_resp_is_from_px_serv(resp: httpx.Response) -> None:\n \"\"\"Check if the response about error info is actively sent by proxy server.\n\n If not, will raise AssertionError\n \"\"\"\n assert resp.is_error, f\"Not a error response: {resp}\"\n try:\n resp_body = resp.json()\n except Exception:\n pytest.fail(f\"Not from proxy server: {resp}\")\n # 这两条消息是代理服务器主动返回的错误信息的API的一部分\n assert \"err_type\" in resp_body[\"detail\"]\n assert \"msg\" in resp_body[\"detail\"]" } ]
import httpx import pytest from fastapi_proxy_lib.core.tool import default_proxy_filter from typing_extensions import override from .conftest import AppFactoryFixture, LifeAppDataclass4Test from .tool import ( DEFAULT_URL, PRIVATE_IP_URL, WRONG_PROTO_URL, AbstractTestProxy, Tool4TestFixture, check_if_err_resp_is_from_px_serv, )
3,551
self, tool_4_test_fixture: Tool4TestFixture, ) -> None: """Testing for fixing cookie leakage vulnerabilities.""" client_for_conn_to_proxy_server = ( tool_4_test_fixture.client_for_conn_to_proxy_server ) proxy_server_base_url = tool_4_test_fixture.proxy_server_base_url # request to set cookie: foo=bar await client_for_conn_to_proxy_server.get( proxy_server_base_url + "get/cookies/set/foo/bar" ) # check if cookie is set assert client_for_conn_to_proxy_server.cookies["foo"] == "bar" r = await client_for_conn_to_proxy_server.get( proxy_server_base_url + "get/cookies" ) assert r.json()["foo"] == "bar" # Then simulate the access of another user's client by clearing cookiejar client_for_conn_to_proxy_server.cookies.clear() # check if cookiejar is cleared assert not client_for_conn_to_proxy_server.cookies # check if cookie is not leaked r = await client_for_conn_to_proxy_server.get( proxy_server_base_url + "get/cookies", cookies={"a": "b"}, ) assert "foo" not in r.json() # not leaked assert r.json()["a"] == "b" # send cookies normally class TestForwardHttpProxy(AbstractTestProxy): """For testing forward http proxy.""" @pytest.fixture() async def tool_4_test_fixture( # pyright: ignore[reportIncompatibleMethodOverride] self, echo_http_test_model: LifeAppDataclass4Test, forward_http_app_fct: AppFactoryFixture, ) -> Tool4TestFixture: """目标服务器请参考`tests.app.echo_http_app.get_app`.""" client_for_conn_to_target_server = httpx.AsyncClient( app=echo_http_test_model.app, base_url=DEFAULT_TARGET_SERVER_BASE_URL ) forward_http_app = await forward_http_app_fct( client=client_for_conn_to_target_server, proxy_filter=default_proxy_filter ) client_for_conn_to_proxy_server = httpx.AsyncClient( app=forward_http_app, base_url=DEFAULT_PROXY_SERVER_BASE_URL ) get_request = echo_http_test_model.get_request return Tool4TestFixture( client_for_conn_to_target_server=client_for_conn_to_target_server, client_for_conn_to_proxy_server=client_for_conn_to_proxy_server, get_request=get_request, target_server_base_url=DEFAULT_TARGET_SERVER_BASE_URL, proxy_server_base_url=DEFAULT_PROXY_SERVER_BASE_URL, ) @pytest.mark.anyio() async def test_all_request_methods( self, tool_4_test_fixture: Tool4TestFixture ) -> None: """测试是否所有的请求方法都能正常工作.""" client_for_conn_to_proxy_server = ( tool_4_test_fixture.client_for_conn_to_proxy_server ) proxy_server_base_url = tool_4_test_fixture.proxy_server_base_url target_server_base_url = tool_4_test_fixture.target_server_base_url test_url = proxy_server_base_url + target_server_base_url resp_lst = ( await client_for_conn_to_proxy_server.get(test_url), await client_for_conn_to_proxy_server.post(test_url), await client_for_conn_to_proxy_server.put(test_url), await client_for_conn_to_proxy_server.head(test_url), await client_for_conn_to_proxy_server.options(test_url), await client_for_conn_to_proxy_server.delete(test_url), await client_for_conn_to_proxy_server.patch(test_url), ) assert all(resp.is_success for resp in resp_lst) @pytest.mark.anyio() async def test_bad_url_request( self, forward_http_app_fct: AppFactoryFixture, ) -> None: """测试坏URL请求的报错功能.""" client_for_conn_to_target_server = httpx.AsyncClient() forward_http_app = await forward_http_app_fct( client=client_for_conn_to_target_server, proxy_filter=default_proxy_filter ) client_for_conn_to_proxy_server = httpx.AsyncClient( app=forward_http_app, base_url=DEFAULT_PROXY_SERVER_BASE_URL ) # 错误的无法发出请求的URL r = await client_for_conn_to_proxy_server.get( DEFAULT_PROXY_SERVER_BASE_URL + WRONG_PROTO_URL ) assert r.status_code == 400 check_if_err_resp_is_from_px_serv(r) # 空URL r = await client_for_conn_to_proxy_server.get(DEFAULT_PROXY_SERVER_BASE_URL) assert r.status_code == 400 check_if_err_resp_is_from_px_serv(r) # 试图访问私有IP的URL r = await client_for_conn_to_proxy_server.get(
# noqa: D100 DEFAULT_TARGET_SERVER_BASE_URL = "http://www.echo.com/" DEFAULT_PROXY_SERVER_BASE_URL = "http://www.proxy.com/" class TestReverseHttpProxy(AbstractTestProxy): """For testing reverse http proxy.""" @override @pytest.fixture() async def tool_4_test_fixture( # pyright: ignore[reportIncompatibleMethodOverride] self, echo_http_test_model: LifeAppDataclass4Test, reverse_http_app_fct: AppFactoryFixture, ) -> Tool4TestFixture: """目标服务器请参考`tests.app.echo_http_app.get_app`.""" client_for_conn_to_target_server = httpx.AsyncClient( app=echo_http_test_model.app, base_url=DEFAULT_TARGET_SERVER_BASE_URL ) reverse_http_app = await reverse_http_app_fct( client=client_for_conn_to_target_server, base_url=DEFAULT_TARGET_SERVER_BASE_URL, ) client_for_conn_to_proxy_server = httpx.AsyncClient( app=reverse_http_app, base_url=DEFAULT_PROXY_SERVER_BASE_URL ) get_request = echo_http_test_model.get_request return Tool4TestFixture( client_for_conn_to_target_server=client_for_conn_to_target_server, client_for_conn_to_proxy_server=client_for_conn_to_proxy_server, get_request=get_request, target_server_base_url=DEFAULT_TARGET_SERVER_BASE_URL, proxy_server_base_url=DEFAULT_PROXY_SERVER_BASE_URL, ) @pytest.mark.anyio() async def test_all_request_methods( self, tool_4_test_fixture: Tool4TestFixture ) -> None: """测试是否所有的请求方法都能正常工作.""" client_for_conn_to_proxy_server = ( tool_4_test_fixture.client_for_conn_to_proxy_server ) proxy_server_base_url = tool_4_test_fixture.proxy_server_base_url resp_lst = ( await client_for_conn_to_proxy_server.get(proxy_server_base_url), await client_for_conn_to_proxy_server.post(proxy_server_base_url), await client_for_conn_to_proxy_server.put(proxy_server_base_url), await client_for_conn_to_proxy_server.head(proxy_server_base_url), await client_for_conn_to_proxy_server.options(proxy_server_base_url), await client_for_conn_to_proxy_server.delete(proxy_server_base_url), await client_for_conn_to_proxy_server.patch(proxy_server_base_url), ) assert all(resp.is_success for resp in resp_lst) @pytest.mark.anyio() async def test_if_the_header_is_properly_handled( self, tool_4_test_fixture: Tool4TestFixture ) -> None: """测试是否正确处理请求头.""" client_for_conn_to_proxy_server = ( tool_4_test_fixture.client_for_conn_to_proxy_server ) proxy_server_base_url = tool_4_test_fixture.proxy_server_base_url target_server_base_url = tool_4_test_fixture.target_server_base_url ########## 测试 keep_alive 检查点 ########## # 客户端关闭连接请求 和 常规操作: # 1.无损转发请求头至目标服务器 # 2.正确处理 host 请求头 proxy_resp = await client_for_conn_to_proxy_server.head( proxy_server_base_url + "head/return_keep_alive_headers", headers={ "foo": "bar", "Connection": "close", }, ) target_server_recv_request = tool_4_test_fixture.get_request() # 测试是否尊重客户端关闭连接请求 assert "close" in proxy_resp.headers["connection"] # 测试是否无损转发请求头至目标服务器 assert target_server_recv_request.headers["foo"] == "bar" # 测试是否代理服务器强制发送"connection: keep-alive"请求头至目标服务器 assert "keep-alive" in target_server_recv_request.headers["connection"] # 测试是否正确处理 host 请求头 assert target_server_recv_request.headers["host"] == httpx.URL( target_server_base_url ).netloc.decode("ascii") # 客户端保活请求 proxy_resp = await client_for_conn_to_proxy_server.head( proxy_server_base_url + "head/return_keep_alive_headers", headers={ "Connection": "keep-alive", "Keep-Alive": "timeout=5, max=1000", }, ) target_server_recv_request = tool_4_test_fixture.get_request() # 测试是否屏蔽了 keep-alive 请求头 assert "keep-alive" not in target_server_recv_request.headers ########## 测试 close_connection 检查点 ########## # 测试是否尊重客户端保活连接请求 proxy_resp = await client_for_conn_to_proxy_server.head( proxy_server_base_url + "head/return_close_connection_headers", headers={ "Connection": "keep-alive", "Keep-Alive": "timeout=5, max=1000", }, ) assert ( "connection" not in proxy_resp.headers or "close" not in proxy_resp.headers["connection"] ) # 测试是否尊重客户端关闭连接请求 proxy_resp = await client_for_conn_to_proxy_server.head( proxy_server_base_url + "head/return_close_connection_headers", headers={ "Connection": "close", }, ) assert "close" in proxy_resp.headers["connection"] @pytest.mark.anyio() async def test_if_the_proxy_forwarding_is_correct( self, tool_4_test_fixture: Tool4TestFixture ) -> None: """测试代理服务器的转发功能是否正常.""" client_for_conn_to_proxy_server = ( tool_4_test_fixture.client_for_conn_to_proxy_server ) proxy_server_base_url = tool_4_test_fixture.proxy_server_base_url # 测试目标服务器响应体转发正常 r = await client_for_conn_to_proxy_server.get( proxy_server_base_url + "get/echo_headers_and_params", headers={"foo": "bar"}, ) assert r.json()["foo"] == "bar" # 测试客户端请求体转发正常 r = await client_for_conn_to_proxy_server.post( proxy_server_base_url + "post/echo_body", json={"foo": "bar"}, ) assert r.json()["foo"] == "bar" # 测试目标服务文件转发正常 file_str = "你好" r = await client_for_conn_to_proxy_server.put( proxy_server_base_url + f"put/echo_file?content={file_str}", ) assert r.content.decode("utf-8") == file_str @pytest.mark.anyio() async def test_bad_url_request( self, reverse_http_app_fct: AppFactoryFixture, ) -> None: """测试坏URL请求的报错功能.""" client_for_conn_to_target_server = httpx.AsyncClient() reverse_http_app = await reverse_http_app_fct( client=client_for_conn_to_target_server, base_url=WRONG_PROTO_URL, ) client_for_conn_to_proxy_server = httpx.AsyncClient( app=reverse_http_app, base_url=DEFAULT_PROXY_SERVER_BASE_URL ) r = await client_for_conn_to_proxy_server.get(DEFAULT_PROXY_SERVER_BASE_URL) assert r.status_code == 502 check_if_err_resp_is_from_px_serv(r) @pytest.mark.anyio() async def test_cookie_leakage( self, tool_4_test_fixture: Tool4TestFixture, ) -> None: """Testing for fixing cookie leakage vulnerabilities.""" client_for_conn_to_proxy_server = ( tool_4_test_fixture.client_for_conn_to_proxy_server ) proxy_server_base_url = tool_4_test_fixture.proxy_server_base_url # request to set cookie: foo=bar await client_for_conn_to_proxy_server.get( proxy_server_base_url + "get/cookies/set/foo/bar" ) # check if cookie is set assert client_for_conn_to_proxy_server.cookies["foo"] == "bar" r = await client_for_conn_to_proxy_server.get( proxy_server_base_url + "get/cookies" ) assert r.json()["foo"] == "bar" # Then simulate the access of another user's client by clearing cookiejar client_for_conn_to_proxy_server.cookies.clear() # check if cookiejar is cleared assert not client_for_conn_to_proxy_server.cookies # check if cookie is not leaked r = await client_for_conn_to_proxy_server.get( proxy_server_base_url + "get/cookies", cookies={"a": "b"}, ) assert "foo" not in r.json() # not leaked assert r.json()["a"] == "b" # send cookies normally class TestForwardHttpProxy(AbstractTestProxy): """For testing forward http proxy.""" @pytest.fixture() async def tool_4_test_fixture( # pyright: ignore[reportIncompatibleMethodOverride] self, echo_http_test_model: LifeAppDataclass4Test, forward_http_app_fct: AppFactoryFixture, ) -> Tool4TestFixture: """目标服务器请参考`tests.app.echo_http_app.get_app`.""" client_for_conn_to_target_server = httpx.AsyncClient( app=echo_http_test_model.app, base_url=DEFAULT_TARGET_SERVER_BASE_URL ) forward_http_app = await forward_http_app_fct( client=client_for_conn_to_target_server, proxy_filter=default_proxy_filter ) client_for_conn_to_proxy_server = httpx.AsyncClient( app=forward_http_app, base_url=DEFAULT_PROXY_SERVER_BASE_URL ) get_request = echo_http_test_model.get_request return Tool4TestFixture( client_for_conn_to_target_server=client_for_conn_to_target_server, client_for_conn_to_proxy_server=client_for_conn_to_proxy_server, get_request=get_request, target_server_base_url=DEFAULT_TARGET_SERVER_BASE_URL, proxy_server_base_url=DEFAULT_PROXY_SERVER_BASE_URL, ) @pytest.mark.anyio() async def test_all_request_methods( self, tool_4_test_fixture: Tool4TestFixture ) -> None: """测试是否所有的请求方法都能正常工作.""" client_for_conn_to_proxy_server = ( tool_4_test_fixture.client_for_conn_to_proxy_server ) proxy_server_base_url = tool_4_test_fixture.proxy_server_base_url target_server_base_url = tool_4_test_fixture.target_server_base_url test_url = proxy_server_base_url + target_server_base_url resp_lst = ( await client_for_conn_to_proxy_server.get(test_url), await client_for_conn_to_proxy_server.post(test_url), await client_for_conn_to_proxy_server.put(test_url), await client_for_conn_to_proxy_server.head(test_url), await client_for_conn_to_proxy_server.options(test_url), await client_for_conn_to_proxy_server.delete(test_url), await client_for_conn_to_proxy_server.patch(test_url), ) assert all(resp.is_success for resp in resp_lst) @pytest.mark.anyio() async def test_bad_url_request( self, forward_http_app_fct: AppFactoryFixture, ) -> None: """测试坏URL请求的报错功能.""" client_for_conn_to_target_server = httpx.AsyncClient() forward_http_app = await forward_http_app_fct( client=client_for_conn_to_target_server, proxy_filter=default_proxy_filter ) client_for_conn_to_proxy_server = httpx.AsyncClient( app=forward_http_app, base_url=DEFAULT_PROXY_SERVER_BASE_URL ) # 错误的无法发出请求的URL r = await client_for_conn_to_proxy_server.get( DEFAULT_PROXY_SERVER_BASE_URL + WRONG_PROTO_URL ) assert r.status_code == 400 check_if_err_resp_is_from_px_serv(r) # 空URL r = await client_for_conn_to_proxy_server.get(DEFAULT_PROXY_SERVER_BASE_URL) assert r.status_code == 400 check_if_err_resp_is_from_px_serv(r) # 试图访问私有IP的URL r = await client_for_conn_to_proxy_server.get(
DEFAULT_PROXY_SERVER_BASE_URL + PRIVATE_IP_URL
2
2023-11-08 04:38:36+00:00
4k
aws-samples/amazon-location-geospatial-agent
geospatial_agent/agent/action_summarizer/action_summarizer.py
[ { "identifier": "_ACTION_SUMMARY_PROMPT", "path": "geospatial_agent/agent/action_summarizer/prompts.py", "snippet": "_ACTION_SUMMARY_PROMPT = \"\"\"\\\n{role_intro}\n{human_role}: A message is provided below.\nYour task is to extract the intended user action and all file paths from the message. Meet the requirements written below:\n\nRequirements:\n{requirements}\n\n\nMessage: {message}\n\n{assistant_role}:\n\"\"\"" }, { "identifier": "_ROLE_INTRO", "path": "geospatial_agent/agent/action_summarizer/prompts.py", "snippet": "_ROLE_INTRO = \"You are a geospatial data analyzer designed to analyze data schema from arbitrary geospatial data sets.\"" }, { "identifier": "_READ_FILE_PROMPT", "path": "geospatial_agent/agent/action_summarizer/prompts.py", "snippet": "_READ_FILE_PROMPT = \"\"\"\\\n{role_intro}\n{human_role}: You are provided a set of file URLs. You need to generate a Python function that meets the following requirements:\n\nRequirements:\n{requirements}\n\nSession Id: {session_id}\nStorage Mode: {storage_mode}\n\nFile Urls:\n{file_urls}\n\nAs\n{assistant_role}:\n\"\"\"" }, { "identifier": "_READ_FILE_REQUIREMENTS", "path": "geospatial_agent/agent/action_summarizer/prompts.py", "snippet": "_READ_FILE_REQUIREMENTS = [\n \"Read each file using geopandas. Each file could be csv, shapefile, or GeoJSON. Otherwise, throw a ValueError.\",\n \"Return a list of python dictionaries with keys: file_url, resolved_file_url, data_frame, column_names.\",\n \"Use built-in function resolved_file_url = get_data_file_url(file_url, session_id) to get downloadable URLs. Do not add import statement for this function.\",\n \"Take 3 random rows with no missing values to each data_frame.\",\n f\"After writing the function, call the function in the end and store the list of data_frame in a global variable named {DATA_FRAMES_VARIABLE_NAME}.\",\n \"Do not use any try except block.\",\n \"Put your reply into a Python code block(enclosed by ```python and ```) without any extra surrounding text.\",\n \"Use pandas, geopandas, numpy, and builtins to solve the problem. Do not use any external data sources or libraries.\"\n]" }, { "identifier": "_ACTION_SUMMARY_REQUIREMENTS", "path": "geospatial_agent/agent/action_summarizer/prompts.py", "snippet": "_ACTION_SUMMARY_REQUIREMENTS = [\n \"Return a JSON object with keys: action, file_paths. The action is the intended user action. The file_paths are the file paths that are extracted from the message.\",\n \"Rephrase user action as a complete sentence with desired user action and include it in the action key.\",\n \"Only return the JSON object as output. Do not add any extra text.\",\n \"If no file paths are found file_paths will be an empty string list.\",\n \"If the file path is a HTTP(S) link, use the full link as output.\",\n \"If the file path is not a URI, add agent:// to the beginning of the filepath.\",\n \"If there are multiple file paths, add all file paths in the output. Follow the rules above for each filepath.\",\n \"File paths are case sensitive. It can have spaces, hyphens, underscores, and periods.\"\n]" }, { "identifier": "DATA_FRAMES_VARIABLE_NAME", "path": "geospatial_agent/agent/action_summarizer/prompts.py", "snippet": "DATA_FRAMES_VARIABLE_NAME = \"dataframes\"" }, { "identifier": "_DATA_SUMMARY_REQUIREMENTS", "path": "geospatial_agent/agent/action_summarizer/prompts.py", "snippet": "_DATA_SUMMARY_REQUIREMENTS = [\n \"The summary should be at maximum two sentences.\",\n \"The first sentence should be summary of the data in the table from the aspect of the user action.\",\n \"If there is no geometry column in the table, the second sentence should note column names that can be used to generate a geometry column in geopandas.\",\n \"Write summary without any extra surrounding text.\"\n]" }, { "identifier": "_DATA_SUMMARY_PROMPT", "path": "geospatial_agent/agent/action_summarizer/prompts.py", "snippet": "_DATA_SUMMARY_PROMPT = \"\"\"\\\n{role_intro}\n{human_role}: You are provided with a table with some rows data. Your task is to generate a summary that describes the data in the table following the requirements below:\n\nRequirements:\n{requirements}\n\nIntended user action: {action}\n\nThe table has following columns:\n{columns}\n\nTable:\n{table}\n\n\n{assistant_role}:\n\"\"\"" }, { "identifier": "AgentSignal", "path": "geospatial_agent/agent/shared.py", "snippet": "class AgentSignal(BaseModel):\n id: str = Field(default_factory=lambda: uuid4().__str__())\n timestamp: str = Field(default_factory=lambda: datetime.now().isoformat())\n event_source: str = Field()\n event_message: str = Field()\n event_data: T = Field(default=None)\n event_type: EventType = Field(default=EventType.Message)\n is_final: bool = Field(default=False)" }, { "identifier": "EventType", "path": "geospatial_agent/agent/shared.py", "snippet": "class EventType(Enum):\n PythonCode = auto()\n Message = auto()\n Error = auto()" }, { "identifier": "SIGNAL_ACTION_CONTEXT_GENERATED", "path": "geospatial_agent/agent/shared.py", "snippet": "SIGNAL_ACTION_CONTEXT_GENERATED = \"action_context_generated\"" }, { "identifier": "SENDER_ACTION_SUMMARIZER", "path": "geospatial_agent/agent/shared.py", "snippet": "SENDER_ACTION_SUMMARIZER = \"action_summarizer\"" }, { "identifier": "SIGNAL_FILE_READ_CODE_GENERATED", "path": "geospatial_agent/agent/shared.py", "snippet": "SIGNAL_FILE_READ_CODE_GENERATED = \"file_read_code_generated\"" }, { "identifier": "SIGNAL_FILE_READ_CODE_EXECUTED", "path": "geospatial_agent/agent/shared.py", "snippet": "SIGNAL_FILE_READ_CODE_EXECUTED = \"file_read_code_executed\"" }, { "identifier": "execute_assembled_code", "path": "geospatial_agent/agent/shared.py", "snippet": "def execute_assembled_code(assembled_code):\n \"\"\"Executes the assembled code and returns the output.\"\"\"\n old_stdout = sys.stdout\n redirected_output = sys.stdout = StringIO()\n try:\n exec(assembled_code, globals(), globals())\n except Exception as e:\n raise e\n finally:\n sys.stdout = old_stdout\n\n output = redirected_output.getvalue()\n return output, globals()" }, { "identifier": "get_claude_v2", "path": "geospatial_agent/shared/bedrock.py", "snippet": "def get_claude_v2(max_tokens_to_sample=8100, temperature=0.001):\n client = get_bedrock_client()\n llm = Bedrock(model_id=\"anthropic.claude-v2\",\n client=client,\n model_kwargs={\n \"max_tokens_to_sample\": max_tokens_to_sample,\n \"temperature\": temperature\n })\n return llm" }, { "identifier": "HUMAN_ROLE", "path": "geospatial_agent/shared/prompts.py", "snippet": "HUMAN_ROLE = HUMAN_STOP_SEQUENCE" }, { "identifier": "ASSISTANT_ROLE", "path": "geospatial_agent/shared/prompts.py", "snippet": "ASSISTANT_ROLE = \"\\n\\nAssistant\"" }, { "identifier": "HUMAN_STOP_SEQUENCE", "path": "geospatial_agent/shared/prompts.py", "snippet": "HUMAN_STOP_SEQUENCE = '\\n\\nHuman'" }, { "identifier": "get_shim_imports", "path": "geospatial_agent/shared/shim.py", "snippet": "def get_shim_imports() -> str:\n shim_map_style_import = f'from {location_map_style.__module__} import {location_map_style.__name__} \\n' \\\n f'from {get_data_file_url.__module__} import {get_data_file_url.__name__}\\n' \\\n f'from {get_local_file_path.__module__} import {get_local_file_path.__name__}\\n'\n return shim_map_style_import" }, { "identifier": "extract_code", "path": "geospatial_agent/shared/utils.py", "snippet": "def extract_code(response):\n \"\"\"Extract python code from LLM response.\"\"\"\n\n python_code_match = re.search(r\"```(?:python)?(.*?)```\", response, re.DOTALL)\n if python_code_match:\n python_code = python_code_match.group(1).strip()\n return python_code\n else:\n raise ExtractionException(\"Failed to extract python code from response\")" } ]
import json from typing import List, Any, Optional from langchain import PromptTemplate, LLMChain from pydantic import BaseModel, ConfigDict from pydispatch import dispatcher from geospatial_agent.agent.action_summarizer.prompts import _ACTION_SUMMARY_PROMPT, _ROLE_INTRO, \ _READ_FILE_PROMPT, _READ_FILE_REQUIREMENTS, _ACTION_SUMMARY_REQUIREMENTS, DATA_FRAMES_VARIABLE_NAME, \ _DATA_SUMMARY_REQUIREMENTS, _DATA_SUMMARY_PROMPT from geospatial_agent.agent.shared import AgentSignal, EventType, SIGNAL_ACTION_CONTEXT_GENERATED, \ SENDER_ACTION_SUMMARIZER, SIGNAL_FILE_READ_CODE_GENERATED, SIGNAL_FILE_READ_CODE_EXECUTED, execute_assembled_code from geospatial_agent.shared.bedrock import get_claude_v2 from geospatial_agent.shared.prompts import HUMAN_ROLE, ASSISTANT_ROLE, HUMAN_STOP_SEQUENCE from geospatial_agent.shared.shim import get_shim_imports from geospatial_agent.shared.utils import extract_code
2,403
class ActionSummarizerException(Exception): def __init__(self, message: str): self.message = message super().__init__(self.message) class ActionContext(BaseModel): action: str file_paths: List[str] class FileSummary(BaseModel): model_config = ConfigDict(arbitrary_types_allowed=True) file_url: str data_frame: Any column_names: List[str] file_summary: Optional[str] = None class ActionSummary(BaseModel): action: str file_summaries: List[FileSummary] class ActionSummarizer: """Action summarizer acts on raw user messages with the following traits 1. It is a geospatial query or analysis such as "Draw me a heatmap". 2. Has URLS of data to be used for the analysis. ActionSummarizer generates a list of ActionSummary. """ def __init__(self, llm=None): if llm is None: claude_v2 = get_claude_v2() self.llm = claude_v2 else: self.llm = llm def invoke(self, user_input: str, session_id: str, storage_mode: str) -> ActionSummary: try: action_context = self._extract_action_context(user_input) dispatcher.send(signal=SIGNAL_ACTION_CONTEXT_GENERATED, sender=SENDER_ACTION_SUMMARIZER, event_data=AgentSignal( event_type=EventType.Message, event_source=SENDER_ACTION_SUMMARIZER, event_message=f'Detected desired action {action_context.action}. And file paths: {action_context.file_paths}.' )) read_file_code = self._gen_file_read_code(action_context, session_id, storage_mode) dispatcher.send(signal=SIGNAL_FILE_READ_CODE_GENERATED, sender=SENDER_ACTION_SUMMARIZER, event_data=AgentSignal( event_type=EventType.PythonCode, event_source=SENDER_ACTION_SUMMARIZER, event_message=f'Generated code to read and understand data schema.', event_data=read_file_code )) data_files_summary = self._gen_file_summaries_from_executing_code(read_file_code)
class ActionSummarizerException(Exception): def __init__(self, message: str): self.message = message super().__init__(self.message) class ActionContext(BaseModel): action: str file_paths: List[str] class FileSummary(BaseModel): model_config = ConfigDict(arbitrary_types_allowed=True) file_url: str data_frame: Any column_names: List[str] file_summary: Optional[str] = None class ActionSummary(BaseModel): action: str file_summaries: List[FileSummary] class ActionSummarizer: """Action summarizer acts on raw user messages with the following traits 1. It is a geospatial query or analysis such as "Draw me a heatmap". 2. Has URLS of data to be used for the analysis. ActionSummarizer generates a list of ActionSummary. """ def __init__(self, llm=None): if llm is None: claude_v2 = get_claude_v2() self.llm = claude_v2 else: self.llm = llm def invoke(self, user_input: str, session_id: str, storage_mode: str) -> ActionSummary: try: action_context = self._extract_action_context(user_input) dispatcher.send(signal=SIGNAL_ACTION_CONTEXT_GENERATED, sender=SENDER_ACTION_SUMMARIZER, event_data=AgentSignal( event_type=EventType.Message, event_source=SENDER_ACTION_SUMMARIZER, event_message=f'Detected desired action {action_context.action}. And file paths: {action_context.file_paths}.' )) read_file_code = self._gen_file_read_code(action_context, session_id, storage_mode) dispatcher.send(signal=SIGNAL_FILE_READ_CODE_GENERATED, sender=SENDER_ACTION_SUMMARIZER, event_data=AgentSignal( event_type=EventType.PythonCode, event_source=SENDER_ACTION_SUMMARIZER, event_message=f'Generated code to read and understand data schema.', event_data=read_file_code )) data_files_summary = self._gen_file_summaries_from_executing_code(read_file_code)
dispatcher.send(signal=SIGNAL_FILE_READ_CODE_EXECUTED,
13
2023-11-09 18:29:25+00:00
4k
Rishit-dagli/Astroformer
pytorch-image-models/timm/layers/mlp.py
[ { "identifier": "GlobalResponseNorm", "path": "pytorch-image-models/timm/layers/grn.py", "snippet": "class GlobalResponseNorm(nn.Module):\n \"\"\" Global Response Normalization layer\n \"\"\"\n def __init__(self, dim, eps=1e-6, channels_last=True):\n super().__init__()\n self.eps = eps\n if channels_last:\n self.spatial_dim = (1, 2)\n self.channel_dim = -1\n self.wb_shape = (1, 1, 1, -1)\n else:\n self.spatial_dim = (2, 3)\n self.channel_dim = 1\n self.wb_shape = (1, -1, 1, 1)\n\n self.weight = nn.Parameter(torch.zeros(dim))\n self.bias = nn.Parameter(torch.zeros(dim))\n\n def forward(self, x):\n x_g = x.norm(p=2, dim=self.spatial_dim, keepdim=True)\n x_n = x_g / (x_g.mean(dim=self.channel_dim, keepdim=True) + self.eps)\n return x + torch.addcmul(self.bias.view(self.wb_shape), self.weight.view(self.wb_shape), x * x_n)" }, { "identifier": "to_2tuple", "path": "pytorch-image-models/timm/layers/helpers.py", "snippet": "def _ntuple(n):\n def parse(x):\ndef make_divisible(v, divisor=8, min_value=None, round_limit=.9):\ndef extend_tuple(x, n):" } ]
from functools import partial from torch import nn as nn from .grn import GlobalResponseNorm from .helpers import to_2tuple
2,326
self.norm = norm_layer(hidden_features) if norm_layer is not None else nn.Identity() self.fc2 = nn.Linear(hidden_features, out_features, bias=bias[1]) self.drop2 = nn.Dropout(drop_probs[1]) def init_weights(self): # override init of fc1 w/ gate portion set to weight near zero, bias=1 nn.init.ones_(self.fc1_g.bias) nn.init.normal_(self.fc1_g.weight, std=1e-6) def forward(self, x): x_gate = self.fc1_g(x) x = self.fc1_x(x) x = self.act(x_gate) * x x = self.drop1(x) x = self.norm(x) x = self.fc2(x) x = self.drop2(x) return x class GatedMlp(nn.Module): """ MLP as used in gMLP """ def __init__( self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, norm_layer=None, gate_layer=None, bias=True, drop=0., ): super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features bias = to_2tuple(bias) drop_probs = to_2tuple(drop) self.fc1 = nn.Linear(in_features, hidden_features, bias=bias[0]) self.act = act_layer() self.drop1 = nn.Dropout(drop_probs[0]) if gate_layer is not None: assert hidden_features % 2 == 0 self.gate = gate_layer(hidden_features) hidden_features = hidden_features // 2 # FIXME base reduction on gate property? else: self.gate = nn.Identity() self.norm = norm_layer(hidden_features) if norm_layer is not None else nn.Identity() self.fc2 = nn.Linear(hidden_features, out_features, bias=bias[1]) self.drop2 = nn.Dropout(drop_probs[1]) def forward(self, x): x = self.fc1(x) x = self.act(x) x = self.drop1(x) x = self.gate(x) x = self.norm(x) x = self.fc2(x) x = self.drop2(x) return x class ConvMlp(nn.Module): """ MLP using 1x1 convs that keeps spatial dims """ def __init__( self, in_features, hidden_features=None, out_features=None, act_layer=nn.ReLU, norm_layer=None, bias=True, drop=0., ): super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features bias = to_2tuple(bias) self.fc1 = nn.Conv2d(in_features, hidden_features, kernel_size=1, bias=bias[0]) self.norm = norm_layer(hidden_features) if norm_layer else nn.Identity() self.act = act_layer() self.drop = nn.Dropout(drop) self.fc2 = nn.Conv2d(hidden_features, out_features, kernel_size=1, bias=bias[1]) def forward(self, x): x = self.fc1(x) x = self.norm(x) x = self.act(x) x = self.drop(x) x = self.fc2(x) return x class GlobalResponseNormMlp(nn.Module): """ MLP w/ Global Response Norm (see grn.py), nn.Linear or 1x1 Conv2d """ def __init__( self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, bias=True, drop=0., use_conv=False, ): super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features bias = to_2tuple(bias) drop_probs = to_2tuple(drop) linear_layer = partial(nn.Conv2d, kernel_size=1) if use_conv else nn.Linear self.fc1 = linear_layer(in_features, hidden_features, bias=bias[0]) self.act = act_layer() self.drop1 = nn.Dropout(drop_probs[0])
""" MLP module w/ dropout and configurable activation layer Hacked together by / Copyright 2020 Ross Wightman """ class Mlp(nn.Module): """ MLP as used in Vision Transformer, MLP-Mixer and related networks """ def __init__( self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, norm_layer=None, bias=True, drop=0., use_conv=False, ): super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features bias = to_2tuple(bias) drop_probs = to_2tuple(drop) linear_layer = partial(nn.Conv2d, kernel_size=1) if use_conv else nn.Linear self.fc1 = linear_layer(in_features, hidden_features, bias=bias[0]) self.act = act_layer() self.drop1 = nn.Dropout(drop_probs[0]) self.norm = norm_layer(hidden_features) if norm_layer is not None else nn.Identity() self.fc2 = linear_layer(hidden_features, out_features, bias=bias[1]) self.drop2 = nn.Dropout(drop_probs[1]) def forward(self, x): x = self.fc1(x) x = self.act(x) x = self.drop1(x) x = self.norm(x) x = self.fc2(x) x = self.drop2(x) return x class GluMlp(nn.Module): """ MLP w/ GLU style gating See: https://arxiv.org/abs/1612.08083, https://arxiv.org/abs/2002.05202 """ def __init__( self, in_features, hidden_features=None, out_features=None, act_layer=nn.Sigmoid, norm_layer=None, bias=True, drop=0., use_conv=False, gate_last=True, ): super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features assert hidden_features % 2 == 0 bias = to_2tuple(bias) drop_probs = to_2tuple(drop) linear_layer = partial(nn.Conv2d, kernel_size=1) if use_conv else nn.Linear self.chunk_dim = 1 if use_conv else -1 self.gate_last = gate_last # use second half of width for gate self.fc1 = linear_layer(in_features, hidden_features, bias=bias[0]) self.act = act_layer() self.drop1 = nn.Dropout(drop_probs[0]) self.norm = norm_layer(hidden_features // 2) if norm_layer is not None else nn.Identity() self.fc2 = linear_layer(hidden_features // 2, out_features, bias=bias[1]) self.drop2 = nn.Dropout(drop_probs[1]) def init_weights(self): # override init of fc1 w/ gate portion set to weight near zero, bias=1 fc1_mid = self.fc1.bias.shape[0] // 2 nn.init.ones_(self.fc1.bias[fc1_mid:]) nn.init.normal_(self.fc1.weight[fc1_mid:], std=1e-6) def forward(self, x): x = self.fc1(x) x1, x2 = x.chunk(2, dim=self.chunk_dim) x = x1 * self.act(x2) if self.gate_last else self.act(x1) * x2 x = self.drop1(x) x = self.norm(x) x = self.fc2(x) x = self.drop2(x) return x SwiGLUPacked = partial(GluMlp, act_layer=nn.SiLU, gate_last=False) class SwiGLU(nn.Module): """ SwiGLU NOTE: GluMLP above can implement SwiGLU, but this impl has split fc1 and better matches some other common impl which makes mapping checkpoints simpler. """ def __init__( self, in_features, hidden_features=None, out_features=None, act_layer=nn.SiLU, norm_layer=None, bias=True, drop=0., ): super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features bias = to_2tuple(bias) drop_probs = to_2tuple(drop) self.fc1_g = nn.Linear(in_features, hidden_features, bias=bias[0]) self.fc1_x = nn.Linear(in_features, hidden_features, bias=bias[0]) self.act = act_layer() self.drop1 = nn.Dropout(drop_probs[0]) self.norm = norm_layer(hidden_features) if norm_layer is not None else nn.Identity() self.fc2 = nn.Linear(hidden_features, out_features, bias=bias[1]) self.drop2 = nn.Dropout(drop_probs[1]) def init_weights(self): # override init of fc1 w/ gate portion set to weight near zero, bias=1 nn.init.ones_(self.fc1_g.bias) nn.init.normal_(self.fc1_g.weight, std=1e-6) def forward(self, x): x_gate = self.fc1_g(x) x = self.fc1_x(x) x = self.act(x_gate) * x x = self.drop1(x) x = self.norm(x) x = self.fc2(x) x = self.drop2(x) return x class GatedMlp(nn.Module): """ MLP as used in gMLP """ def __init__( self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, norm_layer=None, gate_layer=None, bias=True, drop=0., ): super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features bias = to_2tuple(bias) drop_probs = to_2tuple(drop) self.fc1 = nn.Linear(in_features, hidden_features, bias=bias[0]) self.act = act_layer() self.drop1 = nn.Dropout(drop_probs[0]) if gate_layer is not None: assert hidden_features % 2 == 0 self.gate = gate_layer(hidden_features) hidden_features = hidden_features // 2 # FIXME base reduction on gate property? else: self.gate = nn.Identity() self.norm = norm_layer(hidden_features) if norm_layer is not None else nn.Identity() self.fc2 = nn.Linear(hidden_features, out_features, bias=bias[1]) self.drop2 = nn.Dropout(drop_probs[1]) def forward(self, x): x = self.fc1(x) x = self.act(x) x = self.drop1(x) x = self.gate(x) x = self.norm(x) x = self.fc2(x) x = self.drop2(x) return x class ConvMlp(nn.Module): """ MLP using 1x1 convs that keeps spatial dims """ def __init__( self, in_features, hidden_features=None, out_features=None, act_layer=nn.ReLU, norm_layer=None, bias=True, drop=0., ): super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features bias = to_2tuple(bias) self.fc1 = nn.Conv2d(in_features, hidden_features, kernel_size=1, bias=bias[0]) self.norm = norm_layer(hidden_features) if norm_layer else nn.Identity() self.act = act_layer() self.drop = nn.Dropout(drop) self.fc2 = nn.Conv2d(hidden_features, out_features, kernel_size=1, bias=bias[1]) def forward(self, x): x = self.fc1(x) x = self.norm(x) x = self.act(x) x = self.drop(x) x = self.fc2(x) return x class GlobalResponseNormMlp(nn.Module): """ MLP w/ Global Response Norm (see grn.py), nn.Linear or 1x1 Conv2d """ def __init__( self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, bias=True, drop=0., use_conv=False, ): super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features bias = to_2tuple(bias) drop_probs = to_2tuple(drop) linear_layer = partial(nn.Conv2d, kernel_size=1) if use_conv else nn.Linear self.fc1 = linear_layer(in_features, hidden_features, bias=bias[0]) self.act = act_layer() self.drop1 = nn.Dropout(drop_probs[0])
self.grn = GlobalResponseNorm(hidden_features, channels_last=not use_conv)
0
2023-11-05 01:25:14+00:00
4k
dewgenenny/rtl_433_discoverandsubmit
rtl_433_discoverandsubmit/modules/cli_interface.py
[ { "identifier": "connect_mqtt", "path": "rtl_433_discoverandsubmit/modules/mqtt_client.py", "snippet": "def reset_message_counters():\ndef sort_detected_devices():\ndef on_connect(client, userdata, flags, rc):\ndef on_message(client, userdata, msg):\ndef connect_mqtt():\ndef publish_to_topic(client, topic, payload, retain=False):" }, { "identifier": "publish_ha_config", "path": "rtl_433_discoverandsubmit/modules/ha_integration.py", "snippet": "def publish_ha_config(client, data, retain=False):\n \"\"\"\n Publish Home Assistant configuration for a device.\n\n :param client: MQTT client instance.\n :param data: Device data.\n :param retain: Whether to retain the message on the broker or not.\n \"\"\"\n # Get model and instance details\n model = data.get(\"model\")\n id= data.get(\"original_id\")\n uid = data.get(\"id\")\n\n logging.info(f\"Model: {model}\")\n instance = rtl_433_device_topic(data)\n logging.info(f\"Instance: {instance}\")\n topicprefix = data.get(\"topicprefix\")\n\n # Iterate through the mappings and publish configuration for each attribute\n for attribute, mapping in DEVICE_MAPPINGS.items():\n\n if attribute in data:\n # Construct the topic and payload based on the mapping\n logging.debug(\"Attribute = \" + attribute)\n path = f\"homeassistant/{mapping['device_type']}/{uid}_{attribute}_{mapping['object_suffix']}/config\"\n\n logging.debug(f\"Path: {path}\")\n config = mapping[\"config\"].copy()\n config[\"name\"] = attribute\n\n #deal with type in state topic\n logging.debug(\"Type is \" + str(data.get(\"type\")))\n\n if \"type\" in data and \"channel\" in data:\n logging.debug(\"In if statement\")\n type = data.get(\"type\")\n config[\"state_topic\"] = f\"{topicprefix}/devices/{type}/{model}/{channel}/{id}/{attribute}\"\n\n elif \"type\" in data:\n logging.debug(\"In if statement\")\n type = data.get(\"type\")\n config[\"state_topic\"] = f\"{topicprefix}/devices/{type}/{model}/{id}/{attribute}\"\n\n elif \"channel\" in data:\n channel = data.get(\"channel\")\n config[\"state_topic\"] = f\"{topicprefix}/devices/{model}/{channel}/{id}/{attribute}\"\n\n else:\n config[\"state_topic\"] = f\"{topicprefix}/devices/{model}/{id}/{attribute}\"\n\n\n config[\"unique_id\"] = f\"rtl_433_{uid}_{attribute}_{mapping['object_suffix']}\"\n config[\"device\"] = {\n \"identifiers\": instance,\n \"name\": f\"{uid}_{mapping['object_suffix']}\",\n \"model\": model,\n \"manufacturer\": \"rtl_433\"\n }\n logging.debug(f\"Config: {config}\")\n\n # Publish the configuration\n publish_to_topic(client, path, json.dumps(config), retain=retain)" }, { "identifier": "load_devices_from_file", "path": "rtl_433_discoverandsubmit/modules/device_manager.py", "snippet": "def load_devices_from_file():\n \"\"\"Load the list of devices from a JSON file. Return an empty list if the file doesn't exist or is corrupted.\"\"\"\n logging.info(\"Load devices from file called\")\n print(\"At start of load devices from file\")\n device_file = initialize_device_storage()\n logging.debug(\"device file has been intialised = \" + str(device_file))\n try:\n with open(device_file, 'r') as file:\n print(\"inside load file, before load\")\n logging.debug(\"Before json load\")\n devices = json.load(file)\n print(\"inside load file, after load\")\n logging.debug(f\"Loaded {len(devices)} devices from the file.\")\n\n # 0.1.7 adds message count, we need to deal with people that have saved devices from previous versions\n for device in devices:\n if 'message_count' not in device:\n device['message_count'] = 1 # Default value for existing devices\n\n return devices\n except FileNotFoundError:\n logging.info(\"File not found error\")\n return []\n except json.JSONDecodeError:\n logging.warning(f\"Corrupted JSON data in {device_file}. Returning an empty device list.\")\n return []\n except Exception as e:\n logging.error(f\"Unexpected error while loading devices: {e}\")\n return []" }, { "identifier": "save_devices_to_file", "path": "rtl_433_discoverandsubmit/modules/device_manager.py", "snippet": "def save_devices_to_file(devices):\n \"\"\"Save the list of devices to a JSON file.\"\"\"\n device_file = initialize_device_storage()\n logging.debug(\"device file = \" + str(device_file))\n with open(device_file, 'w') as file:\n json.dump(devices, file)" }, { "identifier": "reset_message_counters", "path": "rtl_433_discoverandsubmit/modules/mqtt_client.py", "snippet": "def reset_message_counters():\n global detected_devices\n for device in detected_devices:\n if 'message_count' in device:\n device['message_count'] = 0" }, { "identifier": "config", "path": "rtl_433_discoverandsubmit/config.py", "snippet": "" } ]
from unicurses import * from rtl_433_discoverandsubmit.modules.mqtt_client import connect_mqtt, detected_devices, sort_detected_devices from rtl_433_discoverandsubmit.modules.ha_integration import publish_ha_config from rtl_433_discoverandsubmit.modules.device_manager import load_devices_from_file, save_devices_to_file from rtl_433_discoverandsubmit.modules.mqtt_client import reset_message_counters from rtl_433_discoverandsubmit import config from pprint import pprint import argparse import logging
2,464
def end_ui(): """End the Unicurses UI session.""" endwin() def truncate_string(string, max_length): """Truncate a string to a maximum length, adding an ellipsis if truncated.""" return (string[:max_length-3] + '...') if len(string) > max_length else string def display_device_list(stdscr, devices, selected_index, scroll_offset): """Display the list of detected devices in a table format.""" # Define column widths id_width = 25 message_count_width = 10 first_detected_width = 19 last_detected_width = 19 height, width = getmaxyx(stdscr) y, x = 0, 0 move(y, x) addstr("Device ID".ljust(id_width) + " | " + "Msg Count".ljust(message_count_width) + " | " + "First Detected".ljust(first_detected_width) + " | " + "Last Detected".ljust(last_detected_width)) move(y + 1, x) addstr("-" * 20 + "+" + "-" * 11 + "+" + "-" * 21 + "+" + "-" * 21) move(height - 3, 0) # Move to the third last line of the screen addstr("Press 's' to sort by last detected time, model, or message count. Press 'k' to reset counters") # Display each device entry in the list for idx, device in enumerate(devices[scroll_offset:]): # Start from the scroll offset move(y + idx + 2, x) if idx == selected_index - scroll_offset: # Adjusted for scroll_offset attron(A_REVERSE) device_str = f"{truncate_string(device['id'], id_width).ljust(id_width)} | {str(device['message_count']).ljust(message_count_width)} | " + \ f"{device['first_detected_time'].ljust(first_detected_width)} | " + \ f"{device['last_detected_time'].ljust(last_detected_width)}" addstr(device_str) if idx == selected_index - scroll_offset: # Adjusted for scroll_offset attroff(A_REVERSE) if y + idx + 2 >= height - 2: # Check if we've reached the bottom of the screen break move(height - 2, 0) # Move to second last line of the screen addstr("Choose an entry and hit enter for more details or press q to quit.") def display_device_details(stdscr, device): """Display detailed information about the selected device.""" y, x = 0, 0 move(y, x ) addstr(f"Details for {device.get('model', 'Unknown Model')}:") for key, value in device.items(): y += 1 move(y + 1, x) addstr(f"{key}: {value}") height, width = getmaxyx(stdscr) move(height - 2, 0) # Move to second last line of the screen addstr("Press a to add to Home Assistant, b to go back to the list") def main_loop(stdscr): """Main UI loop.""" global current_sort_criteria global detected_devices scroll_offset = 0 selected_index = 0 in_detailed_view = False mqtt_client = connect_mqtt() while True: clear() height, width = getmaxyx(stdscr) if not in_detailed_view: display_device_list(stdscr, detected_devices, selected_index, scroll_offset) else: display_device_details(stdscr, detected_devices[selected_index]) key = getch() # Check if 'k' is pressed if key == ord('k'): reset_message_counters() if key == ord('s'): # Cycle through sorting criteria current_criteria = config.configuration['current_sort_criteria'] if current_criteria == "last_detected_time": config.configuration['current_sort_criteria'] = "model" elif current_criteria == "model": config.configuration['current_sort_criteria'] = "message_count" else: config.configuration['current_sort_criteria'] = "last_detected_time" sort_detected_devices() refresh() if key == KEY_RESIZE: # Handle the resizing of the console clear() # Clear the screen refresh() # Refresh the entire screen continue # Skip the rest of the loop and redraw on the next iteration if key == KEY_DOWN and not in_detailed_view: if selected_index < len(detected_devices) - 1: selected_index += 1 if selected_index - scroll_offset > height - 4: # -4 accounts for header and footer lines scroll_offset += 1 elif key == KEY_UP and not in_detailed_view: if selected_index > 0: selected_index -= 1 if selected_index < scroll_offset: scroll_offset -= 1 elif key == ord('q'): mqtt_client.disconnect()
global detected_devices log_level = getattr(logging, config.configuration['log_level']) logging.basicConfig(filename=config.configuration['log_filename'], level=log_level) def init_ui(): """Initialize the Unicurses UI.""" stdscr = initscr() cbreak() noecho() keypad(stdscr, True) timeout(1000) # Wait for 1 second return stdscr def end_ui(): """End the Unicurses UI session.""" endwin() def truncate_string(string, max_length): """Truncate a string to a maximum length, adding an ellipsis if truncated.""" return (string[:max_length-3] + '...') if len(string) > max_length else string def display_device_list(stdscr, devices, selected_index, scroll_offset): """Display the list of detected devices in a table format.""" # Define column widths id_width = 25 message_count_width = 10 first_detected_width = 19 last_detected_width = 19 height, width = getmaxyx(stdscr) y, x = 0, 0 move(y, x) addstr("Device ID".ljust(id_width) + " | " + "Msg Count".ljust(message_count_width) + " | " + "First Detected".ljust(first_detected_width) + " | " + "Last Detected".ljust(last_detected_width)) move(y + 1, x) addstr("-" * 20 + "+" + "-" * 11 + "+" + "-" * 21 + "+" + "-" * 21) move(height - 3, 0) # Move to the third last line of the screen addstr("Press 's' to sort by last detected time, model, or message count. Press 'k' to reset counters") # Display each device entry in the list for idx, device in enumerate(devices[scroll_offset:]): # Start from the scroll offset move(y + idx + 2, x) if idx == selected_index - scroll_offset: # Adjusted for scroll_offset attron(A_REVERSE) device_str = f"{truncate_string(device['id'], id_width).ljust(id_width)} | {str(device['message_count']).ljust(message_count_width)} | " + \ f"{device['first_detected_time'].ljust(first_detected_width)} | " + \ f"{device['last_detected_time'].ljust(last_detected_width)}" addstr(device_str) if idx == selected_index - scroll_offset: # Adjusted for scroll_offset attroff(A_REVERSE) if y + idx + 2 >= height - 2: # Check if we've reached the bottom of the screen break move(height - 2, 0) # Move to second last line of the screen addstr("Choose an entry and hit enter for more details or press q to quit.") def display_device_details(stdscr, device): """Display detailed information about the selected device.""" y, x = 0, 0 move(y, x ) addstr(f"Details for {device.get('model', 'Unknown Model')}:") for key, value in device.items(): y += 1 move(y + 1, x) addstr(f"{key}: {value}") height, width = getmaxyx(stdscr) move(height - 2, 0) # Move to second last line of the screen addstr("Press a to add to Home Assistant, b to go back to the list") def main_loop(stdscr): """Main UI loop.""" global current_sort_criteria global detected_devices scroll_offset = 0 selected_index = 0 in_detailed_view = False mqtt_client = connect_mqtt() while True: clear() height, width = getmaxyx(stdscr) if not in_detailed_view: display_device_list(stdscr, detected_devices, selected_index, scroll_offset) else: display_device_details(stdscr, detected_devices[selected_index]) key = getch() # Check if 'k' is pressed if key == ord('k'): reset_message_counters() if key == ord('s'): # Cycle through sorting criteria current_criteria = config.configuration['current_sort_criteria'] if current_criteria == "last_detected_time": config.configuration['current_sort_criteria'] = "model" elif current_criteria == "model": config.configuration['current_sort_criteria'] = "message_count" else: config.configuration['current_sort_criteria'] = "last_detected_time" sort_detected_devices() refresh() if key == KEY_RESIZE: # Handle the resizing of the console clear() # Clear the screen refresh() # Refresh the entire screen continue # Skip the rest of the loop and redraw on the next iteration if key == KEY_DOWN and not in_detailed_view: if selected_index < len(detected_devices) - 1: selected_index += 1 if selected_index - scroll_offset > height - 4: # -4 accounts for header and footer lines scroll_offset += 1 elif key == KEY_UP and not in_detailed_view: if selected_index > 0: selected_index -= 1 if selected_index < scroll_offset: scroll_offset -= 1 elif key == ord('q'): mqtt_client.disconnect()
save_devices_to_file(detected_devices)
3
2023-11-03 19:34:56+00:00
4k
dvruette/pygba
src/pygba/gym_env.py
[ { "identifier": "KEY_MAP", "path": "src/pygba/utils.py", "snippet": "KEY_MAP = {\n \"up\": GBA.KEY_UP,\n \"down\": GBA.KEY_DOWN,\n \"left\": GBA.KEY_LEFT,\n \"right\": GBA.KEY_RIGHT,\n \"A\": GBA.KEY_A,\n \"B\": GBA.KEY_B,\n \"L\": GBA.KEY_L,\n \"R\": GBA.KEY_R,\n \"start\": GBA.KEY_START,\n \"select\": GBA.KEY_SELECT,\n}" }, { "identifier": "PyGBA", "path": "src/pygba/pygba.py", "snippet": "class PyGBA:\n @staticmethod\n def load(gba_file: str, save_file: str | None = None) -> \"PyGBA\":\n # create a temporary directory and copy the gba file into it\n # this is necessary to prevent mgba from overwriting the save file (and to prevent crashes)\n tmp_dir = Path(tempfile.mkdtemp())\n tmp_gba = tmp_dir / \"rom.gba\"\n tmp_gba.write_bytes(Path(gba_file).read_bytes())\n gba_file = str(tmp_gba)\n if save_file is not None:\n tmp_save = tmp_dir / \"rom.sav\"\n tmp_save.write_bytes(Path(save_file).read_bytes())\n save_file = str(tmp_save)\n\n core = mgba.core.load_path(gba_file)\n if core is None:\n raise ValueError(f\"Failed to load GBA file: {gba_file}\")\n if save_file is not None:\n core.autoload_save()\n core.reset()\n return PyGBA(core)\n \n def __init__(self, core: mgba.core.Core):\n self.core = core\n\n self.core.add_frame_callback(self._invalidate_mem_cache)\n self._mem_cache = {}\n\n def wait(self, frames: int):\n for _ in range(frames):\n self.core.run_frame()\n\n def press_key(self, key: str, frames: int = 2):\n if key not in KEY_MAP:\n raise ValueError(f\"Invalid key: {key}\")\n if frames < 2:\n raise ValueError(\"Cannot press a key for less than 2 frames.\")\n \n key = KEY_MAP[key]\n self.core.add_keys(key)\n self.wait(frames - 1)\n self.core.clear_keys(key)\n self.wait(1)\n\n def press_up(self, frames: int = 2):\n self.press_key(\"up\", frames)\n\n def press_down(self, frames: int = 2):\n self.press_key(\"down\", frames)\n\n def press_left(self, frames: int = 2):\n self.press_key(\"left\", frames)\n\n def press_right(self, frames: int = 2):\n self.press_key(\"right\", frames)\n\n def press_a(self, frames: int = 2):\n self.press_key(\"A\", frames)\n\n def press_b(self, frames: int = 2):\n self.press_key(\"B\", frames)\n\n def press_l(self, frames: int = 2):\n self.press_key(\"L\", frames)\n\n def press_r(self, frames: int = 2):\n self.press_key(\"R\", frames)\n\n def press_start(self, frames: int = 2):\n self.press_key(\"start\", frames)\n\n def press_select(self, frames: int = 2):\n self.press_key(\"select\", frames)\n\n def _invalidate_mem_cache(self):\n self._mem_cache = {}\n \n def _get_memory_region(self, region_id: int):\n if region_id not in self._mem_cache:\n mem_core = self.core.memory.u8._core\n size = ffi.new(\"size_t *\")\n ptr = ffi.cast(\"uint8_t *\", mem_core.getMemoryBlock(mem_core, region_id, size))\n self._mem_cache[region_id] = ffi.buffer(ptr, size[0])[:]\n return self._mem_cache[region_id]\n\n def read_memory(self, address: int, size: int = 1):\n region_id = address >> lib.BASE_OFFSET\n mem_region = self._get_memory_region(region_id)\n mask = len(mem_region) - 1\n address &= mask\n return mem_region[address:address + size]\n\n def read_u8(self, address: int):\n return int.from_bytes(self.read_memory(address, 1), byteorder='little', signed=False)\n\n def read_u16(self, address: int):\n return int.from_bytes(self.read_memory(address, 2), byteorder='little', signed=False)\n\n def read_u32(self, address: int):\n return int.from_bytes(self.read_memory(address, 4), byteorder='little', signed=False)" }, { "identifier": "GameWrapper", "path": "src/pygba/game_wrappers/base.py", "snippet": "class GameWrapper(ABC):\n @abstractmethod\n def reward(self, gba: PyGBA, observation: np.ndarray) -> float:\n raise NotImplementedError\n\n def game_over(self, gba: PyGBA, observation: np.ndarray) -> bool:\n return False\n \n def reset(self, gba: PyGBA) -> None:\n pass\n \n def info(self, gba: PyGBA, observation: np.ndarray) -> dict[str, Any]:\n return {}" } ]
import sys import gymnasium as gym import mgba.core import mgba.image import numpy as np import pygame from typing import Any, Literal from .utils import KEY_MAP from .pygba import PyGBA from .game_wrappers.base import GameWrapper from pygame import gfxdraw
2,181
try: except ImportError as e: pass def _pil_image_to_pygame(img): return pygame.image.fromstring(img.tobytes(), img.size, img.mode).convert() class PyGBAEnv(gym.Env): metadata = { "render_modes": ["human", "rgb_array"], "render_fps": 60, } def __init__( self, gba: PyGBA, game_wrapper: GameWrapper | None = None, obs_type: Literal["rgb", "grayscale"] = "rgb", frameskip: int | tuple[int, int] | tuple[int, int, int] = 0, repeat_action_probability: float = 0.0, render_mode: Literal["human", "rgb_array"] | None = None, reset_to_initial_state: bool = True, max_episode_steps: int | None = None, **kwargs, ): self.gba = gba if not isinstance(gba, PyGBA): raise TypeError(f"core must be a PyGBA object (got {type(gba)})") self.game_wrapper = game_wrapper if game_wrapper is not None and not isinstance(game_wrapper, GameWrapper): raise TypeError(f"game_wrapper must be a GameWrapper object (got {type(game_wrapper)})") if game_wrapper is None: gym.logger.warn( "You didn't pass a GameWrapper to the base GBA environment, " "which means that there is no reward calculation and no game over detection." ) self.obs_type = obs_type self.frameskip = frameskip self.repeat_action_probability = repeat_action_probability self.render_mode = render_mode self.max_episode_steps = max_episode_steps self.arrow_keys = [None, "up", "down", "right", "left"] self.buttons = [None, "A", "B", "select", "start", "L", "R"] # cartesian product of arrows and buttons, i.e. can press 1 arrow and 1 button at the same time self.actions = [(a, b) for a in self.arrow_keys for b in self.buttons] self.action_space = gym.spaces.Discrete(len(self.actions)) # Building the observation_space screen_size = self.gba.core.desired_video_dimensions() if obs_type == "rgb": screen_size += (3,) self.observation_space = gym.spaces.Box(low=0, high=255, shape=screen_size, dtype=np.uint8) self._framebuffer = mgba.image.Image(*self.gba.core.desired_video_dimensions()) self.gba.core.set_video_buffer(self._framebuffer) # need to reset after this self._screen = None self._clock = None self._total_reward = 0 self._step = 0 if reset_to_initial_state: self._initial_state = self.gba.core.save_raw_state() pass else: self._initial_state = None self._kwargs = kwargs self.reset() def get_action_by_id(self, action_id: int) -> tuple[Any, Any]: if action_id < 0 or action_id > len(self.actions): raise ValueError(f"action_id {action_id} is invalid") return self.actions[action_id] def get_action_id(self, arrow: str, button: str) -> int: action = (arrow, button) if action not in self.actions: raise ValueError(f"Invalid action: Must be a tuple of (arrow, button)") return self.actions.index(action) def _get_observation(self): img = self._framebuffer.to_pil().convert("RGB") if self.obs_type == "grayscale": img = img.convert("L") return np.array(img).transpose(1, 0, 2) def step(self, action_id): info = {} actions = self.get_action_by_id(action_id)
try: except ImportError as e: pass def _pil_image_to_pygame(img): return pygame.image.fromstring(img.tobytes(), img.size, img.mode).convert() class PyGBAEnv(gym.Env): metadata = { "render_modes": ["human", "rgb_array"], "render_fps": 60, } def __init__( self, gba: PyGBA, game_wrapper: GameWrapper | None = None, obs_type: Literal["rgb", "grayscale"] = "rgb", frameskip: int | tuple[int, int] | tuple[int, int, int] = 0, repeat_action_probability: float = 0.0, render_mode: Literal["human", "rgb_array"] | None = None, reset_to_initial_state: bool = True, max_episode_steps: int | None = None, **kwargs, ): self.gba = gba if not isinstance(gba, PyGBA): raise TypeError(f"core must be a PyGBA object (got {type(gba)})") self.game_wrapper = game_wrapper if game_wrapper is not None and not isinstance(game_wrapper, GameWrapper): raise TypeError(f"game_wrapper must be a GameWrapper object (got {type(game_wrapper)})") if game_wrapper is None: gym.logger.warn( "You didn't pass a GameWrapper to the base GBA environment, " "which means that there is no reward calculation and no game over detection." ) self.obs_type = obs_type self.frameskip = frameskip self.repeat_action_probability = repeat_action_probability self.render_mode = render_mode self.max_episode_steps = max_episode_steps self.arrow_keys = [None, "up", "down", "right", "left"] self.buttons = [None, "A", "B", "select", "start", "L", "R"] # cartesian product of arrows and buttons, i.e. can press 1 arrow and 1 button at the same time self.actions = [(a, b) for a in self.arrow_keys for b in self.buttons] self.action_space = gym.spaces.Discrete(len(self.actions)) # Building the observation_space screen_size = self.gba.core.desired_video_dimensions() if obs_type == "rgb": screen_size += (3,) self.observation_space = gym.spaces.Box(low=0, high=255, shape=screen_size, dtype=np.uint8) self._framebuffer = mgba.image.Image(*self.gba.core.desired_video_dimensions()) self.gba.core.set_video_buffer(self._framebuffer) # need to reset after this self._screen = None self._clock = None self._total_reward = 0 self._step = 0 if reset_to_initial_state: self._initial_state = self.gba.core.save_raw_state() pass else: self._initial_state = None self._kwargs = kwargs self.reset() def get_action_by_id(self, action_id: int) -> tuple[Any, Any]: if action_id < 0 or action_id > len(self.actions): raise ValueError(f"action_id {action_id} is invalid") return self.actions[action_id] def get_action_id(self, arrow: str, button: str) -> int: action = (arrow, button) if action not in self.actions: raise ValueError(f"Invalid action: Must be a tuple of (arrow, button)") return self.actions.index(action) def _get_observation(self): img = self._framebuffer.to_pil().convert("RGB") if self.obs_type == "grayscale": img = img.convert("L") return np.array(img).transpose(1, 0, 2) def step(self, action_id): info = {} actions = self.get_action_by_id(action_id)
actions = [KEY_MAP[a] for a in actions if a is not None]
0
2023-11-08 20:51:13+00:00
4k
AdFiFi/D-FaST
utils/trainer.py
[ { "identifier": "init_model_config", "path": "config.py", "snippet": "def init_model_config(args, data_config: DataConfig):\r\n if args.model == \"BNT\":\r\n model_config = BNTConfig(node_size=data_config.node_size,\r\n sizes=(data_config.node_size, data_config.node_size // 2),\r\n num_classes=data_config.num_class,\r\n pooling=(False, True),\r\n pos_encoding=None, # identity, none\r\n orthogonal=True,\r\n # freeze_center=True,\r\n freeze_center=False,\r\n project_assignment=True,\r\n num_heads=args.num_heads,\r\n pos_embed_dim=data_config.node_size,\r\n dim_feedforward=1024,\r\n )\r\n model = BNT(model_config)\r\n elif args.model == \"FBNetGen\":\r\n model_config = FBNetGenConfig(activation='gelu',\r\n dropout=0.5,\r\n # extractor_type='gru', # gru or cnn\r\n extractor_type='cnn', # gru or cnn\r\n # d_model=16,\r\n d_model=40,\r\n node_size=data_config.node_size,\r\n node_feature_size=data_config.node_feature_size,\r\n time_series_size=data_config.time_series_size,\r\n num_classes=data_config.num_class,\r\n window_size=5,\r\n # window_size=40,\r\n # window_size=50,\r\n cnn_pool_size=16,\r\n graph_generation='product', # product or linear\r\n num_gru_layers=4,\r\n group_loss=True,\r\n sparsity_loss=True,\r\n sparsity_loss_weight=1.0e-4)\r\n model = FBNetGen(model_config)\r\n elif args.model == 'BrainNetCNN':\r\n model_config = BrainNetCNNConfig(node_size=data_config.node_size,\r\n num_classes=data_config.num_class)\r\n model = BrainNetCNN(model_config)\r\n elif args.model == 'STAGIN':\r\n model_config = STAGINConfig(node_size=data_config.node_size,\r\n num_classes=data_config.num_class,\r\n d_model=args.d_model,\r\n num_layers=args.num_layers,\r\n window_size=args.window_size,\r\n window_stride=args.window_stride,\r\n dynamic_length=args.dynamic_length,\r\n sampling_init=args.sampling_init)\r\n model = STAGIN(model_config)\r\n elif args.model == \"Transformer\":\r\n model_config = TransformerConfig(node_size=data_config.node_size,\r\n num_classes=data_config.num_class,\r\n node_feature_size=data_config.node_feature_size,\r\n readout='concat',\r\n num_layers=args.num_layers)\r\n model = Transformer(model_config)\r\n elif args.model == \"EEGNet\":\r\n model_config = EEGNetConfig(node_size=data_config.node_size,\r\n time_series_size=data_config.time_series_size,\r\n node_feature_size=data_config.node_feature_size,\r\n num_classes=data_config.num_class,\r\n frequency=args.frequency,\r\n D=args.D,\r\n num_kernels=args.num_kernels,\r\n p1=args.p1,\r\n p2=args.p2,\r\n dropout=args.dropout)\r\n model_config.class_weight = data_config.class_weight\r\n model = EEGNet(model_config)\r\n elif args.model == \"DFaST\":\r\n model_config = DFaSTConfig(node_size=data_config.node_size,\r\n time_series_size=data_config.time_series_size,\r\n node_feature_size=data_config.node_feature_size,\r\n num_classes=data_config.num_class,\r\n sparsity=args.sparsity,\r\n frequency=args.frequency,\r\n D=args.D,\r\n p1=args.p1,\r\n p2=args.p2,\r\n k=args.k,\r\n num_kernels=args.num_kernels,\r\n d_model=args.d_model,\r\n window_size=args.window_size,\r\n window_stride=args.window_stride,\r\n dynamic_length=args.dynamic_length,\r\n num_heads=args.num_heads,\r\n dim_feedforward=args.dim_feedforward,\r\n num_spatial_layers=args.num_layers,\r\n num_node_temporal_layers=args.num_node_temporal_layers,\r\n num_graph_temporal_layers=args.num_graph_temporal_layers,\r\n attention_depth=args.attention_depth,\r\n activation=args.activation,\r\n dropout=args.dropout,\r\n # distill=(False, ) + (args.num_layers - 1) *\r\n # ((True,) if args.distill else (False,)),\r\n distill=args.num_layers * ((True,) if args.distill else (False,)),\r\n initializer=args.initializer,\r\n label_smoothing=args.epsilon_ls\r\n )\r\n model_config.class_weight = data_config.class_weight\r\n model = DFaSTForClassification(model_config)\r\n elif args.model == \"DFaSTOnlySpatial\":\r\n model_config = DFaSTConfig(node_size=data_config.node_size,\r\n time_series_size=data_config.time_series_size,\r\n node_feature_size=data_config.node_feature_size,\r\n num_classes=data_config.num_class,\r\n sparsity=args.sparsity,\r\n frequency=args.frequency,\r\n D=args.D,\r\n p1=args.p1,\r\n p2=args.p2,\r\n k=args.k,\r\n num_kernels=args.num_kernels,\r\n d_model=args.d_model,\r\n window_size=args.window_size,\r\n window_stride=args.window_stride,\r\n dynamic_length=args.dynamic_length,\r\n num_heads=args.num_heads,\r\n dim_feedforward=args.dim_feedforward,\r\n num_spatial_layers=args.num_layers,\r\n num_node_temporal_layers=args.num_node_temporal_layers,\r\n num_graph_temporal_layers=args.num_graph_temporal_layers,\r\n attention_depth=args.attention_depth,\r\n activation=args.activation,\r\n dropout=args.dropout,\r\n # distill=(False, ) + (args.num_layers - 1) *\r\n # ((True,) if args.distill else (False,)),\r\n distill=args.num_layers * ((True,) if args.distill else (False,)),\r\n initializer=args.initializer,\r\n label_smoothing=args.epsilon_ls\r\n )\r\n model_config.class_weight = data_config.class_weight\r\n model = DFaSTOnlySpatialForClassification(model_config)\r\n elif args.model == \"LMDA\":\r\n model_config = LMDAConfig(node_size=data_config.node_size,\r\n time_series_size=data_config.time_series_size,\r\n node_feature_size=data_config.node_feature_size,\r\n num_classes=data_config.num_class,\r\n depth=9,\r\n channel_depth1=args.num_kernels,\r\n channel_depth2=9,\r\n ave_depth=1,\r\n avepool=5\r\n )\r\n model_config.class_weight = data_config.class_weight\r\n model = LMDA(model_config)\r\n elif args.model == \"ShallowConvNet\":\r\n model_config = ShallowConvNetConfig(node_size=data_config.node_size,\r\n time_series_size=data_config.time_series_size,\r\n node_feature_size=data_config.node_feature_size,\r\n num_classes=data_config.num_class,\r\n num_kernels=args.num_kernels\r\n )\r\n model_config.class_weight = data_config.class_weight\r\n model = ShallowConvNet(model_config)\r\n elif args.model == \"DeepConvNet\":\r\n model_config = DeepConvNetConfig(node_size=data_config.node_size,\r\n time_series_size=data_config.time_series_size,\r\n node_feature_size=data_config.node_feature_size,\r\n num_classes=data_config.num_class,\r\n num_kernels=25\r\n )\r\n model_config.class_weight = data_config.class_weight\r\n model = DeepConvNet(model_config)\r\n elif args.model == \"RACNN\":\r\n model_config = RACNNConfig(node_size=data_config.node_size,\r\n time_series_size=data_config.time_series_size,\r\n node_feature_size=data_config.node_feature_size,\r\n num_classes=data_config.num_class,\r\n k=args.k\r\n )\r\n model_config.class_weight = data_config.class_weight\r\n model = RACNN(model_config)\r\n elif args.model == \"EEGChannelNet\":\r\n model_config = EEGChannelNetConfig(node_size=data_config.node_size,\r\n time_series_size=data_config.time_series_size,\r\n node_feature_size=data_config.node_feature_size,\r\n num_classes=data_config.num_class\r\n )\r\n model_config.class_weight = data_config.class_weight\r\n model = EEGChannelNet(model_config)\r\n elif args.model == \"TCANet\":\r\n model_config = TCANetConfig(node_size=data_config.node_size,\r\n time_series_size=data_config.time_series_size,\r\n node_feature_size=data_config.node_feature_size,\r\n num_classes=data_config.num_class\r\n )\r\n model_config.class_weight = data_config.class_weight\r\n model = TCANet(model_config)\r\n elif args.model == \"TCACNet\":\r\n model_config = TCACNetConfig(node_size=data_config.node_size,\r\n time_series_size=data_config.time_series_size,\r\n node_feature_size=data_config.node_feature_size,\r\n num_classes=data_config.num_class\r\n )\r\n model_config.class_weight = data_config.class_weight\r\n model = TCACNet(model_config)\r\n elif args.model == \"SBLEST\":\r\n model_config = SBLESTConfig(node_size=data_config.node_size,\r\n time_series_size=data_config.time_series_size,\r\n node_feature_size=data_config.node_feature_size,\r\n num_classes=data_config.num_class\r\n )\r\n model_config.class_weight = data_config.class_weight\r\n model = SBLEST(model_config)\r\n else:\r\n model = None\r\n model_config = None\r\n if model is not None:\r\n init_parameters(model, model_config)\r\n return model, model_config\r" }, { "identifier": "init_optimizer", "path": "utils/optimizer.py", "snippet": "def init_optimizer(model: torch.nn.Module, optimizer_config=None) -> torch.optim.Optimizer:\r\n parameters = {\r\n 'lr': optimizer_config.learning_rate,\r\n 'weight_decay': optimizer_config.weight_decay\r\n }\r\n\r\n if optimizer_config.no_weight_decay:\r\n params, _ = get_param_group_no_wd(model,\r\n match_rule=optimizer_config.match_rule,\r\n except_rule=optimizer_config.except_rule)\r\n else:\r\n params = list(model.parameters())\r\n logging.info(f'Parameters [normal] length [{len(params)}]')\r\n\r\n parameters['params'] = params\r\n\r\n optimizer_type = optimizer_config.optimizer\r\n if optimizer_type == 'SGD':\r\n parameters['momentum'] = optimizer_config.momentum\r\n parameters['nesterov'] = optimizer_config.nesterov\r\n return getattr(torch.optim, optimizer_type)(**parameters)\r" }, { "identifier": "init_schedule", "path": "utils/schedule.py", "snippet": "def init_schedule(optimizer, args, t_total):\r\n if args.schedule == 'cos':\r\n schedule = CosineAnnealingLR(optimizer, eta_min=args.target_learning_rate, T_max=t_total)\r\n elif args.schedule == 'cos_w':\r\n schedule = get_cosine_annealing_schedule_with_warmup(optimizer, eta_max=args.learning_rate,\r\n eta_min=args.target_learning_rate,\r\n num_warmup_steps=args.warmup_steps,\r\n num_training_steps=t_total)\r\n elif args.schedule == 'linear':\r\n schedule = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=args.warmup_steps,\r\n num_training_steps=t_total)\r\n elif args.schedule == 'one_cycle':\r\n schedule = OneCycleLR(optimizer,\r\n max_lr=args.max_learning_rate,\r\n epochs=args.num_epochs,\r\n steps_per_epoch=t_total // args.num_epochs,\r\n pct_start=0.2,\r\n div_factor=args.max_learning_rate/args.learning_rate,\r\n final_div_factor=1000)\r\n else:\r\n schedule = None\r\n return schedule\r" }, { "identifier": "accuracy", "path": "utils/accuracy.py", "snippet": "def accuracy(output: torch.Tensor, target: torch.Tensor, top_k=(1,)) -> List[float]:\r\n \"\"\"Computes the precision@k for the specified values of k\"\"\"\r\n max_k = max(top_k)\r\n batch_size = target.size(0)\r\n\r\n _, predict = output.topk(max_k, 1, True, True)\r\n predict = predict.t()\r\n correct = predict.eq(target.view(1, -1).expand_as(predict))\r\n\r\n res = []\r\n for k in top_k:\r\n correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)\r\n res.append(correct_k.mul_(100.0 / batch_size).item())\r\n return res\r" } ]
import json import os import wandb import logging import torch import numpy as np from timeit import default_timer as timer from abc import abstractmethod from torch.nn import functional as F from tqdm import tqdm from sklearn.metrics import roc_auc_score, accuracy_score from sklearn.metrics import precision_recall_fscore_support, classification_report from config import init_model_config from .optimizer import init_optimizer from .schedule import init_schedule from .accuracy import accuracy from data import *
2,932
logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) class Trainer(object): def __init__(self, args, local_rank=0, task_id=0, subject_id=0): self.task_id = task_id self.args = args self.local_rank = local_rank self.subject_id = subject_id self.data_config = DataConfig(args) self.data_loaders = self.load_datasets()
logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) class Trainer(object): def __init__(self, args, local_rank=0, task_id=0, subject_id=0): self.task_id = task_id self.args = args self.local_rank = local_rank self.subject_id = subject_id self.data_config = DataConfig(args) self.data_loaders = self.load_datasets()
model, self.model_config = init_model_config(args, self.data_config)
0
2023-11-07 13:57:36+00:00
4k
seolmango/galaxy_simulation
main.py
[ { "identifier": "Galaxy", "path": "Galaxy.py", "snippet": "class Galaxy:\n\n def __init__(self, galmass, ahalo, vhalo, rthalo, galpos, galvel):\n self.galmass = galmass\n self.ahalo = ahalo\n self.vhalo = vhalo\n self.rthalo = rthalo\n self.galpos = galpos\n self.galvel = galvel\n self.galacc = np.full((3, 1), 0.)\n\n def setPosvel(self, pos, vel):\n self.galpos = pos\n self.galvel = vel\n\n def scaleMass(self, massFact):\n self.galmass = self.galmass * massFact\n self.vhalo = 1.0 * massFact ** 0.25\n self.ahalo = 0.1 * massFact ** 0.5\n a2 = -self.galmass / (self.vhalo**2)\n a1 = -2.0 * self.ahalo * self.galmass / (self.vhalo**2)\n a0 = -self.galmass * (self.ahalo ** 2) / (self.vhalo**2)\n q = a1/3.0 - (a2**2)/9.0\n r = (a1*a2 - 3.0*a0)/6.0 - (a2**3)/27.0\n\n s1 = (r + np.sqrt(q**3 + r**2))**(1.0/3.0)\n s2 = (r - np.sqrt(q**3 + r**2))**(1.0/3.0)\n\n self.rthalo = (s1+s2) - a2/3.0\n\n def MoveGalaxy(self, dtime):\n newpos = self.galpos + self.galvel * dtime + 0.5 * self.galacc * (dtime**2)\n newvel = self.galvel + self.galacc * dtime\n\n self.galpos = newpos\n self.galvel = newvel\n\n def Acceleration(self, posin):\n G = 1.0\n dpos = posin - self.galpos\n\n r = np.sqrt(np.sum(dpos**2, axis=0))\n AccMag = -(G * self.InteriorMass(r))/(r**2)\n calcacc = (dpos*AccMag)/r\n\n return calcacc\n\n def Potential(self, posin):\n G = 1.0\n dpos = posin - self.galpos\n\n r = np.sqrt(np.sum(dpos**2, axis=0))\n pot = G * self.InteriorMass(r)/r\n\n return pot\n\n def InteriorMass(self, r):\n indices = r < self.rthalo\n\n intmass = np.full(r.shape, 0.)\n\n if intmass[indices].shape != (0,):\n intmass[indices] = (self.vhalo**2) * (r[indices]**3) / ((self.ahalo+r[indices])**2)\n\n if intmass[~indices].shape != (0,):\n intmass[~indices] = self.galmass\n\n return intmass\n\n def Density(self, r):\n rinner = r * 0.99\n router = r * 1.01\n minner = self.InteriorMass(rinner)\n mouter = self.InteriorMass(router)\n dm = mouter - minner\n vol = (4.0/3.0) * np.pi * ((router**3) - (rinner**3))\n dens = dm / vol\n\n return dens\n\n def DynFric(self, pmass, ppos, pvel):\n G = 1.0\n in_gamma = 3.0\n dv = pvel - self.galvel\n v = np.linalg.norm(dv)\n dr = ppos - self.galpos\n r = np.linalg.norm(dr)\n galrho = self.Density(r)\n fricmag = 4.0*np.pi*G*in_gamma*pmass*galrho*v/((1+v)**3)\n fric = (-dv/v)*fricmag\n\n return fric" }, { "identifier": "StarGalaxy", "path": "StarGalaxy.py", "snippet": "class StarGalaxy(Galaxy):\n\n def __init__(self, galmass, ahalo, vhalo, rthalo, galpos, galvel, diskSize, galthata, galphi, n):\n super().__init__(galmass, ahalo, vhalo, rthalo, galpos, galvel)\n self.diskSize = diskSize\n self.galthata = galthata\n self.galphi = galphi\n self.n = n\n\n self.starpos = np.full((3, self.n), 0.)\n self.starvel = np.full((3, self.n), 0.)\n self.staracc = np.full((3, self.n), 0.)\n\n def MoveStars(self, dtime):\n newstarpos = self.starpos + self.starvel * dtime + 0.5 * self.staracc * (dtime**2)\n newstarvel = self.starvel + self.staracc * dtime\n\n self.starpos = newstarpos\n self.starvel = newstarvel\n\n def InitStars(self):\n cosphi = np.cos(self.galphi)\n sinphi = np.sin(self.galphi)\n costheta = np.cos(self.galthata)\n sintheta = np.sin(self.galthata)\n for i in range(self.n):\n bad = True\n while bad:\n xtry = self.diskSize*(1.0-2.0*np.random.random())\n ytry = self.diskSize*(1.0-2.0*np.random.random())\n rtry = np.sqrt(xtry**2 + ytry**2)\n if rtry < self.diskSize:\n bad = False\n\n ztry = 0.0\n xrot = xtry*cosphi + ytry*sinphi*costheta + ztry*sinphi*sintheta\n yrot = -xtry*sinphi + ytry*cosphi*costheta + ztry*cosphi*sintheta\n zrot = -ytry*sintheta + ztry*costheta\n rot = np.array([xrot, yrot, zrot])\n self.starpos[:, i] = rot + self.galpos.reshape(-1)\n\n vcirc = np.sqrt(self.InteriorMass(rtry)/rtry)\n\n vxtry = -vcirc*yrot/rtry\n vytry = vcirc*xrot/rtry\n vztry = 0.0\n\n vxrot = vxtry*cosphi + vytry*sinphi*costheta + vztry*sinphi*sintheta\n vyrot = -vxtry*sinphi + vytry*cosphi*costheta + vztry*cosphi*sintheta\n vzrot = -vytry*sintheta + vztry*costheta\n\n vrot = np.array([vxrot, vyrot, vzrot])\n self.starvel[:, i] = vrot + self.galvel.reshape(-1)\n self.staracc = np.full((1, 3), 0.0)\n\n def scaleMass(self, massFact):\n self.diskSize = self.diskSize * np.sqrt(massFact)\n super().scaleMass(massFact)" }, { "identifier": "Orbit", "path": "Orbit.py", "snippet": "class Orbit:\n\n def __init__(self, energy, rp, tp, eccentricity, m1, m2, bod1pos, bod2pos, bod1vel, bod2vel):\n self.energy = energy\n self.rp = rp\n self.tp = tp\n self.eccentricity = eccentricity\n self.m1 = m1\n self.m2 = m2\n self.bod1pos = bod1pos\n self.bod2pos = bod2pos\n self.bod1vel = bod1vel\n self.bod2vel = bod2vel\n self.initOrbit()\n\n def initOrbit(self):\n mu = self.m1 + self.m2\n\n p = 2*self.rp\n nhat = np.sqrt(mu/(p**3))\n cots = 3.0 * nhat * self.tp\n s = np.arctan(1.0/cots)\n cottheta = (1.0/(np.tan(s/2.0)))**(1/3)\n theta = np.arctan(1.0/cottheta)\n tanfon2 = 2.0/np.tan(2.0*theta)\n r = (p/2.0)*(1+tanfon2**2)\n\n vel = np.sqrt(2.0*mu/r)\n sinsqphi = p/(2.0*r)\n phi = np.arcsin(np.sqrt(sinsqphi))\n f = 2.0*np.arctan(tanfon2)\n xc = -r*np.cos(f)\n yc = r*np.sin(f)\n vxc = vel*np.cos(f+phi)\n vyc = -vel*np.sin(f+phi)\n xcom = self.m2 * xc / mu\n ycom = self.m2 * yc / mu\n vxcom = self.m2 * vxc / mu\n vycom = self.m2 * vyc / mu\n\n self.bod1pos = np.array([[-xcom], [-ycom], [0.0]])\n self.bod1vel = np.array([[-vxcom], [-vycom], [0.0]])\n self.bod2pos = np.array([[xc-xcom], [yc-ycom], [0.0]])\n self.bod2vel = np.array([[vxc-vxcom], [vyc-vycom], [0.0]])" } ]
from Galaxy import Galaxy from StarGalaxy import StarGalaxy from Orbit import Orbit import numpy as np import tqdm
2,666
class Sim: def MakeGalaxy(self): # Constants galmass = 4.8 ahalo = 0.1 vhalo = 1.0 rthalo = 5.0 galpos = np.full((3, 1), 0.) galvel = np.full((3, 1), 0.) diskSize = 2.5 # Initial conditions galtheta = float(input("galtheta(은하 1의 세타) > ")) galphi = float(input("galphi(은하 1의 파이) > ")) comptheta = float(input("comptheta(은하 2의 세타) > ")) compphi = float(input("compphi(은하 2의 파이) > ")) total_star_num = int(input("total_star_num(전체 별의 수) > ")) galn = int(0.5*total_star_num) compn = int(0.5*total_star_num)
class Sim: def MakeGalaxy(self): # Constants galmass = 4.8 ahalo = 0.1 vhalo = 1.0 rthalo = 5.0 galpos = np.full((3, 1), 0.) galvel = np.full((3, 1), 0.) diskSize = 2.5 # Initial conditions galtheta = float(input("galtheta(은하 1의 세타) > ")) galphi = float(input("galphi(은하 1의 파이) > ")) comptheta = float(input("comptheta(은하 2의 세타) > ")) compphi = float(input("compphi(은하 2의 파이) > ")) total_star_num = int(input("total_star_num(전체 별의 수) > ")) galn = int(0.5*total_star_num) compn = int(0.5*total_star_num)
self.galaxy = StarGalaxy(galmass, ahalo, vhalo, rthalo, galpos, galvel, diskSize, galtheta, galphi, galn)
1
2023-11-05 05:21:54+00:00
4k
YihePang/DisoFLAG
prepare_model_data.py
[ { "identifier": "load_file_2_data", "path": "load_data.py", "snippet": "def load_file_2_data(file_path):\n\tloadfile = open(file_path,\"r\") \t\n\tload_f = []\n\tfor line in loadfile:\n\t\tline=line.strip('\\n')\n\t\tload_f.append(line)\n\tloadfile.close()\n\n\tload_data = []\n\tfor i in range(len(load_f)):\n\t\tif i % 2 == 0:\n\t\t\tload_data.append(load_f[i:i+2]) #one data: [0]--id [1]--seq \n\t# print(\"load_file: \",file_path,\" data length: \",len(load_data)) \n\treturn load_data" }, { "identifier": "file_2_data", "path": "load_data.py", "snippet": "def file_2_data(data_file_name):\n\tseq_id = [] \t\n\tseq = [] \t\n\n\tseq_label_IDP = [] \t \n\n\tseq_label_F1 = [] \t \n\tseq_label_F2 = [] \t \n\tseq_label_F3 = [] \t \n\n\tseq_label_F4 = [] \t \n\tseq_label_F5 = [] \t \n\tseq_label_F6 = [] \t \n\n\tseq_T5_feature = [] \n\tseq_BERT_feature = [] \n\tseq_IDP_feature = [] \n\n \tdata_list = load_file_2_data(data_file_name)\n\n\tfor i in range(len(data_list)):\n\t\tone_seq_id = data_list[i][0][1:].replace('\\r', '')\n\n\t\tseq_id.append(one_seq_id) \n\t\tseq.append(data_list[i][1].replace('\\r', '')) \n\n\t\tseq_label_IDP.append(['1']*len(data_list[i][1].replace('\\r', ''))) \n\n\t\tseq_label_F1.append(['1']*len(data_list[i][1].replace('\\r', ''))) \n\t\tseq_label_F2.append(['1']*len(data_list[i][1].replace('\\r', ''))) \n\t\tseq_label_F3.append(['1']*len(data_list[i][1].replace('\\r', ''))) \n\n\t\tseq_label_F4.append(['1']*len(data_list[i][1].replace('\\r', ''))) \n\t\tseq_label_F5.append(['1']*len(data_list[i][1].replace('\\r', ''))) \n\t\tseq_label_F6.append(['1']*len(data_list[i][1].replace('\\r', ''))) \n\n\t\t# embeddings\n\t\tT5_embedding_path = './temp/embeddings/T5/'\n\t\t\n\t\tT5_feature_file = T5_embedding_path + one_seq_id + '.npy'\n\t\t\n\t\tone_T5_vec = np.load(T5_feature_file,allow_pickle=True)\n\t\tone_T5_vec = one_T5_vec.reshape(len(one_T5_vec),-1)\n\t\tseq_T5_feature.append(one_T5_vec) \n\n\treturn np.array(seq_id),np.array(seq),np.array(seq_label_IDP),np.array(seq_label_F1),np.array(seq_label_F2),np.array(seq_label_F3),np.array(seq_label_F4),np.array(seq_label_F5),np.array(seq_label_F6),np.array(seq_T5_feature)" } ]
import numpy as np import random from load_data import load_file_2_data, file_2_data
2,439
res_mask_4_new = [] res_mask_5_new = [] res_mask_6_new = [] seq_mask_new = [] for i in range(len(seq)): s = 0 for j in range(int(-(-len(seq[i])//max_seq_length))): if s+max_seq_length >= len(seq[i]): end = len(seq[i]) - s seq_id_new.append(seq_id[i]) seq_new.append(seq[i][s:s+end]) seq_label_0_new.append(seq_label_0[i][s:s+end]) seq_label_1_new.append(seq_label_1[i][s:s+end]) seq_label_2_new.append(seq_label_2[i][s:s+end]) seq_label_3_new.append(seq_label_3[i][s:s+end]) seq_label_4_new.append(seq_label_4[i][s:s+end]) seq_label_5_new.append(seq_label_5[i][s:s+end]) seq_label_6_new.append(seq_label_6[i][s:s+end]) seq_T5_feature_new.append(seq_T5_feature[i][s:s+end]) res_mask_0_new.append(res_mask_0[i][s:s+end]) res_mask_1_new.append(res_mask_1[i][s:s+end]) res_mask_2_new.append(res_mask_2[i][s:s+end]) res_mask_3_new.append(res_mask_3[i][s:s+end]) res_mask_4_new.append(res_mask_4[i][s:s+end]) res_mask_5_new.append(res_mask_5[i][s:s+end]) res_mask_6_new.append(res_mask_6[i][s:s+end]) seq_mask_new.append(seq_mask[i][s:s+end]) elif s+max_seq_length < len(seq[i]): seq_id_new.append(seq_id[i]) seq_new.append(seq[i][s:s+max_seq_length]) seq_label_0_new.append(seq_label_0[i][s:s+max_seq_length]) seq_label_1_new.append(seq_label_1[i][s:s+max_seq_length]) seq_label_2_new.append(seq_label_2[i][s:s+max_seq_length]) seq_label_3_new.append(seq_label_3[i][s:s+max_seq_length]) seq_label_4_new.append(seq_label_4[i][s:s+max_seq_length]) seq_label_5_new.append(seq_label_5[i][s:s+max_seq_length]) seq_label_6_new.append(seq_label_6[i][s:s+max_seq_length]) seq_T5_feature_new.append(seq_T5_feature[i][s:s+max_seq_length]) res_mask_0_new.append(res_mask_0[i][s:s+max_seq_length]) res_mask_1_new.append(res_mask_1[i][s:s+max_seq_length]) res_mask_2_new.append(res_mask_2[i][s:s+max_seq_length]) res_mask_3_new.append(res_mask_3[i][s:s+max_seq_length]) res_mask_4_new.append(res_mask_4[i][s:s+max_seq_length]) res_mask_5_new.append(res_mask_5[i][s:s+max_seq_length]) res_mask_6_new.append(res_mask_6[i][s:s+max_seq_length]) seq_mask_new.append(seq_mask[i][s:s+max_seq_length]) s = s+max_seq_length return seq_id_new, seq_new, seq_label_0_new,seq_label_1_new,seq_label_2_new,seq_label_3_new,seq_label_4_new, seq_label_5_new, seq_label_6_new, seq_T5_feature_new, res_mask_0_new, res_mask_1_new, res_mask_2_new, res_mask_3_new, res_mask_4_new, res_mask_5_new, res_mask_6_new, seq_mask_new def padding_list(input_list, max_seq_length): pad = 0 # zero-padding out_list = [] if len(input_list) < max_seq_length: for i in range(len(input_list)): out_list.append(input_list[i]) for j in range(max_seq_length-len(input_list)): out_list.append(pad) else: for i in range(max_seq_length): out_list.append(input_list[i]) return np.array(out_list) def padding_matrix(input_mat, max_seq_length): input_mat = np.array(input_mat) mat_dim = input_mat.shape[-1] pad_vector = np.zeros([mat_dim]) # zero-padding out_mat = [] if len(input_mat) < max_seq_length: for i in range(len(input_mat)): out_mat.append(input_mat[i]) for j in range(max_seq_length-len(input_mat)): out_mat.append(pad_vector) else: for i in range(max_seq_length): out_mat.append(input_mat[i]) return np.array(out_mat) def seq_lable_padding(seq_label, max_seq_length): out_list = [] for i in range(len(seq_label)): new_list = padding_list(seq_label[i], max_seq_length) # print(new_list) out_list.append(new_list) return np.array(out_list) def seq_feature_padding(seq_feature, max_seq_length): out_mat = [] for i in range(len(seq_feature)): new_f = padding_matrix(seq_feature[i], max_seq_length) out_mat.append(new_f) return np.array(out_mat) def mask_padding(res_mask,max_seq_length): out_list = [] for i in range(len(res_mask)): new_list = padding_list(res_mask[i], max_seq_length) # print(new_list) out_list.append(new_list) return np.array(out_list) def data_2_samples(args, data_file_name, is_slice):
# -*- coding: utf-8 -*- # @Author: Yihe Pang # @Date: 2023-02-27 10:43:18 # @Last Modified by: Yihe Pang # @Last Modified time: 2023-06-14 22:46:51 def residue_mask(seq_label): mask = [] for s in range(len(seq_label)): lable_mask = [] for i in range(len(seq_label[s])): if seq_label[s][i] == '1' or seq_label[s][i] == '0': lable_mask.append(1) else: lable_mask.append(0) mask.append(lable_mask) return mask def sequence_mask(seq): mask = [] for s in range(len(seq)): lable_mask = [] for i in range(len(seq[s])): lable_mask.append(1) mask.append(lable_mask) return mask def lable_2_value(seq_label): new_seq_label = [] for s in range(len(seq_label)): lable = [] for i in range(len(seq_label[s])): if seq_label[s][i] == '1': lable.append(1) else: lable.append(0) new_seq_label.append(lable) return new_seq_label def slice_data(seq_id,seq,seq_label_0,seq_label_1,seq_label_2,seq_label_3,seq_label_4,seq_label_5,seq_label_6,seq_T5_feature,res_mask_0,res_mask_1,res_mask_2,res_mask_3,res_mask_4,res_mask_5,res_mask_6,seq_mask,max_seq_length): seq_id_new = [] seq_new = [] seq_label_0_new = [] seq_label_1_new = [] seq_label_2_new = [] seq_label_3_new = [] seq_label_4_new = [] seq_label_5_new = [] seq_label_6_new = [] seq_T5_feature_new = [] res_mask_0_new = [] res_mask_1_new = [] res_mask_2_new = [] res_mask_3_new = [] res_mask_4_new = [] res_mask_5_new = [] res_mask_6_new = [] seq_mask_new = [] for i in range(len(seq)): s = 0 for j in range(int(-(-len(seq[i])//max_seq_length))): if s+max_seq_length >= len(seq[i]): end = len(seq[i]) - s seq_id_new.append(seq_id[i]) seq_new.append(seq[i][s:s+end]) seq_label_0_new.append(seq_label_0[i][s:s+end]) seq_label_1_new.append(seq_label_1[i][s:s+end]) seq_label_2_new.append(seq_label_2[i][s:s+end]) seq_label_3_new.append(seq_label_3[i][s:s+end]) seq_label_4_new.append(seq_label_4[i][s:s+end]) seq_label_5_new.append(seq_label_5[i][s:s+end]) seq_label_6_new.append(seq_label_6[i][s:s+end]) seq_T5_feature_new.append(seq_T5_feature[i][s:s+end]) res_mask_0_new.append(res_mask_0[i][s:s+end]) res_mask_1_new.append(res_mask_1[i][s:s+end]) res_mask_2_new.append(res_mask_2[i][s:s+end]) res_mask_3_new.append(res_mask_3[i][s:s+end]) res_mask_4_new.append(res_mask_4[i][s:s+end]) res_mask_5_new.append(res_mask_5[i][s:s+end]) res_mask_6_new.append(res_mask_6[i][s:s+end]) seq_mask_new.append(seq_mask[i][s:s+end]) elif s+max_seq_length < len(seq[i]): seq_id_new.append(seq_id[i]) seq_new.append(seq[i][s:s+max_seq_length]) seq_label_0_new.append(seq_label_0[i][s:s+max_seq_length]) seq_label_1_new.append(seq_label_1[i][s:s+max_seq_length]) seq_label_2_new.append(seq_label_2[i][s:s+max_seq_length]) seq_label_3_new.append(seq_label_3[i][s:s+max_seq_length]) seq_label_4_new.append(seq_label_4[i][s:s+max_seq_length]) seq_label_5_new.append(seq_label_5[i][s:s+max_seq_length]) seq_label_6_new.append(seq_label_6[i][s:s+max_seq_length]) seq_T5_feature_new.append(seq_T5_feature[i][s:s+max_seq_length]) res_mask_0_new.append(res_mask_0[i][s:s+max_seq_length]) res_mask_1_new.append(res_mask_1[i][s:s+max_seq_length]) res_mask_2_new.append(res_mask_2[i][s:s+max_seq_length]) res_mask_3_new.append(res_mask_3[i][s:s+max_seq_length]) res_mask_4_new.append(res_mask_4[i][s:s+max_seq_length]) res_mask_5_new.append(res_mask_5[i][s:s+max_seq_length]) res_mask_6_new.append(res_mask_6[i][s:s+max_seq_length]) seq_mask_new.append(seq_mask[i][s:s+max_seq_length]) s = s+max_seq_length return seq_id_new, seq_new, seq_label_0_new,seq_label_1_new,seq_label_2_new,seq_label_3_new,seq_label_4_new, seq_label_5_new, seq_label_6_new, seq_T5_feature_new, res_mask_0_new, res_mask_1_new, res_mask_2_new, res_mask_3_new, res_mask_4_new, res_mask_5_new, res_mask_6_new, seq_mask_new def padding_list(input_list, max_seq_length): pad = 0 # zero-padding out_list = [] if len(input_list) < max_seq_length: for i in range(len(input_list)): out_list.append(input_list[i]) for j in range(max_seq_length-len(input_list)): out_list.append(pad) else: for i in range(max_seq_length): out_list.append(input_list[i]) return np.array(out_list) def padding_matrix(input_mat, max_seq_length): input_mat = np.array(input_mat) mat_dim = input_mat.shape[-1] pad_vector = np.zeros([mat_dim]) # zero-padding out_mat = [] if len(input_mat) < max_seq_length: for i in range(len(input_mat)): out_mat.append(input_mat[i]) for j in range(max_seq_length-len(input_mat)): out_mat.append(pad_vector) else: for i in range(max_seq_length): out_mat.append(input_mat[i]) return np.array(out_mat) def seq_lable_padding(seq_label, max_seq_length): out_list = [] for i in range(len(seq_label)): new_list = padding_list(seq_label[i], max_seq_length) # print(new_list) out_list.append(new_list) return np.array(out_list) def seq_feature_padding(seq_feature, max_seq_length): out_mat = [] for i in range(len(seq_feature)): new_f = padding_matrix(seq_feature[i], max_seq_length) out_mat.append(new_f) return np.array(out_mat) def mask_padding(res_mask,max_seq_length): out_list = [] for i in range(len(res_mask)): new_list = padding_list(res_mask[i], max_seq_length) # print(new_list) out_list.append(new_list) return np.array(out_list) def data_2_samples(args, data_file_name, is_slice):
seq_id,seq,seq_label_IDP,seq_label_F1,seq_label_F2,seq_label_F3,seq_label_F4,seq_label_F5,seq_label_F6,seq_T5_feature = file_2_data(data_file_name)
1
2023-11-09 15:08:24+00:00
4k