text
stringlengths
5
22M
id
stringlengths
12
177
metadata
dict
__index_level_0__
int64
0
1.37k
# Quick Start BitBLAS provides two Python APIs to perform mixed-precision matrix multiplication: - ```bitblas.Matmul``` implements the $W_{wdtype}A_{adtype}$ mixed-precision matrix multiplication of $C_{cdtype}[M, N] = A_{adtype}[M, K] \times W_{wdtype}[N, K]$ where $W_{wdtype}$ indicates the weight of $wtype$, A_{adtype} indicates the activation of $adtype$, and C_{cdtype} indicates the output of $cdtype$. - ```bitblas.Linear``` is a PyTorch ```nn.Linear```-like module to support a Linear of mixed-precision. ## Example: $W_{INT4}A_{FP16}$ mixed-precision matrix multiplication Here is an example for a $W_{INT4}A_{FP16}$ mixed-precision matrix multiplication: $out_{FP16}[M, N] = A_{FP16}[M, K] \times W_{INT4}[N, K]$, the example includes the creation of input matrices, quantization of weight matrices, and execution of the multiplication. The result is then compared against a reference result obtained through conventional methods to ensure accuracy. ```python import bitblas import torch matmul_config = bitblas.MatmulConfig( M=1, # M dimension N=1024, # N dimension K=1024, # K dimension A_dtype="float16", # activation A dtype W_dtype="int4", # weight W dtype accum_dtype="float16", # accumulation dtype out_dtype="float16", # output dtype layout="nt", # matrix layout, "nt" indicates the layout of A is non-transpose and the layout of W is transpose with_bias=False, # bias # configs for weight only quantization group_size=None, # setting for grouped quantization with_scaling=False, # setting for scaling factor with_zeros=False, # setting for zeros zeros_mode=None, # setting for how to calculating zeros ) matmul = bitblas.Matmul(config=matmul_config) # Create input matrices input_tensor = torch.rand((1, 1024), dtype=torch.float16).cuda() weight_tensor = torch.randint(0, 7, (1024, 1024), dtype=torch.int8).cuda() # Transform weight tensor to int4 data type weight_tensor_int4 = matmul.transform_weight(weight_tensor) # Perform mixed-precision matrix multiplication output_tensor = matmul(input_tensor, weight_tensor_int4) # Reference result using PyTorch matmul for comparison ref_result = torch.matmul(input_tensor, weight_tensor.t().to(torch.float16)) # Assert that the results are close within a specified tolerance, note that the int4 randint value is a little bigger than the float16 value, so we set the atol to 1.0 print("Ref output:", ref_result) print("BitBLAS output:", output_tensor) torch.testing.assert_close(output_tensor, ref_result, rtol=1e-2, atol=1e-0) ``` The same example can be extended to include the quantization of the weight tensor with scaling and zeros. The following code snippet demonstrates how to quantize the weight tensor with scaling and zeros and execute the mixed-precision matrix multiplication. ```python import bitblas import torch in_features = 1024 out_features = 1024 group_size = 128 matmul_config = bitblas.MatmulConfig( M=1, # M dimension N=out_features, # N dimension K=in_features, # K dimension A_dtype="float16", # activation A dtype W_dtype="uint4", # weight W dtype accum_dtype="float16", # accumulation dtype out_dtype="float16", # output dtype layout="nt", # matrix layout, "nt" indicates the layout of A is non-transpose and the layout of W is transpose with_bias=False, # bias # configs for weight only quantization group_size=group_size, # setting for grouped quantization with_scaling=True, # setting for scaling factor with_zeros=True, # setting for zeros zeros_mode="original", # setting for how to calculating zeros ) matmul = bitblas.Matmul(config=matmul_config) # Define shapes for tensors input_shape = (1, 1024) weight_shape = (1024, 1024) scaling_shape = (1024, 1024 // 128) zeros_shape = (1024, 1024 // 128) output_shape = (1, 1024) # Create scaling and zeros tensors for quantization scaling = torch.rand(scaling_shape, dtype=torch.float16).cuda() zeros = torch.rand(zeros_shape, dtype=torch.float16).cuda() # Create input tensor input_tensor = torch.rand(input_shape, dtype=torch.float16).cuda() # Create and transform weight tensor weight_tensor = torch.randint(0, 7, weight_shape, dtype=torch.int8).cuda() weight_tensor_int4 = matmul.transform_weight(weight_tensor) # Perform mixed-precision matrix multiplication with quantization output_tensor = matmul(input_tensor, weight_tensor_int4, scale=scaling, zeros=zeros) rescaling_tensor = torch.zeros_like(weight_tensor, dtype=torch.float16).cuda() # Compute reference result with manual scaling and zero-point adjustment # rescale = (weight - zeros) * scaling for i in range(in_features // group_size): for j in range(group_size): rescaling_tensor[:, i * group_size + j] = ( weight_tensor[:, i * group_size + j].to(torch.float16) - zeros[:, i] ) * scaling[:, i] ref_result = torch.matmul(input_tensor, rescaling_tensor.t().to(torch.float16)) # Assert that the results are close within a specified tolerance print("Ref output:", ref_result) print("BitBLAS output:", output_tensor) torch.testing.assert_close(output_tensor, ref_result, rtol=1e-2, atol=1e-2) ``` The init stage of the ```bitblas.Matmul``` class will take minutes to finish, as it will use hardware informations to do a one-time kernel library initialization. ## Example: bitblas.Linear module for PyTorch BitBLAS also implemented a variant PyTorch ```nn.Linear``` module, i.e., ```bitblas.Linear```, to support a Linear of mixed-precision. See code [implementation](../python/bitblas/module/__init__.py) Here is an example to define a ```bitblas.Linear``` of $W_{INT4}A_{FP16}$: ```python import bitblas import torch model = bitblas.Linear( in_features=1024, out_features=1024, bias=False, A_dtype="float16", # activation A dtype W_dtype="int4", # weight W dtype accum_dtype="float16", # accumulation dtype out_dtype="float16", # output dtype # configs for weight only quantization group_size=None, # setting for grouped quantization with_scaling=False, # setting for scaling factor with_zeros=False, # setting for zeros zeros_mode=None, # setting for how to calculating zeros # Target optimization var for dynamic symbolic. # For detailed information please checkout docs/PythonAPI.md # By default, the optimization var is [1, 16, 32, 64, 128, 256, 512] opt_M=[1, 16, 32, 64, 128], ) # Create an integer weight tensor intweight = torch.randint(-7, 7, (1024, 1024), dtype=torch.int8) # Load and transform weights into the BitBLAS linear module model.load_and_transform_weight(intweight) # Save the state of the model torch.save(model.state_dict(), "./model.pth") # Load the model state model.load_state_dict(torch.load("./model.pth")) # Set the model to evaluation mode model.eval() # Create a dummy input tensor dummpy_input = torch.randn(1, 1024, dtype=torch.float16) # Perform inference output = model(dummpy_input) print("BitBLAS output:", output) # Please checkout the correctness evaluation code in `testing/python/module/test_bitblas_linear.py` ``` we also provide repack interface to repack the pretrained weight of AutoGPTQ into the format of BitBLAS. Here is an example to repack the pretrained weight of AutoGPTQ: ```python # !pip install auto-gptq import bitblas import torch from auto_gptq.nn_modules.qlinear.qlinear_cuda_old import ( QuantLinear as CudaOldQuantLinear, ) in_features = 1024 out_features = 1024 group_size = 128 original_w, linear, s, qw = bitblas.quantization.gen_quant4( in_features, out_features, group_size ) zeros = torch.full((in_features // group_size, out_features), 7, dtype=torch.int32) cuda_old_linear = CudaOldQuantLinear( bits=4, group_size=group_size, infeatures=in_features, outfeatures=out_features, bias=False, ) cuda_old_linear.pack(linear, s.T, zeros.T, g_idx=None) bitblas_linear = bitblas.Linear( in_features=in_features, out_features=out_features, bias=False, A_dtype="float16", # activation A dtype W_dtype="uint4", # weight W dtype accum_dtype="float16", # accumulation dtype out_dtype="float16", # output dtype # configs for weight only quantization group_size=group_size, # setting for grouped quantization with_scaling=True, # setting for scaling factor with_zeros=True, # setting for zeros zeros_mode="quantized", # setting for how to calculating zeros ) # Repack weights from CudaOldQuantLinear to BitBLAS linear module bitblas_linear.repack_from_gptq(cuda_old_linear) # Prepare input data m = 1 # Batch size inp = torch.rand(m, in_features, dtype=torch.float16, device="cuda") # Move models to CUDA for execution cuda_old_linear = cuda_old_linear.to("cuda") bitblas_linear = bitblas_linear.to("cuda") # Perform inference without gradient calculations with torch.no_grad(): res_cuda_old = cuda_old_linear(inp) res_bitblas = bitblas_linear(inp) print("CudaOldQuantLinear output:", res_cuda_old) print("BitBLAS output:", res_bitblas) # Verify the outputs are close within specified tolerances torch.testing.assert_close(res_bitblas, res_cuda_old, rtol=1e-0, atol=1e-1) ```
BitBLAS/docs/QuickStart.md/0
{ "file_path": "BitBLAS/docs/QuickStart.md", "repo_id": "BitBLAS", "token_count": 3231 }
138
# Copyright (c) Microsoft Corporation. # Licensed under the MIT License. # pylint: disable=missing-docstring, invalid-name """This is modified from https://huggingface.co/1bitLLM/bitnet_b1_58-3B/blob/main/utils_quant.py.""" import math import argparse import torch import random from eval_utils import get_test_dataset from modeling_bitnet import BitnetForCausalLM from tokenization_bitnet import BitnetTokenizer from tqdm import tqdm torch.set_grad_enabled(False) parser = argparse.ArgumentParser() parser.add_argument('--seed', default=0, type=int) parser.add_argument('--hf_path', default='1bitLLM/bitnet_b1_58-3B', type=str) parser.add_argument('--seqlen', default=2048, type=int) def calulate_loss(model, input, loss_fct): output = model(input, use_cache=False, output_hidden_states=False, output_attentions=False)[0] shift_logits = output[:, :-1, :].contiguous() shift_labels = input[:, 1:] loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1)) return loss def main(args): datasets = ['c4', 'wikitext2'] model = BitnetForCausalLM.from_pretrained( args.hf_path, use_flash_attention_2=True, torch_dtype=torch.float16, ).cuda().half() with torch.no_grad(): model._post_process_weights() tokenizer = BitnetTokenizer.from_pretrained(args.hf_path, use_fast=False) loss_fct = torch.nn.CrossEntropyLoss(reduction="sum").cuda() ppl = [] for dataset in datasets: testdata = get_test_dataset(dataset, tokenizer, seqlen=args.seqlen) acc_loss, count = 0.0, 0 progress = tqdm(range(len(testdata))) for ii in progress: input = torch.Tensor(testdata[ii]).long().cuda().view(1, -1) loss = calulate_loss(model, input, loss_fct) count += (input.size(-1) - 1) acc_loss += loss.item() progress.set_description(f"avg_loss = {acc_loss/ count / math.log(2)}") avg_loss = acc_loss / count / math.log(2) ppl.append(2**avg_loss) print("{} PPL: {}".format(dataset, ppl[-1])) print(ppl) print("Avg PPL:", sum(ppl) / len(ppl)) if __name__ == '__main__': torch.set_grad_enabled(False) args = parser.parse_args() random.seed(args.seed) torch.random.manual_seed(args.seed) main(args)
BitBLAS/integration/BitNet/eval_ppl.py/0
{ "file_path": "BitBLAS/integration/BitNet/eval_ppl.py", "repo_id": "BitBLAS", "token_count": 986 }
139
[tool.yapf] based_on_style = "yapf" column_limit = 100 indent_width = 4 [tool.codespell] ignore-words-list = "nd, te" [tool.ruff.lint] select = [ # pycodestyle "E", # Pyflakes "F", # pyupgrade # "UP", # flake8-bugbear "B", # flake8-simplify "SIM", # isort # "I", ] ignore = [ # Module level import not at top of file "E402", # star imports "F405", "F403", # ambigous name "E741", # line too long "E501", # key in dict.keys() "SIM118", # memory leaks "B019", # No such file or directory "E902", ]
BitBLAS/pyproject.toml/0
{ "file_path": "BitBLAS/pyproject.toml", "repo_id": "BitBLAS", "token_count": 299 }
140
# Copyright (c) Microsoft Corporation. # Licensed under the MIT License. """Policy for tensorcore schedule""" import tvm from typing import Dict, List, Tuple, Optional import numpy as np from ..arch import TileDevice from ..hint import Hint, Stride, TileDict, IntrinInfo from ..node import PrimFuncNode from .common import coalesced_factor, factorize, get_all_factors from .default import DefaultPolicy from ..rasterization import NoRasterization, Rasterization2DColumn class TensorCorePolicy(DefaultPolicy): def __init__(self, func: tvm.tir.PrimFunc, arch: TileDevice, tags: Optional[Dict] = None) -> None: super().__init__(func, arch, tags) # this is the trick for wmma. # However, for int8 mma, the wmma_k should be 32. self.wmma_k = 16 self.pipeline_stage: int = 1 self.use_async_copy: bool = False self._legalize_info() def _legalize_info(self): pipleline_stage = self.prim_func_node.get_tag("pipeline_stage") if pipleline_stage: self.pipeline_stage = pipleline_stage else: if self.arch.compute_capability == "sm_80": self.pipeline_stage = 2 else: self.pipeline_stage = 1 use_async_copy = self.prim_func_node.get_tag("use_async_copy") if use_async_copy: self.use_async_copy = use_async_copy else: if self.arch.compute_capability == "sm_80": self.use_async_copy = True else: self.use_async_copy = False def _compute_tc_strides( self, node: PrimFuncNode, tile: List[int], rstep: Optional[Dict[str, int]] = None, ) -> Tuple[Stride, Stride, Stride]: if rstep is None: rstep = {} # strides was used for shared memory padding. which is necessary for avoiding # shared memory load bank conflict when we do not applying tensorcore layout. shapes = node.propagate_reduction_inputs(tile, rstep) AS_shape, BS_shape = shapes.values() CS_shape = tile A_ax_m, A_ax_k, B_ax_k, B_ax_n, C_ax_m, C_ax_n = node.infer_tensorcore_axis() # applying strides # TODO(leiwang1999): offset should be dynamically set. we can use tag -> enable_offset to control this option.. offset = 8 A_high_ax = min(A_ax_m, A_ax_k) B_high_ax = min(B_ax_n, B_ax_k) C_high_ax = min(C_ax_m, C_ax_n) A_stride = Stride(stride=np.prod(AS_shape[A_high_ax + 1:]) + offset, ax=A_high_ax) B_stride = Stride(stride=np.prod(BS_shape[B_high_ax + 1:]) + offset, ax=B_high_ax) C_stride = Stride(stride=np.prod(CS_shape[C_high_ax + 1:]) + offset, ax=C_high_ax) return A_stride, B_stride, C_stride def infer_node_smem_usage(self, td: TileDict, node: PrimFuncNode): value, cached_tensors = super().infer_node_smem_usage(td, node) value *= self.pipeline_stage return value, cached_tensors def _assign_reduce_step(self, node): if not node.get_tag("tensorcore_config"): return super()._assign_reduce_step(node) # get reduce input size target_transaction = self.arch.transaction_size[0] * 2 # 512 bytes // type bits reduce_input_dtype = node.get_buffer_dtype( node.block_analyzer.get_input_buffers(node.reduction_block)[0]) basic = (target_transaction * 8) // reduce_input_dtype.bits result = {} for iter_info in node.raxis: iter_name = iter_info.var.name iter_dom = iter_info.dom.extent if iter_dom % 16 > 0: result[iter_name] = (16 if iter_dom < basic else basic) # for the case of padding elif iter_dom % basic == 0: result[iter_name] = basic else: return super()._assign_reduce_step(node) return result def _expand_reduce_axis(self, td: TileDict): # For tensorcore program, if we got a small tilesize, we should consider expand the reduce axis # to improve compute efficiency. def _check_small_tile(td: TileDict): minimal_threadhold = 32 for node in self.ordered_nodes: tile = td.get_tile(node) if any([t <= minimal_threadhold for t in tile]): return True return False if not _check_small_tile(td): return None smem_limit = min(self.arch.max_smem_usage // td.block_per_SM, self.arch.smem_cap) rstep_map = td.rstep_map.copy() def _optimize(node, rstep): all_steps = self.get_node_reduce_step_candidates(node) # todo(lei): optimize the all_steps enlarge policy to be a multiple of the original all_steps[k] for k in all_steps: all_steps[k] = list(filter(lambda x: x % rstep[k] == 0, all_steps[k])) if any([v == [] for v in all_steps.values()]): return rstep def _shared_memory_usage(td: TileDict): return node.footprint(td.output_tile, new_rstep_map, td.tensor_strides_map[node]) def _score(rstep_id): rstep = { k.var.name: all_steps[k.var.name][rstep_id[k.var.name]] for k in node.raxis } score = 0 shape = node.propagate_inputs_on_reduction(td.get_tile(node), rstep=rstep) input_buffers = node.block_analyzer.get_input_buffers(node.reduction_block) for i, input_buffer in enumerate(input_buffers): score += coalesced_factor(shape[i], input_buffer.shape) return score def _enlarge(rstep_id): candidates = [] for ax in rstep_id: if rstep_id[ax] + 1 == len(all_steps[ax]): continue r = rstep_id.copy() r[ax] += 1 candidates.append((r, _score(r))) if len(candidates) == 0: return None return max(candidates, key=lambda x: x[1])[0] cur_rstep_id = { k.var.name: all_steps[k.var.name].index(rstep[k.var.name]) for k in node.raxis } new_rstep_map = rstep_map.copy() while True: new_rstep_id = _enlarge(cur_rstep_id) if new_rstep_id is None: break new_rstep_map = { k.var.name: all_steps[k.var.name][new_rstep_id[k.var.name]] for k in node.raxis } old_rstep_map = td.rstep_map td.rstep_map = new_rstep_map smem_usage, _ = _shared_memory_usage(td) td.rstep_map = old_rstep_map if smem_usage > smem_limit: break else: cur_rstep_id = new_rstep_id rstep = { k.var.name: all_steps[k.var.name][cur_rstep_id[k.var.name]] for k in node.raxis } return rstep for node in self.ordered_nodes: if len(node.raxis) > 0: rstep = _optimize(node, rstep_map) rstep_map = rstep td.rstep_map = rstep_map td.smem_cost, td.cached_tensors_map = self._compute_shared_memory_usage(td) return def get_node_reduce_step_candidates(self, node): if not node.get_tag("tensorcore_config"): return super().get_node_reduce_step_candidates(node) else: # must be a a multiple of wmma_k return { k.var.name: [x * self.wmma_k for x in get_all_factors(int(k.dom.extent) // self.wmma_k)] for k in node.raxis } def check_tile_shape_isvalid(self, td: TileDict): for node in self.ordered_nodes: if node.get_tag("tensorcore_config"): ax_m, ax_n = node.get_tag("tensorcore_config") block_m, block_n = ( td.tile_map[node][ax_m], td.tile_map[node][ax_n], ) # check the tile size is valid wmma_invalid = [ block_m < wmma_m or block_n < wmma_n for wmma_m, wmma_n in self.arch.get_avaliable_tensorintrin_shapes() ] if all(wmma_invalid): return False if any([y % x for x, y in zip(td.tile_map[node], node.get_space_dim())]): return False return super().check_tile_shape_isvalid(td) def _can_implement_layout(self, node: PrimFuncNode, td: TileDict): # Not implemented yet # This function is used to check whether we can implement swizzling # layout under this tile config return False def compute_node_stride_map(self, node: PrimFuncNode, td: TileDict): if not node.get_tag("tensorcore_config"): return super().compute_node_stride_map(node, td) use_layout = self._can_implement_layout(node, td) AS_stride, BS_stride, C_stride = self._compute_tc_strides(node, td.get_tile(node), td.get_rstep(node)) A_stride, B_stride, _ = self._compute_tc_strides(node, td.get_tile(node)) tensor_strides = {} output_strides = { int(i + len(node.input_buffers)): Stride() for i, _ in enumerate(node.output_buffers) } tensor_strides = {} # when connected to shared input, should use full stride without rstep for i, (_, _) in enumerate(zip([AS_stride, BS_stride], [A_stride, B_stride])): if use_layout: continue _ = node.block_analyzer.get_input_buffers(node.reduction_block)[i].name # TODO(lei): should dig further for shared memory connection case. return output_strides, tensor_strides def _assign_block_size(self, node: PrimFuncNode, td: TileDict, block_size: int): if not node.get_tag("tensorcore_config"): return super()._assign_block_size(node, td, block_size) ax_m, ax_n = node.get_tag("tensorcore_config") if block_size % self.arch.warp_size != 0: return None tile, rsteps = td.get_tile(node), td.get_rstep(node) warps = block_size // self.arch.warp_size ndim = len(tile) wmma = self.arch.get_avaliable_tensorintrin_shapes()[-1] wmma_tile = [1 for _ in range(ndim)] wmma_tile[ax_m] = wmma[0] wmma_tile[ax_n] = wmma[1] space = [tile[i] // wmma_tile[i] for i in range(ndim)] if tile[ax_m] < wmma_tile[ax_m] or tile[ax_n] < wmma_tile[ax_n]: # allow pad, otherwise, we can not get a valid tile shape return None factors = factorize(np.prod(space) // warps) def _score(node, thread): # small is better score = 0 block_tile = [int(np.ceil(tile[i] / thread[i])) for i in range(ndim)] shape = node.propagate_inputs_on_reduction(block_tile) input_buffers = node.block_analyzer.get_input_buffers(node.reduction_block) for i, _ in enumerate(input_buffers): score += np.prod(shape[i]) / self.arch.bandwidth[1] return score warp_tile = wmma_tile.copy() for factor in reversed(factors): score_map = {} for i in range(ndim): if tile[i] % (warp_tile[i] * factor) != 0: continue warp_tile[i] *= factor score_map[i] = (_score(node, warp_tile), i) warp_tile[i] //= factor if len(score_map) == 0: return None dim_order = sorted(score_map.keys(), key=lambda x: score_map[x]) warp_tile[dim_order[0]] *= factor codegen_dict = Hint() codegen_dict.block = tile codegen_dict.warp = warp_tile codegen_dict.use_tc = True codegen_dict.pipeline_stage = self.pipeline_stage codegen_dict.use_async = self.use_async_copy codegen_dict.rstep = [int(rsteps[ax.var.name]) for ax in node.raxis] codegen_dict.cached_tensors = td.cached_tensors_map[node] codegen_dict.rasterization_plan = self.plan_rasterization(td) intrin_info = node.get_tag("intrin_info") if intrin_info: codegen_dict.intrin_info = IntrinInfo(**intrin_info) # smem capacity if td.smem_cost > self.arch.smem_cap: codegen_dict.shared_scope = "shared.dyn" codegen_dict.complete_config(node) codegen_dict.vectorize = self._plan_vectorize(self.prim_func_node, td, block_size) codegen_dict.arch = self.arch codegen_dict.opt_shapes = self.prim_func_node.get_tag("opt_shapes") return codegen_dict def plan_rasterization(self, td: TileDict): conditions = [] # only support single node for now conditions.append(len(self.ordered_nodes) > 1) # only on Ampere+ arch conditions.append(self.arch.compute_capability < "80") def _check_memory_size(): overall_gmem_size_in_bytes: int = 0 for node in self.ordered_nodes: for buffer in node.input_buffers: overall_gmem_size_in_bytes += ( int(np.prod(buffer.shape)) * tvm.DataType(buffer.dtype).bits // 8) return overall_gmem_size_in_bytes < self.arch.l2_cache_size_bytes conditions.append(_check_memory_size()) if any(conditions): return NoRasterization() # otherwise, simply provide a block rasterization factor raster_factor = int(self.arch.compute_max_core**0.5) return Rasterization2DColumn(raster_factor)
BitBLAS/python/bitblas/base/roller/policy/tensorcore.py/0
{ "file_path": "BitBLAS/python/bitblas/base/roller/policy/tensorcore.py", "repo_id": "BitBLAS", "token_count": 7099 }
141
# Copyright (c) Microsoft Corporation. # Licensed under the MIT License. """A rule for GEMV and DecodeGEMV.""" from functools import reduce from typing import List, Dict from tvm.target import Target from tvm.tir.function import PrimFunc from tvm import DataType, tir import logging from ..base import ( normalize_prim_func, get_output_blocks, get_block, ) from .base import GPUScheduleRule from .matmul_analysis import auto_inline_producers, auto_inline_consumers logger = logging.getLogger(__name__) class GEMVWithDequantizeInfo(GPUScheduleRule): """A rule for Dequantized GEMV.""" def apply( # pylint: disable=too-many-locals,too-many-branches,too-many-return-statements self, func: tir.PrimFunc, target: Target, _: bool, ): sch = tir.Schedule(func) from .intrin import get_lop3_intrin_group dequantize_info = func.attrs["dequantize_info"] def check_dequantize_info(dequantize_info): conditions = [] # currently only support weight only dequantization conditions.append(len(dequantize_info) == 1) # TODO(@lei) check if the dequantize value name is weight return all(conditions) if not check_dequantize_info(dequantize_info): logger.debug("Dequantize info is not valid") return None (weight_decode_info,) = list(dequantize_info.values()) def check_weight_decode_info(weight_decode_info): conditions = [] # check source format in ["int", "fp", "nf"] conditions.append("source_format" in weight_decode_info) conditions.append( weight_decode_info["source_format"]["format"] in ["uint", "int", "fp", "nf"]) # check source bits in [1, 2, 4, 8] conditions.append(weight_decode_info["source_format"]["bits"] in [1, 2, 4, 8]) # check target format in ["float16", "int8"] conditions.append("target_format" in weight_decode_info) conditions.append(weight_decode_info["target_format"] in ["float16", "int8"]) return all(conditions) if not check_weight_decode_info(weight_decode_info): logger.debug("Weight Dequantize info is not valid") return None block_infos = normalize_prim_func(sch) if block_infos is None: return None reduction_block: tir.schedule.BlockRV = None for block in block_infos: s_loops: List[tir.schedule.LoopRV] = [] r_loops: List[tir.schedule.LoopRV] = [] o_loops: List[tir.schedule.LoopRV] = [] dom_kind = block.dom_kind() block = block.block_rv if (any([ sch.get(loop_rv).thread_binding is not None for loop_rv in sch.get_loops(block) ]) or len(sch.get_loops(block)) == 0): continue for loop, iter_type in zip(sch.get_loops(block), dom_kind): {"S": s_loops, "R": r_loops, "O": o_loops}[iter_type].append(loop) if not s_loops: s_loops.append(sch.add_unit_loop(block)) if len(r_loops) > 0: reduction_block = block def prod(iterable): return reduce(lambda x, y: x * y, iterable, 1) def get_vectorize_factor(target_format): # coalesced access requires the vectorize factor to be the same as the transaction size return 128 // DataType(target_format).bits vec = get_vectorize_factor(weight_decode_info["target_format"]) num_warps = 1 warp_size = 32 block_b = reduction_block output_blocks = get_output_blocks(sch, block_infos) # noqa: F841 B_decode_block = get_block(sch, block_infos, weight_decode_info["decode_block"]) block_decode_B = sch.cache_read(block_b, 1, "local") sch.compute_inline(B_decode_block) j, k = sch.get_loops(block_b)[-2:] # get target dequantize buffer's idx def get_idx(weight_decode_info: Dict): # for LUT dequantize, the expr is LUT(w), the idx is 1 # maybe we can use a more general and structural based way # to analysis the idx if weight_decode_info["source_format"]["format"] == "nf": return 1 return 0 block_shared_local_A = sch.cache_read(block_b, 0, "local") block_shared_local_B = sch.cache_read(block_decode_B, get_idx(weight_decode_info), "local") block_local_C = sch.cache_write(block_b, 0, "local") auto_inline_producers(sch, block_shared_local_B) auto_inline_consumers(sch, block_local_C) bx, j = sch.split(j, factors=[None, num_warps]) k, tx, vk = sch.split(k, factors=[None, warp_size, vec]) # for dp4a/hfma2 inst_factor = 2 if weight_decode_info["target_format"] == "float16" else 4 _, vk = sch.split(vk, factors=[None, inst_factor]) sch.reorder(bx, j, k, tx) sch.bind(bx, "blockIdx.x") sch.bind(tx, "threadIdx.x") sch.bind(j, "threadIdx.y") self.block_size = [sch.get(tx).extent, sch.get(j).extent, 1] self.grid_size = [sch.get(bx).extent, 1, 1] sch.compute_at(block_decode_B, tx, preserve_unit_loops=True) sch.compute_at(block_shared_local_A, tx, preserve_unit_loops=True) sch.compute_at(block_shared_local_B, tx, preserve_unit_loops=True) sch.reverse_compute_at(block_local_C, j, preserve_unit_loops=True) block_local_a_v = sch.get_loops(block_shared_local_A)[-1] sch.vectorize(block_local_a_v) block_local_b_v = sch.get_loops(block_shared_local_B)[-1] sch.vectorize(block_local_b_v) skip_blocks = [block_shared_local_B] if "zeros_mode" in weight_decode_info and weight_decode_info["zeros_mode"] == "quantized": if "with_scaling" in weight_decode_info and weight_decode_info["with_scaling"]: block_local_scales = sch.cache_read(block_decode_B, get_idx(weight_decode_info) + 1, "local") sch.compute_at(block_local_scales, tx, preserve_unit_loops=True) auto_inline_producers(sch, block_local_scales) skip_blocks.append(block_local_scales) if "with_zeros" in weight_decode_info and weight_decode_info["with_zeros"]: block_local_zeros = sch.cache_read(block_decode_B, get_idx(weight_decode_info) + 2, "local") sch.compute_at(block_local_zeros, tx, preserve_unit_loops=True) auto_inline_producers(sch, block_local_zeros) skip_blocks.append(block_local_zeros) auto_inline_producers(sch, block_decode_B, skip_blocks) if ("fast_decoding" in weight_decode_info and weight_decode_info["fast_decoding"]): source_bit = weight_decode_info["source_format"]["bits"] out_dtype = weight_decode_info["target_format"] intrin_info = get_lop3_intrin_group( out_dtype=out_dtype, storage_dtype=weight_decode_info["storage_dtype"], source_format=weight_decode_info["source_format"]["format"], source_bit=source_bit, with_scaling=weight_decode_info["with_scaling"], with_zeros=weight_decode_info["with_zeros"], zeros_mode=weight_decode_info["zeros_mode"], ) sch.tensorize(sch.get_loops(block_decode_B)[-1], intrin_info["compute"]) sch.annotate(block_b, ann_key="pragma_import_c", ann_val=intrin_info["c_source"]) return sch def sch_inner_reduction_with_config( # pylint: disable=too-many-locals,too-many-branches,too-many-return-statements self, func: tir.PrimFunc, config, ): sch = tir.Schedule(func) from .intrin import get_lop3_intrin_group dequantize_info = func.attrs["dequantize_info"] def check_dequantize_info(dequantize_info): conditions = [] # currently only support weight only dequantization conditions.append(len(dequantize_info) == 1) # TODO(@lei) check if the dequantize value name is weight return all(conditions) if not check_dequantize_info(dequantize_info): logger.debug("Dequantize info is not valid") return None (weight_decode_info,) = list(dequantize_info.values()) def check_weight_decode_info(weight_decode_info): conditions = [] # check source format in ["int", "fp", "nf"] conditions.append("source_format" in weight_decode_info) conditions.append( weight_decode_info["source_format"]["format"] in ["uint", "int", "fp", "nf"]) # check source bits in [1, 2, 4, 8] conditions.append(weight_decode_info["source_format"]["bits"] in [1, 2, 4, 8]) # check target format in ["float16", "int8"] conditions.append("target_format" in weight_decode_info) conditions.append(weight_decode_info["target_format"] in ["float16", "int8"]) return all(conditions) if not check_weight_decode_info(weight_decode_info): logger.debug("Weight Dequantize info is not valid") return None block_infos = normalize_prim_func(sch) if block_infos is None: return None reduction_block: tir.schedule.BlockRV = None for block in block_infos: s_loops: List[tir.schedule.LoopRV] = [] r_loops: List[tir.schedule.LoopRV] = [] o_loops: List[tir.schedule.LoopRV] = [] dom_kind = block.dom_kind() block = block.block_rv if (any([ sch.get(loop_rv).thread_binding is not None for loop_rv in sch.get_loops(block) ]) or len(sch.get_loops(block)) == 0): continue for loop, iter_type in zip(sch.get_loops(block), dom_kind): {"S": s_loops, "R": r_loops, "O": o_loops}[iter_type].append(loop) if not s_loops: s_loops.append(sch.add_unit_loop(block)) if len(r_loops) > 0: reduction_block = block def prod(iterable): return reduce(lambda x, y: x * y, iterable, 1) def get_vectorize_factor(target_format): # coalesced access requires the vectorize factor to be the same as the transaction size return config.arch.transaction_size[-1] // DataType(target_format).bits vec = get_vectorize_factor(weight_decode_info["target_format"]) num_warps = int(prod(config.thread)) warp_size = int(prod(config.reduce_thread)) block_b = reduction_block output_blocks = get_output_blocks(sch, block_infos) # noqa: F841 B_decode_block = get_block(sch, block_infos, weight_decode_info["decode_block"]) block_decode_B = sch.cache_read(block_b, 1, "local") sch.compute_inline(B_decode_block) j, k = sch.get_loops(block_b)[-2:] # get target dequantize buffer's idx def get_idx(weight_decode_info: Dict): # for LUT dequantize, the expr is LUT(w), the idx is 1 # maybe we can use a more general and structural based way # to analysis the idx if weight_decode_info["source_format"]["format"] == "nf": return 1 return 0 block_shared_local_A = sch.cache_read(block_b, 0, "local") block_shared_local_B = sch.cache_read(block_decode_B, get_idx(weight_decode_info), "local") block_local_C = sch.cache_write(block_b, 0, "local") auto_inline_producers(sch, block_shared_local_B) auto_inline_consumers(sch, block_local_C) bx, j = sch.split(j, factors=[None, num_warps]) k, tx, vk = sch.split(k, factors=[None, warp_size, vec]) # for dp4a/hfma2 inst_factor = 2 if weight_decode_info["target_format"] == "float16" else 4 _, vk = sch.split(vk, factors=[None, inst_factor]) sch.reorder(bx, j, k, tx) sch.bind(bx, "blockIdx.x") sch.bind(tx, "threadIdx.x") sch.bind(j, "threadIdx.y") self.block_size = [sch.get(tx).extent, sch.get(j).extent, 1] self.grid_size = [sch.get(bx).extent, 1, 1] sch.compute_at(block_decode_B, tx, preserve_unit_loops=True) sch.compute_at(block_shared_local_A, tx, preserve_unit_loops=True) sch.compute_at(block_shared_local_B, tx, preserve_unit_loops=True) sch.reverse_compute_at(block_local_C, j, preserve_unit_loops=True) block_local_a_v = sch.get_loops(block_shared_local_A)[-1] sch.vectorize(block_local_a_v) block_local_b_v = sch.get_loops(block_shared_local_B)[-1] sch.vectorize(block_local_b_v) skip_blocks = [block_shared_local_B] if "zeros_mode" in weight_decode_info and weight_decode_info["zeros_mode"] == "quantized": if "with_scaling" in weight_decode_info and weight_decode_info["with_scaling"]: block_local_scales = sch.cache_read(block_decode_B, get_idx(weight_decode_info) + 1, "local") sch.compute_at(block_local_scales, tx, preserve_unit_loops=True) auto_inline_producers(sch, block_local_scales) skip_blocks.append(block_local_scales) if "with_zeros" in weight_decode_info and weight_decode_info["with_zeros"]: block_local_zeros = sch.cache_read(block_decode_B, get_idx(weight_decode_info) + 2, "local") sch.compute_at(block_local_zeros, tx, preserve_unit_loops=True) auto_inline_producers(sch, block_local_zeros) skip_blocks.append(block_local_zeros) auto_inline_producers(sch, block_decode_B, skip_blocks) if ("fast_decoding" in weight_decode_info and weight_decode_info["fast_decoding"]): source_bit = weight_decode_info["source_format"]["bits"] out_dtype = weight_decode_info["target_format"] intrin_info = get_lop3_intrin_group( out_dtype=out_dtype, storage_dtype=weight_decode_info["storage_dtype"], source_format=weight_decode_info["source_format"]["format"], source_bit=source_bit, with_scaling=weight_decode_info["with_scaling"], with_zeros=weight_decode_info["with_zeros"], zeros_mode=weight_decode_info["zeros_mode"], ) sch.tensorize(sch.get_loops(block_decode_B)[-1], intrin_info["compute"]) sch.annotate(block_b, ann_key="pragma_import_c", ann_val=intrin_info["c_source"]) return sch def apply_config(self, func: PrimFunc, config): if any([t > 1 for t in config.reduce_thread]): return self.sch_inner_reduction_with_config(func, config) else: return None
BitBLAS/python/bitblas/gpu/gemv_dequantize.py/0
{ "file_path": "BitBLAS/python/bitblas/gpu/gemv_dequantize.py", "repo_id": "BitBLAS", "token_count": 7391 }
142
# Copyright (c) Microsoft Corporation. # Licensed under the MIT License. from .lop3_permutate_impl import tir_interleave_weight
BitBLAS/python/bitblas/ops/impl/__init__.py/0
{ "file_path": "BitBLAS/python/bitblas/ops/impl/__init__.py", "repo_id": "BitBLAS", "token_count": 36 }
143
# Copyright (c) Microsoft Corporation. # Licensed under the MIT License. from tvm.relax.block_builder import BlockBuilder from tvm.relax.expr import Call, Expr from tvm.relax.transform.legalize_ops.common import register_legalize from bitblas.ops.impl import tir_interleave_weight @register_legalize("bitblas.interleave_weight") def _interleave_weight(bb: BlockBuilder, call: Call) -> Expr: nbits = call.attrs.nbits target_dtype = call.attrs.target_dtype out_dtype = call.attrs.out_dtype return bb.call_te( tir_interleave_weight(nbits, target_dtype, out_dtype), call.args[0], primfunc_name_hint="interleave_weight", ) __all__ = ["_interleave_weight"]
BitBLAS/python/bitblas/relax/op/interleave_weight.py/0
{ "file_path": "BitBLAS/python/bitblas/relax/op/interleave_weight.py", "repo_id": "BitBLAS", "token_count": 264 }
144
# Copyright (c) Microsoft Corporation. # Licensed under the MIT License. import tvm import torch import numpy as np import tvm.testing from tvm.script import tir as T import os from tvm import te import numpy as np def general_compress_to_int8(lowprecision_weight, source_bits=4): elems_per_byte = 8 // source_bits if lowprecision_weight.dtype == np.float16: lowprecision_weight = lowprecision_weight.astype(dtype=np.int8) int8_weight = np.zeros( ( *lowprecision_weight.shape[:-1], lowprecision_weight.shape[-1] // elems_per_byte, ), dtype=np.int8, ) for j in range(lowprecision_weight.shape[-1] // elems_per_byte): for k in range(elems_per_byte): int8_weight[:, j] |= lowprecision_weight[:, j * elems_per_byte + k] << ( source_bits * k ) return int8_weight def interleave_weight(qweight, nbits=4, target_dtype="float16"): assert target_dtype in ["float16", "int8"] # reinterpret the data type of qweight to int32 qweight = qweight.view(np.int32) new_qweight = np.zeros_like(qweight) bits_stride = 8 if target_dtype == "int8" else 16 mask = (1 << nbits) - 1 # for 4bit the val is 0x0000000f num_groups = 32 // bits_stride elems_per_group = bits_stride // nbits for i in range(num_groups): for j in range(elems_per_group): offset = i * elems_per_group + j shift = (offset % num_groups) * bits_stride + (offset // num_groups) * nbits new_qweight |= ((qweight >> (nbits * offset)) & mask) << shift if nbits == 1 and target_dtype == "int8": # special handling for 1b interleave n16_weight = new_qweight & np.int32(0xF0F00F0F) n16_weight |= ((new_qweight & np.int32(0x000000F0)) >> 4) << 16 n16_weight |= ((new_qweight & np.int32(0x0000F000)) >> 12) << 24 n16_weight |= ((new_qweight & np.int32(0x000F0000)) >> 16) << 4 n16_weight |= ((new_qweight & np.int32(0x0F000000)) >> 24) << 12 return n16_weight.view(np.int8) elif nbits == 2 and target_dtype == "float16": n8_weight = new_qweight & np.int32(0xFF0000FF) n8_weight |= ((new_qweight & np.int32(0x0000FF00)) >> 8) << 16 n8_weight |= ((new_qweight & np.int32(0x00FF0000)) >> 16) << 8 return n8_weight.view(np.int8) elif nbits == 1 and target_dtype == "float16": n8_weight = new_qweight & 0xF000000F n8_weight |= ((new_qweight & 0x000000F0) >> 4) << 8 n8_weight |= ((new_qweight & 0x00000F00) >> 8) << 16 n8_weight |= ((new_qweight & 0x0000F000) >> 12) << 24 n8_weight |= ((new_qweight & 0x000F0000) >> 16) << 4 n8_weight |= ((new_qweight & 0x00F00000) >> 20) << 12 n8_weight |= ((new_qweight & 0x0F000000) >> 24) << 20 return new_qweight.view(np.int8) def tir_interleave_weight(N=2, K=16, bits=4, target_dtype="float16"): QK = K * bits // 32 bits_stride = 16 mask = (1 << bits) - 1 # for 4bit the val is 0x0000000f num_groups = 32 // bits_stride elems_per_group = bits_stride // bits @T.prim_func def interleave_weight(A: T.Buffer((N, QK), "int32"), B: T.Buffer((N, QK), "int32")): for ax0, ax1, ax2, ax3 in T.grid(N, QK, num_groups, elems_per_group): with T.block("B"): v0, v1, v2, v3 = T.axis.remap("SSSS", [ax0, ax1, ax2, ax3]) offset = v2 * elems_per_group + v3 shift = (offset % num_groups) * bits_stride + ( offset // num_groups ) * bits B[v0, v1] = B[v0, v1] | ( ((A[v0, v1] >> (bits * offset)) & mask) << shift ) @T.prim_func def interleave_weight_f16_2b( A: T.Buffer((N, QK), "int32"), B: T.Buffer((N, QK), "int32") ): B_tmp_1 = T.alloc_buffer((N, QK), "int32", scope="local") B_tmp_2 = T.alloc_buffer((N, QK), "int32", scope="local") B_tmp_3 = T.alloc_buffer((N, QK), "int32", scope="local") for ax0, ax1, ax2, ax3 in T.grid(N, QK, num_groups, elems_per_group): with T.block("B_tmp"): v0, v1, v2, v3 = T.axis.remap("SSSS", [ax0, ax1, ax2, ax3]) offset = v2 * elems_per_group + v3 shift = (offset % num_groups) * bits_stride + ( offset // num_groups ) * bits B[v0, v1] = B[v0, v1] | ( ((A[v0, v1] >> (bits * offset)) & mask) << shift ) for ax0, ax1 in T.grid(N, QK): with T.block("B"): v0, v1 = T.axis.remap("SS", [ax0, ax1]) B_tmp_1[v0, v1] = B[v0, v1] & T.uint32(0xFF0000FF) B_tmp_2[v0, v1] = ((B[v0, v1] & T.uint32(0x00FF0000)) << 8) >> 16 B_tmp_3[v0, v1] = ((B[v0, v1] & T.uint32(0x0000FF00)) << 16) >> 8 B[v0, v1] = B_tmp_1[v0, v1] | B_tmp_2[v0, v1] | B_tmp_3[v0, v1] @T.prim_func def interleave_weight_f16_1b( A: T.Buffer((N, QK), "int32"), B: T.Buffer((N, QK), "int32") ): B_tmp_1 = T.alloc_buffer((N, QK), "int32", scope="local") B_tmp_2 = T.alloc_buffer((N, QK), "int32", scope="local") B_tmp_3 = T.alloc_buffer((N, QK), "int32", scope="local") B_tmp_4 = T.alloc_buffer((N, QK), "int32", scope="local") B_tmp_5 = T.alloc_buffer((N, QK), "int32", scope="local") B_tmp_6 = T.alloc_buffer((N, QK), "int32", scope="local") B_tmp_7 = T.alloc_buffer((N, QK), "int32", scope="local") for ax0, ax1, ax2, ax3 in T.grid(N, QK, num_groups, elems_per_group): with T.block("B_tmp"): v0, v1, v2, v3 = T.axis.remap("SSSS", [ax0, ax1, ax2, ax3]) offset = v2 * elems_per_group + v3 shift = (offset % num_groups) * bits_stride + ( offset // num_groups ) * bits B[v0, v1] = B[v0, v1] | ( ((A[v0, v1] >> (bits * offset)) & mask) << shift ) for ax0, ax1 in T.grid(N, QK): with T.block("B"): v0, v1 = T.axis.remap("SS", [ax0, ax1]) B_tmp_1[v0, v1] = B[v0, v1] & T.uint32(0xF000000F) B_tmp_2[v0, v1] = ((B[v0, v1] & T.uint32(0x000000F0)) >> 4) << 8 B_tmp_3[v0, v1] = ((B[v0, v1] & T.uint32(0x00000F00)) >> 8) << 16 B_tmp_4[v0, v1] = ((B[v0, v1] & T.uint32(0x0000F000)) >> 12) << 24 B_tmp_5[v0, v1] = ((B[v0, v1] & T.uint32(0x000F0000)) >> 16) << 8 B_tmp_6[v0, v1] = ((B[v0, v1] & T.uint32(0x00F00000)) >> 20) << 12 B_tmp_7[v0, v1] = ((B[v0, v1] & T.uint32(0x00F00000)) >> 24) << 20 B[v0, v1] = ( B_tmp_1[v0, v1] | B_tmp_2[v0, v1] | B_tmp_3[v0, v1] | B_tmp_4[v0, v1] | B_tmp_5[v0, v1] | B_tmp_6[v0, v1] | B_tmp_7[v0, v1] ) @T.prim_func def interleave_weight_int8_1b( A: T.Buffer((N, QK), "int32"), B: T.Buffer((N, QK), "int32") ): B_tmp_1 = T.alloc_buffer((N, QK), "int32", scope="local") B_tmp_2 = T.alloc_buffer((N, QK), "int32", scope="local") B_tmp_3 = T.alloc_buffer((N, QK), "int32", scope="local") B_tmp_4 = T.alloc_buffer((N, QK), "int32", scope="local") B_tmp_5 = T.alloc_buffer((N, QK), "int32", scope="local") for ax0, ax1, ax2, ax3 in T.grid(N, QK, num_groups, elems_per_group): with T.block("B_tmp"): v0, v1, v2, v3 = T.axis.remap("SSSS", [ax0, ax1, ax2, ax3]) offset = v2 * elems_per_group + v3 shift = (offset % num_groups) * bits_stride + ( offset // num_groups ) * bits B[v0, v1] = B[v0, v1] | ( ((A[v0, v1] >> (bits * offset)) & mask) << shift ) for ax0, ax1 in T.grid(N, QK): with T.block("B"): v0, v1 = T.axis.remap("SS", [ax0, ax1]) B_tmp_1[v0, v1] = B[v0, v1] & T.uint32(0xF0F00F0F) B_tmp_2[v0, v1] = ((B[v0, v1] & T.uint32(0x000000F0)) >> 4) << 16 B_tmp_3[v0, v1] = ((B[v0, v1] & T.uint32(0x0000F000)) >> 12) << 24 B_tmp_4[v0, v1] = ((B[v0, v1] & T.uint32(0x000F0000)) >> 16) << 4 B_tmp_5[v0, v1] = ((B[v0, v1] & T.uint32(0x0F000000)) >> 24) << 12 B[v0, v1] = ( B_tmp_1[v0, v1] | B_tmp_2[v0, v1] | B_tmp_3[v0, v1] | B_tmp_4[v0, v1] | B_tmp_5[v0, v1] ) if target_dtype == "float16" and bits == 2: return interleave_weight_f16_2b elif target_dtype == "float16" and bits == 1: return interleave_weight_f16_1b elif target_dtype == "int8" and bits == 1: return interleave_weight_int8_1b return interleave_weight def test_lop3_interleave_weight(): source_nbits = 2 N = 2 K = 16 target_dtype = "float16" torch.manual_seed(0) uint_max = 2 ** (source_nbits) - 1 raw_data = torch.randint(0, uint_max, (N, K), dtype=torch.int8).cpu().numpy() compressed_b = general_compress_to_int8(raw_data, source_nbits) interleaved_weight = interleave_weight(compressed_b, source_nbits, target_dtype) interleave_func = tir_interleave_weight(N, K, source_nbits, target_dtype) ref_func = tvm.build(interleave_func, target="llvm") ctx = tvm.cpu(0) compressed_b_cast_32 = compressed_b.view(np.int32) tvm_compress_b = tvm.nd.array(compressed_b_cast_32, ctx) tvm_interleaved_b = tvm.nd.array(np.zeros_like(compressed_b_cast_32), ctx) ref_func(tvm_compress_b, tvm_interleaved_b) tvm_interleaved_b_np = tvm_interleaved_b.asnumpy() tvm_interleaved_b_np_int8 = tvm_interleaved_b_np.view(np.int8) np.testing.assert_allclose(tvm_interleaved_b_np_int8, interleaved_weight, atol=1e-5) test_lop3_interleave_weight()
BitBLAS/testing/python/type_conversion/int4b_fp16_convert.py/0
{ "file_path": "BitBLAS/testing/python/type_conversion/int4b_fp16_convert.py", "repo_id": "BitBLAS", "token_count": 5629 }
145
date ; hostname ; pwd EXP_NODES=1 EXP_IS=384 EXP_PGB=4 EXP_PGEB=16 EXP_LR=1.1e-5 EXP_BS=2048 EXP_ME=20 EXP_WS=0.1 EXP_WD=0.01 EXP_LMH=5 EXP_LMC=5 EXP_LP=BridgeTower_pt_base.ckpt EXP_RGM=blip_randaug_wc EXP_PGEBT=256 EXP_PGEBI=128 EXP_PGEBFT=500 export MASTER_ADDR=$HOSTNAME export MASTER_PORT=19800 export NODE_RANK=0 PREFIX_NAME="ftfpt" echo $MASTER_ADDR, $MASTER_PORT, $NODE_RANK, $EXP_NODES, $EXP_IS, $EXP_PGB, $EXP_PGEB, $EXP_LR, $EXP_BS, $EXP_ME, $EXP_WS, $EXP_WD, $EXP_LMH, $EXP_LMC, $EXP_RGM TIME=$(date "+%Y%m%d%H%M") RUN_NAME=""$PREFIX_NAME"_"$EXP_IS"_"$EXP_PGB"_"$EXP_PGEB"_"$EXP_LR"_"$EXP_BS"_"$EXP_ME"_"$EXP_WS"_"$EXP_WD"_"$EXP_LMH"_"$EXP_LMC"_"$EXP_RGM"_"$TIME"" echo $RUN_NAME python run.py with run_name=$RUN_NAME task_finetune_irtr_f30k_clip_bert bt clip16 text_roberta $EXP_RGM num_gpus=8 num_nodes=$EXP_NODES load_path=~/BT/best_checkpoints/$EXP_LP image_size=$EXP_IS per_gpu_batchsize=$EXP_PGB per_gpu_eval_batchsize=$EXP_PGEB learning_rate=$EXP_LR batch_size=$EXP_BS max_epoch=$EXP_ME warmup_steps=$EXP_WS weight_decay=$EXP_WD lr_mult_head=$EXP_LMH lr_mult_cross_modal=$EXP_LMC per_gpu_eval_batchsize_text=$EXP_PGEBT per_gpu_eval_batchsize_image=$EXP_PGEBI per_gpu_eval_batchsize_fusion_text=$EXP_PGEBFT date
BridgeTower/scripts/ftfpt_base_irtr_flickr.sh/0
{ "file_path": "BridgeTower/scripts/ftfpt_base_irtr_flickr.sh", "repo_id": "BridgeTower", "token_count": 625 }
146
from glob import glob from .base_dataset import BaseDataset import io from PIL import Image class ConceptualCaptionDataset(BaseDataset): def __init__(self, *args, split="", **kwargs): assert split in ["train", "val", "test"] if split == "test": split = "val" if split == "train": names = [f"conceptual_caption_train_{i}" for i in range(31)] elif split == "val": names = [] # METER # names = ["conceptual_caption_val_0"] # ViLT super().__init__(*args, **kwargs, names=names, text_column_name="caption") def __getitem__(self, index): return self.get_suite(index)
BridgeTower/src/datasets/conceptual_caption_dataset.py/0
{ "file_path": "BridgeTower/src/datasets/conceptual_caption_dataset.py", "repo_id": "BridgeTower", "token_count": 291 }
147
import torch import random from transformers.optimization import AdamW from transformers import ( get_polynomial_decay_schedule_with_warmup, get_cosine_schedule_with_warmup, ) from .objectives import compute_irtr_recall, compute_irtr_itm_itc_recall, compute_irtr_itm_itc_recall_meter, compute_irtr_itc_recall from ..gadgets.my_metrics import Accuracy, VQAScore, Scalar def set_metrics(pl_module): for split in ["train", "val", "test"]: for k, v in pl_module.hparams.config["loss_names"].items(): if v <= pl_module.hparams.config["task_threshold"]: continue if k == "vqa": setattr(pl_module, f"{split}_{k}_score", VQAScore()) setattr(pl_module, f"{split}_{k}_loss", Scalar()) elif k == "nlvr2" or k == "snli" or k == "itm" or k == "mlm": setattr(pl_module, f"{split}_{k}_accuracy", Accuracy()) setattr(pl_module, f"{split}_{k}_loss", Scalar()) elif k == "irtr" or k == "itc": setattr(pl_module, f"{split}_{k}_loss", Scalar()) elif k == "itm_itc": setattr(pl_module, f"{split}_itm_accuracy", Accuracy()) setattr(pl_module, f"{split}_itm_loss", Scalar()) setattr(pl_module, f"{split}_itc_loss", Scalar()) elif k == "irtr_itm_itc": setattr(pl_module, f"{split}_irtr_itm_accuracy", Accuracy()) setattr(pl_module, f"{split}_irtr_itm_loss", Scalar()) setattr(pl_module, f"{split}_irtr_itc_loss", Scalar()) else: raise ValueError(f"Unknown loss name: {k}") if pl_module.hparams.config["test_only"]: split = "test" else: split = "val" setattr(pl_module, "best_metric_log", {f"{split}/the_metric": 0}) for k, v in pl_module.hparams.config["loss_names"].items(): if v <= pl_module.hparams.config["task_threshold"]: continue if k == "vqa" and split == "val": getattr(pl_module, f"best_metric_log").update({ f"{split}/{k}_score": -1e9, f"{split}/{k}_loss": 1e9, }) if k == "nlvr2" or k == "snli": getattr(pl_module, f"best_metric_log").update({ f"val/{k}_accuracy": -1e9, f"test/{k}_accuracy": -1e9, f"val/{k}_loss": 1e9, f"test/{k}_loss": 1e9, }) if k == "mlm" and split == "val": getattr(pl_module, f"best_metric_log").update({ f"{split}/{k}_accuracy": -1e9, f"{split}/{k}_loss": 1e9, }) if k == "itm": getattr(pl_module, f"best_metric_log").update({ f"{split}/{k}_accuracy": -1e9, f"{split}/{k}_loss": 1e9, }) if k == "itc": getattr(pl_module, f"best_metric_log").update({ f"{split}/{k}_loss": 1e9, }) if k == "itm_itc": getattr(pl_module, f"best_metric_log").update({ f"{split}/itm_accuracy": -1e9, f"{split}/itm_loss": 1e9, f"{split}/itc_loss": 1e9, }) if k == "irtr_itm_itc": getattr(pl_module, f"best_metric_log").update({ f"{split}/irtr_itm_accuracy": -1e9, f"{split}/irtr_itm_loss": 1e9, f"{split}/irtr_itc_loss": 1e9, }) if k == "irtr" or k == "irtr_itm_itc": getattr(pl_module, f"best_metric_log").update({ f"{split}/ir_r1": -1e9, f"{split}/ir_r5": -1e9, f"{split}/ir_r10": -1e9, f"{split}/tr_r1": -1e9, f"{split}/tr_r5": -1e9, f"{split}/tr_r10": -1e9, }) if k == "irtr": getattr(pl_module, f"best_metric_log").update({ f"{split}/{k}_loss": 1e9, }) def epoch_wrapup(pl_module, split): the_metric = 0 metric_log = {} if pl_module.hparams.config["get_recall_metric"] and not pl_module.training: if "irtr_itm_itc" in pl_module.hparams.config["group_name"]: if pl_module.hparams.config["model_type"] == "BT": (ir_r1, ir_r5, ir_r10, tr_r1, tr_r5, tr_r10, ir_mean, tr_mean, r_mean) = compute_irtr_itm_itc_recall(pl_module, split) else: if pl_module.hparams.config["num_layers"] == 0: (ir_r1, ir_r5, ir_r10, tr_r1, tr_r5, tr_r10, ir_mean, tr_mean, r_mean) = compute_irtr_itc_recall(pl_module, split) else: (ir_r1, ir_r5, ir_r10, tr_r1, tr_r5, tr_r10, ir_mean, tr_mean, r_mean) = compute_irtr_itm_itc_recall_meter(pl_module, split) else: (ir_r1, ir_r5, ir_r10, tr_r1, tr_r5, tr_r10, ir_mean, tr_mean, r_mean) = compute_irtr_recall(pl_module, split) print('### Recall metrics ###') print(f"{ir_r1 * 100:.2f} \t {ir_r5 * 100:.2f} \t {ir_r10 * 100:.2f} \t {tr_r1 * 100:.2f} \t {tr_r5 * 100:.2f} \t {tr_r10 * 100:.2f} \t {ir_mean * 100:.2f} \t {tr_mean * 100:.2f} \t {r_mean * 100:.2f}") print('######################') metric_log.update({ f"{split}/ir_r1": ir_r1 * 100, f"{split}/ir_r5": ir_r5 * 100, f"{split}/ir_r10": ir_r10 * 100, f"{split}/tr_r1": tr_r1 * 100, f"{split}/tr_r5": tr_r5 * 100, f"{split}/tr_r10": tr_r10 * 100, f"{split}/ir_mean": ir_mean * 100, f"{split}/tr_mean": tr_mean * 100, f"{split}/r_mean": r_mean * 100, }) pl_module.log_dict(metric_log) the_metric += r_mean.item() for loss_name, v in pl_module.hparams.config["loss_names"].items(): if v <= pl_module.hparams.config["task_threshold"]: continue value = 0 if loss_name == "vqa" and split != "test": value = getattr(pl_module, f"{split}_{loss_name}_score").compute() pl_module.log(f"{split}/{loss_name}/score_epoch", value) getattr(pl_module, f"{split}_{loss_name}_score").reset() loss_epoch = getattr(pl_module, f"{split}_{loss_name}_loss").compute() pl_module.log(f"{split}/{loss_name}/loss_epoch", loss_epoch) getattr(pl_module, f"{split}_{loss_name}_loss").reset() if split == "val": metric_log[f'{split}/{loss_name}_score'] = value metric_log[f'{split}/{loss_name}_loss'] = loss_epoch elif loss_name == "nlvr2" or loss_name == 'snli': if split == "train": value = getattr(pl_module, f"{split}_{loss_name}_accuracy").compute() pl_module.log(f"{split}/{loss_name}/accuracy_epoch", value) getattr(pl_module, f"{split}_{loss_name}_accuracy").reset() loss_epoch = getattr(pl_module, f"{split}_{loss_name}_loss").compute() pl_module.log(f"{split}/{loss_name}/loss_epoch", loss_epoch) getattr(pl_module, f"{split}_{loss_name}_loss").reset() else: value1 = getattr(pl_module, f"test_{loss_name}_accuracy").compute() pl_module.log(f"test/{loss_name}/accuracy_epoch", value1) getattr(pl_module, f"test_{loss_name}_accuracy").reset() test_loss_epoch = getattr(pl_module, f"test_{loss_name}_loss").compute() pl_module.log(f"test/{loss_name}/loss_epoch", test_loss_epoch) getattr(pl_module, f"test_{loss_name}_loss").reset() metric_log[f'test/{loss_name}_accuracy'] = value1 metric_log[f'test/{loss_name}_loss'] = test_loss_epoch value = getattr(pl_module, f"val_{loss_name}_accuracy").compute() pl_module.log(f"val/{loss_name}/accuracy_epoch", value) getattr(pl_module, f"val_{loss_name}_accuracy").reset() val_loss_epoch = getattr(pl_module, f"val_{loss_name}_loss").compute() pl_module.log(f"val/{loss_name}/loss_epoch", val_loss_epoch) getattr(pl_module, f"val_{loss_name}_loss").reset() metric_log[f'val/{loss_name}_accuracy'] = value metric_log[f'val/{loss_name}_loss'] = val_loss_epoch elif loss_name == "itm" or loss_name == "mlm": value = getattr(pl_module, f"{split}_{loss_name}_accuracy").compute() pl_module.log(f"{split}/{loss_name}/accuracy_epoch", value) getattr(pl_module, f"{split}_{loss_name}_accuracy").reset() loss_epoch = getattr(pl_module, f"{split}_{loss_name}_loss").compute() pl_module.log(f"{split}/{loss_name}/loss_epoch", loss_epoch) getattr(pl_module, f"{split}_{loss_name}_loss").reset() if split == "val" or (split == "test" and loss_name == "itm"): metric_log[f'{split}/{loss_name}_accuracy'] = value metric_log[f'{split}/{loss_name}_loss'] = loss_epoch elif loss_name == "itc": loss_epoch = getattr(pl_module, f"{split}_{loss_name}_loss").compute() pl_module.log(f"{split}/{loss_name}/loss_epoch", loss_epoch) getattr(pl_module, f"{split}_{loss_name}_loss").reset() if split == "val": metric_log[f'{split}/{loss_name}_loss'] = loss_epoch elif loss_name == "itm_itc": value = getattr(pl_module, f"{split}_itm_accuracy").compute() pl_module.log(f"{split}/itm/accuracy_epoch", value) getattr(pl_module, f"{split}_itm_accuracy").reset() loss_epoch = getattr(pl_module, f"{split}_itm_loss").compute() pl_module.log(f"{split}/itm/loss_epoch", loss_epoch) getattr(pl_module, f"{split}_itm_loss").reset() loss_epoch = getattr(pl_module, f"{split}_itc_loss").compute() pl_module.log(f"{split}/itc/loss_epoch", loss_epoch) getattr(pl_module, f"{split}_itc_loss").reset() if split == "val" or (split == "test" and loss_name == "itm"): metric_log[f'{split}/itm_accuracy'] = value metric_log[f'{split}/itm_loss'] = loss_epoch metric_log[f'{split}/itc_loss'] = loss_epoch elif loss_name == "irtr_itm_itc": value = getattr(pl_module, f"{split}_irtr_itm_accuracy").compute() pl_module.log(f"{split}/irtr_itm/accuracy_epoch", value) getattr(pl_module, f"{split}_irtr_itm_accuracy").reset() loss_epoch = getattr(pl_module, f"{split}_irtr_itm_loss").compute() pl_module.log(f"{split}/irtr_itm/loss_epoch", loss_epoch) getattr(pl_module, f"{split}_irtr_itm_loss").reset() loss_epoch = getattr(pl_module, f"{split}_irtr_itc_loss").compute() pl_module.log(f"{split}/irtr_itc/loss_epoch", loss_epoch) getattr(pl_module, f"{split}_irtr_itc_loss").reset() if split == "val" or (split == "test" and loss_name == "itm"): metric_log[f'{split}/irtr_itm_accuracy'] = value metric_log[f'{split}/irtr_itm_loss'] = loss_epoch metric_log[f'{split}/irtr_itc_loss'] = loss_epoch value = 0 elif loss_name == "irtr": loss_epoch = getattr(pl_module, f"{split}_{loss_name}_loss").compute() pl_module.log(f"{split}/{loss_name}/loss_epoch", loss_epoch) getattr(pl_module, f"{split}_{loss_name}_loss").reset() if split != "train": metric_log[f'{split}/{loss_name}_loss'] = loss_epoch irtr_loss_epoch = loss_epoch the_metric += value # use irtr_loss for selecting checkpoints if pl_module.hparams.config["loss_names"]["irtr"] == 1: the_metric = -irtr_loss_epoch pl_module.log(f"{split}/the_metric", the_metric) if split == 'val': prev_the_metric = getattr(pl_module, f"best_metric_log")["val/the_metric"] if the_metric > prev_the_metric: metric_log["val/the_metric"] = the_metric setattr(pl_module, f"best_metric_log", metric_log) pl_module.log(f"val/best_the_metric", max(the_metric, prev_the_metric)) if split == 'test' and pl_module.hparams.config["group_name"] != "vqa": metric_log["test/the_metric"] = the_metric setattr(pl_module, f"best_metric_log", metric_log) def check_non_acc_grad(pl_module): if pl_module.token_type_embeddings.weight.grad is None: return True else: grad = pl_module.token_type_embeddings.weight.grad return (grad.sum() == 0).item() def set_task(pl_module): pl_module.current_tasks = [ k for k, v in pl_module.hparams.config["loss_names"].items() if v > pl_module.hparams.config["task_threshold"] ] return def set_schedule(pl_module): lr = pl_module.hparams.config["learning_rate"] wd = pl_module.hparams.config["weight_decay"] no_decay = [ "bias", "LayerNorm.bias", "LayerNorm.weight", "norm.bias", "norm.weight", "norm1.bias", "norm1.weight", "norm2.bias", "norm2.weight", ] head_names = ["vqa_classifier", "nlvr2_classifier", "snli_classifier", "mlm_score", "itm_score", "itc_text_head", "itc_image_head"] cross_modal_names = ['cross_modal'] lr_mult_head = pl_module.hparams.config["lr_mult_head"] lr_head = lr * lr_mult_head if type(lr_mult_head) == int else lr_mult_head lr_mult_cross_modal = pl_module.hparams.config["lr_mult_cross_modal"] lr_cross_modal = lr * lr_mult_cross_modal if type(lr_mult_cross_modal) == int else lr_mult_cross_modal print(f"lr_head: {lr_head}, lr_cross_modal: {lr_cross_modal}") end_lr = pl_module.hparams.config["end_lr"] decay_power = pl_module.hparams.config["decay_power"] optim_type = pl_module.hparams.config["optim_type"] optimizer_grouped_parameters = [ { "params": [ p for n, p in pl_module.named_parameters() if not any(nd in n for nd in no_decay) and not any(bb in n for bb in head_names) and not any(ht in n for ht in cross_modal_names) ], "weight_decay": wd, "lr": lr, }, { "params": [ p for n, p in pl_module.named_parameters() if any(nd in n for nd in no_decay) and not any(bb in n for bb in head_names) and not any(ht in n for ht in cross_modal_names) ], "weight_decay": 0.0, "lr": lr, }, { "params": [ p for n, p in pl_module.named_parameters() if not any(nd in n for nd in no_decay) and any(bb in n for bb in head_names) and not any(ht in n for ht in cross_modal_names) ], "weight_decay": wd, "lr": lr_head, }, { "params": [ p for n, p in pl_module.named_parameters() if any(nd in n for nd in no_decay) and any(bb in n for bb in head_names) and not any(ht in n for ht in cross_modal_names) ], "weight_decay": 0.0, "lr": lr_head, }, { "params": [ p for n, p in pl_module.named_parameters() if not any(nd in n for nd in no_decay) and not any(bb in n for bb in head_names) and any(ht in n for ht in cross_modal_names) ], "weight_decay": wd, "lr": lr_cross_modal, }, { "params": [ p for n, p in pl_module.named_parameters() if any(nd in n for nd in no_decay) and not any(bb in n for bb in head_names) and any(ht in n for ht in cross_modal_names) ], "weight_decay": 0.0, "lr": lr_cross_modal, }, ] if optim_type == "adamw": optimizer = AdamW( optimizer_grouped_parameters, lr=lr, eps=1e-8, betas=(0.9, 0.98) ) elif optim_type == "adam": optimizer = torch.optim.Adam(optimizer_grouped_parameters, lr=lr) elif optim_type == "sgd": optimizer = torch.optim.SGD(optimizer_grouped_parameters, lr=lr, momentum=0.9) if pl_module.trainer.max_steps == -1: max_steps = ( len(pl_module.trainer.datamodule.train_dataloader()) * pl_module.trainer.max_epochs // pl_module.trainer.accumulate_grad_batches # // pl_module.trainer.gpus # Very Strange that the len(pl_module.trainer.datamodule.train_dataloader()) haven't been divided by pl_module.trainer.gpus, So I fix it to get the correct max_steps for scheduler. # Alert: Only when the replace_sampler_ddp is True, the len(pl_module.trainer.datamodule.train_dataloader()) need to be divided by pl_module.trainer.gpus. ) else: max_steps = pl_module.trainer.max_steps warmup_steps = pl_module.hparams.config["warmup_steps"] if isinstance(pl_module.hparams.config["warmup_steps"], float): warmup_steps = int(max_steps * warmup_steps) if decay_power == "cosine": scheduler = get_cosine_schedule_with_warmup( optimizer, num_warmup_steps=warmup_steps, num_training_steps=max_steps, ) else: scheduler = get_polynomial_decay_schedule_with_warmup( optimizer, num_warmup_steps=warmup_steps, num_training_steps=max_steps, lr_end=end_lr, power=decay_power, ) sched = {"scheduler": scheduler, "interval": "step"} return ( [optimizer], [sched], )
BridgeTower/src/modules/meter_utils.py/0
{ "file_path": "BridgeTower/src/modules/meter_utils.py", "repo_id": "BridgeTower", "token_count": 9817 }
148
import json import pandas as pd import pyarrow as pa import os from tqdm import tqdm from collections import defaultdict def process(root, iden, row): texts = [r["sentence"] for r in row] labels = [r["label"] for r in row] split = iden.split("-")[0] if iden.startswith("train"): directory = row[0]["directory"] path = f"{root}/images/train/{directory}/{iden}" else: path = f"{root}/{split}/{iden}" with open(f"{path}-img0.png", "rb") as fp: img0 = fp.read() with open(f"{path}-img1.png", "rb") as fp: img1 = fp.read() return [img0, img1, texts, labels, iden] def make_arrow(root, dataset_root): train_data = list( map(json.loads, open(f"{root}/nlvr2/data/train.json").readlines()) ) test1_data = list( map(json.loads, open(f"{root}/nlvr2/data/test1.json").readlines()) ) dev_data = list(map(json.loads, open(f"{root}/nlvr2/data/dev.json").readlines())) balanced_test1_data = list( map( json.loads, open(f"{root}/nlvr2/data/balanced/balanced_test1.json").readlines(), ) ) balanced_dev_data = list( map( json.loads, open(f"{root}/nlvr2/data/balanced/balanced_dev.json").readlines(), ) ) unbalanced_test1_data = list( map( json.loads, open(f"{root}/nlvr2/data/unbalanced/unbalanced_test1.json").readlines(), ) ) unbalanced_dev_data = list( map( json.loads, open(f"{root}/nlvr2/data/unbalanced/unbalanced_dev.json").readlines(), ) ) splits = [ "train", "dev", "test1", "balanced_dev", "balanced_test1", "unbalanced_dev", "unbalanced_test1", ] datas = [ train_data, dev_data, test1_data, balanced_dev_data, balanced_test1_data, unbalanced_dev_data, unbalanced_test1_data, ] annotations = dict() for split, data in zip(splits, datas): _annot = defaultdict(list) for row in tqdm(data): _annot["-".join(row["identifier"].split("-")[:-1])].append(row) annotations[split] = _annot for split in splits: bs = [ process(root, iden, row) for iden, row in tqdm(annotations[split].items()) ] dataframe = pd.DataFrame( bs, columns=["image_0", "image_1", "questions", "answers", "identifier"], ) table = pa.Table.from_pandas(dataframe) os.makedirs(dataset_root, exist_ok=True) with pa.OSFile(f"{dataset_root}/nlvr2_{split}.arrow", "wb") as sink: with pa.RecordBatchFileWriter(sink, table.schema) as writer: writer.write_table(table) # https://lil.nlp.cornell.edu/resources/NLVR2/ make_arrow('~/BT/dataset/nlvr', '~/BT/dataset/fine-tune')
BridgeTower/src/utils/write_nlvr2.py/0
{ "file_path": "BridgeTower/src/utils/write_nlvr2.py", "repo_id": "BridgeTower", "token_count": 1462 }
149
# Copyright (c) Microsoft Corporation. # Licensed under the MIT License. import io import os import struct from PIL import Image class BigFileMemoryLoader(object): def __load_bigfile(self): print('start load bigfile (%0.02f GB) into memory' % (os.path.getsize(self.file_path)/1024/1024/1024)) with open(self.file_path, 'rb') as fid: self.img_num = struct.unpack('i', fid.read(4))[0] self.img_names = [] self.img_bytes = [] print('find total %d images' % self.img_num) for i in range(self.img_num): img_name_len = struct.unpack('i', fid.read(4))[0] img_name = fid.read(img_name_len).decode('utf-8') self.img_names.append(img_name) img_bytes_len = struct.unpack('i', fid.read(4))[0] self.img_bytes.append(fid.read(img_bytes_len)) if i % 5000 == 0: print('load %d images done' % i) print('load all %d images done' % self.img_num) def __init__(self, file_path): super(BigFileMemoryLoader, self).__init__() self.file_path = file_path self.__load_bigfile() def __getitem__(self, index): try: img = Image.open(io.BytesIO(self.img_bytes[index])).convert('RGB') return self.img_names[index], img except Exception: print('Image read error for index %d: %s' % (index, self.img_names[index])) return self.__getitem__((index+1)%self.img_num) def __len__(self): return self.img_num
Bringing-Old-Photos-Back-to-Life/Global/data/Load_Bigfile.py/0
{ "file_path": "Bringing-Old-Photos-Back-to-Life/Global/data/Load_Bigfile.py", "repo_id": "Bringing-Old-Photos-Back-to-Life", "token_count": 762 }
150
# Copyright (c) Microsoft Corporation. # Licensed under the MIT License. import numpy as np import torch import torch.nn as nn import torch.nn.functional as F import os import functools from torch.autograd import Variable from util.image_pool import ImagePool from .base_model import BaseModel from . import networks import math from .NonLocal_feature_mapping_model import * class Mapping_Model(nn.Module): def __init__(self, nc, mc=64, n_blocks=3, norm="instance", padding_type="reflect", opt=None): super(Mapping_Model, self).__init__() norm_layer = networks.get_norm_layer(norm_type=norm) activation = nn.ReLU(True) model = [] tmp_nc = 64 n_up = 4 print("Mapping: You are using the mapping model without global restoration.") for i in range(n_up): ic = min(tmp_nc * (2 ** i), mc) oc = min(tmp_nc * (2 ** (i + 1)), mc) model += [nn.Conv2d(ic, oc, 3, 1, 1), norm_layer(oc), activation] for i in range(n_blocks): model += [ networks.ResnetBlock( mc, padding_type=padding_type, activation=activation, norm_layer=norm_layer, opt=opt, dilation=opt.mapping_net_dilation, ) ] for i in range(n_up - 1): ic = min(64 * (2 ** (4 - i)), mc) oc = min(64 * (2 ** (3 - i)), mc) model += [nn.Conv2d(ic, oc, 3, 1, 1), norm_layer(oc), activation] model += [nn.Conv2d(tmp_nc * 2, tmp_nc, 3, 1, 1)] if opt.feat_dim > 0 and opt.feat_dim < 64: model += [norm_layer(tmp_nc), activation, nn.Conv2d(tmp_nc, opt.feat_dim, 1, 1)] # model += [nn.Conv2d(64, 1, 1, 1, 0)] self.model = nn.Sequential(*model) def forward(self, input): return self.model(input) class Pix2PixHDModel_Mapping(BaseModel): def name(self): return "Pix2PixHDModel_Mapping" def init_loss_filter(self, use_gan_feat_loss, use_vgg_loss, use_smooth_l1, stage_1_feat_l2): flags = (True, True, use_gan_feat_loss, use_vgg_loss, True, True, use_smooth_l1, stage_1_feat_l2) def loss_filter(g_feat_l2, g_gan, g_gan_feat, g_vgg, d_real, d_fake, smooth_l1, stage_1_feat_l2): return [ l for (l, f) in zip( (g_feat_l2, g_gan, g_gan_feat, g_vgg, d_real, d_fake, smooth_l1, stage_1_feat_l2), flags ) if f ] return loss_filter def initialize(self, opt): BaseModel.initialize(self, opt) if opt.resize_or_crop != "none" or not opt.isTrain: torch.backends.cudnn.benchmark = True self.isTrain = opt.isTrain input_nc = opt.label_nc if opt.label_nc != 0 else opt.input_nc ##### define networks # Generator network netG_input_nc = input_nc self.netG_A = networks.GlobalGenerator_DCDCv2( netG_input_nc, opt.output_nc, opt.ngf, opt.k_size, opt.n_downsample_global, networks.get_norm_layer(norm_type=opt.norm), opt=opt, ) self.netG_B = networks.GlobalGenerator_DCDCv2( netG_input_nc, opt.output_nc, opt.ngf, opt.k_size, opt.n_downsample_global, networks.get_norm_layer(norm_type=opt.norm), opt=opt, ) if opt.non_local == "Setting_42" or opt.NL_use_mask: if opt.mapping_exp==1: self.mapping_net = Mapping_Model_with_mask_2( min(opt.ngf * 2 ** opt.n_downsample_global, opt.mc), opt.map_mc, n_blocks=opt.mapping_n_block, opt=opt, ) else: self.mapping_net = Mapping_Model_with_mask( min(opt.ngf * 2 ** opt.n_downsample_global, opt.mc), opt.map_mc, n_blocks=opt.mapping_n_block, opt=opt, ) else: self.mapping_net = Mapping_Model( min(opt.ngf * 2 ** opt.n_downsample_global, opt.mc), opt.map_mc, n_blocks=opt.mapping_n_block, opt=opt, ) self.mapping_net.apply(networks.weights_init) if opt.load_pretrain != "": self.load_network(self.mapping_net, "mapping_net", opt.which_epoch, opt.load_pretrain) if not opt.no_load_VAE: self.load_network(self.netG_A, "G", opt.use_vae_which_epoch, opt.load_pretrainA) self.load_network(self.netG_B, "G", opt.use_vae_which_epoch, opt.load_pretrainB) for param in self.netG_A.parameters(): param.requires_grad = False for param in self.netG_B.parameters(): param.requires_grad = False self.netG_A.eval() self.netG_B.eval() if opt.gpu_ids: self.netG_A.cuda(opt.gpu_ids[0]) self.netG_B.cuda(opt.gpu_ids[0]) self.mapping_net.cuda(opt.gpu_ids[0]) if not self.isTrain: self.load_network(self.mapping_net, "mapping_net", opt.which_epoch) # Discriminator network if self.isTrain: use_sigmoid = opt.no_lsgan netD_input_nc = opt.ngf * 2 if opt.feat_gan else input_nc + opt.output_nc if not opt.no_instance: netD_input_nc += 1 self.netD = networks.define_D(netD_input_nc, opt.ndf, opt.n_layers_D, opt, opt.norm, use_sigmoid, opt.num_D, not opt.no_ganFeat_loss, gpu_ids=self.gpu_ids) # set loss functions and optimizers if self.isTrain: if opt.pool_size > 0 and (len(self.gpu_ids)) > 1: raise NotImplementedError("Fake Pool Not Implemented for MultiGPU") self.fake_pool = ImagePool(opt.pool_size) self.old_lr = opt.lr # define loss functions self.loss_filter = self.init_loss_filter(not opt.no_ganFeat_loss, not opt.no_vgg_loss, opt.Smooth_L1, opt.use_two_stage_mapping) self.criterionGAN = networks.GANLoss(use_lsgan=not opt.no_lsgan, tensor=self.Tensor) self.criterionFeat = torch.nn.L1Loss() self.criterionFeat_feat = torch.nn.L1Loss() if opt.use_l1_feat else torch.nn.MSELoss() if self.opt.image_L1: self.criterionImage=torch.nn.L1Loss() else: self.criterionImage = torch.nn.SmoothL1Loss() print(self.criterionFeat_feat) if not opt.no_vgg_loss: self.criterionVGG = networks.VGGLoss_torch(self.gpu_ids) # Names so we can breakout loss self.loss_names = self.loss_filter('G_Feat_L2', 'G_GAN', 'G_GAN_Feat', 'G_VGG','D_real', 'D_fake', 'Smooth_L1', 'G_Feat_L2_Stage_1') # initialize optimizers # optimizer G if opt.no_TTUR: beta1,beta2=opt.beta1,0.999 G_lr,D_lr=opt.lr,opt.lr else: beta1,beta2=0,0.9 G_lr,D_lr=opt.lr/2,opt.lr*2 if not opt.no_load_VAE: params = list(self.mapping_net.parameters()) self.optimizer_mapping = torch.optim.Adam(params, lr=G_lr, betas=(beta1, beta2)) # optimizer D params = list(self.netD.parameters()) self.optimizer_D = torch.optim.Adam(params, lr=D_lr, betas=(beta1, beta2)) print("---------- Optimizers initialized -------------") def encode_input(self, label_map, inst_map=None, real_image=None, feat_map=None, infer=False): if self.opt.label_nc == 0: input_label = label_map.data.cuda() else: # create one-hot vector for label map size = label_map.size() oneHot_size = (size[0], self.opt.label_nc, size[2], size[3]) input_label = torch.cuda.FloatTensor(torch.Size(oneHot_size)).zero_() input_label = input_label.scatter_(1, label_map.data.long().cuda(), 1.0) if self.opt.data_type == 16: input_label = input_label.half() # get edges from instance map if not self.opt.no_instance: inst_map = inst_map.data.cuda() edge_map = self.get_edges(inst_map) input_label = torch.cat((input_label, edge_map), dim=1) input_label = Variable(input_label, volatile=infer) # real images for training if real_image is not None: real_image = Variable(real_image.data.cuda()) return input_label, inst_map, real_image, feat_map def discriminate(self, input_label, test_image, use_pool=False): input_concat = torch.cat((input_label, test_image.detach()), dim=1) if use_pool: fake_query = self.fake_pool.query(input_concat) return self.netD.forward(fake_query) else: return self.netD.forward(input_concat) def forward(self, label, inst, image, feat, pair=True, infer=False, last_label=None, last_image=None): # Encode Inputs input_label, inst_map, real_image, feat_map = self.encode_input(label, inst, image, feat) # Fake Generation input_concat = input_label label_feat = self.netG_A.forward(input_concat, flow='enc') # print('label:') # print(label_feat.min(), label_feat.max(), label_feat.mean()) #label_feat = label_feat / 16.0 if self.opt.NL_use_mask: label_feat_map=self.mapping_net(label_feat.detach(),inst) else: label_feat_map = self.mapping_net(label_feat.detach()) fake_image = self.netG_B.forward(label_feat_map, flow='dec') image_feat = self.netG_B.forward(real_image, flow='enc') loss_feat_l2_stage_1=0 loss_feat_l2 = self.criterionFeat_feat(label_feat_map, image_feat.data) * self.opt.l2_feat if self.opt.feat_gan: # Fake Detection and Loss pred_fake_pool = self.discriminate(label_feat.detach(), label_feat_map, use_pool=True) loss_D_fake = self.criterionGAN(pred_fake_pool, False) # Real Detection and Loss pred_real = self.discriminate(label_feat.detach(), image_feat) loss_D_real = self.criterionGAN(pred_real, True) # GAN loss (Fake Passability Loss) pred_fake = self.netD.forward(torch.cat((label_feat.detach(), label_feat_map), dim=1)) loss_G_GAN = self.criterionGAN(pred_fake, True) else: # Fake Detection and Loss pred_fake_pool = self.discriminate(input_label, fake_image, use_pool=True) loss_D_fake = self.criterionGAN(pred_fake_pool, False) # Real Detection and Loss if pair: pred_real = self.discriminate(input_label, real_image) else: pred_real = self.discriminate(last_label, last_image) loss_D_real = self.criterionGAN(pred_real, True) # GAN loss (Fake Passability Loss) pred_fake = self.netD.forward(torch.cat((input_label, fake_image), dim=1)) loss_G_GAN = self.criterionGAN(pred_fake, True) # GAN feature matching loss loss_G_GAN_Feat = 0 if not self.opt.no_ganFeat_loss and pair: feat_weights = 4.0 / (self.opt.n_layers_D + 1) D_weights = 1.0 / self.opt.num_D for i in range(self.opt.num_D): for j in range(len(pred_fake[i])-1): tmp = self.criterionFeat(pred_fake[i][j], pred_real[i][j].detach()) * self.opt.lambda_feat loss_G_GAN_Feat += D_weights * feat_weights * tmp else: loss_G_GAN_Feat = torch.zeros(1).to(label.device) # VGG feature matching loss loss_G_VGG = 0 if not self.opt.no_vgg_loss: loss_G_VGG = self.criterionVGG(fake_image, real_image) * self.opt.lambda_feat if pair else torch.zeros(1).to(label.device) smooth_l1_loss=0 if self.opt.Smooth_L1: smooth_l1_loss=self.criterionImage(fake_image,real_image)*self.opt.L1_weight return [ self.loss_filter(loss_feat_l2, loss_G_GAN, loss_G_GAN_Feat, loss_G_VGG, loss_D_real, loss_D_fake,smooth_l1_loss,loss_feat_l2_stage_1), None if not infer else fake_image ] def inference(self, label, inst): use_gpu = len(self.opt.gpu_ids) > 0 if use_gpu: input_concat = label.data.cuda() inst_data = inst.cuda() else: input_concat = label.data inst_data = inst label_feat = self.netG_A.forward(input_concat, flow="enc") if self.opt.NL_use_mask: if self.opt.inference_optimize: label_feat_map=self.mapping_net.inference_forward(label_feat.detach(),inst_data) else: label_feat_map = self.mapping_net(label_feat.detach(), inst_data) else: label_feat_map = self.mapping_net(label_feat.detach()) fake_image = self.netG_B.forward(label_feat_map, flow="dec") return fake_image class InferenceModel(Pix2PixHDModel_Mapping): def forward(self, label, inst): return self.inference(label, inst)
Bringing-Old-Photos-Back-to-Life/Global/models/mapping_model.py/0
{ "file_path": "Bringing-Old-Photos-Back-to-Life/Global/models/mapping_model.py", "repo_id": "Bringing-Old-Photos-Back-to-Life", "token_count": 7186 }
151
# Copyright (c) Microsoft Corporation. # Licensed under the MIT License. import numpy as np import os import ntpath import time from . import util #from . import html import scipy.misc try: from StringIO import StringIO # Python 2.7 except ImportError: from io import BytesIO # Python 3.x class Visualizer(): def __init__(self, opt): # self.opt = opt self.tf_log = opt.tf_log self.use_html = opt.isTrain and not opt.no_html self.win_size = opt.display_winsize self.name = opt.name if self.tf_log: import tensorflow as tf self.tf = tf self.log_dir = os.path.join(opt.checkpoints_dir, opt.name, 'logs') self.writer = tf.summary.FileWriter(self.log_dir) if self.use_html: self.web_dir = os.path.join(opt.checkpoints_dir, opt.name, 'web') self.img_dir = os.path.join(self.web_dir, 'images') print('create web directory %s...' % self.web_dir) util.mkdirs([self.web_dir, self.img_dir]) self.log_name = os.path.join(opt.checkpoints_dir, opt.name, 'loss_log.txt') with open(self.log_name, "a") as log_file: now = time.strftime("%c") log_file.write('================ Training Loss (%s) ================\n' % now) # |visuals|: dictionary of images to display or save def display_current_results(self, visuals, epoch, step): if self.tf_log: # show images in tensorboard output img_summaries = [] for label, image_numpy in visuals.items(): # Write the image to a string try: s = StringIO() except: s = BytesIO() scipy.misc.toimage(image_numpy).save(s, format="jpeg") # Create an Image object img_sum = self.tf.Summary.Image(encoded_image_string=s.getvalue(), height=image_numpy.shape[0], width=image_numpy.shape[1]) # Create a Summary value img_summaries.append(self.tf.Summary.Value(tag=label, image=img_sum)) # Create and write Summary summary = self.tf.Summary(value=img_summaries) self.writer.add_summary(summary, step) if self.use_html: # save images to a html file for label, image_numpy in visuals.items(): if isinstance(image_numpy, list): for i in range(len(image_numpy)): img_path = os.path.join(self.img_dir, 'epoch%.3d_%s_%d.jpg' % (epoch, label, i)) util.save_image(image_numpy[i], img_path) else: img_path = os.path.join(self.img_dir, 'epoch%.3d_%s.jpg' % (epoch, label)) util.save_image(image_numpy, img_path) # update website webpage = html.HTML(self.web_dir, 'Experiment name = %s' % self.name, refresh=30) for n in range(epoch, 0, -1): webpage.add_header('epoch [%d]' % n) ims = [] txts = [] links = [] for label, image_numpy in visuals.items(): if isinstance(image_numpy, list): for i in range(len(image_numpy)): img_path = 'epoch%.3d_%s_%d.jpg' % (n, label, i) ims.append(img_path) txts.append(label+str(i)) links.append(img_path) else: img_path = 'epoch%.3d_%s.jpg' % (n, label) ims.append(img_path) txts.append(label) links.append(img_path) if len(ims) < 10: webpage.add_images(ims, txts, links, width=self.win_size) else: num = int(round(len(ims)/2.0)) webpage.add_images(ims[:num], txts[:num], links[:num], width=self.win_size) webpage.add_images(ims[num:], txts[num:], links[num:], width=self.win_size) webpage.save() # errors: dictionary of error labels and values def plot_current_errors(self, errors, step): if self.tf_log: for tag, value in errors.items(): summary = self.tf.Summary(value=[self.tf.Summary.Value(tag=tag, simple_value=value)]) self.writer.add_summary(summary, step) # errors: same format as |errors| of plotCurrentErrors def print_current_errors(self, epoch, i, errors, t, lr): message = '(epoch: %d, iters: %d, time: %.3f lr: %.5f) ' % (epoch, i, t, lr) for k, v in errors.items(): if v != 0: message += '%s: %.3f ' % (k, v) print(message) with open(self.log_name, "a") as log_file: log_file.write('%s\n' % message) def print_save(self,message): print(message) with open(self.log_name,"a") as log_file: log_file.write('%s\n'%message) # save image to the disk def save_images(self, webpage, visuals, image_path): image_dir = webpage.get_image_dir() short_path = ntpath.basename(image_path[0]) name = os.path.splitext(short_path)[0] webpage.add_header(name) ims = [] txts = [] links = [] for label, image_numpy in visuals.items(): image_name = '%s_%s.jpg' % (name, label) save_path = os.path.join(image_dir, image_name) util.save_image(image_numpy, save_path) ims.append(image_name) txts.append(label) links.append(image_name) webpage.add_images(ims, txts, links, width=self.win_size)
Bringing-Old-Photos-Back-to-Life/Global/util/visualizer.py/0
{ "file_path": "Bringing-Old-Photos-Back-to-Life/Global/util/visualizer.py", "repo_id": "Bringing-Old-Photos-Back-to-Life", "token_count": 3002 }
152
# Ke Chen # [email protected] # HTS-AT: A HIERARCHICAL TOKEN-SEMANTIC AUDIO TRANSFORMER FOR SOUND CLASSIFICATION AND DETECTION # The configuration for training the model exp_name = "exp_htsat_pretrain" # the saved ckpt prefix name of the model workspace = "/home/kechen/Research/HTSAT" # the folder of your code dataset_path = "/home/Research/audioset" # the dataset path desed_folder = "/home/Research/DESED" # the desed file dataset_type = "audioset" # "audioset" "esc-50" "scv2" index_type = "full_train" # only works for audioset balanced_data = True # only works for audioset loss_type = "clip_bce" # # AudioSet & SCV2: "clip_bce" | ESC-50: "clip_ce" # trained from a checkpoint, or evaluate a single model resume_checkpoint = None # "/home/Research/model_backup/AudioSet/HTSAT_AudioSet_Saved_1.ckpt" esc_fold = 0 # just for esc dataset, select the fold you need for evaluation and (+1) validation debug = False random_seed = 970131 # 19970318 970131 12412 127777 1009 34047 batch_size = 32 * 4 # batch size per GPU x GPU number , default is 32 x 4 = 128 learning_rate = 1e-3 # 1e-4 also workable max_epoch = 100 num_workers = 3 lr_scheduler_epoch = [10,20,30] lr_rate = [0.02, 0.05, 0.1] # these data preparation optimizations do not bring many improvements, so deprecated enable_token_label = False # token label class_map_path = "class_hier_map.npy" class_filter = None retrieval_index = [15382, 9202, 130, 17618, 17157, 17516, 16356, 6165, 13992, 9238, 5550, 5733, 1914, 1600, 3450, 13735, 11108, 3762, 9840, 11318, 8131, 4429, 16748, 4992, 16783, 12691, 4945, 8779, 2805, 9418, 2797, 14357, 5603, 212, 3852, 12666, 1338, 10269, 2388, 8260, 4293, 14454, 7677, 11253, 5060, 14938, 8840, 4542, 2627, 16336, 8992, 15496, 11140, 446, 6126, 10691, 8624, 10127, 9068, 16710, 10155, 14358, 7567, 5695, 2354, 8057, 17635, 133, 16183, 14535, 7248, 4560, 14429, 2463, 10773, 113, 2462, 9223, 4929, 14274, 4716, 17307, 4617, 2132, 11083, 1039, 1403, 9621, 13936, 2229, 2875, 17840, 9359, 13311, 9790, 13288, 4750, 17052, 8260, 14900] token_label_range = [0.2,0.6] enable_time_shift = False # shift time enable_label_enhance = False # enhance hierarchical label enable_repeat_mode = False # repeat the spectrogram / reshape the spectrogram # for model's design enable_tscam = True # enbale the token-semantic layer # for signal processing sample_rate = 32000 # 16000 for scv2, 32000 for audioset and esc-50 clip_samples = sample_rate * 10 # audio_set 10-sec clip window_size = 1024 hop_size = 320 # 160 for scv2, 320 for audioset and esc-50 mel_bins = 64 fmin = 50 fmax = 14000 shift_max = int(clip_samples * 0.5) # for data collection classes_num = 527 # esc: 50 | audioset: 527 | scv2: 35 patch_size = (25, 4) # deprecated crop_size = None # int(clip_samples * 0.5) deprecated # for htsat hyperparamater htsat_window_size = 8 htsat_spec_size = 256 htsat_patch_size = 4 htsat_stride = (4, 4) htsat_num_head = [4,8,16,32] htsat_dim = 96 htsat_depth = [2,2,6,2] swin_pretrain_path = None # "/home/Research/model_backup/pretrain/swin_tiny_c24_patch4_window8_256.pth" # Some Deprecated Optimization in the model design, check the model code for details htsat_attn_heatmap = False htsat_hier_output = False htsat_use_max = False # for ensemble test ensemble_checkpoints = [] ensemble_strides = [] # weight average folder wa_folder = "/home/version_0/checkpoints/" # weight average output filename wa_model_path = "HTSAT_AudioSet_Saved_x.ckpt" esm_model_pathes = [ "/home/Research/model_backup/AudioSet/HTSAT_AudioSet_Saved_1.ckpt", "/home/Research/model_backup/AudioSet/HTSAT_AudioSet_Saved_2.ckpt", "/home/Research/model_backup/AudioSet/HTSAT_AudioSet_Saved_3.ckpt", "/home/Research/model_backup/AudioSet/HTSAT_AudioSet_Saved_4.ckpt", "/home/Research/model_backup/AudioSet/HTSAT_AudioSet_Saved_5.ckpt", "/home/Research/model_backup/AudioSet/HTSAT_AudioSet_Saved_6.ckpt" ] # for framewise localization heatmap_dir = "/home/Research/heatmap_output" test_file = "htsat-test-ensemble" fl_local = False # indicate if we need to use this dataset for the framewise detection fl_dataset = "/home/Research/desed/desed_eval.npy" fl_class_num = [ "Speech", "Frying", "Dishes", "Running_water", "Blender", "Electric_shaver_toothbrush", "Alarm_bell_ringing", "Cat", "Dog", "Vacuum_cleaner" ] # map 527 classes into 10 classes fl_audioset_mapping = [ [0,1,2,3,4,5,6,7], [366, 367, 368], [364], [288, 289, 290, 291, 292, 293, 294, 295, 296, 297], [369], [382], [310, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402], [81, 82, 83, 84, 85], [74, 75, 76, 77, 78, 79], [377] ]
CLAP/msclap/models/config.py/0
{ "file_path": "CLAP/msclap/models/config.py", "repo_id": "CLAP", "token_count": 1874 }
153
# Adaptive Span Adaptive Span is a novel self-attention mechanism that can learn its optimal attention span. This allows us to extend significantly the maximum context size used in Transformer, while maintaining control over their memory footprint and computational time. It uses the Truncated BPTT technique for training, as in [transformerXL](https://github.com/pytorch/fairseq/blob/master/examples/truncated_bptt/README.md). Adaptive Span was introduced by paper: [Adaptive Attention Span in Transformers](https://arxiv.org/abs/1905.07799), which achieved state-of-the-art language modeling results at the time of publication. We manage to reproduce their result in fairseq and keep most of the [original implementation](https://github.com/facebookresearch/adaptive-span) untouched. You can refer to the their sweep file as well if any combination of hyperparameter is not clear. ##### 0. Setup First you need to process the Enwik8 dataset, we use the pre-tokenized dataset from [adaptive span paper](https://github.com/facebookresearch/adaptive-span/blob/master/get_data.sh). You can download the dataset, and then run: ```bash fairseq-preprocess --only-source --trainpref ~/data/enwik8/train.txt \ --validpref ~/data/enwik8/valid.txt --testpref ~/data/enwik8/test.txt \ --destdir ~/data/enwik8/data-bin/ --joined-dictionary --workers 20 ``` ##### 1. Train a Adaptive Span model on Enwik8 We will train a 12-layer Adaptive Span model following the [hyperparameters used in the original paper](https://github.com/facebookresearch/adaptive-span/blob/master/experiments/enwik8.sh). The following command assumes 4 GPUs, so that the total batch size is 64 sequences (4 x 16). Training should take 2-3 days on 4 V100 GPUs: ```bash CUDA_VISIBLE_DEVICES=0,1,2,3 fairseq-train \ --user-dir examples/adaptive_span \ --data ~/data/enwik8/data-bin/ \ --fp16 --fp16-no-flatten-grads --max-update 600000 \ --task truncated_bptt_lm --tokens-per-sample 512 --arch adaptive_span \ --n-layer 12 --d-model 512 --n-head 8 --d-inner 2048 --dropout 0.3 \ --attn-span 8192 --optimizer adagrad_with_grad_clip --adagrad-clip 0.03 \ --validate-interval-updates 1000 \ --lr-scheduler fixed --warmup-updates 32000 --batch-size-valid 32 \ --lr 0.07 --criterion adaptive_span_loss --batch-size 16 --update-freq 1 \ --seed 2 --log-format json --log-interval 25 --aux-loss-scaler 5e-07 ``` This should land around 1.05 on validation, 1.03 on test. You can lower the --aux-loss-scaler for better performance (longer span). It gives ~0.03 bpc improvement to the transformerXL baseline here. If training on a single GPU, set `--update-freq=4` to accumulate 4x gradients and simulate training on 4 GPUs. You can also reproduce the transformerXL result on enwik8 using this code base. It should land around 1.06 on test,matching the [original paper](https://github.com/kimiyoung/transformer-xl/blob/master/pytorch/run_enwik8_base.sh). You can try by ```bash CUDA_VISIBLE_DEVICES=0,1,2,3 fairseq-train \ --user-dir examples/truncated_bptt \ ~/data/enwik8/data-bin/ \ --task truncated_bptt_lm --fp16 --max-update 400000 \ --tokens-per-sample 512 --arch transformer_xl --n-layer 12 \ --d-model 512 --n-head 8 --d-head 64 --d-inner 2048 --dropout 0.1 \ --dropatt 0.0 --mem-len 512 --optimizer adam --clip-norm 0.25 \ --lr-scheduler cosine --warmup-updates 0 \ --lr 0.0 --lr 0.00025 --batch-size 15 \ --update-freq 1 --seed 2 --log-format json --log-interval 25 \ --fp16 ``` ##### 2. Evaluate For Adaptive Span: ```bash fairseq-eval-lm ~/data/enwik8/data-bin/ --path model/checkpoint_best.pt \ --user-dir examples/adaptive_span \ --task truncated_bptt_lm --batch-size 8 --tokens-per-sample 512 --gen-subset test ``` For Transformer-XL evaluation: ```bash fairseq-eval-lm ~/data/enwik8/data-bin/ --path model/checkpoint_best.pt \ --user-dir examples/truncated_bptt/ --task truncated_bptt_lm --batch-size 8 \ --tokens-per-sample 80 \ --model-overrides '{"mem_len":2100,"clamp_len":820,"same_length":True}' \ --gen-subset valid ``` *Note:* During training the model saw 512 tokens of context (``--tokens-per-sample=512``), with batch size 8. These settings match the evaluation settings from [the original paper](https://github.com/facebookresearch/adaptive-span/blob/master/experiments/enwik8.sh).
COCO-LM/fairseq/examples/adaptive_span/README.md/0
{ "file_path": "COCO-LM/fairseq/examples/adaptive_span/README.md", "repo_id": "COCO-LM", "token_count": 1496 }
154
# Fine-tuning BART on CNN-Dailymail summarization task ### 1) Download the CNN and Daily Mail data and preprocess it into data files with non-tokenized cased samples. Follow the instructions [here](https://github.com/abisee/cnn-dailymail) to download the original CNN and Daily Mail datasets. To preprocess the data, refer to the pointers in [this issue](https://github.com/pytorch/fairseq/issues/1391) or check out the code [here](https://github.com/artmatsak/cnn-dailymail). Follow the instructions [here](https://github.com/EdinburghNLP/XSum) to download the original Extreme Summarization datasets, or check out the code [here](https://github.com/EdinburghNLP/XSum/tree/master/XSum-Dataset), Please keep the raw dataset and make sure no tokenization nor BPE on the dataset. ### 2) BPE preprocess: ```bash wget -N 'https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/encoder.json' wget -N 'https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/vocab.bpe' wget -N 'https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/dict.txt' TASK=cnn_dm for SPLIT in train val do for LANG in source target do python -m examples.roberta.multiprocessing_bpe_encoder \ --encoder-json encoder.json \ --vocab-bpe vocab.bpe \ --inputs "$TASK/$SPLIT.$LANG" \ --outputs "$TASK/$SPLIT.bpe.$LANG" \ --workers 60 \ --keep-empty; done done ``` ### 3) Binarize dataset: ```bash fairseq-preprocess \ --source-lang "source" \ --target-lang "target" \ --trainpref "${TASK}/train.bpe" \ --validpref "${TASK}/val.bpe" \ --destdir "${TASK}-bin/" \ --workers 60 \ --srcdict dict.txt \ --tgtdict dict.txt; ``` ### 4) Fine-tuning on CNN-DM summarization task: Example fine-tuning CNN-DM ```bash TOTAL_NUM_UPDATES=20000 WARMUP_UPDATES=500 LR=3e-05 MAX_TOKENS=2048 UPDATE_FREQ=4 BART_PATH=/path/to/bart/model.pt CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 fairseq-train cnn_dm-bin \ --restore-file $BART_PATH \ --max-tokens $MAX_TOKENS \ --task translation \ --source-lang source --target-lang target \ --truncate-source \ --layernorm-embedding \ --share-all-embeddings \ --share-decoder-input-output-embed \ --reset-optimizer --reset-dataloader --reset-meters \ --required-batch-size-multiple 1 \ --arch bart_large \ --criterion label_smoothed_cross_entropy \ --label-smoothing 0.1 \ --dropout 0.1 --attention-dropout 0.1 \ --weight-decay 0.01 --optimizer adam --adam-betas "(0.9, 0.999)" --adam-eps 1e-08 \ --clip-norm 0.1 \ --lr-scheduler polynomial_decay --lr $LR --total-num-update $TOTAL_NUM_UPDATES --warmup-updates $WARMUP_UPDATES \ --fp16 --update-freq $UPDATE_FREQ \ --skip-invalid-size-inputs-valid-test \ --find-unused-parameters; ``` Above is expected to run on `1` node with `8 32gb-V100`. Expected training time is about `5 hours`. Training time can be reduced with distributed training on `4` nodes and `--update-freq 1`. Use TOTAL_NUM_UPDATES=15000 UPDATE_FREQ=2 for Xsum task ### Inference for CNN-DM test data using above trained checkpoint. After training the model as mentioned in previous step, you can perform inference with checkpoints in `checkpoints/` directory using `eval_cnn.py`, for example ```bash cp data-bin/cnn_dm/dict.source.txt checkpoints/ python examples/bart/summarize.py \ --model-dir checkpoints \ --model-file checkpoint_best.pt \ --src cnn_dm/test.source \ --out cnn_dm/test.hypo ``` For XSUM, which uses beam=6, lenpen=1.0, max_len_b=60, min_len=10: ```bash cp data-bin/cnn_dm/dict.source.txt checkpoints/ python examples/bart/summarize.py \ --model-dir checkpoints \ --model-file checkpoint_best.pt \ --src cnn_dm/test.source \ --out cnn_dm/test.hypo \ --xsum-kwargs ```
COCO-LM/fairseq/examples/bart/README.summarization.md/0
{ "file_path": "COCO-LM/fairseq/examples/bart/README.summarization.md", "repo_id": "COCO-LM", "token_count": 1444 }
155
#!/usr/bin/env python3 -u # Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. """ Translate pre-processed data with a trained model. """ import numpy as np import torch from fairseq import checkpoint_utils, options, progress_bar, tasks, utils from fairseq.sequence_generator import EnsembleModel def get_avg_pool( models, sample, prefix_tokens, src_dict, remove_bpe, has_langtok=False ): model = EnsembleModel(models) # model.forward normally channels prev_output_tokens into the decoder # separately, but SequenceGenerator directly calls model.encoder encoder_input = { k: v for k, v in sample["net_input"].items() if k != "prev_output_tokens" } # compute the encoder output for each beam encoder_outs = model.forward_encoder(encoder_input) np_encoder_outs = encoder_outs[0].encoder_out.cpu().numpy().astype(np.float32) encoder_mask = 1 - encoder_outs[0].encoder_padding_mask.cpu().numpy().astype( np.float32 ) encoder_mask = np.expand_dims(encoder_mask.T, axis=2) if has_langtok: encoder_mask = encoder_mask[1:, :, :] np_encoder_outs = np_encoder_outs[1, :, :] masked_encoder_outs = encoder_mask * np_encoder_outs avg_pool = (masked_encoder_outs / encoder_mask.sum(axis=0)).sum(axis=0) return avg_pool def main(args): assert args.path is not None, "--path required for generation!" assert ( not args.sampling or args.nbest == args.beam ), "--sampling requires --nbest to be equal to --beam" assert ( args.replace_unk is None or args.raw_text ), "--replace-unk requires a raw text dataset (--raw-text)" args.beam = 1 utils.import_user_module(args) if args.max_tokens is None: args.max_tokens = 12000 print(args) use_cuda = torch.cuda.is_available() and not args.cpu # Load dataset splits task = tasks.setup_task(args) task.load_dataset(args.gen_subset) # Set dictionaries try: src_dict = getattr(task, "source_dictionary", None) except NotImplementedError: src_dict = None tgt_dict = task.target_dictionary # Load ensemble print("| loading model(s) from {}".format(args.path)) models, _model_args = checkpoint_utils.load_model_ensemble( args.path.split(":"), arg_overrides=eval(args.model_overrides), task=task, ) # Optimize ensemble for generation for model in models: model.make_generation_fast_( beamable_mm_beam_size=None if args.no_beamable_mm else args.beam, need_attn=args.print_alignment, ) if args.fp16: model.half() if use_cuda: model.cuda() # Load alignment dictionary for unknown word replacement # (None if no unknown word replacement, empty if no path to align dictionary) align_dict = utils.load_align_dict(args.replace_unk) # Load dataset (possibly sharded) itr = task.get_batch_iterator( dataset=task.dataset(args.gen_subset), max_tokens=args.max_tokens, max_positions=utils.resolve_max_positions( task.max_positions(), ), ignore_invalid_inputs=args.skip_invalid_size_inputs_valid_test, required_batch_size_multiple=args.required_batch_size_multiple, num_shards=args.num_shards, shard_id=args.shard_id, num_workers=args.num_workers, ).next_epoch_itr(shuffle=False) num_sentences = 0 source_sentences = [] shard_id = 0 all_avg_pool = None encoder_has_langtok = ( hasattr(task.args, "encoder_langtok") and task.args.encoder_langtok is not None and hasattr(task.args, "lang_tok_replacing_bos_eos") and not task.args.lang_tok_replacing_bos_eos ) with progress_bar.build_progress_bar(args, itr) as t: for sample in t: if sample is None: print("Skipping None") continue sample = utils.move_to_cuda(sample) if use_cuda else sample if "net_input" not in sample: continue prefix_tokens = None if args.prefix_size > 0: prefix_tokens = sample["target"][:, : args.prefix_size] with torch.no_grad(): avg_pool = get_avg_pool( models, sample, prefix_tokens, src_dict, args.post_process, has_langtok=encoder_has_langtok, ) if all_avg_pool is not None: all_avg_pool = np.concatenate((all_avg_pool, avg_pool)) else: all_avg_pool = avg_pool if not isinstance(sample["id"], list): sample_ids = sample["id"].tolist() else: sample_ids = sample["id"] for i, sample_id in enumerate(sample_ids): # Remove padding src_tokens = utils.strip_pad( sample["net_input"]["src_tokens"][i, :], tgt_dict.pad() ) # Either retrieve the original sentences or regenerate them from tokens. if align_dict is not None: src_str = task.dataset(args.gen_subset).src.get_original_text( sample_id ) else: if src_dict is not None: src_str = src_dict.string(src_tokens, args.post_process) else: src_str = "" if not args.quiet: if src_dict is not None: print("S-{}\t{}".format(sample_id, src_str)) source_sentences.append(f"{sample_id}\t{src_str}") num_sentences += sample["nsentences"] if all_avg_pool.shape[0] >= 1000000: with open( f"{args.encoder_save_dir}/all_avg_pool.{args.source_lang}.{shard_id}", "w", ) as avg_pool_file: all_avg_pool.tofile(avg_pool_file) with open( f"{args.encoder_save_dir}/sentences.{args.source_lang}.{shard_id}", "w", ) as sentence_file: sentence_file.writelines(f"{line}\n" for line in source_sentences) all_avg_pool = None source_sentences = [] shard_id += 1 if all_avg_pool is not None: with open( f"{args.encoder_save_dir}/all_avg_pool.{args.source_lang}.{shard_id}", "w" ) as avg_pool_file: all_avg_pool.tofile(avg_pool_file) with open( f"{args.encoder_save_dir}/sentences.{args.source_lang}.{shard_id}", "w" ) as sentence_file: sentence_file.writelines(f"{line}\n" for line in source_sentences) return None def cli_main(): parser = options.get_generation_parser() parser.add_argument( "--encoder-save-dir", default="", type=str, metavar="N", help="directory to save encoder outputs", ) args = options.parse_args_and_arch(parser) main(args) if __name__ == "__main__": cli_main()
COCO-LM/fairseq/examples/criss/save_encoder.py/0
{ "file_path": "COCO-LM/fairseq/examples/criss/save_encoder.py", "repo_id": "COCO-LM", "token_count": 3651 }
156
# Neural Language Modeling ## Pre-trained models Model | Description | Dataset | Download ---|---|---|--- `transformer_lm.gbw.adaptive_huge` | Adaptive Inputs <br> ([Baevski and Auli, 2018](https://arxiv.org/abs/1809.10853)) <br> 1026M params | [Google Billion Words](https://github.com/ciprian-chelba/1-billion-word-language-modeling-benchmark) | [download (.tar.bz2)](https://dl.fbaipublicfiles.com/fairseq/models/lm/adaptive_lm_gbw_huge.tar.bz2) `transformer_lm.wiki103.adaptive` | Adaptive Inputs <br> ([Baevski and Auli, 2018](https://arxiv.org/abs/1809.10853)) <br> 247M params | [WikiText-103](https://blog.einstein.ai/the-wikitext-long-term-dependency-language-modeling-dataset) | [download (.tar.bz2)](https://dl.fbaipublicfiles.com/fairseq/models/lm/adaptive_lm_wiki103.v2.tar.bz2) `transformer_lm.wmt19.en` | English LM <br> ([Ng et al., 2019](https://arxiv.org/abs/1907.06616)) | [WMT News Crawl](http://data.statmt.org/news-crawl/) | [download (.tar.gz)](https://dl.fbaipublicfiles.com/fairseq/models/lm/wmt19.en.tar.gz) `transformer_lm.wmt19.de` | German LM <br> ([Ng et al., 2019](https://arxiv.org/abs/1907.06616)) | [WMT News Crawl](http://data.statmt.org/news-crawl/) | [download (.tar.gz)](https://dl.fbaipublicfiles.com/fairseq/models/lm/wmt19.de.tar.gz) `transformer_lm.wmt19.ru` | Russian LM <br> ([Ng et al., 2019](https://arxiv.org/abs/1907.06616)) | [WMT News Crawl](http://data.statmt.org/news-crawl/) | [download (.tar.gz)](https://dl.fbaipublicfiles.com/fairseq/models/lm/wmt19.ru.tar.gz) ## Example usage We require a few additional Python dependencies for preprocessing: ```bash pip install fastBPE sacremoses ``` To sample from a language model using PyTorch Hub: ```python import torch # List available models torch.hub.list('pytorch/fairseq') # [..., 'transformer_lm.wmt19.en', ...] # Load an English LM trained on WMT'19 News Crawl data en_lm = torch.hub.load('pytorch/fairseq', 'transformer_lm.wmt19.en', tokenizer='moses', bpe='fastbpe') en_lm.eval() # disable dropout # Move model to GPU en_lm.cuda() # Sample from the language model en_lm.sample('Barack Obama', beam=1, sampling=True, sampling_topk=10, temperature=0.8) # "Barack Obama is coming to Sydney and New Zealand (...)" # Compute perplexity for a sequence en_lm.score('Barack Obama is coming to Sydney and New Zealand')['positional_scores'].mean().neg().exp() # tensor(15.1474) # The same interface can be used with custom models as well from fairseq.models.transformer_lm import TransformerLanguageModel custom_lm = TransformerLanguageModel.from_pretrained('/path/to/model/dir', 'checkpoint100.pt', tokenizer='moses', bpe='fastbpe') custom_lm.sample('Barack Obama', beam=5) # "Barack Obama (...)" ``` ## Training a transformer language model with the CLI tools ### 1) Preprocess the data First download and prepare the [WikiText-103 dataset](https://www.salesforce.com/products/einstein/ai-research/the-wikitext-dependency-language-modeling-dataset/): ```bash cd examples/language_model/ bash prepare-wikitext-103.sh cd ../.. ``` Next preprocess/binarize the data: ```bash TEXT=examples/language_model/wikitext-103 fairseq-preprocess \ --only-source \ --trainpref $TEXT/wiki.train.tokens \ --validpref $TEXT/wiki.valid.tokens \ --testpref $TEXT/wiki.test.tokens \ --destdir data-bin/wikitext-103 \ --workers 20 ``` ### 2) Train a language model Next we'll train a basic transformer language model on wikitext-103. For more advanced usage, see the [adaptive inputs README](README.adaptive_inputs.md). To train a basic LM (assumes 2 GPUs): ``` $ fairseq-train --task language_modeling \ data-bin/wikitext-103 \ --save-dir checkpoints/transformer_wikitext-103 \ --arch transformer_lm --share-decoder-input-output-embed \ --dropout 0.1 \ --optimizer adam --adam-betas '(0.9, 0.98)' --weight-decay 0.01 --clip-norm 0.0 \ --lr 0.0005 --lr-scheduler inverse_sqrt --warmup-updates 4000 --warmup-init-lr 1e-07 \ --tokens-per-sample 512 --sample-break-mode none \ --max-tokens 2048 --update-freq 16 \ --fp16 \ --max-update 50000 ``` If you run out of memory, try reducing `--max-tokens` (max number of tokens per batch) or `--tokens-per-sample` (max sequence length). You can also adjust `--update-freq` to accumulate gradients and simulate training on a different number of GPUs. ### 3) Evaluate ```bash fairseq-eval-lm data-bin/wikitext-103 \ --path checkpoints/transformer_wiki103/checkpoint_best.pt \ --batch-size 2 \ --tokens-per-sample 512 \ --context-window 400 # | Evaluated 245569 tokens in 56.1s (4379.02 tokens/s) # | Loss: 3.4164, Perplexity: 30.46 ``` *Note:* The `--context-window` option controls how much context is provided to each token when computing perplexity. When the window size is 0, the dataset is chunked into segments of length 512 and perplexity is computed over each segment normally. However, this results in worse (higher) perplexity since tokens that appear earlier in each segment have less conditioning. When the maximum window size is used (511 in this case), then we compute perplexity for each token fully conditioned on 511 tokens of context. This slows down evaluation significantly, since we must run a separate forward pass for every token in the dataset, but results in better (lower) perplexity. ## Convolutional language models Please see the [convolutional LM README](README.conv.md) for instructions on training convolutional language models.
COCO-LM/fairseq/examples/language_model/README.md/0
{ "file_path": "COCO-LM/fairseq/examples/language_model/README.md", "repo_id": "COCO-LM", "token_count": 1923 }
157
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import torch import torch.nn as nn class LayerSelect(nn.Module): """Compute samples (from a Gumbel-Sigmoid distribution) which is used as either (soft) weighting or (hard) selection of residual connection. https://arxiv.org/abs/2009.13102 """ def __init__(self, num_layers, num_logits, soft_select=False, sampling_tau=5.): super(LayerSelect, self).__init__() self.layer_logits = torch.nn.Parameter( torch.Tensor(num_logits, num_layers), requires_grad=True, ) self.hard_select = not soft_select self.tau = sampling_tau self.detach_grad = False self.layer_samples = [None] * num_logits def sample(self, logit_idx): """To leverage the efficiency of distributed training, samples for all layers are computed at once for each logit_idx. Logits are parameters learnt independent of each other. Args: logit_idx: The index of logit parameters used for sampling. """ assert logit_idx is not None self.samples = self._gumbel_sigmoid( self.layer_logits[logit_idx, :].detach() if self.detach_grad else self.layer_logits[logit_idx, :], dim=-1, tau=self.tau, hard=self.hard_select, ) self.layer_samples[logit_idx] = self.samples def forward(self, i): sample = self.samples[i] return sample def _gumbel_sigmoid( self, logits, tau=1, hard=False, eps=1e-10, dim=-1, threshold=0.5 ): # ~Gumbel(0,1) gumbels1 = ( -torch.empty_like(logits, memory_format=torch.legacy_contiguous_format) .exponential_() .log() ) gumbels2 = ( -torch.empty_like(logits, memory_format=torch.legacy_contiguous_format) .exponential_() .log() ) # Difference of two gumbels because we apply a sigmoid gumbels1 = (logits + gumbels1 - gumbels2) / tau y_soft = gumbels1.sigmoid() if hard: # Straight through. y_hard = torch.zeros_like( logits, memory_format=torch.legacy_contiguous_format ).masked_fill(y_soft > threshold, 1.0) ret = y_hard - y_soft.detach() + y_soft else: # Reparametrization trick. ret = y_soft return ret
COCO-LM/fairseq/examples/latent_depth/latent_depth_src/modules/latent_layers.py/0
{ "file_path": "COCO-LM/fairseq/examples/latent_depth/latent_depth_src/modules/latent_layers.py", "repo_id": "COCO-LM", "token_count": 1234 }
158
#!/usr/bin/env bash # Copyright (c) 2019-present, Facebook, Inc. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # set -e TOKENIZERS_SCRIPTS=tokenizers INSTALL_PATH=$TOKENIZERS_SCRIPTS/thirdparty N_THREADS=8 lg=$1 MOSES=$INSTALL_PATH/mosesdecoder REPLACE_UNICODE_PUNCT=$MOSES/scripts/tokenizer/replace-unicode-punctuation.perl NORM_PUNC=$MOSES/scripts/tokenizer/normalize-punctuation.perl REM_NON_PRINT_CHAR=$MOSES/scripts/tokenizer/remove-non-printing-char.perl TOKENIZER=$MOSES/scripts/tokenizer/tokenizer.perl # special tokenization for Romanian WMT16_SCRIPTS=$INSTALL_PATH/wmt16-scripts NORMALIZE_ROMANIAN=$WMT16_SCRIPTS/preprocess/normalise-romanian.py REMOVE_DIACRITICS=$WMT16_SCRIPTS/preprocess/remove-diacritics.py # Burmese MY_SEGMENT=$INSTALL_PATH/seg_my.py # Arabic AR_TOKENIZER=$TOKENIZERS_SCRIPTS/tokenizer_ar.sh # Korean KO_SEGMENT=$TOKENIZERS_SCRIPTS/seg_ko.sh # Japanese JA_SEGMENT=$TOKENIZERS_SCRIPTS/seg_ja.sh # Indic IN_TOKENIZER=$TOKENIZERS_SCRIPTS/tokenize_indic.py INDIC_RESOURCES_PATH=$INSTALL_PATH/indic_nlp_resources # Thai THAI_TOKENIZER=$TOKENIZERS_SCRIPTS/tokenize_thai.py # Chinese CHINESE_TOKENIZER=$TOKENIZERS_SCRIPTS/tokenize_zh.py # Chinese if [ "$lg" = "zh" ]; then cat - | $REPLACE_UNICODE_PUNCT | $NORM_PUNC -l $lg | $REM_NON_PRINT_CHAR | python $CHINESE_TOKENIZER # Thai elif [ "$lg" = "th" ]; then cat - | python $THAI_TOKENIZER # Japanese elif [ "$lg" = "ja" ]; then cat - | $REPLACE_UNICODE_PUNCT | $NORM_PUNC -l $lg | $REM_NON_PRINT_CHAR | ${JA_SEGMENT} # Korean elif [ "$lg" = "ko" ]; then cat - | $REM_NON_PRINT_CHAR | ${KO_SEGMENT} # Romanian elif [ "$lg" = "ro" ]; then cat - | $REPLACE_UNICODE_PUNCT | $NORM_PUNC -l $lg | $REM_NON_PRINT_CHAR | $NORMALIZE_ROMANIAN | $REMOVE_DIACRITICS | $TOKENIZER -no-escape -threads $N_THREADS -l $lg # Burmese elif [ "$lg" = "my" ]; then cat - | python ${MY_SEGMENT} # Arabic elif [ "$lg" = "ar" ]; then cat - | ${AR_TOKENIZER} # Indic elif [ "$lg" = "ne" ]; then cat - | python ${IN_TOKENIZER} $lg elif [ "$lg" = "si" ]; then cat - | python ${IN_TOKENIZER} $lg elif [ "$lg" = "hi" ]; then cat - | python ${IN_TOKENIZER} $lg # other languages else cat - | $REPLACE_UNICODE_PUNCT | $NORM_PUNC -l $lg | $REM_NON_PRINT_CHAR | $TOKENIZER -no-escape -threads $N_THREADS -l $lg fi
COCO-LM/fairseq/examples/m2m_100/tok.sh/0
{ "file_path": "COCO-LM/fairseq/examples/m2m_100/tok.sh", "repo_id": "COCO-LM", "token_count": 1073 }
159
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import os, sys import subprocess import re from subprocess import check_call, check_output WORKDIR_ROOT = os.environ.get('WORKDIR_ROOT', None) if WORKDIR_ROOT is None or not WORKDIR_ROOT.strip(): print('please specify your working directory root in OS environment variable WORKDIR_ROOT. Exitting..."') sys.exit(-1) BLEU_REGEX = re.compile("^BLEU\\S* = (\\S+) ") def run_eval_bleu(cmd): output = check_output(cmd, shell=True, stderr=subprocess.STDOUT).decode("utf-8").strip() print(output) bleu = -1.0 for line in output.strip().split('\n'): m = BLEU_REGEX.search(line) if m is not None: bleu = m.groups()[0] bleu = float(bleu) break return bleu def check_data_test_bleu(raw_folder, data_lang_pairs): not_matchings = [] for sacrebleu_set, src_tgts in data_lang_pairs: for src_tgt in src_tgts: print(f'checking test bleus for: {src_tgt} at {sacrebleu_set}') src, tgt = src_tgt.split('-') ssrc, stgt = src[:2], tgt[:2] if os.path.exists(f'{raw_folder}/test.{tgt}-{src}.{src}'): # reversed direction may have different test set test_src = f'{raw_folder}/test.{tgt}-{src}.{src}' else: test_src = f'{raw_folder}/test.{src}-{tgt}.{src}' cmd1 = f'cat {test_src} | sacrebleu -t "{sacrebleu_set}" -l {stgt}-{ssrc}; [ $? -eq 0 ] || echo ""' test_tgt = f'{raw_folder}/test.{src}-{tgt}.{tgt}' cmd2 = f'cat {test_tgt} | sacrebleu -t "{sacrebleu_set}" -l {ssrc}-{stgt}; [ $? -eq 0 ] || echo ""' bleu1 = run_eval_bleu(cmd1) if bleu1 != 100.0: not_matchings.append(f'{sacrebleu_set}:{src_tgt} source side not matching: {test_src}') bleu2 = run_eval_bleu(cmd2) if bleu2 != 100.0: not_matchings.append(f'{sacrebleu_set}:{src_tgt} target side not matching: {test_tgt}') return not_matchings if __name__ == "__main__": to_data_path = f'{WORKDIR_ROOT}/iwsltv2' not_matching = check_data_test_bleu( f'{to_data_path}/raw', [ ('iwslt17', ['en_XX-ar_AR', 'en_XX-ko_KR', 'ar_AR-en_XX', 'ko_KR-en_XX']), ('iwslt17', ['en_XX-it_IT', 'en_XX-nl_XX', 'it_IT-en_XX', 'nl_XX-en_XX']), ('iwslt17/tst2015', ['en_XX-vi_VN', "vi_VN-en_XX"]), ] ) if len(not_matching) > 0: print('the following datasets do not have matching test datasets:\n\t', '\n\t'.join(not_matching))
COCO-LM/fairseq/examples/multilingual/data_scripts/check_iswlt_test_data.py/0
{ "file_path": "COCO-LM/fairseq/examples/multilingual/data_scripts/check_iswlt_test_data.py", "repo_id": "COCO-LM", "token_count": 1389 }
160
# Non-autoregressive Neural Machine Translation (NAT) This page mainly includes instructions for reproducing results from the following papers * [Levenshtein Transformer (Gu et al., 2019)](https://arxiv.org/abs/1905.11006). * [Understanding Knowledge Distillation in Non-autoregressive Machine Translation (Zhou et al., 2019)](https://arxiv.org/abs/1911.02727). We also provided our own implementations for several popular non-autoregressive-based models as reference:<br> * [Non-Autoregressive Neural Machine Translation (Gu et al., 2017)](https://arxiv.org/abs/1711.02281)<br> * [Deterministic Non-Autoregressive Neural Sequence Modeling by Iterative Refinement (Lee et al., 2018)](https://arxiv.org/abs/1802.06901)<br> * [Insertion Transformer: Flexible Sequence Generation via Insertion Operations (Stern et al., 2019)](https://arxiv.org/abs/1902.03249)<br> * [Mask-Predict: Parallel Decoding of Conditional Masked Language Models (Ghazvininejad et al., 2019)](https://arxiv.org/abs/1904.09324v2)<br> * [Fast Structured Decoding for Sequence Models (Sun et al., 2019)](https://arxiv.org/abs/1910.11555) ## Dataset First, follow the [instructions to download and preprocess the WMT'14 En-De dataset](../translation#wmt14-english-to-german-convolutional). Make sure to learn a joint vocabulary by passing the `--joined-dictionary` option to `fairseq-preprocess`. ### Knowledge Distillation Following [Gu et al. 2019](https://arxiv.org/abs/1905.11006), [knowledge distillation](https://arxiv.org/abs/1606.07947) from an autoregressive model can effectively simplify the training data distribution, which is sometimes essential for NAT-based models to learn good translations. The easiest way of performing distillation is to follow the [instructions of training a standard transformer model](../translation) on the same data, and then decode the training set to produce a distillation dataset for NAT. ### Download We also provided the preprocessed [original](http://dl.fbaipublicfiles.com/nat/original_dataset.zip) and [distillation](http://dl.fbaipublicfiles.com/nat/distill_dataset.zip) datasets. Please build the binarized dataset on your own. ## Train a model Then we can train a nonautoregressive model using the `translation_lev` task and a new criterion `nat_loss`. Use the `--noise` flag to specify the input noise used on the target sentences. In default, we run the task for *Levenshtein Transformer*, with `--noise='random_delete'`. Full scripts to run other models can also be found [here](./scripts.md). The following command will train a *Levenshtein Transformer* on the binarized dataset. ```bash fairseq-train \ data-bin/wmt14_en_de_distill \ --save-dir checkpoints \ --ddp-backend=legacy_ddp \ --task translation_lev \ --criterion nat_loss \ --arch levenshtein_transformer \ --noise random_delete \ --share-all-embeddings \ --optimizer adam --adam-betas '(0.9,0.98)' \ --lr 0.0005 --lr-scheduler inverse_sqrt \ --stop-min-lr '1e-09' --warmup-updates 10000 \ --warmup-init-lr '1e-07' --label-smoothing 0.1 \ --dropout 0.3 --weight-decay 0.01 \ --decoder-learned-pos \ --encoder-learned-pos \ --apply-bert-init \ --log-format 'simple' --log-interval 100 \ --fixed-validation-seed 7 \ --max-tokens 8000 \ --save-interval-updates 10000 \ --max-update 300000 ``` ## Translate Once a model is trained, we can generate translations using an `iterative_refinement_generator` which will based on the model's initial output and iteratively read and greedily refine the translation until (1) the model predicts the same translations for two consecutive iterations; or (2) the generator reaches the maximum iterations (`--iter-decode-max-iter`). Use `--print-step` to check the actual # of iteration for each sentence. For *Levenshtein Transformer*, it sometimes helps to apply a `--iter-decode-eos-penalty` (typically, 0~3) to penalize the model finishing generation too early and generating too short translations. For example, to generate with `--iter-decode-max-iter=9`: ```bash fairseq-generate \ data-bin/wmt14_en_de_distill \ --gen-subset test \ --task translation_lev \ --path checkpoints/checkpoint_best.pt \ --iter-decode-max-iter 9 \ --iter-decode-eos-penalty 0 \ --beam 1 --remove-bpe \ --print-step \ --batch-size 400 ``` In the end of the generation, we can see the tokenized BLEU score for the translation. ## Advanced Decoding Methods ### Ensemble The NAT models use special implementations of [ensembling](https://github.com/fairinternal/fairseq-py/blob/b98d88da52f2f21f1b169bab8c70c1c4ca19a768/fairseq/sequence_generator.py#L522) to support iterative refinement and a variety of parallel operations in different models, while it shares the same API as standard autoregressive models as follows: ```bash fairseq-generate \ data-bin/wmt14_en_de_distill \ --gen-subset test \ --task translation_lev \ --path checkpoint_1.pt:checkpoint_2.pt:checkpoint_3.pt \ --iter-decode-max-iter 9 \ --iter-decode-eos-penalty 0 \ --beam 1 --remove-bpe \ --print-step \ --batch-size 400 ``` We use ``:`` to split multiple models. Note that, not all NAT models support ensembling for now. ### Length-beam For models that predict lengths before decoding (e.g. the vanilla NAT, Mask-Predict, etc), it is possible to improve the translation quality by varying the target lengths around the predicted value, and translating the same example multiple times in parallel. We can select the best translation with the highest scores defined by your model's output. Note that, not all models support length beams. For models which dynamically change the lengths (e.g. *Insertion Transformer*, *Levenshtein Transformer*), the same trick does not apply. ### Re-ranking If the model generates multiple translations with length beam, we can also introduce an autoregressive model to rerank the translations considering scoring from an autoregressive model is much faster than decoding from that. For example, to generate translations with length beam and reranking, ```bash fairseq-generate \ data-bin/wmt14_en_de_distill \ --gen-subset test \ --task translation_lev \ --path checkpoints/checkpoint_best.pt:at_checkpoints/checkpoint_best.pt \ --iter-decode-max-iter 9 \ --iter-decode-eos-penalty 0 \ --iter-decode-with-beam 9 \ --iter-decode-with-external-reranker \ --beam 1 --remove-bpe \ --print-step \ --batch-size 100 ``` Note that we need to make sure the autoregressive model shares the same vocabulary as our target non-autoregressive model. ## Citation ```bibtex @incollection{NIPS2019_9297, title = {Levenshtein Transformer}, author = {Gu, Jiatao and Wang, Changhan and Zhao, Junbo}, booktitle = {Advances in Neural Information Processing Systems 32}, editor = {H. Wallach and H. Larochelle and A. Beygelzimer and F. d\textquotesingle Alch\'{e}-Buc and E. Fox and R. Garnett}, pages = {11179--11189}, year = {2019}, publisher = {Curran Associates, Inc.}, url = {http://papers.nips.cc/paper/9297-levenshtein-transformer.pdf} } ``` ```bibtex @article{zhou2019understanding, title={Understanding Knowledge Distillation in Non-autoregressive Machine Translation}, author={Zhou, Chunting and Neubig, Graham and Gu, Jiatao}, journal={arXiv preprint arXiv:1911.02727}, year={2019} } ```
COCO-LM/fairseq/examples/nonautoregressive_translation/README.md/0
{ "file_path": "COCO-LM/fairseq/examples/nonautoregressive_translation/README.md", "repo_id": "COCO-LM", "token_count": 2373 }
161
# Pretraining RoBERTa using your own data This tutorial will walk you through pretraining RoBERTa over your own data. ### 1) Preprocess the data Data should be preprocessed following the [language modeling format](/examples/language_model), i.e. each document should be separated by an empty line (only useful with `--sample-break-mode complete_doc`). Lines will be concatenated as a 1D text stream during training. We'll use the [WikiText-103 dataset](https://www.salesforce.com/products/einstein/ai-research/the-wikitext-dependency-language-modeling-dataset/) to demonstrate how to preprocess raw text data with the GPT-2 BPE. Of course this dataset is quite small, so the resulting pretrained model will perform poorly, but it gives the general idea. First download the dataset: ```bash wget https://s3.amazonaws.com/research.metamind.io/wikitext/wikitext-103-raw-v1.zip unzip wikitext-103-raw-v1.zip ``` Next encode it with the GPT-2 BPE: ```bash mkdir -p gpt2_bpe wget -O gpt2_bpe/encoder.json https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/encoder.json wget -O gpt2_bpe/vocab.bpe https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/vocab.bpe for SPLIT in train valid test; do \ python -m examples.roberta.multiprocessing_bpe_encoder \ --encoder-json gpt2_bpe/encoder.json \ --vocab-bpe gpt2_bpe/vocab.bpe \ --inputs wikitext-103-raw/wiki.${SPLIT}.raw \ --outputs wikitext-103-raw/wiki.${SPLIT}.bpe \ --keep-empty \ --workers 60; \ done ``` Finally preprocess/binarize the data using the GPT-2 fairseq dictionary: ```bash wget -O gpt2_bpe/dict.txt https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/dict.txt fairseq-preprocess \ --only-source \ --srcdict gpt2_bpe/dict.txt \ --trainpref wikitext-103-raw/wiki.train.bpe \ --validpref wikitext-103-raw/wiki.valid.bpe \ --testpref wikitext-103-raw/wiki.test.bpe \ --destdir data-bin/wikitext-103 \ --workers 60 ``` ### 2) Train RoBERTa base ```bash TOTAL_UPDATES=125000 # Total number of training steps WARMUP_UPDATES=10000 # Warmup the learning rate over this many updates PEAK_LR=0.0005 # Peak learning rate, adjust as needed TOKENS_PER_SAMPLE=512 # Max sequence length MAX_POSITIONS=512 # Num. positional embeddings (usually same as above) MAX_SENTENCES=16 # Number of sequences per batch (batch size) UPDATE_FREQ=16 # Increase the batch size 16x DATA_DIR=data-bin/wikitext-103 fairseq-train --fp16 $DATA_DIR \ --task masked_lm --criterion masked_lm \ --arch roberta_base --sample-break-mode complete --tokens-per-sample $TOKENS_PER_SAMPLE \ --optimizer adam --adam-betas '(0.9,0.98)' --adam-eps 1e-6 --clip-norm 0.0 \ --lr-scheduler polynomial_decay --lr $PEAK_LR --warmup-updates $WARMUP_UPDATES --total-num-update $TOTAL_UPDATES \ --dropout 0.1 --attention-dropout 0.1 --weight-decay 0.01 \ --batch-size $MAX_SENTENCES --update-freq $UPDATE_FREQ \ --max-update $TOTAL_UPDATES --log-format simple --log-interval 1 ``` **Note:** You can optionally resume training the released RoBERTa base model by adding `--restore-file /path/to/roberta.base/model.pt`. **Note:** The above command assumes training on 8x32GB V100 GPUs. Each GPU uses a batch size of 16 sequences (`$MAX_SENTENCES`) and accumulates gradients to further increase the batch size by 16x (`$UPDATE_FREQ`), for a total batch size of 2048 sequences. If you have fewer GPUs or GPUs with less memory you may need to reduce `$MAX_SENTENCES` and increase `$UPDATE_FREQ` to compensate. Alternatively if you have more GPUs you can decrease `$UPDATE_FREQ` accordingly to increase training speed. **Note:** The learning rate and batch size are tightly connected and need to be adjusted together. We generally recommend increasing the learning rate as you increase the batch size according to the following table (although it's also dataset dependent, so don't rely on the following values too closely): batch size | peak learning rate ---|--- 256 | 0.0001 2048 | 0.0005 8192 | 0.0007 ### 3) Load your pretrained model ```python from fairseq.models.roberta import RobertaModel roberta = RobertaModel.from_pretrained('checkpoints', 'checkpoint_best.pt', 'path/to/data') assert isinstance(roberta.model, torch.nn.Module) ```
COCO-LM/fairseq/examples/roberta/README.pretraining.md/0
{ "file_path": "COCO-LM/fairseq/examples/roberta/README.pretraining.md", "repo_id": "COCO-LM", "token_count": 1550 }
162
from . import criterions, models, tasks # noqa
COCO-LM/fairseq/examples/speech_recognition/__init__.py/0
{ "file_path": "COCO-LM/fairseq/examples/speech_recognition/__init__.py", "repo_id": "COCO-LM", "token_count": 15 }
163
#!/usr/bin/env python3 -u # Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. """ Run inference for pre-processed data with a trained model. """ import ast import logging import math import os import sys import editdistance import numpy as np import torch from fairseq import checkpoint_utils, options, progress_bar, tasks, utils from fairseq.data.data_utils import post_process from fairseq.logging.meters import StopwatchMeter, TimeMeter logging.basicConfig() logging.root.setLevel(logging.INFO) logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def add_asr_eval_argument(parser): parser.add_argument("--kspmodel", default=None, help="sentence piece model") parser.add_argument( "--wfstlm", default=None, help="wfstlm on dictonary output units" ) parser.add_argument( "--rnnt_decoding_type", default="greedy", help="wfstlm on dictonary\ output units", ) try: parser.add_argument( "--lm-weight", "--lm_weight", type=float, default=0.2, help="weight for lm while interpolating with neural score", ) except: pass parser.add_argument( "--rnnt_len_penalty", default=-0.5, help="rnnt length penalty on word level" ) parser.add_argument( "--w2l-decoder", choices=["viterbi", "kenlm", "fairseqlm"], help="use a w2l decoder", ) parser.add_argument("--lexicon", help="lexicon for w2l decoder") parser.add_argument("--unit-lm", action="store_true", help="if using a unit lm") parser.add_argument("--kenlm-model", "--lm-model", help="lm model for w2l decoder") parser.add_argument("--beam-threshold", type=float, default=25.0) parser.add_argument("--beam-size-token", type=float, default=100) parser.add_argument("--word-score", type=float, default=1.0) parser.add_argument("--unk-weight", type=float, default=-math.inf) parser.add_argument("--sil-weight", type=float, default=0.0) parser.add_argument( "--dump-emissions", type=str, default=None, help="if present, dumps emissions into this file and exits", ) parser.add_argument( "--dump-features", type=str, default=None, help="if present, dumps features into this file and exits", ) parser.add_argument( "--load-emissions", type=str, default=None, help="if present, loads emissions from this file", ) return parser def check_args(args): # assert args.path is not None, "--path required for generation!" # assert args.results_path is not None, "--results_path required for generation!" assert ( not args.sampling or args.nbest == args.beam ), "--sampling requires --nbest to be equal to --beam" assert ( args.replace_unk is None or args.raw_text ), "--replace-unk requires a raw text dataset (--raw-text)" def get_dataset_itr(args, task, models): return task.get_batch_iterator( dataset=task.dataset(args.gen_subset), max_tokens=args.max_tokens, max_sentences=args.batch_size, max_positions=(sys.maxsize, sys.maxsize), ignore_invalid_inputs=args.skip_invalid_size_inputs_valid_test, required_batch_size_multiple=args.required_batch_size_multiple, num_shards=args.num_shards, shard_id=args.shard_id, num_workers=args.num_workers, data_buffer_size=args.data_buffer_size, ).next_epoch_itr(shuffle=False) def process_predictions( args, hypos, sp, tgt_dict, target_tokens, res_files, speaker, id ): for hypo in hypos[: min(len(hypos), args.nbest)]: hyp_pieces = tgt_dict.string(hypo["tokens"].int().cpu()) if "words" in hypo: hyp_words = " ".join(hypo["words"]) else: hyp_words = post_process(hyp_pieces, args.post_process) if res_files is not None: print( "{} ({}-{})".format(hyp_pieces, speaker, id), file=res_files["hypo.units"], ) print( "{} ({}-{})".format(hyp_words, speaker, id), file=res_files["hypo.words"], ) tgt_pieces = tgt_dict.string(target_tokens) tgt_words = post_process(tgt_pieces, args.post_process) if res_files is not None: print( "{} ({}-{})".format(tgt_pieces, speaker, id), file=res_files["ref.units"], ) print( "{} ({}-{})".format(tgt_words, speaker, id), file=res_files["ref.words"] ) if not args.quiet: logger.info("HYPO:" + hyp_words) logger.info("TARGET:" + tgt_words) logger.info("___________________") hyp_words = hyp_words.split() tgt_words = tgt_words.split() return editdistance.eval(hyp_words, tgt_words), len(tgt_words) def prepare_result_files(args): def get_res_file(file_prefix): if args.num_shards > 1: file_prefix = f"{args.shard_id}_{file_prefix}" path = os.path.join( args.results_path, "{}-{}-{}.txt".format( file_prefix, os.path.basename(args.path), args.gen_subset ), ) return open(path, "w", buffering=1) if not args.results_path: return None return { "hypo.words": get_res_file("hypo.word"), "hypo.units": get_res_file("hypo.units"), "ref.words": get_res_file("ref.word"), "ref.units": get_res_file("ref.units"), } def optimize_models(args, use_cuda, models): """Optimize ensemble for generation""" for model in models: model.make_generation_fast_( beamable_mm_beam_size=None if args.no_beamable_mm else args.beam, need_attn=args.print_alignment, ) if args.fp16: model.half() if use_cuda: model.cuda() class ExistingEmissionsDecoder(object): def __init__(self, decoder, emissions): self.decoder = decoder self.emissions = emissions def generate(self, models, sample, **unused): ids = sample["id"].cpu().numpy() try: emissions = np.stack(self.emissions[ids]) except: print([x.shape for x in self.emissions[ids]]) raise Exception("invalid sizes") emissions = torch.from_numpy(emissions) return self.decoder.decode(emissions) def main(args, task=None, model_state=None): check_args(args) if args.max_tokens is None and args.batch_size is None: args.max_tokens = 4000000 logger.info(args) use_cuda = torch.cuda.is_available() and not args.cpu logger.info("| decoding with criterion {}".format(args.criterion)) task = tasks.setup_task(args) # Load ensemble if args.load_emissions: models, criterions = [], [] task.load_dataset(args.gen_subset) else: logger.info("| loading model(s) from {}".format(args.path)) models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task( utils.split_paths(args.path), arg_overrides=ast.literal_eval(args.model_overrides), task=task, suffix=args.checkpoint_suffix, strict=(args.checkpoint_shard_count == 1), num_shards=args.checkpoint_shard_count, state=model_state, ) optimize_models(args, use_cuda, models) task.load_dataset(args.gen_subset, task_cfg=saved_cfg.task) # Set dictionary tgt_dict = task.target_dictionary logger.info( "| {} {} {} examples".format( args.data, args.gen_subset, len(task.dataset(args.gen_subset)) ) ) # hack to pass transitions to W2lDecoder if args.criterion == "asg_loss": raise NotImplementedError("asg_loss is currently not supported") # trans = criterions[0].asg.trans.data # args.asg_transitions = torch.flatten(trans).tolist() # Load dataset (possibly sharded) itr = get_dataset_itr(args, task, models) # Initialize generator gen_timer = StopwatchMeter() def build_generator(args): w2l_decoder = getattr(args, "w2l_decoder", None) if w2l_decoder == "viterbi": from examples.speech_recognition.w2l_decoder import W2lViterbiDecoder return W2lViterbiDecoder(args, task.target_dictionary) elif w2l_decoder == "kenlm": from examples.speech_recognition.w2l_decoder import W2lKenLMDecoder return W2lKenLMDecoder(args, task.target_dictionary) elif w2l_decoder == "fairseqlm": from examples.speech_recognition.w2l_decoder import W2lFairseqLMDecoder return W2lFairseqLMDecoder(args, task.target_dictionary) else: print( "only flashlight decoders with (viterbi, kenlm, fairseqlm) options are supported at the moment" ) # please do not touch this unless you test both generate.py and infer.py with audio_pretraining task generator = build_generator(args) if args.load_emissions: generator = ExistingEmissionsDecoder( generator, np.load(args.load_emissions, allow_pickle=True) ) logger.info("loaded emissions from " + args.load_emissions) num_sentences = 0 if args.results_path is not None and not os.path.exists(args.results_path): os.makedirs(args.results_path) max_source_pos = ( utils.resolve_max_positions( task.max_positions(), *[model.max_positions() for model in models] ), ) if max_source_pos is not None: max_source_pos = max_source_pos[0] if max_source_pos is not None: max_source_pos = max_source_pos[0] - 1 if args.dump_emissions: emissions = {} if args.dump_features: features = {} models[0].bert.proj = None else: res_files = prepare_result_files(args) errs_t = 0 lengths_t = 0 with progress_bar.build_progress_bar(args, itr) as t: wps_meter = TimeMeter() for sample in t: sample = utils.move_to_cuda(sample) if use_cuda else sample if "net_input" not in sample: continue prefix_tokens = None if args.prefix_size > 0: prefix_tokens = sample["target"][:, : args.prefix_size] gen_timer.start() if args.dump_emissions: with torch.no_grad(): encoder_out = models[0](**sample["net_input"]) emm = models[0].get_normalized_probs(encoder_out, log_probs=True) emm = emm.transpose(0, 1).cpu().numpy() for i, id in enumerate(sample["id"]): emissions[id.item()] = emm[i] continue elif args.dump_features: with torch.no_grad(): encoder_out = models[0](**sample["net_input"]) feat = encoder_out["encoder_out"].transpose(0, 1).cpu().numpy() for i, id in enumerate(sample["id"]): padding = ( encoder_out["encoder_padding_mask"][i].cpu().numpy() if encoder_out["encoder_padding_mask"] is not None else None ) features[id.item()] = (feat[i], padding) continue hypos = task.inference_step(generator, models, sample, prefix_tokens) num_generated_tokens = sum(len(h[0]["tokens"]) for h in hypos) gen_timer.stop(num_generated_tokens) for i, sample_id in enumerate(sample["id"].tolist()): speaker = None # id = task.dataset(args.gen_subset).ids[int(sample_id)] id = sample_id toks = ( sample["target"][i, :] if "target_label" not in sample else sample["target_label"][i, :] ) target_tokens = utils.strip_pad(toks, tgt_dict.pad()).int().cpu() # Process top predictions errs, length = process_predictions( args, hypos[i], None, tgt_dict, target_tokens, res_files, speaker, id, ) errs_t += errs lengths_t += length wps_meter.update(num_generated_tokens) t.log({"wps": round(wps_meter.avg)}) num_sentences += ( sample["nsentences"] if "nsentences" in sample else sample["id"].numel() ) wer = None if args.dump_emissions: emm_arr = [] for i in range(len(emissions)): emm_arr.append(emissions[i]) np.save(args.dump_emissions, emm_arr) logger.info(f"saved {len(emissions)} emissions to {args.dump_emissions}") elif args.dump_features: feat_arr = [] for i in range(len(features)): feat_arr.append(features[i]) np.save(args.dump_features, feat_arr) logger.info(f"saved {len(features)} emissions to {args.dump_features}") else: if lengths_t > 0: wer = errs_t * 100.0 / lengths_t logger.info(f"WER: {wer}") logger.info( "| Processed {} sentences ({} tokens) in {:.1f}s ({:.2f}" "sentences/s, {:.2f} tokens/s)".format( num_sentences, gen_timer.n, gen_timer.sum, num_sentences / gen_timer.sum, 1.0 / gen_timer.avg, ) ) logger.info("| Generate {} with beam={}".format(args.gen_subset, args.beam)) return task, wer def make_parser(): parser = options.get_generation_parser() parser = add_asr_eval_argument(parser) return parser def cli_main(): parser = make_parser() args = options.parse_args_and_arch(parser) main(args) if __name__ == "__main__": cli_main()
COCO-LM/fairseq/examples/speech_recognition/infer.py/0
{ "file_path": "COCO-LM/fairseq/examples/speech_recognition/infer.py", "repo_id": "COCO-LM", "token_count": 6964 }
164
#!/usr/bin/env python3 # Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import argparse import logging from pathlib import Path import shutil from tempfile import NamedTemporaryFile import pandas as pd from examples.speech_to_text.data_utils import ( create_zip, extract_fbank_features, gen_config_yaml, gen_vocab, get_zip_manifest, save_df_to_tsv, ) from torchaudio.datasets import LIBRISPEECH from tqdm import tqdm log = logging.getLogger(__name__) SPLITS = [ "train-clean-100", "train-clean-360", "train-other-500", "dev-clean", "dev-other", "test-clean", "test-other", ] MANIFEST_COLUMNS = ["id", "audio", "n_frames", "tgt_text", "speaker"] def process(args): out_root = Path(args.output_root).absolute() out_root.mkdir(exist_ok=True) # Extract features feature_root = out_root / "fbank80" feature_root.mkdir(exist_ok=True) for split in SPLITS: print(f"Fetching split {split}...") dataset = LIBRISPEECH(out_root.as_posix(), url=split, download=True) print("Extracting log mel filter bank features...") for wav, sample_rate, _, spk_id, chapter_no, utt_no in tqdm(dataset): sample_id = f"{spk_id}-{chapter_no}-{utt_no}" extract_fbank_features( wav, sample_rate, feature_root / f"{sample_id}.npy" ) # Pack features into ZIP zip_path = out_root / "fbank80.zip" print("ZIPing features...") create_zip(feature_root, zip_path) print("Fetching ZIP manifest...") zip_manifest = get_zip_manifest(zip_path) # Generate TSV manifest print("Generating manifest...") train_text = [] for split in SPLITS: manifest = {c: [] for c in MANIFEST_COLUMNS} dataset = LIBRISPEECH(out_root.as_posix(), url=split) for wav, sample_rate, utt, spk_id, chapter_no, utt_no in tqdm(dataset): sample_id = f"{spk_id}-{chapter_no}-{utt_no}" manifest["id"].append(sample_id) manifest["audio"].append(zip_manifest[sample_id]) duration_ms = int(wav.size(1) / sample_rate * 1000) manifest["n_frames"].append(int(1 + (duration_ms - 25) / 10)) manifest["tgt_text"].append(utt.lower()) manifest["speaker"].append(spk_id) save_df_to_tsv( pd.DataFrame.from_dict(manifest), out_root / f"{split}.tsv" ) if split.startswith("train"): train_text.extend(manifest["tgt_text"]) # Generate vocab vocab_size = "" if args.vocab_type == "char" else str(args.vocab_size) spm_filename_prefix = f"spm_{args.vocab_type}{vocab_size}" with NamedTemporaryFile(mode="w") as f: for t in train_text: f.write(t + "\n") gen_vocab( Path(f.name), out_root / spm_filename_prefix, args.vocab_type, args.vocab_size, ) # Generate config YAML gen_config_yaml( out_root, spm_filename_prefix + ".model", specaugment_policy="ld" ) # Clean up shutil.rmtree(feature_root) def main(): parser = argparse.ArgumentParser() parser.add_argument("--output-root", "-o", required=True, type=str) parser.add_argument( "--vocab-type", default="unigram", required=True, type=str, choices=["bpe", "unigram", "char"], ), parser.add_argument("--vocab-size", default=10000, type=int) args = parser.parse_args() process(args) if __name__ == "__main__": main()
COCO-LM/fairseq/examples/speech_to_text/prep_librispeech_data.py/0
{ "file_path": "COCO-LM/fairseq/examples/speech_to_text/prep_librispeech_data.py", "repo_id": "COCO-LM", "token_count": 1661 }
165
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. from dataclasses import dataclass, field import torch from omegaconf import II from fairseq import metrics, utils from fairseq.dataclass import ChoiceEnum from fairseq.tasks import register_task from fairseq.tasks.translation import TranslationConfig, TranslationTask from .logsumexp_moe import LogSumExpMoE from .mean_pool_gating_network import MeanPoolGatingNetwork METHOD_CHOICES = ChoiceEnum(["sMoElp", "sMoEup", "hMoElp", "hMoEup"]) @dataclass class TranslationMoEConfig(TranslationConfig): method: METHOD_CHOICES = field( default="hMoEup", metadata={"help": "MoE method"}, ) num_experts: int = field( default=3, metadata={"help": "number of experts"}, ) mean_pool_gating_network: bool = field( default=False, metadata={"help": "use a simple mean-pooling gating network"}, ) mean_pool_gating_network_dropout: float = field( default=0, metadata={"help": "dropout for mean-pooling gating network"}, ) mean_pool_gating_network_encoder_dim: int = field( default=0, metadata={"help": "encoder output dim for mean-pooling gating network"}, ) gen_expert: int = field( default=0, metadata={"help": "which expert to use for generation"}, ) sentence_avg: bool = II("optimization.sentence_avg") @register_task("translation_moe", dataclass=TranslationMoEConfig) class TranslationMoETask(TranslationTask): """ Translation task for Mixture of Experts (MoE) models. See `"Mixture Models for Diverse Machine Translation: Tricks of the Trade" (Shen et al., 2019) <https://arxiv.org/abs/1902.07816>`_. Args: src_dict (~fairseq.data.Dictionary): dictionary for the source language tgt_dict (~fairseq.data.Dictionary): dictionary for the target language .. note:: The translation task is compatible with :mod:`fairseq-train`, :mod:`fairseq-generate` and :mod:`fairseq-interactive`. The translation task provides the following additional command-line arguments: .. argparse:: :ref: fairseq.tasks.translation_parser :prog: """ cfg: TranslationMoEConfig def __init__(self, cfg: TranslationMoEConfig, src_dict, tgt_dict): if cfg.method == "sMoElp": # soft MoE with learned prior self.uniform_prior = False self.hard_selection = False elif cfg.method == "sMoEup": # soft MoE with uniform prior self.uniform_prior = True self.hard_selection = False elif cfg.method == "hMoElp": # hard MoE with learned prior self.uniform_prior = False self.hard_selection = True elif cfg.method == "hMoEup": # hard MoE with uniform prior self.uniform_prior = True self.hard_selection = True # add indicator tokens for each expert for i in range(cfg.num_experts): # add to both dictionaries in case we're sharing embeddings src_dict.add_symbol("<expert_{}>".format(i)) tgt_dict.add_symbol("<expert_{}>".format(i)) super().__init__(cfg, src_dict, tgt_dict) def build_model(self, cfg): from fairseq import models model = models.build_model(cfg, self) if not self.uniform_prior and not hasattr(model, "gating_network"): if self.cfg.mean_pool_gating_network: if self.cfg.mean_pool_gating_network_encoder_dim > 0: encoder_dim = self.cfg.mean_pool_gating_network_encoder_dim elif getattr(cfg, "encoder_embed_dim", None): # assume that encoder_embed_dim is the encoder's output dimension encoder_dim = cfg.encoder_embed_dim else: raise ValueError( "Must specify --mean-pool-gating-network-encoder-dim" ) if self.cfg.mean_pool_gating_network_dropout > 0: dropout = self.cfg.mean_pool_gating_network_dropout elif getattr(cfg, "dropout", None): dropout = cfg.dropout else: raise ValueError("Must specify task.mean_pool_gating_network_dropout") model.gating_network = MeanPoolGatingNetwork( encoder_dim, self.cfg.num_experts, dropout, ) else: raise ValueError( "translation_moe task with learned prior requires the model to " "have a gating network; try using --mean-pool-gating-network" ) return model def expert_index(self, i): return i + self.tgt_dict.index("<expert_0>") def _get_loss(self, sample, model, criterion): assert hasattr( criterion, "compute_loss" ), "translation_moe task requires the criterion to implement the compute_loss() method" k = self.cfg.num_experts bsz = sample["target"].size(0) def get_lprob_y(encoder_out, prev_output_tokens_k): net_output = model.decoder( prev_output_tokens=prev_output_tokens_k, encoder_out=encoder_out, ) loss, _ = criterion.compute_loss(model, net_output, sample, reduce=False) loss = loss.view(bsz, -1) return -loss.sum(dim=1, keepdim=True) # -> B x 1 def get_lprob_yz(winners=None): encoder_out = model.encoder( src_tokens=sample["net_input"]["src_tokens"], src_lengths=sample["net_input"]["src_lengths"], ) if winners is None: lprob_y = [] for i in range(k): prev_output_tokens_k = sample["net_input"][ "prev_output_tokens" ].clone() assert not prev_output_tokens_k.requires_grad prev_output_tokens_k[:, 0] = self.expert_index(i) lprob_y.append(get_lprob_y(encoder_out, prev_output_tokens_k)) lprob_y = torch.cat(lprob_y, dim=1) # -> B x K else: prev_output_tokens_k = sample["net_input"]["prev_output_tokens"].clone() prev_output_tokens_k[:, 0] = self.expert_index(winners) lprob_y = get_lprob_y(encoder_out, prev_output_tokens_k) # -> B if self.uniform_prior: lprob_yz = lprob_y else: lprob_z = model.gating_network(encoder_out) # B x K if winners is not None: lprob_z = lprob_z.gather(dim=1, index=winners.unsqueeze(-1)) lprob_yz = lprob_y + lprob_z.type_as(lprob_y) # B x K return lprob_yz # compute responsibilities without dropout with utils.model_eval(model): # disable dropout with torch.no_grad(): # disable autograd lprob_yz = get_lprob_yz() # B x K prob_z_xy = torch.nn.functional.softmax(lprob_yz, dim=1) assert not prob_z_xy.requires_grad # compute loss with dropout if self.hard_selection: winners = prob_z_xy.max(dim=1)[1] loss = -get_lprob_yz(winners) else: lprob_yz = get_lprob_yz() # B x K loss = -LogSumExpMoE.apply(lprob_yz, prob_z_xy, 1) loss = loss.sum() sample_size = ( sample["target"].size(0) if self.cfg.sentence_avg else sample["ntokens"] ) logging_output = { "loss": utils.item(loss.data), "ntokens": sample["ntokens"], "nsentences": bsz, "sample_size": sample_size, "posterior": prob_z_xy.float().sum(dim=0).cpu(), } return loss, sample_size, logging_output def train_step( self, sample, model, criterion, optimizer, update_num, ignore_grad=False ): model.train() loss, sample_size, logging_output = self._get_loss(sample, model, criterion) if ignore_grad: loss *= 0 optimizer.backward(loss) return loss, sample_size, logging_output def valid_step(self, sample, model, criterion): model.eval() with torch.no_grad(): loss, sample_size, logging_output = self._get_loss(sample, model, criterion) return loss, sample_size, logging_output def inference_step( self, generator, models, sample, prefix_tokens=None, expert=None, constraints=None, ): expert = expert or self.cfg.gen_expert with torch.no_grad(): return generator.generate( models, sample, prefix_tokens=prefix_tokens, constraints=constraints, bos_token=self.expert_index(expert), ) def reduce_metrics(self, logging_outputs, criterion): super().reduce_metrics(logging_outputs, criterion) metrics.log_scalar( "posterior", sum(log["posterior"] for log in logging_outputs if "posterior" in log), )
COCO-LM/fairseq/examples/translation_moe/translation_moe_src/translation_moe.py/0
{ "file_path": "COCO-LM/fairseq/examples/translation_moe/translation_moe_src/translation_moe.py", "repo_id": "COCO-LM", "token_count": 4609 }
166
# @package _group_ hydra: run: dir: . defaults: - task: null - model: null - criterion: cross_entropy - optimizer: null - lr_scheduler: fixed - bpe: null - tokenizer: null - scoring: null - generation: null - common_eval: null - eval_lm: null
COCO-LM/fairseq/fairseq/config/config.yaml/0
{ "file_path": "COCO-LM/fairseq/fairseq/config/config.yaml", "repo_id": "COCO-LM", "token_count": 132 }
167
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import math from dataclasses import dataclass import torch.nn.functional as F from fairseq import metrics, utils from fairseq.criterions import FairseqCriterion, register_criterion from fairseq.dataclass import FairseqDataclass from omegaconf import II @dataclass class CrossEntropyCriterionConfig(FairseqDataclass): sentence_avg: bool = II("optimization.sentence_avg") @register_criterion("cross_entropy", dataclass=CrossEntropyCriterionConfig) class CrossEntropyCriterion(FairseqCriterion): def __init__(self, task, sentence_avg): super().__init__(task) self.sentence_avg = sentence_avg def forward(self, model, sample, reduce=True): """Compute the loss for the given sample. Returns a tuple with three elements: 1) the loss 2) the sample size, which is used as the denominator for the gradient 3) logging outputs to display while training """ net_output = model(**sample["net_input"]) loss, _ = self.compute_loss(model, net_output, sample, reduce=reduce) sample_size = ( sample["target"].size(0) if self.sentence_avg else sample["ntokens"] ) logging_output = { "loss": loss.data, "ntokens": sample["ntokens"], "nsentences": sample["target"].size(0), "sample_size": sample_size, } return loss, sample_size, logging_output def compute_loss(self, model, net_output, sample, reduce=True): lprobs = model.get_normalized_probs(net_output, log_probs=True) lprobs = lprobs.view(-1, lprobs.size(-1)) target = model.get_targets(sample, net_output).view(-1) loss = F.nll_loss( lprobs, target, ignore_index=self.padding_idx, reduction="sum" if reduce else "none", ) return loss, loss @staticmethod def reduce_metrics(logging_outputs) -> None: """Aggregate logging outputs from data parallel training.""" loss_sum = sum(log.get("loss", 0) for log in logging_outputs) ntokens = sum(log.get("ntokens", 0) for log in logging_outputs) sample_size = sum(log.get("sample_size", 0) for log in logging_outputs) # we divide by log(2) to convert the loss from base e to base 2 metrics.log_scalar( "loss", loss_sum / sample_size / math.log(2), sample_size, round=3 ) if sample_size != ntokens: metrics.log_scalar( "nll_loss", loss_sum / ntokens / math.log(2), ntokens, round=3 ) metrics.log_derived( "ppl", lambda meters: utils.get_perplexity(meters["nll_loss"].avg) ) else: metrics.log_derived( "ppl", lambda meters: utils.get_perplexity(meters["loss"].avg) ) @staticmethod def logging_outputs_can_be_summed() -> bool: """ Whether the logging outputs returned by `forward` can be summed across workers prior to calling `reduce_metrics`. Setting this to True will improves distributed training speed. """ return True
COCO-LM/fairseq/fairseq/criterions/cross_entropy.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/criterions/cross_entropy.py", "repo_id": "COCO-LM", "token_count": 1420 }
168
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import numpy as np import torch from . import BaseWrapperDataset class AppendTokenDataset(BaseWrapperDataset): def __init__(self, dataset, token=None): super().__init__(dataset) self.token = token if token is not None: self._sizes = np.array(dataset.sizes) + 1 else: self._sizes = dataset.sizes def __getitem__(self, idx): item = self.dataset[idx] if self.token is not None: item = torch.cat([item, item.new([self.token])]) return item @property def sizes(self): return self._sizes def num_tokens(self, index): n = self.dataset.num_tokens(index) if self.token is not None: n += 1 return n def size(self, index): n = self.dataset.size(index) if self.token is not None: n += 1 return n
COCO-LM/fairseq/fairseq/data/append_token_dataset.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/data/append_token_dataset.py", "repo_id": "COCO-LM", "token_count": 474 }
169
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. from dataclasses import dataclass, field from fairseq import file_utils from fairseq.data.encoders import register_bpe from fairseq.dataclass import FairseqDataclass from fairseq.data import Dictionary import unicodedata import sys import re def _is_punctuation(char): """Checks whether `chars` is a punctuation character.""" cp = ord(char) # We treat all non-letter/number ASCII as punctuation. # Characters such as "^", "$", and "`" are not in the Unicode # Punctuation class but we treat them as punctuation anyways, for # consistency. if ((cp >= 33 and cp <= 47) or (cp >= 58 and cp <= 64) or (cp >= 91 and cp <= 96) or (cp >= 123 and cp <= 126)): return True cat = unicodedata.category(char) if cat.startswith("P"): return True return False class SentencepiecePreTokenizer(object): def __init__(self): self.transl_table = dict( [ (ord(x), ord(y)) for x,y in zip( u"‘’´“”—–-", u"'''\"\"---") ] ) def handle_single_quote(self, tokens): line = ' '.join(tokens) line = re.sub(r"' ([smdSMDtT])\b", r"'\1", line) line = re.sub(r"' ll\b", "'ll", line) line = re.sub(r"' re\b", "'re", line) line = re.sub(r"' ve\b", "'ve", line) line = re.sub(r"' LL\b", "'LL ", line) line = re.sub(r"' RE\b", "'RE ", line) line = re.sub(r"' VE\b", "'VE ", line) return line.split() def split_on_cont_punc(self, tokens): new_tokens = [] for token in tokens: if len(token) > 1: last_j = 0 pre_is_punc = _is_punctuation(token[0]) for j, ch in enumerate(token): is_punc = _is_punctuation(ch) if is_punc != pre_is_punc: new_tokens.append(token[last_j: j]) last_j = j pre_is_punc = is_punc if last_j < len(token): new_tokens.append(token[last_j:]) else: new_tokens.append(token) return new_tokens def split_pre_and_post_punc(self, tokens): def pre_punc(token): last_j = 0 for j in range(1, len(token)): if not _is_punctuation(token[j]): last_j = j break return token[:last_j], token[last_j:] def post_punc(token): last_j = len(token) for j in range(len(token) - 2, -1, -1): is_punc = _is_punctuation(token[j]) if not _is_punctuation(token[j]): last_j = j + 1 break return token[:last_j], token[last_j:] new_tokens = [] for token in tokens: if len(token) > 1 and _is_punctuation(token[0]): a, b = pre_punc(token) if a: new_tokens.append(a) if b: if _is_punctuation(b[-1]): c, d = post_punc(b) if c: new_tokens.append(c) if d: new_tokens.append(d) else: new_tokens.append(b) elif len(token) > 1 and _is_punctuation(token[-1]): a, b = post_punc(token) if a: new_tokens.append(a) if b: new_tokens.append(b) else: new_tokens.append(token) return new_tokens def tokenize(self, line): line = line.strip() line = line.replace("``", '"').replace("''", '"') line = line.translate(self.transl_table) tokens = line.split() tokens = self.split_pre_and_post_punc(tokens) tokens = self.handle_single_quote(tokens) return tokens @dataclass class SentencepieceConfig(FairseqDataclass): sentencepiece_model: str = field( default="???", metadata={"help": "path to sentencepiece model"} ) vocab: str = field( default="???", metadata={"help": "path to dict.txt"} ) @register_bpe("sentencepiece", dataclass=SentencepieceConfig) class SentencepieceBPE(object): def __init__(self, cfg): sentencepiece_model = file_utils.cached_path(cfg.sentencepiece_model) try: import sentencepiece as spm self.sp = spm.SentencePieceProcessor() self.sp.Load(sentencepiece_model) self.pre_tokenizer = SentencepiecePreTokenizer() self.dictionary = Dictionary.load(cfg.vocab) except ImportError: raise ImportError('Please install sentencepiece with: pip install sentencepiece') def encode(self, x: str) -> str: return ' '.self.tokenize(x) def decode(self, x: str) -> str: return x.replace(' ', '').replace('\u2581', ' ').strip() def skip_space(self, tokens): new_tokens = [] for i, token in enumerate(tokens): skip = False # skip single space, to reduce total length if token == '\u2581': if i == len(tokens) - 1 or _is_punctuation(tokens[i + 1][0]): skip = True if not skip: new_tokens.append(token) return new_tokens def tokenize(self, x): x = ' '.join(self.pre_tokenizer.tokenize(x)) tokens = self.sp.EncodeAsPieces(x) tokens = self.skip_space(tokens) return tokens def convert_tokens_to_ids(self, tokens: list): ret = [] for token in tokens: ret.append(self.dictionary.index(token)) return ret def convert_tokens_to_string(self, tokens: list): return self.decode(" ".join(tokens)) def is_beginning_of_word(self, x: str) -> bool: if x in ["<unk>", "<s>", "</s>", "<pad>", "[CLS]", "[PAD]", "[SEP]", "[UNK]"]: # special elements are always considered beginnings # HACK: this logic is already present in fairseq/tasks/masked_lm.py # but these special tokens are also contained in the sentencepiece # vocabulary which causes duplicate special tokens. This hack makes # sure that they are all taken into account. return True return x.startswith("\u2581")
COCO-LM/fairseq/fairseq/data/encoders/sentencepiece_bpe.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/data/encoders/sentencepiece_bpe.py", "repo_id": "COCO-LM", "token_count": 3325 }
170
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. from functools import lru_cache from . import BaseWrapperDataset class LRUCacheDataset(BaseWrapperDataset): def __init__(self, dataset, token=None): super().__init__(dataset) @lru_cache(maxsize=16) def __getitem__(self, index): return self.dataset[index] @lru_cache(maxsize=16) def collater(self, samples): return self.dataset.collater(samples)
COCO-LM/fairseq/fairseq/data/lru_cache_dataset.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/data/lru_cache_dataset.py", "repo_id": "COCO-LM", "token_count": 212 }
171
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. from fairseq.data import data_utils from . import BaseWrapperDataset class PadDataset(BaseWrapperDataset): def __init__(self, dataset, pad_idx, left_pad): super().__init__(dataset) self.pad_idx = pad_idx self.left_pad = left_pad def collater(self, samples): return data_utils.collate_tokens(samples, self.pad_idx, left_pad=self.left_pad, pad_to_multiple=8) class LeftPadDataset(PadDataset): def __init__(self, dataset, pad_idx): super().__init__(dataset, pad_idx, left_pad=True) class RightPadDataset(PadDataset): def __init__(self, dataset, pad_idx): super().__init__(dataset, pad_idx, left_pad=False)
COCO-LM/fairseq/fairseq/data/pad_dataset.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/data/pad_dataset.py", "repo_id": "COCO-LM", "token_count": 339 }
172
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. from . import BaseWrapperDataset class StripTokenDataset(BaseWrapperDataset): def __init__(self, dataset, id_to_strip): super().__init__(dataset) self.id_to_strip = id_to_strip def __getitem__(self, index): item = self.dataset[index] while len(item) > 0 and item[-1] == self.id_to_strip: item = item[:-1] while len(item) > 0 and item[0] == self.id_to_strip: item = item[1:] return item
COCO-LM/fairseq/fairseq/data/strip_token_dataset.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/data/strip_token_dataset.py", "repo_id": "COCO-LM", "token_count": 267 }
173
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. from torch import nn class ModuleProxyWrapper(nn.Module): """ Wrap a DistributedDataParallel module and forward requests for missing attributes to the module wrapped by DDP (the twice-wrapped module). Also forward calls to :func:`state_dict` and :func:`load_state_dict`. Usage:: module.xyz = "hello world" wrapped_module = DistributedDataParallel(module, **ddp_args) wrapped_module = ModuleProxyWrapper(wrapped_module) assert wrapped_module.xyz == "hello world" assert wrapped_module.state_dict().keys() == module.state_dict().keys() Args: module (nn.Module): module to wrap """ def __init__(self, module: nn.Module): super().__init__() assert hasattr(module, "module"), \ "ModuleProxyWrapper expects input to wrap another module" self.module = module def __getattr__(self, name): """Forward missing attributes to twice-wrapped module.""" try: # defer to nn.Module's logic return super().__getattr__(name) except AttributeError: try: # forward to the once-wrapped module return getattr(self.module, name) except AttributeError: # forward to the twice-wrapped module return getattr(self.module.module, name) def state_dict(self, *args, **kwargs): """Forward to the twice-wrapped module.""" return self.module.module.state_dict(*args, **kwargs) def load_state_dict(self, *args, **kwargs): """Forward to the twice-wrapped module.""" return self.module.module.load_state_dict(*args, **kwargs) def forward(self, *args, **kwargs): return self.module(*args, **kwargs)
COCO-LM/fairseq/fairseq/distributed/module_proxy_wrapper.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/distributed/module_proxy_wrapper.py", "repo_id": "COCO-LM", "token_count": 765 }
174
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import importlib import os # automatically import any Python files in the models/ directory models_dir = os.path.dirname(__file__) for file in os.listdir(models_dir): path = os.path.join(models_dir, file) if ( not file.startswith("_") and not file.startswith(".") and (file.endswith(".py") or os.path.isdir(path)) ): model_name = file[: file.find(".py")] if file.endswith(".py") else file module = importlib.import_module("fairseq.model_parallel.models." + model_name)
COCO-LM/fairseq/fairseq/model_parallel/models/__init__.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/model_parallel/models/__init__.py", "repo_id": "COCO-LM", "token_count": 253 }
175
# Copyright (c) Microsoft Corporation. # Licensed under the MIT license. """ COCO-LM: Correcting and Contrasting Text Sequences for Language Model Pretraining """ import logging import torch import torch.nn as nn import torch.nn.functional as F from fairseq import utils from fairseq.models import ( FairseqEncoder, FairseqEncoderModel, register_model, register_model_architecture, ) from fairseq.modules import ( LayerNorm, TransformerSentenceEncoder, ) from fairseq.models.squad import SQuADHead from fairseq.modules.transformer_sentence_encoder import init_bert_params from fairseq.modules.quant_noise import quant_noise as apply_quant_noise_ logger = logging.getLogger(__name__) @register_model('cocolm') class COCOLM_Model(FairseqEncoderModel): def __init__(self, args, auxiliary, main_encoder): super().__init__(main_encoder) self.auxiliary = auxiliary self.args = args # We follow BERT's random weight initialization self.apply(init_bert_params) self.classification_heads = nn.ModuleDict() @staticmethod def add_args(parser): """Add model-specific arguments to the parser.""" parser.add_argument('--encoder-layers', type=int, metavar='L', help='num encoder layers') parser.add_argument('--encoder-embed-dim', type=int, metavar='H', help='encoder embedding dimension') parser.add_argument('--encoder-ffn-embed-dim', type=int, metavar='F', help='encoder embedding dimension for FFN') parser.add_argument('--encoder-attention-heads', type=int, metavar='A', help='num encoder attention heads') parser.add_argument('--generator-sample-mode', choices=['train', 'eval', 'zero-dropout'], help='which mode the generator is in when sampling from its MLM output') parser.add_argument('--generator-layers', type=int) parser.add_argument('--activation-fn', choices=utils.get_available_activation_fns(), help='activation function to use') parser.add_argument('--pooler-activation-fn', choices=utils.get_available_activation_fns(), help='activation function to use for pooler layer') parser.add_argument('--encoder-normalize-before', action='store_true', help='apply layernorm before each encoder block') parser.add_argument('--dropout', type=float, metavar='D', help='dropout probability') parser.add_argument('--attention-dropout', type=float, metavar='D', help='dropout probability for attention weights') parser.add_argument('--activation-dropout', type=float, metavar='D', help='dropout probability after activation in FFN') parser.add_argument('--pooler-dropout', type=float, metavar='D', help='dropout probability in the masked_lm pooler layers') parser.add_argument('--max-positions', type=int, help='number of positional embeddings to learn') parser.add_argument('--load-checkpoint-heads', action='store_true', help='(re-)register and load heads when loading checkpoints') # args for "Reducing Transformer Depth on Demand with Structured Dropout" (Fan et al., 2019) parser.add_argument('--encoder-layerdrop', type=float, metavar='D', default=0, help='LayerDrop probability for encoder') parser.add_argument('--encoder-layers-to-keep', default=None, help='which layers to *keep* when pruning as a comma-separated list') # args for Training with Quantization Noise for Extreme Model Compression ({Fan*, Stock*} et al., 2020) parser.add_argument('--quant-noise-pq', type=float, metavar='D', default=0, help='iterative PQ quantization noise at training time') parser.add_argument('--quant-noise-pq-block-size', type=int, metavar='D', default=8, help='block size of quantization noise at training time') parser.add_argument('--quant-noise-scalar', type=float, metavar='D', default=0, help='scalar quantization noise and scalar quantization at training time') parser.add_argument('--rel-pos', type=int, help='whether to use relative position or not; 0 = not use; 1 = use') parser.add_argument('--checkpoint-activations', action='store_true', help='checkpoint activations at each layer, which saves GPU ' 'memory usage at the cost of some additional compute') parser.add_argument('--offload-activations', action='store_true', help='checkpoint activations at each layer, then save to gpu. Sets --checkpoint-activations.') parser.add_argument('--seq-contrast', action='store_true', help='perform sequence contrast learning') parser.add_argument('--seq-contrast-dim', type=int, help='sequence contrast embedding dimension') parser.add_argument('--span', type=float, help='span seq length') parser.add_argument('--add-span-cls', action='store_true', help='add cls token to span tokens') parser.add_argument('--temperature', type=float, help='temperature in sequence constrast learning') parser.add_argument('--mask-cls', action='store_true', help='has probability to mask cls') parser.add_argument('--binary-loss-weight', type=float, help='loss weight for the binary loss') parser.add_argument('--scl-loss-weight', type=float, help='loss weight for the SCL loss') parser.add_argument('--clm', action='store_true', help='perform correction langugae modeling') parser.add_argument('--rel-pos-bins', type=int, help='number of relative position buckets') parser.add_argument('--max-rel-pos', type=int, help='max relative positions') @classmethod def build_model(cls, args, task): """Build a new model instance.""" # make sure all arguments are present base_architecture(args) if not hasattr(args, 'max_positions'): args.max_positions = args.tokens_per_sample main_encoder = GenEncoder(args, task.source_dictionary) if args.task == 'cocolm': auxiliary = Generator(args, task.source_dictionary, main_encoder) else: auxiliary = None return cls(args, auxiliary, main_encoder) def forward(self, src_tokens, span_tokens=None, features_only=False, return_all_hiddens=False, classification_head_name=None, masked_tokens=None, targets=None, **kwargs): seq_contrast = self.args.seq_contrast if classification_head_name is not None: features_only = True # don't enable seq_contrast in finetuning seq_contrast = False def get_padding_mask(tokens): padding_mask = tokens.eq(self.encoder.sentence_encoder.padding_idx) if not padding_mask.any(): padding_mask = None return padding_mask padding_mask = get_padding_mask(src_tokens) replace_tokens = None span_seq_emb = None # in pretraining if not features_only: used_eval_mode = False if self.training and self.args.generator_sample_mode == "eval": self.auxiliary.eval() with torch.no_grad(): small_gen_x_mask_eval , _ = self.auxiliary( src_tokens, features_only=False, return_all_hiddens=False, padding_mask=padding_mask, masked_tokens=masked_tokens, **kwargs ) # Float[num_masked, vocab] self.auxiliary.train() used_eval_mode = True small_gen_x_mask, _ = self.auxiliary( src_tokens, features_only=False, return_all_hiddens=False, padding_mask=padding_mask, masked_tokens=masked_tokens, **kwargs ) # Float[num_masked, vocab] if not used_eval_mode: small_gen_x_mask_eval = small_gen_x_mask.detach() with torch.no_grad(): sample_probs = small_gen_x_mask_eval.view(-1, small_gen_x_mask_eval.size(-1)) sample_probs = torch.softmax(sample_probs, -1, dtype=torch.float32) sampled_input = torch.multinomial(sample_probs, 1).view(-1) src_tokens = src_tokens.clone() src_tokens[masked_tokens] = sampled_input replace_tokens = (src_tokens != targets) if seq_contrast and self.args.span > 0: assert span_tokens is not None span_padding_mask = get_padding_mask(span_tokens) _, extra = self.encoder( span_tokens, features_only=True, return_all_hiddens=return_all_hiddens, padding_mask=span_padding_mask, seq_contrast=seq_contrast, **kwargs ) span_seq_emb = extra["seq_emb"] gen_x, extra = self.encoder( src_tokens, features_only=features_only, return_all_hiddens=return_all_hiddens, padding_mask=padding_mask, masked_tokens=masked_tokens, seq_contrast=seq_contrast, **kwargs ) if span_seq_emb is not None: extra["span_seq_emb"] = span_seq_emb if classification_head_name is not None: gen_x = self.classification_heads[classification_head_name](gen_x) if self.args.task == 'cocolm': binary_target = ~replace_tokens if padding_mask is not None: binary_target = binary_target[~padding_mask] return small_gen_x_mask, gen_x, binary_target, replace_tokens, extra else: return gen_x, extra def get_normalized_probs(self, net_output, log_probs, sample=None): """Get normalized probabilities (or log probs) from a net's output.""" logits = net_output[0].float() if log_probs: return F.log_softmax(logits, dim=-1) else: return F.softmax(logits, dim=-1) def register_classification_head(self, name, num_classes=None, inner_dim=None, **kwargs): """Register a classification head.""" if name in self.classification_heads: prev_num_classes = self.classification_heads[name].out_proj.out_features prev_inner_dim = self.classification_heads[name].dense.out_features if num_classes != prev_num_classes or inner_dim != prev_inner_dim: logger.warning( 're-registering head "{}" with num_classes {} (prev: {}) ' 'and inner_dim {} (prev: {})'.format( name, num_classes, prev_num_classes, inner_dim, prev_inner_dim ) ) self.classification_heads[name] = COCOLM_ClassificationHead( self.args.encoder_embed_dim, inner_dim or self.args.encoder_embed_dim, num_classes, self.args.pooler_activation_fn, self.args.pooler_dropout, self.args.quant_noise_pq, self.args.quant_noise_pq_block_size, ) def register_question_answering_head(self, name, num_classes=None): self.classification_heads[name] = SQuADHead( self.args.encoder_embed_dim, ) @property def supported_targets(self): return {'self'} def upgrade_state_dict_named(self, state_dict, name): prefix = name + '.' if name != '' else '' # rename decoder -> encoder before upgrading children modules for k in list(state_dict.keys()): if k.startswith(prefix + 'decoder'): new_k = prefix + 'encoder' + k[len(prefix + 'decoder'):] state_dict[new_k] = state_dict[k] del state_dict[k] # upgrade children modules super().upgrade_state_dict_named(state_dict, name) # Handle new classification heads present in the state dict. current_head_names = ( [] if not hasattr(self, 'classification_heads') else self.classification_heads.keys() ) keys_to_delete = [] for k in state_dict.keys(): if not k.startswith(prefix + 'classification_heads.'): continue head_name = k[len(prefix + 'classification_heads.'):].split('.')[0] num_classes = state_dict[prefix + 'classification_heads.' + head_name + '.out_proj.weight'].size(0) inner_dim = state_dict[prefix + 'classification_heads.' + head_name + '.dense.weight'].size(0) if getattr(self.args, 'load_checkpoint_heads', False): if head_name not in current_head_names: self.register_classification_head(head_name, num_classes, inner_dim) else: if head_name not in current_head_names: logger.warning( 'deleting classification head ({}) from checkpoint ' 'not present in current model: {}'.format(head_name, k) ) keys_to_delete.append(k) elif ( num_classes != self.classification_heads[head_name].out_proj.out_features or inner_dim != self.classification_heads[head_name].dense.out_features ): logger.warning( 'deleting classification head ({}) from checkpoint ' 'with different dimensions than current model: {}'.format(head_name, k) ) keys_to_delete.append(k) for k in keys_to_delete: del state_dict[k] # Copy any newly-added classification heads into the state dict # with their current weights. if hasattr(self, 'classification_heads'): cur_state = self.classification_heads.state_dict() for k, v in cur_state.items(): if prefix + 'classification_heads.' + k not in state_dict: logger.info('Overwriting ' + prefix + 'classification_heads.' + k) state_dict[prefix + 'classification_heads.' + k] = v class MaskedLMHead(nn.Module): """Head for masked language modeling.""" def __init__(self, hidden_dim, embed_dim, output_dim, activation_fn, weight, bias=None): super().__init__() self.dense = nn.Linear(hidden_dim, embed_dim) self.activation_fn = utils.get_activation_fn(activation_fn) self.layer_norm = LayerNorm(embed_dim) self.weight = weight self.bias = nn.Parameter(torch.zeros(output_dim)) if bias is None else bias def forward(self, features, masked_tokens=None, **kwargs): # Only project the masked tokens while training, # saves both memory and computation if masked_tokens is not None: features = features[masked_tokens, :] x = self.dense(features) x = self.activation_fn(x) x = self.layer_norm(x) # project back to size of vocabulary with bias x = F.linear(x, self.weight, bias=self.bias) return x class CLMHead(nn.Module): """Head for masked language modeling.""" def __init__(self, output_dim, weight, bias=None): super().__init__() # self.dense = nn.Linear(hidden_dim, embed_dim) # self.activation_fn = utils.get_activation_fn(activation_fn) # self.layer_norm = LayerNorm(embed_dim) self.weight = weight self.bias = nn.Parameter(torch.zeros(output_dim)) if bias is None else bias def forward(self, x, masked_tokens=None, **kwargs): # Only project the masked tokens while training, # saves both memory and computation if masked_tokens is not None: x = x[masked_tokens, :] # x = self.dense(x) # x = self.activation_fn(x) # x = self.layer_norm(x) # project back to size of vocabulary with bias x = F.linear(x, self.weight, bias=self.bias) return x class BinaryHead(nn.Module): """Head for masked language modeling.""" def __init__(self, embed_dim, activation_fn): super().__init__() self.embed_dim = embed_dim # Todo: check projection is needed or not # self.dense = nn.Linear(embed_dim, embed_dim) # self.activation_fn = utils.get_activation_fn(activation_fn) # self.layer_norm = LayerNorm(embed_dim) self.out_proj = nn.Linear(embed_dim, 1, bias=True) # self.out_proj.bias.data.zero_() def forward(self, x, padding_mask=None, **kwargs): # Only project the unmasked tokens while training, # saves both memory and computation if padding_mask is not None: x = x[~padding_mask, :] # x = self.dense(x) # x = self.activation_fn(x) # x = self.layer_norm(x) return self.out_proj(x) class SCLHead(nn.Module): """Head for sentence-level contrastive learning.""" def __init__(self, input_dim, output_dim, activation_fn): super().__init__() self.dense = nn.Linear(input_dim, output_dim) self.activation_fn = utils.get_activation_fn(activation_fn) self.layer_norm = LayerNorm(output_dim) def forward(self, features, **kwargs): x = features[:, 0, :] # take <s> token (equiv. to [CLS]) x = self.dense(x) x = self.activation_fn(x) x = self.layer_norm(x) return x class COCOLM_ClassificationHead(nn.Module): """Head for sentence-level classification tasks.""" def __init__(self, input_dim, inner_dim, num_classes, activation_fn, pooler_dropout, q_noise=0, qn_block_size=8): super().__init__() self.dense = nn.Linear(input_dim, inner_dim) self.activation_fn = utils.get_activation_fn(activation_fn) self.dropout = nn.Dropout(p=pooler_dropout) self.out_proj = apply_quant_noise_( nn.Linear(inner_dim, num_classes), q_noise, qn_block_size ) def forward(self, features, **kwargs): x = features[:, 0, :] # take <s> token (equiv. to [CLS]) x = self.dropout(x) x = self.dense(x) x = self.activation_fn(x) x = self.dropout(x) x = self.out_proj(x) return x class Generator(FairseqEncoder): """COCO-LM auxiliary encoder. Implements the :class:`~fairseq.models.FairseqEncoder` interface required by :class:`~fairseq.models.FairseqLanguageModel`. """ def __init__(self, args, dictionary, main_encoder): super().__init__(dictionary) self.args = args self.sentence_encoder = TransformerSentenceEncoder( padding_idx=dictionary.pad(), vocab_size=len(dictionary), num_encoder_layers=args.generator_layers, embedding_dim=int(args.encoder_embed_dim), ffn_embedding_dim=int(args.encoder_ffn_embed_dim), num_attention_heads=int(args.encoder_attention_heads), dropout=args.dropout if args.generator_sample_mode != "zero-dropout" else 0, attention_dropout=args.attention_dropout if args.generator_sample_mode != "zero-dropout" else 0, activation_dropout=args.activation_dropout if args.generator_sample_mode != "zero-dropout" else 0, layerdrop=args.encoder_layerdrop, max_seq_len=args.max_positions, num_segments=0, encoder_normalize_before=False, apply_bert_init=True, activation_fn=args.activation_fn, q_noise=args.quant_noise_pq, qn_block_size=args.quant_noise_pq_block_size, rel_pos=args.rel_pos, checkpoint_activations=args.checkpoint_activations, offload_activations=args.offload_activations, share_embed_tokens=main_encoder.sentence_encoder.embed_tokens, share_embed_positions=main_encoder.sentence_encoder.embed_positions if args.generator_sample_mode != "zero-dropout" else None, share_emb_layer_norm=main_encoder.sentence_encoder.emb_layer_norm if args.generator_sample_mode != "zero-dropout" else None, shared_embedding_dim=args.encoder_embed_dim, rel_pos_bins=args.rel_pos_bins, max_rel_pos=args.max_rel_pos ) self.lm_head = MaskedLMHead( hidden_dim=int(args.encoder_embed_dim), embed_dim=int(args.encoder_embed_dim), output_dim=len(dictionary), activation_fn=args.activation_fn, weight=main_encoder.sentence_encoder.embed_tokens.weight, ) def forward(self, src_tokens, features_only=False, return_all_hiddens=False, padding_mask=None, masked_tokens=None, **unused): """ Args: src_tokens (LongTensor): input tokens of shape `(batch, src_len)` features_only (bool, optional): skip LM head and just return features. If True, the output will be of shape `(batch, src_len, embed_dim)`. return_all_hiddens (bool, optional): also return all of the intermediate hidden states (default: False). Returns: tuple: - the LM output of shape `(batch, src_len, vocab)` - a dictionary of additional data, where 'inner_states' is a list of hidden states. """ x, extra = self.extract_features(src_tokens, return_all_hiddens, padding_mask) if not features_only: x = self.output_layer(x, masked_tokens=masked_tokens) return x, extra def extract_features(self, src_tokens, return_all_hiddens=False, padding_mask=None, **unused): inner_states, _ = self.sentence_encoder( src_tokens, last_state_only=not return_all_hiddens, use_ext_padding_mask=True, padding_mask=padding_mask ) features = inner_states[-1] return features, {'inner_states': inner_states if return_all_hiddens else None} def output_layer(self, features, masked_tokens=None, **unused): return self.lm_head(features, masked_tokens) def max_positions(self): """Maximum output length supported by the encoder.""" return self.args.max_positions class GenEncoder(FairseqEncoder): """COCO-LM main encoder. Implements the :class:`~fairseq.models.FairseqEncoder` interface required by :class:`~fairseq.models.FairseqLanguageModel`. """ def __init__(self, args, dictionary): super().__init__(dictionary) self.args = args if args.encoder_layers_to_keep: args.encoder_layers = len(args.encoder_layers_to_keep.split(",")) self.sentence_encoder = TransformerSentenceEncoder( padding_idx=dictionary.pad(), vocab_size=len(dictionary), num_encoder_layers=args.encoder_layers, embedding_dim=args.encoder_embed_dim, ffn_embedding_dim=args.encoder_ffn_embed_dim, num_attention_heads=args.encoder_attention_heads, dropout=args.dropout, attention_dropout=args.attention_dropout, activation_dropout=args.activation_dropout, layerdrop=args.encoder_layerdrop, max_seq_len=args.max_positions, num_segments=0, encoder_normalize_before=False, apply_bert_init=True, activation_fn=args.activation_fn, q_noise=args.quant_noise_pq, qn_block_size=args.quant_noise_pq_block_size, rel_pos=args.rel_pos, checkpoint_activations=args.checkpoint_activations, offload_activations=args.offload_activations, rel_pos_bins=args.rel_pos_bins, max_rel_pos=args.max_rel_pos ) self.binary_head = BinaryHead( embed_dim=int(args.encoder_embed_dim), activation_fn=args.activation_fn, ) if args.clm: self.lm_head = CLMHead( output_dim=len(dictionary), weight=self.sentence_encoder.embed_tokens.weight, ) else: self.lm_head = None self.seq_head = None if args.seq_contrast: self.seq_head = SCLHead( input_dim=int(args.encoder_embed_dim), output_dim=int(args.seq_contrast_dim), activation_fn=args.activation_fn, ) def forward(self, src_tokens, features_only=False, return_all_hiddens=False, padding_mask=None, masked_tokens=None, seq_contrast=False, **unused): """ Args: src_tokens (LongTensor): input tokens of shape `(batch, src_len)` features_only (bool, optional): skip LM head and just return features. If True, the output will be of shape `(batch, src_len, embed_dim)`. return_all_hiddens (bool, optional): also return all of the intermediate hidden states (default: False). Returns: tuple: - the LM output of shape `(batch, src_len, vocab)` - a dictionary of additional data, where 'inner_states' is a list of hidden states. """ x, extra = self.extract_features(src_tokens, return_all_hiddens, padding_mask) if seq_contrast: seq_emb = self.seq_head(x) extra["seq_emb"] = seq_emb if not features_only: if self.lm_head is not None: assert masked_tokens is not None extra['clm_outputs'] = self.lm_head(x, masked_tokens) x = self.output_layer(x, padding_mask=padding_mask) return x, extra def extract_features(self, src_tokens, return_all_hiddens=False, padding_mask=None, **unused): inner_states, _ = self.sentence_encoder( src_tokens, last_state_only=not return_all_hiddens, use_ext_padding_mask=True, padding_mask=padding_mask, ) features = inner_states[-1] return features, {'inner_states': inner_states if return_all_hiddens else None} def output_layer(self, features, padding_mask=None, **unused): return self.binary_head(features, padding_mask=padding_mask) def max_positions(self): """Maximum output length supported by the encoder.""" return self.args.max_positions @register_model_architecture('cocolm', 'cocolm') def base_architecture(args): args.encoder_layers = getattr(args, 'encoder_layers', 12) args.encoder_embed_dim = getattr(args, 'encoder_embed_dim', 768) args.encoder_ffn_embed_dim = getattr(args, 'encoder_ffn_embed_dim', 3072) args.encoder_attention_heads = getattr(args, 'encoder_attention_heads', 12) args.generator_layers = getattr(args, 'generator_layers', 4) args.generator_sample_mode = getattr(args, 'generator_sample_mode', 'eval') args.activation_fn = getattr(args, 'activation_fn', 'gelu') args.pooler_activation_fn = getattr(args, 'pooler_activation_fn', 'tanh') args.dropout = getattr(args, 'dropout', 0.1) args.attention_dropout = getattr(args, 'attention_dropout', 0.1) args.activation_dropout = getattr(args, 'activation_dropout', 0.0) args.pooler_dropout = getattr(args, 'pooler_dropout', 0.0) args.encoder_layers_to_keep = getattr(args, 'encoder_layers_to_keep', None) args.encoder_layerdrop = getattr(args, 'encoder_layerdrop', 0.0) args.rel_pos = getattr(args, 'rel_pos', 1) args.binary_loss_weight = getattr(args, 'binary_loss_weight', 50) args.mask_cls = getattr(args, 'mask_cls', False) args.rel_pos_bins = getattr(args, 'rel_pos_bins', 32) args.max_rel_pos = getattr(args, 'max_rel_pos', 128) args.checkpoint_activations = getattr(args, "checkpoint_activations", False) args.offload_activations = getattr(args, "offload_activations", False) if args.offload_activations: args.checkpoint_activations = True # SCL args.seq_contrast = getattr(args, 'seq_contrast', False) args.span = getattr(args, 'span', 0) args.add_span_cls = getattr(args, 'add_span_cls', False) args.seq_contrast_dim = getattr(args, 'seq_contrast_dim', 768) args.temperature = getattr(args, 'temperature', 1) args.scl_loss_weight = getattr(args, 'scl_loss_weight', 1) # CLM args.clm = getattr(args, 'clm', False) @register_model_architecture('cocolm', 'cocolm_base') def cocolm_base_architecture(args): base_architecture(args) @register_model_architecture('cocolm', 'cocolm_small') def cocolm_small_architecture(args): args.encoder_layers = getattr(args, 'encoder_layers', 12) args.encoder_embed_dim = getattr(args, 'encoder_embed_dim', 256) args.encoder_ffn_embed_dim = getattr(args, 'encoder_ffn_embed_dim', 1024) args.encoder_attention_heads = getattr(args, 'encoder_attention_heads', 4) args.generator_layers = getattr(args, 'generator_layers', 2) base_architecture(args) @register_model_architecture('cocolm', 'cocolm_large') def cocolm_large_architecture(args): args.encoder_layers = getattr(args, 'encoder_layers', 24) args.encoder_embed_dim = getattr(args, 'encoder_embed_dim', 1024) args.encoder_ffn_embed_dim = getattr(args, 'encoder_ffn_embed_dim', 4096) args.encoder_attention_heads = getattr(args, 'encoder_attention_heads', 16) args.generator_layers = getattr(args, 'generator_layers', 4) base_architecture(args)
COCO-LM/fairseq/fairseq/models/cocolm/model.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/models/cocolm/model.py", "repo_id": "COCO-LM", "token_count": 14169 }
176
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import logging import torch import torch.nn as nn import torch.nn.functional as F from fairseq import utils from fairseq.models import ( FairseqEncoder, FairseqEncoderModel, register_model, register_model_architecture, ) from fairseq.modules import ( LayerNorm, SinusoidalPositionalEmbedding, TransformerSentenceEncoder, ) from fairseq.modules.transformer_sentence_encoder import init_bert_params logger = logging.getLogger(__name__) @register_model("masked_lm") class MaskedLMModel(FairseqEncoderModel): """ Class for training a Masked Language Model. It also supports an additional sentence level prediction if the sent-loss argument is set. """ def __init__(self, args, encoder): super().__init__(encoder) self.args = args # if specified then apply bert initialization on the model. We need # to explictly call this to make sure that the output embeddings # and projection layers are also correctly initialized if getattr(args, "apply_bert_init", False): self.apply(init_bert_params) @staticmethod def add_args(parser): """Add model-specific arguments to the parser.""" # Arguments related to dropout parser.add_argument( "--dropout", type=float, metavar="D", help="dropout probability" ) parser.add_argument( "--attention-dropout", type=float, metavar="D", help="dropout probability for" " attention weights", ) parser.add_argument( "--act-dropout", type=float, metavar="D", help="dropout probability after" " activation in FFN", ) # Arguments related to hidden states and self-attention parser.add_argument( "--encoder-ffn-embed-dim", type=int, metavar="N", help="encoder embedding dimension for FFN", ) parser.add_argument( "--encoder-layers", type=int, metavar="N", help="num encoder layers" ) parser.add_argument( "--encoder-attention-heads", type=int, metavar="N", help="num encoder attention heads", ) # Arguments related to input and output embeddings parser.add_argument( "--encoder-embed-dim", type=int, metavar="N", help="encoder embedding dimension", ) parser.add_argument( "--share-encoder-input-output-embed", action="store_true", help="share encoder input" " and output embeddings", ) parser.add_argument( "--encoder-learned-pos", action="store_true", help="use learned positional embeddings in the encoder", ) parser.add_argument( "--no-token-positional-embeddings", action="store_true", help="if set, disables positional embeddings" " (outside self attention)", ) parser.add_argument( "--num-segment", type=int, metavar="N", help="num segment in the input" ) parser.add_argument( "--max-positions", type=int, help="number of positional embeddings to learn" ) # Arguments related to sentence level prediction parser.add_argument( "--sentence-class-num", type=int, metavar="N", help="number of classes for sentence task", ) parser.add_argument( "--sent-loss", action="store_true", help="if set," " calculate sentence level predictions", ) # Arguments related to parameter initialization parser.add_argument( "--apply-bert-init", action="store_true", help="use custom param initialization for BERT", ) # misc params parser.add_argument( "--activation-fn", choices=utils.get_available_activation_fns(), help="activation function to use", ) parser.add_argument( "--pooler-activation-fn", choices=utils.get_available_activation_fns(), help="Which activation function to use for pooler layer.", ) parser.add_argument( "--encoder-normalize-before", action="store_true", help="apply layernorm before each encoder block", ) def forward(self, src_tokens, segment_labels=None, **kwargs): return self.encoder(src_tokens, segment_labels=segment_labels, **kwargs) def max_positions(self): return self.encoder.max_positions @classmethod def build_model(cls, args, task): """Build a new model instance.""" # make sure all arguments are present in older models base_architecture(args) if not hasattr(args, "max_positions"): args.max_positions = args.tokens_per_sample logger.info(args) encoder = MaskedLMEncoder(args, task.dictionary) return cls(args, encoder) class MaskedLMEncoder(FairseqEncoder): """ Encoder for Masked Language Modelling. """ def __init__(self, args, dictionary): super().__init__(dictionary) self.padding_idx = dictionary.pad() self.vocab_size = dictionary.__len__() self.max_positions = args.max_positions self.sentence_encoder = TransformerSentenceEncoder( padding_idx=self.padding_idx, vocab_size=self.vocab_size, num_encoder_layers=args.encoder_layers, embedding_dim=args.encoder_embed_dim, ffn_embedding_dim=args.encoder_ffn_embed_dim, num_attention_heads=args.encoder_attention_heads, dropout=args.dropout, attention_dropout=args.attention_dropout, activation_dropout=args.act_dropout, max_seq_len=self.max_positions, num_segments=args.num_segment, use_position_embeddings=not args.no_token_positional_embeddings, encoder_normalize_before=args.encoder_normalize_before, apply_bert_init=args.apply_bert_init, activation_fn=args.activation_fn, learned_pos_embedding=args.encoder_learned_pos, ) self.share_input_output_embed = args.share_encoder_input_output_embed self.embed_out = None self.sentence_projection_layer = None self.sentence_out_dim = args.sentence_class_num self.lm_output_learned_bias = None # Remove head is set to true during fine-tuning self.load_softmax = not getattr(args, "remove_head", False) self.masked_lm_pooler = nn.Linear( args.encoder_embed_dim, args.encoder_embed_dim ) self.pooler_activation = utils.get_activation_fn(args.pooler_activation_fn) self.lm_head_transform_weight = nn.Linear( args.encoder_embed_dim, args.encoder_embed_dim ) self.activation_fn = utils.get_activation_fn(args.activation_fn) self.layer_norm = LayerNorm(args.encoder_embed_dim) self.lm_output_learned_bias = None if self.load_softmax: self.lm_output_learned_bias = nn.Parameter(torch.zeros(self.vocab_size)) if not self.share_input_output_embed: self.embed_out = nn.Linear( args.encoder_embed_dim, self.vocab_size, bias=False ) if args.sent_loss: self.sentence_projection_layer = nn.Linear( args.encoder_embed_dim, self.sentence_out_dim, bias=False ) def forward(self, src_tokens, segment_labels=None, masked_tokens=None, **unused): """ Forward pass for Masked LM encoder. This first computes the token embedding using the token embedding matrix, position embeddings (if specified) and segment embeddings (if specified). Here we assume that the sentence representation corresponds to the output of the classification_token (see bert_task or cross_lingual_lm task for more details). Args: - src_tokens: B x T matrix representing sentences - segment_labels: B x T matrix representing segment label for tokens Returns: - a tuple of the following: - logits for predictions in format B x T x C to be used in softmax afterwards - a dictionary of additional data, where 'pooled_output' contains the representation for classification_token and 'inner_states' is a list of internal model states used to compute the predictions (similar in ELMO). 'sentence_logits' is the prediction logit for NSP task and is only computed if this is specified in the input arguments. """ inner_states, sentence_rep = self.sentence_encoder( src_tokens, segment_labels=segment_labels, ) x = inner_states[-1].transpose(0, 1) # project masked tokens only if masked_tokens is not None: x = x[masked_tokens, :] x = self.layer_norm(self.activation_fn(self.lm_head_transform_weight(x))) pooled_output = self.pooler_activation(self.masked_lm_pooler(sentence_rep)) # project back to size of vocabulary if self.share_input_output_embed and hasattr( self.sentence_encoder.embed_tokens, "weight" ): x = F.linear(x, self.sentence_encoder.embed_tokens.weight) elif self.embed_out is not None: x = self.embed_out(x) if self.lm_output_learned_bias is not None: x = x + self.lm_output_learned_bias sentence_logits = None if self.sentence_projection_layer: sentence_logits = self.sentence_projection_layer(pooled_output) return x, { "inner_states": inner_states, "pooled_output": pooled_output, "sentence_logits": sentence_logits, } def max_positions(self): """Maximum output length supported by the encoder.""" return self.max_positions def upgrade_state_dict_named(self, state_dict, name): if isinstance( self.sentence_encoder.embed_positions, SinusoidalPositionalEmbedding ): state_dict[ name + ".sentence_encoder.embed_positions._float_tensor" ] = torch.FloatTensor(1) if not self.load_softmax: for k in list(state_dict.keys()): if ( "embed_out.weight" in k or "sentence_projection_layer.weight" in k or "lm_output_learned_bias" in k ): del state_dict[k] return state_dict @register_model_architecture("masked_lm", "masked_lm") def base_architecture(args): args.dropout = getattr(args, "dropout", 0.1) args.attention_dropout = getattr(args, "attention_dropout", 0.1) args.act_dropout = getattr(args, "act_dropout", 0.0) args.encoder_ffn_embed_dim = getattr(args, "encoder_ffn_embed_dim", 4096) args.encoder_layers = getattr(args, "encoder_layers", 6) args.encoder_attention_heads = getattr(args, "encoder_attention_heads", 8) args.encoder_embed_dim = getattr(args, "encoder_embed_dim", 1024) args.share_encoder_input_output_embed = getattr( args, "share_encoder_input_output_embed", False ) args.encoder_learned_pos = getattr(args, "encoder_learned_pos", False) args.no_token_positional_embeddings = getattr( args, "no_token_positional_embeddings", False ) args.num_segment = getattr(args, "num_segment", 2) args.sentence_class_num = getattr(args, "sentence_class_num", 2) args.sent_loss = getattr(args, "sent_loss", False) args.apply_bert_init = getattr(args, "apply_bert_init", False) args.activation_fn = getattr(args, "activation_fn", "relu") args.pooler_activation_fn = getattr(args, "pooler_activation_fn", "tanh") args.encoder_normalize_before = getattr(args, "encoder_normalize_before", False) @register_model_architecture("masked_lm", "bert_base") def bert_base_architecture(args): args.encoder_embed_dim = getattr(args, "encoder_embed_dim", 768) args.share_encoder_input_output_embed = getattr( args, "share_encoder_input_output_embed", True ) args.no_token_positional_embeddings = getattr( args, "no_token_positional_embeddings", False ) args.encoder_learned_pos = getattr(args, "encoder_learned_pos", True) args.num_segment = getattr(args, "num_segment", 2) args.encoder_layers = getattr(args, "encoder_layers", 12) args.encoder_attention_heads = getattr(args, "encoder_attention_heads", 12) args.encoder_ffn_embed_dim = getattr(args, "encoder_ffn_embed_dim", 3072) args.sentence_class_num = getattr(args, "sentence_class_num", 2) args.sent_loss = getattr(args, "sent_loss", True) args.apply_bert_init = getattr(args, "apply_bert_init", True) args.activation_fn = getattr(args, "activation_fn", "gelu") args.pooler_activation_fn = getattr(args, "pooler_activation_fn", "tanh") args.encoder_normalize_before = getattr(args, "encoder_normalize_before", True) base_architecture(args) @register_model_architecture("masked_lm", "bert_large") def bert_large_architecture(args): args.encoder_embed_dim = getattr(args, "encoder_embed_dim", 1024) args.encoder_layers = getattr(args, "encoder_layers", 24) args.encoder_attention_heads = getattr(args, "encoder_attention_heads", 16) args.encoder_ffn_embed_dim = getattr(args, "encoder_ffn_embed_dim", 4096) bert_base_architecture(args) @register_model_architecture("masked_lm", "xlm_base") def xlm_architecture(args): args.encoder_embed_dim = getattr(args, "encoder_embed_dim", 1024) args.share_encoder_input_output_embed = getattr( args, "share_encoder_input_output_embed", True ) args.no_token_positional_embeddings = getattr( args, "no_token_positional_embeddings", False ) args.encoder_learned_pos = getattr(args, "encoder_learned_pos", True) args.num_segment = getattr(args, "num_segment", 1) args.encoder_layers = getattr(args, "encoder_layers", 6) args.encoder_attention_heads = getattr(args, "encoder_attention_heads", 8) args.encoder_ffn_embed_dim = getattr(args, "encoder_ffn_embed_dim", 4096) args.sent_loss = getattr(args, "sent_loss", False) args.activation_fn = getattr(args, "activation_fn", "gelu") args.encoder_normalize_before = getattr(args, "encoder_normalize_before", False) args.pooler_activation_fn = getattr(args, "pooler_activation_fn", "tanh") args.apply_bert_init = getattr(args, "apply_bert_init", True) base_architecture(args)
COCO-LM/fairseq/fairseq/models/masked_lm.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/models/masked_lm.py", "repo_id": "COCO-LM", "token_count": 6742 }
177
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. """ RoBERTa: A Robustly Optimized BERT Pretraining Approach. """ import logging import torch import torch.nn as nn import torch.nn.functional as F from fairseq import utils from fairseq.models import ( FairseqEncoder, FairseqEncoderModel, register_model, register_model_architecture, ) from fairseq.models.transformer import DEFAULT_MIN_PARAMS_TO_WRAP, TransformerEncoder from fairseq.modules import LayerNorm from fairseq.modules.quant_noise import quant_noise as apply_quant_noise_ from fairseq.modules.transformer_sentence_encoder import init_bert_params from .hub_interface import RobertaHubInterface logger = logging.getLogger(__name__) @register_model("roberta") class RobertaModel(FairseqEncoderModel): @classmethod def hub_models(cls): return { "roberta.base": "http://dl.fbaipublicfiles.com/fairseq/models/roberta.base.tar.gz", "roberta.large": "http://dl.fbaipublicfiles.com/fairseq/models/roberta.large.tar.gz", "roberta.large.mnli": "http://dl.fbaipublicfiles.com/fairseq/models/roberta.large.mnli.tar.gz", "roberta.large.wsc": "http://dl.fbaipublicfiles.com/fairseq/models/roberta.large.wsc.tar.gz", } def __init__(self, args, encoder): super().__init__(encoder) self.args = args # We follow BERT's random weight initialization self.apply(init_bert_params) self.classification_heads = nn.ModuleDict() @staticmethod def add_args(parser): """Add model-specific arguments to the parser.""" parser.add_argument( "--encoder-layers", type=int, metavar="L", help="num encoder layers" ) parser.add_argument( "--encoder-embed-dim", type=int, metavar="H", help="encoder embedding dimension", ) parser.add_argument( "--encoder-ffn-embed-dim", type=int, metavar="F", help="encoder embedding dimension for FFN", ) parser.add_argument( "--encoder-attention-heads", type=int, metavar="A", help="num encoder attention heads", ) parser.add_argument( "--activation-fn", choices=utils.get_available_activation_fns(), help="activation function to use", ) parser.add_argument( "--pooler-activation-fn", choices=utils.get_available_activation_fns(), help="activation function to use for pooler layer", ) parser.add_argument( "--encoder-normalize-before", action="store_true", help="apply layernorm before each encoder block", ) parser.add_argument( "--layernorm-embedding", action="store_true", help="add layernorm to embedding", ) parser.add_argument( "--dropout", type=float, metavar="D", help="dropout probability" ) parser.add_argument( "--attention-dropout", type=float, metavar="D", help="dropout probability for attention weights", ) parser.add_argument( "--activation-dropout", type=float, metavar="D", help="dropout probability after activation in FFN", ) parser.add_argument( "--pooler-dropout", type=float, metavar="D", help="dropout probability in the masked_lm pooler layers", ) parser.add_argument( "--max-positions", type=int, help="number of positional embeddings to learn" ) parser.add_argument( "--load-checkpoint-heads", action="store_true", help="(re-)register and load heads when loading checkpoints", ) parser.add_argument( "--untie-weights-roberta", action="store_true", help="Untie weights between embeddings and classifiers in RoBERTa", ) # args for "Reducing Transformer Depth on Demand with Structured Dropout" (Fan et al., 2019) parser.add_argument( "--encoder-layerdrop", type=float, metavar="D", default=0, help="LayerDrop probability for encoder", ) parser.add_argument( "--encoder-layers-to-keep", default=None, help="which layers to *keep* when pruning as a comma-separated list", ) # args for Training with Quantization Noise for Extreme Model Compression ({Fan*, Stock*} et al., 2020) parser.add_argument( "--quant-noise-pq", type=float, metavar="D", default=0, help="iterative PQ quantization noise at training time", ) parser.add_argument( "--quant-noise-pq-block-size", type=int, metavar="D", default=8, help="block size of quantization noise at training time", ) parser.add_argument( "--quant-noise-scalar", type=float, metavar="D", default=0, help="scalar quantization noise and scalar quantization at training time", ) # args for "Better Fine-Tuning by Reducing Representational Collapse" (Aghajanyan et al. 2020) parser.add_argument( "--spectral-norm-classification-head", action="store_true", default=False, help="Apply spectral normalization on the classification head", ) # args for Fully Sharded Data Parallel (FSDP) training parser.add_argument( "--min-params-to-wrap", type=int, metavar="D", default=DEFAULT_MIN_PARAMS_TO_WRAP, help=( "minimum number of params for a layer to be wrapped with FSDP() when " "training with --ddp-backend=fully_sharded. Smaller values will " "improve memory efficiency, but may make torch.distributed " "communication less efficient due to smaller input sizes. This option " "is set to 0 (i.e., always wrap) when --checkpoint-activations or " "--offload-activations are passed." ) ) @classmethod def build_model(cls, args, task): """Build a new model instance.""" # make sure all arguments are present base_architecture(args) if not hasattr(args, "max_positions"): args.max_positions = args.tokens_per_sample encoder = RobertaEncoder(args, task.source_dictionary) return cls(args, encoder) def forward( self, src_tokens, features_only=False, return_all_hiddens=False, classification_head_name=None, **kwargs ): if classification_head_name is not None: features_only = True x, extra = self.encoder(src_tokens, features_only, return_all_hiddens, **kwargs) if classification_head_name is not None: x = self.classification_heads[classification_head_name](x) return x, extra def get_normalized_probs(self, net_output, log_probs, sample=None): """Get normalized probabilities (or log probs) from a net's output.""" logits = net_output[0].float() if log_probs: return F.log_softmax(logits, dim=-1) else: return F.softmax(logits, dim=-1) def register_classification_head( self, name, num_classes=None, inner_dim=None, **kwargs ): """Register a classification head.""" if name in self.classification_heads: prev_num_classes = self.classification_heads[name].out_proj.out_features prev_inner_dim = self.classification_heads[name].dense.out_features if num_classes != prev_num_classes or inner_dim != prev_inner_dim: logger.warning( 're-registering head "{}" with num_classes {} (prev: {}) ' "and inner_dim {} (prev: {})".format( name, num_classes, prev_num_classes, inner_dim, prev_inner_dim ) ) self.classification_heads[name] = RobertaClassificationHead( input_dim=self.args.encoder_embed_dim, inner_dim=inner_dim or self.args.encoder_embed_dim, num_classes=num_classes, activation_fn=self.args.pooler_activation_fn, pooler_dropout=self.args.pooler_dropout, q_noise=self.args.quant_noise_pq, qn_block_size=self.args.quant_noise_pq_block_size, do_spectral_norm=self.args.spectral_norm_classification_head, ) @property def supported_targets(self): return {"self"} @classmethod def from_pretrained( cls, model_name_or_path, checkpoint_file="model.pt", data_name_or_path=".", bpe="gpt2", **kwargs ): from fairseq import hub_utils x = hub_utils.from_pretrained( model_name_or_path, checkpoint_file, data_name_or_path, archive_map=cls.hub_models(), bpe=bpe, load_checkpoint_heads=True, **kwargs, ) logger.info(x["args"]) return RobertaHubInterface(x["args"], x["task"], x["models"][0]) def upgrade_state_dict_named(self, state_dict, name): prefix = name + "." if name != "" else "" # rename decoder -> encoder before upgrading children modules for k in list(state_dict.keys()): if k.startswith(prefix + "decoder"): new_k = prefix + "encoder" + k[len(prefix + "decoder") :] state_dict[new_k] = state_dict[k] del state_dict[k] # rename emb_layer_norm -> layernorm_embedding for k in list(state_dict.keys()): if ".emb_layer_norm." in k: new_k = k.replace(".emb_layer_norm.", ".layernorm_embedding.") state_dict[new_k] = state_dict[k] del state_dict[k] # upgrade children modules super().upgrade_state_dict_named(state_dict, name) # Handle new classification heads present in the state dict. current_head_names = ( [] if not hasattr(self, "classification_heads") else self.classification_heads.keys() ) keys_to_delete = [] for k in state_dict.keys(): if not k.startswith(prefix + "classification_heads."): continue head_name = k[len(prefix + "classification_heads.") :].split(".")[0] num_classes = state_dict[ prefix + "classification_heads." + head_name + ".out_proj.weight" ].size(0) inner_dim = state_dict[ prefix + "classification_heads." + head_name + ".dense.weight" ].size(0) if getattr(self.args, "load_checkpoint_heads", False): if head_name not in current_head_names: self.register_classification_head(head_name, num_classes, inner_dim) else: if head_name not in current_head_names: logger.warning( "deleting classification head ({}) from checkpoint " "not present in current model: {}".format(head_name, k) ) keys_to_delete.append(k) elif ( num_classes != self.classification_heads[head_name].out_proj.out_features or inner_dim != self.classification_heads[head_name].dense.out_features ): logger.warning( "deleting classification head ({}) from checkpoint " "with different dimensions than current model: {}".format( head_name, k ) ) keys_to_delete.append(k) for k in keys_to_delete: del state_dict[k] # Copy any newly-added classification heads into the state dict # with their current weights. if hasattr(self, "classification_heads"): cur_state = self.classification_heads.state_dict() for k, v in cur_state.items(): if prefix + "classification_heads." + k not in state_dict: logger.info("Overwriting " + prefix + "classification_heads." + k) state_dict[prefix + "classification_heads." + k] = v class RobertaLMHead(nn.Module): """Head for masked language modeling.""" def __init__(self, embed_dim, output_dim, activation_fn, weight=None): super().__init__() self.dense = nn.Linear(embed_dim, embed_dim) self.activation_fn = utils.get_activation_fn(activation_fn) self.layer_norm = LayerNorm(embed_dim) if weight is None: weight = nn.Linear(embed_dim, output_dim, bias=False).weight self.weight = weight self.bias = nn.Parameter(torch.zeros(output_dim)) def forward(self, features, masked_tokens=None, **kwargs): # Only project the masked tokens while training, # saves both memory and computation if masked_tokens is not None: features = features[masked_tokens, :] x = self.dense(features) x = self.activation_fn(x) x = self.layer_norm(x) # project back to size of vocabulary with bias x = F.linear(x, self.weight) + self.bias return x class RobertaClassificationHead(nn.Module): """Head for sentence-level classification tasks.""" def __init__( self, input_dim, inner_dim, num_classes, activation_fn, pooler_dropout, q_noise=0, qn_block_size=8, do_spectral_norm=False, ): super().__init__() self.dense = nn.Linear(input_dim, inner_dim) self.activation_fn = utils.get_activation_fn(activation_fn) self.dropout = nn.Dropout(p=pooler_dropout) self.out_proj = apply_quant_noise_( nn.Linear(inner_dim, num_classes), q_noise, qn_block_size ) if do_spectral_norm: if q_noise != 0: raise NotImplementedError( "Attempting to use Spectral Normalization with Quant Noise. This is not officially supported" ) self.out_proj = torch.nn.utils.spectral_norm(self.out_proj) def forward(self, features, **kwargs): x = features[:, 0, :] # take <s> token (equiv. to [CLS]) x = self.dropout(x) x = self.dense(x) x = self.activation_fn(x) x = self.dropout(x) x = self.out_proj(x) return x class RobertaEncoder(FairseqEncoder): """RoBERTa encoder.""" def __init__(self, args, dictionary): super().__init__(dictionary) # set any missing default values base_architecture(args) self.args = args if args.encoder_layers_to_keep: args.encoder_layers = len(args.encoder_layers_to_keep.split(",")) embed_tokens = self.build_embedding( len(dictionary), args.encoder_embed_dim, dictionary.pad() ) self.sentence_encoder = self.build_encoder(args, dictionary, embed_tokens) self.lm_head = self.build_lm_head( embed_dim=args.encoder_embed_dim, output_dim=len(dictionary), activation_fn=args.activation_fn, weight=( self.sentence_encoder.embed_tokens.weight if not args.untie_weights_roberta else None ), ) def build_embedding(self, vocab_size, embedding_dim, padding_idx): return nn.Embedding(vocab_size, embedding_dim, padding_idx) def build_encoder(self, args, dictionary, embed_tokens): encoder = TransformerEncoder(args, dictionary, embed_tokens) encoder.apply(init_bert_params) return encoder def build_lm_head(self, embed_dim, output_dim, activation_fn, weight): return RobertaLMHead(embed_dim, output_dim, activation_fn, weight) def forward( self, src_tokens, features_only=False, return_all_hiddens=False, masked_tokens=None, **unused ): """ Args: src_tokens (LongTensor): input tokens of shape `(batch, src_len)` features_only (bool, optional): skip LM head and just return features. If True, the output will be of shape `(batch, src_len, embed_dim)`. return_all_hiddens (bool, optional): also return all of the intermediate hidden states (default: False). Returns: tuple: - the LM output of shape `(batch, src_len, vocab)` - a dictionary of additional data, where 'inner_states' is a list of hidden states. Note that the hidden states have shape `(src_len, batch, vocab)`. """ x, extra = self.extract_features( src_tokens, return_all_hiddens=return_all_hiddens ) if not features_only: x = self.output_layer(x, masked_tokens=masked_tokens) return x, extra def extract_features(self, src_tokens, return_all_hiddens=False, **kwargs): encoder_out = self.sentence_encoder( src_tokens, return_all_hiddens=return_all_hiddens, token_embeddings=kwargs.get("token_embeddings", None), ) features = encoder_out["encoder_out"][0] inner_states = encoder_out["encoder_states"] if return_all_hiddens else None return features, {"inner_states": inner_states} def output_layer(self, features, masked_tokens=None, **unused): return self.lm_head(features, masked_tokens) def max_positions(self): """Maximum output length supported by the encoder.""" return self.args.max_positions @register_model_architecture("roberta", "roberta") def base_architecture(args): args.encoder_layers = getattr(args, "encoder_layers", 12) args.encoder_embed_dim = getattr(args, "encoder_embed_dim", 768) args.encoder_ffn_embed_dim = getattr(args, "encoder_ffn_embed_dim", 3072) args.encoder_attention_heads = getattr(args, "encoder_attention_heads", 12) args.dropout = getattr(args, "dropout", 0.1) args.attention_dropout = getattr(args, "attention_dropout", 0.1) args.activation_dropout = getattr(args, "activation_dropout", 0.0) args.pooler_dropout = getattr(args, "pooler_dropout", 0.0) args.max_source_positions = getattr(args, "max_positions", 512) args.no_token_positional_embeddings = getattr( args, "no_token_positional_embeddings", False ) # BERT has a few structural differences compared to the original Transformer args.encoder_learned_pos = getattr(args, "encoder_learned_pos", True) args.layernorm_embedding = getattr(args, "layernorm_embedding", True) args.no_scale_embedding = getattr(args, "no_scale_embedding", True) args.activation_fn = getattr(args, "activation_fn", "gelu") args.encoder_normalize_before = getattr(args, "encoder_normalize_before", False) args.pooler_activation_fn = getattr(args, "pooler_activation_fn", "tanh") args.untie_weights_roberta = getattr(args, "untie_weights_roberta", False) # Adaptive input config args.adaptive_input = getattr(args, "adaptive_input", False) # LayerDrop config args.encoder_layerdrop = getattr(args, "encoder_layerdrop", 0.0) args.encoder_layers_to_keep = getattr(args, "encoder_layers_to_keep", None) # Quantization noise config args.quant_noise_pq = getattr(args, "quant_noise_pq", 0) args.quant_noise_pq_block_size = getattr(args, "quant_noise_pq_block_size", 8) args.quant_noise_scalar = getattr(args, "quant_noise_scalar", 0) # R4F config args.spectral_norm_classification_head = getattr( args, "spectral_norm_classification_head", False ) @register_model_architecture("roberta", "roberta_prenorm") def roberta_prenorm_architecture(args): args.layernorm_embedding = getattr(args, "layernorm_embedding", False) args.encoder_normalize_before = getattr(args, "encoder_normalize_before", True) base_architecture(args) @register_model_architecture("roberta", "roberta_base") def roberta_base_architecture(args): base_architecture(args) @register_model_architecture("roberta", "roberta_large") def roberta_large_architecture(args): args.encoder_layers = getattr(args, "encoder_layers", 24) args.encoder_embed_dim = getattr(args, "encoder_embed_dim", 1024) args.encoder_ffn_embed_dim = getattr(args, "encoder_ffn_embed_dim", 4096) args.encoder_attention_heads = getattr(args, "encoder_attention_heads", 16) base_architecture(args) @register_model_architecture("roberta", "xlm") def xlm_architecture(args): args.encoder_layers = getattr(args, "encoder_layers", 16) args.encoder_embed_dim = getattr(args, "encoder_embed_dim", 1280) args.encoder_ffn_embed_dim = getattr(args, "encoder_ffn_embed_dim", 1280 * 4) args.encoder_attention_heads = getattr(args, "encoder_attention_heads", 16) base_architecture(args)
COCO-LM/fairseq/fairseq/models/roberta/model.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/models/roberta/model.py", "repo_id": "COCO-LM", "token_count": 10143 }
178
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. from dataclasses import dataclass, field from typing import Optional from fairseq import options, utils from fairseq.dataclass import ChoiceEnum, FairseqDataclass from fairseq.models import ( FairseqLanguageModel, register_model, register_model_architecture, ) from fairseq.models.transformer import ( DEFAULT_MIN_PARAMS_TO_WRAP, Embedding, TransformerDecoder ) from fairseq.modules import AdaptiveInput, CharacterTokenEmbedder from omegaconf import II DEFAULT_MAX_TARGET_POSITIONS = 1024 @dataclass class TransformerLanguageModelConfig(FairseqDataclass): activation_fn: ChoiceEnum(utils.get_available_activation_fns()) = field( default="relu", metadata={"help": "activation function to use"} ) dropout: float = field(default=0.1, metadata={"help": "dropout probability"}) attention_dropout: float = field( default=0.0, metadata={"help": "dropout probability for attention weights"} ) activation_dropout: float = field( default=0.0, metadata={"help": "dropout probability after activation in FFN."} ) relu_dropout: float = field( default=0.0, metadata={"help": "dropout probability after activation in FFN."} ) decoder_embed_dim: int = field( default=512, metadata={"help": "decoder embedding dimension"} ) decoder_output_dim: int = field( default=512, metadata={"help": "decoder output dimension"} ) decoder_input_dim: int = field( default=512, metadata={"help": "decoder input dimension"} ) decoder_ffn_embed_dim: int = field( default=2048, metadata={"help": "decoder embedding dimension for FFN"} ) decoder_layers: int = field(default=6, metadata={"help": "num decoder layers"}) decoder_attention_heads: int = field( default=8, metadata={"help": "num decoder attention heads"} ) decoder_normalize_before: bool = field( default=False, metadata={"help": "apply layernorm before each decoder block"} ) no_decoder_final_norm: bool = field( default=False, metadata={"help": "don't add an extra layernorm after the last decoder block"}, ) adaptive_softmax_cutoff: Optional[str] = field( default=None, metadata={ "help": "comma separated list of adaptive softmax cutoff points. " "Must be used with adaptive_loss criterion" }, ) adaptive_softmax_dropout: float = field( default=0, metadata={"help": "sets adaptive softmax dropout for the tail projections"}, ) adaptive_softmax_factor: float = field( default=4, metadata={"help": "adaptive input factor"} ) no_token_positional_embeddings: bool = field( default=False, metadata={ "help": "if set, disables positional embeddings (outside self attention)" }, ) share_decoder_input_output_embed: bool = field( default=False, metadata={"help": "share decoder input and output embeddings"} ) character_embeddings: bool = field( default=False, metadata={ "help": "if set, uses character embedding convolutions to produce token embeddings" }, ) character_filters: str = field( default="[(1, 64), (2, 128), (3, 192), (4, 256), (5, 256), (6, 256), (7, 256)]", metadata={"help": "size of character embeddings"}, ) character_embedding_dim: int = field( default=4, metadata={"help": "size of character embeddings"} ) char_embedder_highway_layers: int = field( default=2, metadata={"help": "number of highway layers for character token embeddder"}, ) adaptive_input: bool = field( default=False, metadata={"help": "if set, uses adaptive input"} ) adaptive_input_factor: float = field( default=4, metadata={"help": "adaptive input factor"} ) adaptive_input_cutoff: Optional[str] = field( default=None, metadata={"help": "comma separated list of adaptive input cutoff points."}, ) tie_adaptive_weights: bool = field( default=False, metadata={ "help": "if set, ties the weights of adaptive softmax and adaptive input" }, ) tie_adaptive_proj: bool = field( default=False, metadata={ "help": "if set, ties the projection weights of adaptive softmax and adaptive input" }, ) decoder_learned_pos: bool = field( default=False, metadata={"help": "use learned positional embeddings in the decoder"}, ) layernorm_embedding: bool = field( default=False, metadata={"help": "add layernorm to embedding"} ) no_scale_embedding: bool = field( default=False, metadata={"help": "if True, dont scale embeddings"} ) checkpoint_activations: bool = field( default=False, metadata={"help": "checkpoint activations at each layer"} ) offload_activations: bool = field( default=False, metadata={"help": "move checkpointed activations to CPU after they are used."}, ) # config for "Reducing Transformer Depth on Demand with Structured Dropout" (Fan et al., 2019) decoder_layerdrop: float = field( default=0.0, metadata={"help": "LayerDrop probability for decoder"} ) decoder_layers_to_keep: Optional[str] = field( default=None, metadata={ "help": "which layers to *keep* when pruning as a comma-separated list" }, ) # config for Training with Quantization Noise for Extreme Model Compression ({Fan*, Stock*} et al., 2020) quant_noise_pq: float = field( default=0.0, metadata={"help": "iterative PQ quantization noise at training time"}, ) quant_noise_pq_block_size: int = field( default=8, metadata={"help": "block size of quantization noise at training time"}, ) quant_noise_scalar: float = field( default=0.0, metadata={ "help": "scalar quantization noise and scalar quantization at training time" }, ) # config for Fully Sharded Data Parallel (FSDP) training min_params_to_wrap: int = field( default=DEFAULT_MIN_PARAMS_TO_WRAP, metadata={ "help": ( "minimum number of params for a layer to be wrapped with FSDP() when " "training with --ddp-backend=fully_sharded. Smaller values will " "improve memory efficiency, but may make torch.distributed " "communication less efficient due to smaller input sizes. This option " "is set to 0 (i.e., always wrap) when --checkpoint-activations or " "--offload-activations are passed." ) } ) # options from other parts of the config add_bos_token: bool = II("task.add_bos_token") tokens_per_sample: int = II("task.tokens_per_sample") max_target_positions: Optional[int] = II("task.max_target_positions") tpu: bool = II("common.tpu") @register_model("transformer_lm", dataclass=TransformerLanguageModelConfig) class TransformerLanguageModel(FairseqLanguageModel): @classmethod def hub_models(cls): def moses_fastbpe(path): return {"path": path, "tokenizer": "moses", "bpe": "fastbpe"} def spm(path): return {"path": path, "tokenizer": "space", "bpe": "sentencepiece"} return { "transformer_lm.gbw.adaptive_huge": "https://dl.fbaipublicfiles.com/fairseq/models/lm/adaptive_lm_gbw_huge.tar.bz2", "transformer_lm.wiki103.adaptive": "https://dl.fbaipublicfiles.com/fairseq/models/lm/adaptive_lm_wiki103.v2.tar.bz2", "transformer_lm.wmt19.en": moses_fastbpe( "https://dl.fbaipublicfiles.com/fairseq/models/lm/wmt19.en.tar.bz2" ), "transformer_lm.wmt19.de": moses_fastbpe( "https://dl.fbaipublicfiles.com/fairseq/models/lm/wmt19.de.tar.bz2" ), "transformer_lm.wmt19.ru": moses_fastbpe( "https://dl.fbaipublicfiles.com/fairseq/models/lm/wmt19.ru.tar.bz2" ), "transformer_lm.wmt20.en": spm( "https://dl.fbaipublicfiles.com/fairseq/models/lm/wmt20.en.tar.gz" ), "transformer_lm.wmt20.ta": spm( "https://dl.fbaipublicfiles.com/fairseq/models/lm/wmt20.ta.tar.gz" ), "transformer_lm.wmt20.iu.news": spm( "https://dl.fbaipublicfiles.com/fairseq/models/lm/wmt20.iu.news.tar.gz" ), "transformer_lm.wmt20.iu.nh": spm( "https://dl.fbaipublicfiles.com/fairseq/models/lm/wmt20.iu.nh.tar.gz" ), } def __init__(self, decoder): super().__init__(decoder) @classmethod def build_model(cls, args, task): """Build a new model instance.""" if args.decoder_layers_to_keep: args.decoder_layers = len(args.decoder_layers_to_keep.split(",")) if getattr(args, "max_target_positions", None) is None: args.max_target_positions = getattr( args, "tokens_per_sample", DEFAULT_MAX_TARGET_POSITIONS ) if args.character_embeddings: embed_tokens = CharacterTokenEmbedder( task.source_dictionary, eval(args.character_filters), args.character_embedding_dim, args.decoder_embed_dim, args.char_embedder_highway_layers, ) elif args.adaptive_input: embed_tokens = AdaptiveInput( len(task.source_dictionary), task.source_dictionary.pad(), args.decoder_input_dim, args.adaptive_input_factor, args.decoder_embed_dim, options.eval_str_list(args.adaptive_input_cutoff, type=int), args.quant_noise_pq, args.quant_noise_pq_block_size, ) else: embed_tokens = cls.build_embedding( args, task.source_dictionary, args.decoder_input_dim ) if args.tie_adaptive_weights: assert args.adaptive_input assert args.adaptive_input_factor == args.adaptive_softmax_factor assert ( args.adaptive_softmax_cutoff == args.adaptive_input_cutoff ), "{} != {}".format( args.adaptive_softmax_cutoff, args.adaptive_input_cutoff ) assert args.decoder_input_dim == args.decoder_output_dim decoder = TransformerDecoder( args, task.target_dictionary, embed_tokens, no_encoder_attn=True ) return cls(decoder) @classmethod def build_embedding(cls, args, dictionary, embed_dim, path=None): embed_tokens = Embedding(len(dictionary), embed_dim, dictionary.pad()) return embed_tokens def base_lm_architecture(args): # backward compatibility for older model checkpoints if hasattr(args, "no_tie_adaptive_proj"): # previous models defined --no-tie-adaptive-proj, so use the existence of # that option to determine if this is an "old" model checkpoint args.no_decoder_final_norm = True # old models always set this to True if args.no_tie_adaptive_proj is False: args.tie_adaptive_proj = True if hasattr(args, "decoder_final_norm"): args.no_decoder_final_norm = not args.decoder_final_norm args.dropout = getattr(args, "dropout", 0.1) args.attention_dropout = getattr(args, "attention_dropout", 0.0) args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 512) args.decoder_ffn_embed_dim = getattr(args, "decoder_ffn_embed_dim", 2048) args.decoder_layers = getattr(args, "decoder_layers", 6) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 8) args.adaptive_softmax_cutoff = getattr(args, "adaptive_softmax_cutoff", None) args.adaptive_softmax_dropout = getattr(args, "adaptive_softmax_dropout", 0) args.adaptive_softmax_factor = getattr(args, "adaptive_softmax_factor", 4) args.decoder_learned_pos = getattr(args, "decoder_learned_pos", False) args.activation_fn = getattr(args, "activation_fn", "relu") args.decoder_layerdrop = getattr(args, "decoder_layerdrop", 0) args.decoder_layers_to_keep = getattr(args, "decoder_layers_to_keep", None) args.quant_noise_pq = getattr(args, "quant_noise_pq", 0) args.quant_noise_pq_block_size = getattr(args, "quant_noise_pq_block_size", 8) args.quant_noise_scalar = getattr(args, "quant_noise_scalar", 0) args.add_bos_token = getattr(args, "add_bos_token", False) args.no_token_positional_embeddings = getattr( args, "no_token_positional_embeddings", False ) args.share_decoder_input_output_embed = getattr( args, "share_decoder_input_output_embed", False ) args.character_embeddings = getattr(args, "character_embeddings", False) args.decoder_output_dim = getattr( args, "decoder_output_dim", args.decoder_embed_dim ) args.decoder_input_dim = getattr(args, "decoder_input_dim", args.decoder_embed_dim) # Model training is not stable without this args.decoder_normalize_before = True args.no_decoder_final_norm = getattr(args, "no_decoder_final_norm", False) args.adaptive_input = getattr(args, "adaptive_input", False) args.adaptive_input_factor = getattr(args, "adaptive_input_factor", 4) args.adaptive_input_cutoff = getattr(args, "adaptive_input_cutoff", None) args.tie_adaptive_weights = getattr(args, "tie_adaptive_weights", False) args.tie_adaptive_proj = getattr(args, "tie_adaptive_proj", False) args.no_scale_embedding = getattr(args, "no_scale_embedding", False) args.layernorm_embedding = getattr(args, "layernorm_embedding", False) args.checkpoint_activations = getattr(args, "checkpoint_activations", False) args.offload_activations = getattr(args, "offload_activations", False) if args.offload_activations: args.checkpoint_activations = True @register_model_architecture("transformer_lm", "transformer_lm_big") def transformer_lm_big(args): args.decoder_layers = getattr(args, "decoder_layers", 12) args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 1024) args.decoder_ffn_embed_dim = getattr(args, "decoder_ffn_embed_dim", 4096) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 16) base_lm_architecture(args) @register_model_architecture("transformer_lm", "transformer_lm_wiki103") @register_model_architecture("transformer_lm", "transformer_lm_baevski_wiki103") def transformer_lm_baevski_wiki103(args): args.decoder_layers = getattr(args, "decoder_layers", 16) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 8) args.dropout = getattr(args, "dropout", 0.3) args.adaptive_input = getattr(args, "adaptive_input", True) args.tie_adaptive_weights = getattr(args, "tie_adaptive_weights", True) args.adaptive_input_cutoff = getattr(args, "adaptive_input_cutoff", "20000,60000") args.adaptive_softmax_cutoff = getattr( args, "adaptive_softmax_cutoff", "20000,60000" ) args.adaptive_softmax_dropout = getattr(args, "adaptive_softmax_dropout", 0.2) args.attention_dropout = getattr(args, "attention_dropout", 0.1) args.activation_dropout = getattr(args, "activation_dropout", 0.1) args.no_decoder_final_norm = getattr(args, "no_decoder_final_norm", True) args.tie_adaptive_proj = getattr(args, "tie_adaptive_proj", True) transformer_lm_big(args) @register_model_architecture("transformer_lm", "transformer_lm_gbw") @register_model_architecture("transformer_lm", "transformer_lm_baevski_gbw") def transformer_lm_baevski_gbw(args): args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 512) args.dropout = getattr(args, "dropout", 0.1) args.attention_dropout = getattr(args, "attention_dropout", 0.1) args.no_decoder_final_norm = getattr(args, "no_decoder_final_norm", True) transformer_lm_big(args) @register_model_architecture("transformer_lm", "transformer_lm_gpt") def transformer_lm_gpt(args): args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 768) args.decoder_ffn_embed_dim = getattr(args, "decoder_ffn_embed_dim", 3072) args.decoder_layers = getattr(args, "decoder_layers", 12) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 12) args.dropout = getattr(args, "dropout", 0.1) args.attention_dropout = getattr(args, "attention_dropout", 0.1) args.activation_fn = getattr(args, "activation_fn", "gelu") base_lm_architecture(args) @register_model_architecture("transformer_lm", "transformer_lm_gpt2_small") def transformer_lm_gpt2_small(args): args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 1024) args.decoder_ffn_embed_dim = getattr(args, "decoder_ffn_embed_dim", 4096) args.decoder_layers = getattr(args, "decoder_layers", 24) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 16) args.dropout = getattr(args, "dropout", 0.1) args.attention_dropout = getattr(args, "attention_dropout", 0.1) args.activation_fn = getattr(args, "activation_fn", "gelu") base_lm_architecture(args) @register_model_architecture("transformer_lm", "transformer_lm_gpt2_tiny") def transformer_lm_gpt2_tiny(args): args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 64) args.decoder_ffn_embed_dim = getattr(args, "decoder_ffn_embed_dim", 64) args.decoder_layers = getattr(args, "decoder_layers", 2) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 1) args.dropout = getattr(args, "dropout", 0.1) args.attention_dropout = getattr(args, "attention_dropout", 0.1) args.activation_fn = getattr(args, "activation_fn", "gelu") base_lm_architecture(args) @register_model_architecture("transformer_lm", "transformer_lm_gpt2_medium") def transformer_lm_gpt2_medium(args): args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 1280) args.decoder_ffn_embed_dim = getattr(args, "decoder_ffn_embed_dim", 5120) args.decoder_layers = getattr(args, "decoder_layers", 36) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 20) args.dropout = getattr(args, "dropout", 0.1) args.attention_dropout = getattr(args, "attention_dropout", 0.1) args.activation_fn = getattr(args, "activation_fn", "gelu") base_lm_architecture(args) @register_model_architecture("transformer_lm", "transformer_lm_gpt2_big") def transformer_lm_gpt2_big(args): args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 1600) args.decoder_ffn_embed_dim = getattr(args, "decoder_ffn_embed_dim", 6400) args.decoder_layers = getattr(args, "decoder_layers", 48) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 25) args.dropout = getattr(args, "dropout", 0.1) args.attention_dropout = getattr(args, "attention_dropout", 0.1) args.activation_fn = getattr(args, "activation_fn", "gelu") base_lm_architecture(args) def base_gpt3_architecture(args): args.decoder_input_dim = args.decoder_embed_dim args.decoder_output_dim = args.decoder_embed_dim args.decoder_ffn_embed_dim = getattr(args, "decoder_ffn_embed_dim", args.decoder_embed_dim * 4) # GPT-3 used learned positional embeddings, rather than sinusoidal args.decoder_learned_pos = getattr(args, "decoder_learned_pos", True) args.dropout = getattr(args, "dropout", 0.0) args.attention_dropout = getattr(args, "attention_dropout", 0.0) args.activation_fn = getattr(args, "activation_fn", "gelu") args.share_decoder_input_output_embed = True base_lm_architecture(args) @register_model_architecture("transformer_lm", "transformer_lm_gpt3_small") def transformer_lm_gpt3_small(args): # 125M params args.decoder_layers = getattr(args, "decoder_layers", 12) args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 768) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 12) base_gpt3_architecture(args) @register_model_architecture("transformer_lm", "transformer_lm_gpt3_medium") def transformer_lm_gpt3_medium(args): # 350M params args.decoder_layers = getattr(args, "decoder_layers", 24) args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 1024) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 16) base_gpt3_architecture(args) @register_model_architecture("transformer_lm", "transformer_lm_gpt3_large") def transformer_lm_gpt3_large(args): # 760M params args.decoder_layers = getattr(args, "decoder_layers", 24) args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 1536) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 16) base_gpt3_architecture(args) @register_model_architecture("transformer_lm", "transformer_lm_gpt3_xl") def transformer_lm_gpt3_xl(args): # 1.3B params args.decoder_layers = getattr(args, "decoder_layers", 24) args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 2048) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 32) base_gpt3_architecture(args) @register_model_architecture("transformer_lm", "transformer_lm_gpt3_2_7") def transformer_lm_gpt3_2_7(args): # 2.7B params args.decoder_layers = getattr(args, "decoder_layers", 32) args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 2560) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 32) base_gpt3_architecture(args) @register_model_architecture("transformer_lm", "transformer_lm_gpt3_6_7") def transformer_lm_gpt3_6_7(args): # 6.7B params args.decoder_layers = getattr(args, "decoder_layers", 32) args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 4096) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 32) base_gpt3_architecture(args) @register_model_architecture("transformer_lm", "transformer_lm_gpt3_13") def transformer_lm_gpt3_13(args): # 13B params args.decoder_layers = getattr(args, "decoder_layers", 40) args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 5120) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 40) base_gpt3_architecture(args) @register_model_architecture("transformer_lm", "transformer_lm_gpt3_175") def transformer_lm_gpt3_175(args): # 175B params args.decoder_layers = getattr(args, "decoder_layers", 96) args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 12288) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 96) base_gpt3_architecture(args)
COCO-LM/fairseq/fairseq/models/transformer_lm.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/models/transformer_lm.py", "repo_id": "COCO-LM", "token_count": 9716 }
179
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. """ This file is to re-implemented the low-rank and beam approximation of CRF layer Proposed by: Sun, Zhiqing, et al. Fast Structured Decoding for Sequence Models https://arxiv.org/abs/1910.11555 The CRF implementation is mainly borrowed from https://github.com/kmkurn/pytorch-crf/blob/master/torchcrf/__init__.py """ import numpy as np import torch import torch.nn as nn def logsumexp(x, dim=1): return torch.logsumexp(x.float(), dim=dim).type_as(x) class DynamicCRF(nn.Module): """Dynamic CRF layer is used to approximate the traditional Conditional Random Fields (CRF) $P(y | x) = 1/Z(x) exp(sum_i s(y_i, x) + sum_i t(y_{i-1}, y_i, x))$ where in this function, we assume the emition scores (s) are given, and the transition score is a |V| x |V| matrix $M$ in the following two aspects: (1) it used a low-rank approximation for the transition matrix: $M = E_1 E_2^T$ (2) it used a beam to estimate the normalizing factor Z(x) """ def __init__(self, num_embedding, low_rank=32, beam_size=64): super().__init__() self.E1 = nn.Embedding(num_embedding, low_rank) self.E2 = nn.Embedding(num_embedding, low_rank) self.vocb = num_embedding self.rank = low_rank self.beam = beam_size def extra_repr(self): return "vocab_size={}, low_rank={}, beam_size={}".format( self.vocb, self.rank, self.beam ) def forward(self, emissions, targets, masks, beam=None): """ Compute the conditional log-likelihood of a sequence of target tokens given emission scores Args: emissions (`~torch.Tensor`): Emission score are usually the unnormalized decoder output ``(batch_size, seq_len, vocab_size)``. We assume batch-first targets (`~torch.LongTensor`): Sequence of target token indices ``(batch_size, seq_len) masks (`~torch.ByteTensor`): Mask tensor with the same size as targets Returns: `~torch.Tensor`: approximated log-likelihood """ numerator = self._compute_score(emissions, targets, masks) denominator = self._compute_normalizer(emissions, targets, masks, beam) return numerator - denominator def forward_decoder(self, emissions, masks=None, beam=None): """ Find the most likely output sequence using Viterbi algorithm. Args: emissions (`~torch.Tensor`): Emission score are usually the unnormalized decoder output ``(batch_size, seq_len, vocab_size)``. We assume batch-first masks (`~torch.ByteTensor`): Mask tensor with the same size as targets Returns: `~torch.LongTensor`: decoded sequence from the CRF model """ return self._viterbi_decode(emissions, masks, beam) def _compute_score(self, emissions, targets, masks=None): batch_size, seq_len = targets.size() emission_scores = emissions.gather(2, targets[:, :, None])[:, :, 0] # B x T transition_scores = (self.E1(targets[:, :-1]) * self.E2(targets[:, 1:])).sum(2) scores = emission_scores scores[:, 1:] += transition_scores if masks is not None: scores = scores * masks.type_as(scores) return scores.sum(-1) def _compute_normalizer(self, emissions, targets=None, masks=None, beam=None): # HACK: we include "target" which is a hueristic for training # HACK: we use a beam of tokens to approximate the normalizing factor (which is bad?) beam = beam if beam is not None else self.beam batch_size, seq_len = emissions.size()[:2] if targets is not None: _emissions = emissions.scatter(2, targets[:, :, None], np.float("inf")) beam_targets = _emissions.topk(beam, 2)[1] beam_emission_scores = emissions.gather(2, beam_targets) else: beam_emission_scores, beam_targets = emissions.topk(beam, 2) beam_transition_score1 = self.E1(beam_targets[:, :-1]) # B x (T-1) x K x D beam_transition_score2 = self.E2(beam_targets[:, 1:]) # B x (T-1) x K x D beam_transition_matrix = torch.bmm( beam_transition_score1.view(-1, beam, self.rank), beam_transition_score2.view(-1, beam, self.rank).transpose(1, 2), ) beam_transition_matrix = beam_transition_matrix.view(batch_size, -1, beam, beam) # compute the normalizer in the log-space score = beam_emission_scores[:, 0] # B x K for i in range(1, seq_len): next_score = score[:, :, None] + beam_transition_matrix[:, i - 1] next_score = logsumexp(next_score, dim=1) + beam_emission_scores[:, i] if masks is not None: score = torch.where(masks[:, i : i + 1], next_score, score) else: score = next_score # Sum (log-sum-exp) over all possible tags return logsumexp(score, dim=1) def _viterbi_decode(self, emissions, masks=None, beam=None): # HACK: we use a beam of tokens to approximate the normalizing factor (which is bad?) beam = beam if beam is not None else self.beam batch_size, seq_len = emissions.size()[:2] beam_emission_scores, beam_targets = emissions.topk(beam, 2) beam_transition_score1 = self.E1(beam_targets[:, :-1]) # B x (T-1) x K x D beam_transition_score2 = self.E2(beam_targets[:, 1:]) # B x (T-1) x K x D beam_transition_matrix = torch.bmm( beam_transition_score1.view(-1, beam, self.rank), beam_transition_score2.view(-1, beam, self.rank).transpose(1, 2), ) beam_transition_matrix = beam_transition_matrix.view(batch_size, -1, beam, beam) traj_tokens, traj_scores = [], [] finalized_tokens, finalized_scores = [], [] # compute the normalizer in the log-space score = beam_emission_scores[:, 0] # B x K dummy = ( torch.arange(beam, device=score.device).expand(*score.size()).contiguous() ) for i in range(1, seq_len): traj_scores.append(score) _score = score[:, :, None] + beam_transition_matrix[:, i - 1] _score, _index = _score.max(dim=1) _score = _score + beam_emission_scores[:, i] if masks is not None: score = torch.where(masks[:, i : i + 1], _score, score) index = torch.where(masks[:, i : i + 1], _index, dummy) else: score, index = _score, _index traj_tokens.append(index) # now running the back-tracing and find the best best_score, best_index = score.max(dim=1) finalized_tokens.append(best_index[:, None]) finalized_scores.append(best_score[:, None]) for idx, scs in zip(reversed(traj_tokens), reversed(traj_scores)): previous_index = finalized_tokens[-1] finalized_tokens.append(idx.gather(1, previous_index)) finalized_scores.append(scs.gather(1, previous_index)) finalized_tokens.reverse() finalized_tokens = torch.cat(finalized_tokens, 1) finalized_tokens = beam_targets.gather(2, finalized_tokens[:, :, None])[:, :, 0] finalized_scores.reverse() finalized_scores = torch.cat(finalized_scores, 1) finalized_scores[:, 1:] = finalized_scores[:, 1:] - finalized_scores[:, :-1] return finalized_scores, finalized_tokens
COCO-LM/fairseq/fairseq/modules/dynamic_crf_layer.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/modules/dynamic_crf_layer.py", "repo_id": "COCO-LM", "token_count": 3388 }
180
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import torch import torch.nn as nn import torch.nn.functional as F try: from fused_ops.layernorm import FusedLayerNorm as _FusedLayerNorm has_fused_layernorm = True class FusedLayerNorm(_FusedLayerNorm): @torch.jit.unused def forward(self, x): if not x.is_cuda: return super().forward(x) else: with torch.cuda.device(x.device): return super().forward(x) except ImportError: has_fused_layernorm = False def LayerNorm(normalized_shape, eps=1e-5, elementwise_affine=True, export=False): if torch.jit.is_scripting(): export = True if not export and torch.cuda.is_available() and has_fused_layernorm: return FusedLayerNorm(normalized_shape, eps, elementwise_affine) return torch.nn.LayerNorm(normalized_shape, eps, elementwise_affine) class Fp32LayerNorm(nn.LayerNorm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def forward(self, input): output = F.layer_norm( input.float(), self.normalized_shape, self.weight.float() if self.weight is not None else None, self.bias.float() if self.bias is not None else None, self.eps, ) return output.type_as(input)
COCO-LM/fairseq/fairseq/modules/layer_norm.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/modules/layer_norm.py", "repo_id": "COCO-LM", "token_count": 636 }
181
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import logging import os import random from collections import Counter import torch class EM: """ EM algorithm used to quantize the columns of W to minimize ||W - W_hat||^2 Args: - W: weight matrix of size (in_features x out_features) - n_iter: number of k-means iterations - n_centroids: number of centroids (size of codebook) - eps: for cluster reassignment when an empty cluster is found - max_tentatives for cluster reassignment when an empty cluster is found - verbose: print error after each iteration Remarks: - If one cluster is empty, the most populated cluster is split into two clusters - All the relevant dimensions are specified in the code """ def __init__( self, W, n_centroids=256, n_iter=20, eps=1e-6, max_tentatives=30, verbose=True ): self.W = W self.n_centroids = n_centroids self.n_iter = n_iter self.eps = eps self.max_tentatives = max_tentatives self.verbose = verbose self.centroids = torch.Tensor() self.assignments = torch.Tensor() self.objective = [] def initialize_centroids(self): """ Initializes the centroids by sampling random columns from W. """ in_features, out_features = self.W.size() indices = torch.randint( low=0, high=out_features, size=(self.n_centroids,) ).long() self.centroids = self.W[:, indices].t() # (n_centroids x in_features) def step(self, i): """ There are two standard steps for each iteration: expectation (E) and minimization (M). The E-step (assignment) is performed with an exhaustive search and the M-step (centroid computation) is performed with the exact solution. Args: - i: step number Remarks: - The E-step heavily uses PyTorch broadcasting to speed up computations and reduce the memory overhead """ # assignments (E-step) distances = self.compute_distances() # (n_centroids x out_features) self.assignments = torch.argmin(distances, dim=0) # (out_features) n_empty_clusters = self.resolve_empty_clusters() # centroids (M-step) for k in range(self.n_centroids): W_k = self.W[:, self.assignments == k] # (in_features x size_of_cluster_k) self.centroids[k] = W_k.mean(dim=1) # (in_features) # book-keeping obj = (self.centroids[self.assignments].t() - self.W).norm(p=2).item() self.objective.append(obj) if self.verbose: logging.info( f"Iteration: {i},\t" f"objective: {obj:.6f},\t" f"resolved empty clusters: {n_empty_clusters}" ) def resolve_empty_clusters(self): """ If one cluster is empty, the most populated cluster is split into two clusters by shifting the respective centroids. This is done iteratively for a fixed number of tentatives. """ # empty clusters counts = Counter(map(lambda x: x.item(), self.assignments)) empty_clusters = set(range(self.n_centroids)) - set(counts.keys()) n_empty_clusters = len(empty_clusters) tentatives = 0 while len(empty_clusters) > 0: # given an empty cluster, find most populated cluster and split it into two k = random.choice(list(empty_clusters)) m = counts.most_common(1)[0][0] e = torch.randn_like(self.centroids[m]) * self.eps self.centroids[k] = self.centroids[m].clone() self.centroids[k] += e self.centroids[m] -= e # recompute assignments distances = self.compute_distances() # (n_centroids x out_features) self.assignments = torch.argmin(distances, dim=0) # (out_features) # check for empty clusters counts = Counter(map(lambda x: x.item(), self.assignments)) empty_clusters = set(range(self.n_centroids)) - set(counts.keys()) # increment tentatives if tentatives == self.max_tentatives: logging.info( f"Could not resolve all empty clusters, {len(empty_clusters)} remaining" ) raise EmptyClusterResolveError tentatives += 1 return n_empty_clusters def compute_distances(self): """ For every centroid m, computes ||M - m[None, :]||_2 Remarks: - We rely on PyTorch's broadcasting to speed up computations and reduce the memory overhead - Without chunking, the sizes in the broadcasting are modified as: (n_centroids x n_samples x out_features) -> (n_centroids x out_features) - The broadcasting computation is automatically chunked so that the tensors fit into the memory of the GPU """ nb_centroids_chunks = 1 while True: try: return torch.cat( [ (self.W[None, :, :] - centroids_c[:, :, None]).norm(p=2, dim=1) for centroids_c in self.centroids.chunk( nb_centroids_chunks, dim=0 ) ], dim=0, ) except RuntimeError: nb_centroids_chunks *= 2 def assign(self): """ Assigns each column of W to its closest centroid, thus essentially performing the E-step in train(). Remarks: - The function must be called after train() or after loading centroids using self.load(), otherwise it will return empty tensors """ distances = self.compute_distances() # (n_centroids x out_features) self.assignments = torch.argmin(distances, dim=0) # (out_features) def save(self, path, layer): """ Saves centroids and assignments. Args: - path: folder used to save centroids and assignments """ torch.save(self.centroids, os.path.join(path, "{}_centroids.pth".format(layer))) torch.save( self.assignments, os.path.join(path, "{}_assignments.pth".format(layer)) ) torch.save(self.objective, os.path.join(path, "{}_objective.pth".format(layer))) def load(self, path, layer): """ Loads centroids and assignments from a given path Args: - path: folder use to load centroids and assignments """ self.centroids = torch.load( os.path.join(path, "{}_centroids.pth".format(layer)) ) self.assignments = torch.load( os.path.join(path, "{}_assignments.pth".format(layer)) ) self.objective = torch.load( os.path.join(path, "{}_objective.pth".format(layer)) ) class EmptyClusterResolveError(Exception): pass
COCO-LM/fairseq/fairseq/modules/quantization/pq/em.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/modules/quantization/pq/em.py", "repo_id": "COCO-LM", "token_count": 3381 }
182
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. from torch import nn class SamePad(nn.Module): def __init__(self, kernel_size, causal=False): super().__init__() if causal: self.remove = kernel_size - 1 else: self.remove = 1 if kernel_size % 2 == 0 else 0 def forward(self, x): if self.remove > 0: x = x[:, :, : -self.remove] return x
COCO-LM/fairseq/fairseq/modules/same_pad.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/modules/same_pad.py", "repo_id": "COCO-LM", "token_count": 224 }
183
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import math import torch import torch.optim from . import LegacyFairseqOptimizer, register_optimizer @register_optimizer("adafactor") class FairseqAdafactor(LegacyFairseqOptimizer): def __init__(self, args, params): super().__init__(args) self._optimizer = Adafactor(params, **self.optimizer_config) @staticmethod def add_args(parser): """Add optimizer-specific arguments to the parser.""" # fmt: off parser.add_argument('--adafactor-eps', default='(1e-30, 1e-3)', metavar="E", help='epsilons for Adafactor optimizer') parser.add_argument('--clip-threshold', type=float, default=1.0, metavar="C", help='threshold for clipping update root mean square') parser.add_argument('--decay-rate', type=float, default=-0.8, metavar="D", help='decay rate of the second moment estimator') parser.add_argument('--beta1', type=float, default=None, metavar="B", help='beta for first moment estimator. Optional') parser.add_argument('--weight-decay', '--wd', default=0.0, type=float, metavar='WD', help='weight decay') parser.add_argument('--scale-parameter', action='store_true', help='scale learning rate by root mean square of parameter') parser.add_argument('--relative-step', action='store_true', help='set learning rate to inverse square root of timestep,' 'otherwise use external learning rate') parser.add_argument('--warmup-init', action='store_true', help='use relative step for warm-up learning rate schedule') # fmt: on @property def optimizer_config(self): """ Return a kwarg dictionary that will be used to override optimizer args stored in checkpoints. This allows us to load a checkpoint and resume training using a different set of optimizer args, e.g., with a different learning rate. Note : Convergence issues empirically observed with fp16 on. Might require search for appropriate configuration. """ return { "lr": self.args.lr[0], "eps": eval(self.args.adafactor_eps), "clip_threshold": self.args.clip_threshold, "decay_rate": self.args.decay_rate, "beta1": self.args.beta1, "weight_decay": self.args.weight_decay, "scale_parameter": self.args.scale_parameter, # defaults to False "relative_step": self.args.relative_step, # defaults to False "warmup_init": self.args.warmup_init, } class Adafactor(torch.optim.Optimizer): """Implements Adafactor algorithm. This implementation is based on: `Adafactor: Adaptive Learning Rates with Sublinear Memory Cost` (see https://arxiv.org/abs/1804.04235) Note that this optimizer internally adjusts the learning rate depending on the *scale_parameter*, *relative_step* and *warmup_init* options. To use a manual (external) learning rate schedule you should set `scale_parameter=False` and `relative_step=False`. Args: params (iterable): iterable of parameters to optimize or dicts defining parameter groups lr (float, optional): external learning rate (default: None) eps (tuple[float, float]): regularization constans for square gradient and parameter scale respectively (default: (1e-30, 1e-3)) clip_threshold (float): threshold of root mean square of final gradient update (default: 1.0) decay_rate (float): coefficient used to compute running averages of square gradient (default: -0.8) beta1 (float): coefficient used for computing running averages of gradient (default: None) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) scale_parameter (bool): if True, learning rate is scaled by root mean square of parameter (default: True) relative_step (bool): if True, time-dependent learning rate is computed instead of external learning rate (default: True) warmup_init (bool): time-dependent learning rate computation depends on whether warm-up initialization is being used (default: False) """ def __init__( self, params, lr=None, eps=(1e-30, 1e-3), clip_threshold=1.0, decay_rate=-0.8, beta1=None, weight_decay=0.0, scale_parameter=True, relative_step=True, warmup_init=False, ): if lr is not None and relative_step: raise ValueError("Cannot combine manual lr and relative_step options") if warmup_init and not relative_step: raise ValueError("warmup_init requires relative_step=True") defaults = dict( lr=lr, eps=eps, clip_threshold=clip_threshold, decay_rate=decay_rate, beta1=beta1, weight_decay=weight_decay, scale_parameter=scale_parameter, relative_step=relative_step, warmup_init=warmup_init, ) super(Adafactor, self).__init__(params, defaults) @property def supports_memory_efficient_fp16(self): return True @property def supports_flat_params(self): return False def _get_lr(self, param_group, param_state): rel_step_sz = param_group["lr"] if param_group["relative_step"]: min_step = ( 1e-6 * param_state["step"] if param_group["warmup_init"] else 1e-2 ) rel_step_sz = min(min_step, 1.0 / math.sqrt(param_state["step"])) param_scale = 1.0 if param_group["scale_parameter"]: param_scale = max(param_group["eps"][1], param_state["RMS"]) return param_scale * rel_step_sz def _get_options(self, param_group, param_shape): factored = len(param_shape) >= 2 use_first_moment = param_group["beta1"] is not None return factored, use_first_moment def _rms(self, tensor): return tensor.norm(2) / (tensor.numel() ** 0.5) def _approx_sq_grad(self, exp_avg_sq_row, exp_avg_sq_col): r_factor = ( (exp_avg_sq_row / exp_avg_sq_row.mean(dim=-1, keepdim=True)) .rsqrt_() .unsqueeze(-1) ) c_factor = exp_avg_sq_col.unsqueeze(-2).rsqrt() return torch.mul(r_factor, c_factor) def step(self, closure=None): """Performs a single optimization step. Args: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: loss = closure() for group in self.param_groups: for p in group["params"]: if p.grad is None: continue grad = p.grad.data if grad.dtype in {torch.float16, torch.bfloat16}: grad = grad.float() if grad.is_sparse: raise RuntimeError("Adafactor does not support sparse gradients.") state = self.state[p] grad_shape = grad.shape factored, use_first_moment = self._get_options(group, grad_shape) # State Initialization if len(state) == 0: state["step"] = 0 if use_first_moment: # Exponential moving average of gradient values state["exp_avg"] = torch.zeros_like(grad) if factored: state["exp_avg_sq_row"] = torch.zeros(grad_shape[:-1]).to(grad) state["exp_avg_sq_col"] = torch.zeros( grad_shape[:-2] + grad_shape[-1:] ).to(grad) else: state["exp_avg_sq"] = torch.zeros_like(grad) state["RMS"] = 0 else: if use_first_moment: state["exp_avg"] = state["exp_avg"].to(grad) if factored: state["exp_avg_sq_row"] = state["exp_avg_sq_row"].to(grad) state["exp_avg_sq_col"] = state["exp_avg_sq_col"].to(grad) else: state["exp_avg_sq"] = state["exp_avg_sq"].to(grad) p_data_fp32 = p.data if p.data.dtype in {torch.float16, torch.bfloat16}: p_data_fp32 = p_data_fp32.float() state["step"] += 1 state["RMS"] = self._rms(p_data_fp32) group["lr"] = self._get_lr(group, state) beta2t = 1.0 - math.pow(state["step"], group["decay_rate"]) update = (grad ** 2) + group["eps"][0] if factored: exp_avg_sq_row = state["exp_avg_sq_row"] exp_avg_sq_col = state["exp_avg_sq_col"] exp_avg_sq_row.mul_(beta2t).add_( update.mean(dim=-1), alpha=1.0 - beta2t ) exp_avg_sq_col.mul_(beta2t).add_( update.mean(dim=-2), alpha=1.0 - beta2t ) # Approximation of exponential moving average of square of gradient update = self._approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col) update.mul_(grad) else: exp_avg_sq = state["exp_avg_sq"] exp_avg_sq.mul_(beta2t).add_(update, alpha=1.0 - beta2t) update = exp_avg_sq.rsqrt().mul_(grad) update.div_( (self._rms(update) / group["clip_threshold"]).clamp_(min=1.0) ) update.mul_(group["lr"]) if use_first_moment: exp_avg = state["exp_avg"] exp_avg.mul_(group["beta1"]).add_(update, alpha=1 - group["beta1"]) update = exp_avg if group["weight_decay"] != 0: p_data_fp32.add_( p_data_fp32, alpha=-group["weight_decay"] * group["lr"] ) p_data_fp32.add_(-update) if p.data.dtype in {torch.float16, torch.bfloat16}: p.data.copy_(p_data_fp32) return loss
COCO-LM/fairseq/fairseq/optim/adafactor.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/optim/adafactor.py", "repo_id": "COCO-LM", "token_count": 5432 }
184
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. from collections.abc import Collection from dataclasses import dataclass, field from typing import List from omegaconf import II from fairseq.dataclass import FairseqDataclass from fairseq.optim.lr_scheduler import FairseqLRScheduler, register_lr_scheduler @dataclass class InverseSquareRootLRScheduleConfig(FairseqDataclass): warmup_updates: int = field( default=4000, metadata={"help": "warmup the learning rate linearly for the first N updates"}, ) warmup_init_lr: float = field( default=-1, metadata={ "help": "initial learning rate during warmup phase; default is cfg.lr" }, ) lr: List[float] = II("optimization.lr") @register_lr_scheduler("inverse_sqrt", dataclass=InverseSquareRootLRScheduleConfig) class InverseSquareRootSchedule(FairseqLRScheduler): """Decay the LR based on the inverse square root of the update number. We also support a warmup phase where we linearly increase the learning rate from some initial learning rate (``--warmup-init-lr``) until the configured learning rate (``--lr``). Thereafter we decay proportional to the number of updates, with a decay factor set to align with the configured learning rate. During warmup:: lrs = torch.linspace(cfg.warmup_init_lr, cfg.lr, cfg.warmup_updates) lr = lrs[update_num] After warmup:: decay_factor = cfg.lr * sqrt(cfg.warmup_updates) lr = decay_factor / sqrt(update_num) """ def __init__(self, cfg: InverseSquareRootLRScheduleConfig, optimizer): super().__init__(cfg, optimizer) if isinstance(cfg.lr, Collection) and len(cfg.lr) > 1: raise ValueError( "Cannot use a fixed learning rate schedule with inverse_sqrt." " Consider --lr-scheduler=fixed instead." ) warmup_end_lr = cfg.lr[0] if isinstance(cfg.lr, Collection) else cfg.lr if cfg.warmup_init_lr < 0: cfg.warmup_init_lr = 0 if cfg.warmup_updates > 0 else warmup_end_lr # linearly warmup for the first cfg.warmup_updates self.lr_step = (warmup_end_lr - cfg.warmup_init_lr) / cfg.warmup_updates # then, decay prop. to the inverse square root of the update number self.decay_factor = warmup_end_lr * cfg.warmup_updates ** 0.5 # initial learning rate self.lr = cfg.warmup_init_lr self.optimizer.set_lr(self.lr) def step(self, epoch, val_loss=None): """Update the learning rate at the end of the given epoch.""" super().step(epoch, val_loss) # we don't change the learning rate at epoch boundaries return self.optimizer.get_lr() def step_update(self, num_updates): """Update the learning rate after each update.""" if num_updates < self.cfg.warmup_updates: self.lr = self.cfg.warmup_init_lr + num_updates * self.lr_step else: self.lr = self.decay_factor * num_updates ** -0.5 self.optimizer.set_lr(self.lr) return self.lr
COCO-LM/fairseq/fairseq/optim/lr_scheduler/inverse_square_root_schedule.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/optim/lr_scheduler/inverse_square_root_schedule.py", "repo_id": "COCO-LM", "token_count": 1273 }
185
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. from fairseq.scoring import BaseScorer, register_scorer @register_scorer("chrf") class ChrFScorer(BaseScorer): def __init__(self, args): super(ChrFScorer, self).__init__(args) import sacrebleu self.sacrebleu = sacrebleu def add_string(self, ref, pred): self.ref.append(ref) self.pred.append(pred) def score(self, order=4): return self.result_string(order).score def result_string(self, order=4): if order != 4: raise NotImplementedError return self.sacrebleu.corpus_chrf(self.pred, [self.ref]).format()
COCO-LM/fairseq/fairseq/scoring/chrf.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/scoring/chrf.py", "repo_id": "COCO-LM", "token_count": 309 }
186
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import contextlib import logging import os from collections import OrderedDict import torch from fairseq import metrics, options, utils from fairseq.data import ( Dictionary, LanguagePairDataset, RoundRobinZipDatasets, TransformEosLangPairDataset, ) from fairseq.models import FairseqMultiModel from fairseq.tasks.translation import load_langpair_dataset from . import LegacyFairseqTask, register_task logger = logging.getLogger(__name__) def _lang_token(lang: str): return "__{}__".format(lang) def _lang_token_index(dic: Dictionary, lang: str): """Return language token index.""" idx = dic.index(_lang_token(lang)) assert idx != dic.unk_index, "cannot find language token for lang {}".format(lang) return idx @register_task("multilingual_translation") class MultilingualTranslationTask(LegacyFairseqTask): """A task for training multiple translation models simultaneously. We iterate round-robin over batches from multiple language pairs, ordered according to the `--lang-pairs` argument. The training loop is roughly: for i in range(len(epoch)): for lang_pair in args.lang_pairs: batch = next_batch_for_lang_pair(lang_pair) loss = criterion(model_for_lang_pair(lang_pair), batch) loss.backward() optimizer.step() In practice, `next_batch_for_lang_pair` is abstracted in a FairseqDataset (e.g., `RoundRobinZipDatasets`) and `model_for_lang_pair` is a model that implements the `FairseqMultiModel` interface. During inference it is required to specify a single `--source-lang` and `--target-lang`, which indicates the inference langauge direction. `--lang-pairs`, `--encoder-langtok`, `--decoder-langtok` have to be set to the same value as training. """ @staticmethod def add_args(parser): """Add task-specific arguments to the parser.""" # fmt: off parser.add_argument('data', metavar='DIR', help='path to data directory') parser.add_argument('--lang-pairs', default=None, metavar='PAIRS', help='comma-separated list of language pairs (in training order): en-de,en-fr,de-fr') parser.add_argument('-s', '--source-lang', default=None, metavar='SRC', help='source language (only needed for inference)') parser.add_argument('-t', '--target-lang', default=None, metavar='TARGET', help='target language (only needed for inference)') parser.add_argument('--left-pad-source', default='True', type=str, metavar='BOOL', help='pad the source on the left (default: True)') parser.add_argument('--left-pad-target', default='False', type=str, metavar='BOOL', help='pad the target on the left (default: False)') parser.add_argument('--max-source-positions', default=1024, type=int, metavar='N', help='max number of tokens in the source sequence') parser.add_argument('--max-target-positions', default=1024, type=int, metavar='N', help='max number of tokens in the target sequence') parser.add_argument('--upsample-primary', default=1, type=int, help='amount to upsample primary dataset') parser.add_argument('--encoder-langtok', default=None, type=str, choices=['src', 'tgt'], metavar='SRCTGT', help='replace beginning-of-sentence in source sentence with source or target ' 'language token. (src/tgt)') parser.add_argument('--decoder-langtok', action='store_true', help='replace beginning-of-sentence in target sentence with target language token') # fmt: on def __init__(self, args, dicts, training): super().__init__(args) self.dicts = dicts self.training = training if training: self.lang_pairs = args.lang_pairs else: self.lang_pairs = ["{}-{}".format(args.source_lang, args.target_lang)] # eval_lang_pairs for multilingual translation is usually all of the # lang_pairs. However for other multitask settings or when we want to # optimize for certain languages we want to use a different subset. Thus # the eval_lang_pairs class variable is provided for classes that extend # this class. self.eval_lang_pairs = self.lang_pairs # model_lang_pairs will be used to build encoder-decoder model pairs in # models.build_model(). This allows multitask type of sub-class can # build models other than the input lang_pairs self.model_lang_pairs = self.lang_pairs self.langs = list(dicts.keys()) @classmethod def setup_task(cls, args, **kwargs): dicts, training = cls.prepare(args, **kwargs) return cls(args, dicts, training) @classmethod def update_args(cls, args): args.left_pad_source = utils.eval_bool(args.left_pad_source) args.left_pad_target = utils.eval_bool(args.left_pad_target) if args.lang_pairs is None: raise ValueError( "--lang-pairs is required. List all the language pairs in the training objective." ) if isinstance(args.lang_pairs, str): args.lang_pairs = args.lang_pairs.split(",") @classmethod def prepare(cls, args, **kargs): cls.update_args(args) sorted_langs = sorted( list({x for lang_pair in args.lang_pairs for x in lang_pair.split("-")}) ) if args.source_lang is not None or args.target_lang is not None: training = False else: training = True # load dictionaries dicts = OrderedDict() for lang in sorted_langs: paths = utils.split_paths(args.data) assert len(paths) > 0 dicts[lang] = cls.load_dictionary( os.path.join(paths[0], "dict.{}.txt".format(lang)) ) if len(dicts) > 0: assert dicts[lang].pad() == dicts[sorted_langs[0]].pad() assert dicts[lang].eos() == dicts[sorted_langs[0]].eos() assert dicts[lang].unk() == dicts[sorted_langs[0]].unk() if args.encoder_langtok is not None or args.decoder_langtok: for lang_to_add in sorted_langs: dicts[lang].add_symbol(_lang_token(lang_to_add)) logger.info("[{}] dictionary: {} types".format(lang, len(dicts[lang]))) return dicts, training def get_encoder_langtok(self, src_lang, tgt_lang): if self.args.encoder_langtok is None: return self.dicts[src_lang].eos() if self.args.encoder_langtok == "src": return _lang_token_index(self.dicts[src_lang], src_lang) else: return _lang_token_index(self.dicts[src_lang], tgt_lang) def get_decoder_langtok(self, tgt_lang): if not self.args.decoder_langtok: return self.dicts[tgt_lang].eos() return _lang_token_index(self.dicts[tgt_lang], tgt_lang) def alter_dataset_langtok( self, lang_pair_dataset, src_eos=None, src_lang=None, tgt_eos=None, tgt_lang=None, ): if self.args.encoder_langtok is None and not self.args.decoder_langtok: return lang_pair_dataset new_src_eos = None if ( self.args.encoder_langtok is not None and src_eos is not None and src_lang is not None and tgt_lang is not None ): new_src_eos = self.get_encoder_langtok(src_lang, tgt_lang) else: src_eos = None new_tgt_bos = None if self.args.decoder_langtok and tgt_eos is not None and tgt_lang is not None: new_tgt_bos = self.get_decoder_langtok(tgt_lang) else: tgt_eos = None return TransformEosLangPairDataset( lang_pair_dataset, src_eos=src_eos, new_src_eos=new_src_eos, tgt_bos=tgt_eos, new_tgt_bos=new_tgt_bos, ) def load_dataset(self, split, epoch=1, **kwargs): """Load a dataset split.""" paths = utils.split_paths(self.args.data) assert len(paths) > 0 data_path = paths[(epoch - 1) % len(paths)] def language_pair_dataset(lang_pair): src, tgt = lang_pair.split("-") langpair_dataset = load_langpair_dataset( data_path, split, src, self.dicts[src], tgt, self.dicts[tgt], combine=True, dataset_impl=self.args.dataset_impl, upsample_primary=self.args.upsample_primary, left_pad_source=self.args.left_pad_source, left_pad_target=self.args.left_pad_target, max_source_positions=self.args.max_source_positions, max_target_positions=self.args.max_target_positions, ) return self.alter_dataset_langtok( langpair_dataset, src_eos=self.dicts[src].eos(), src_lang=src, tgt_eos=self.dicts[tgt].eos(), tgt_lang=tgt, ) self.datasets[split] = RoundRobinZipDatasets( OrderedDict( [ (lang_pair, language_pair_dataset(lang_pair)) for lang_pair in self.lang_pairs ] ), eval_key=None if self.training else "%s-%s" % (self.args.source_lang, self.args.target_lang), ) def build_dataset_for_inference(self, src_tokens, src_lengths, constraints=None): if constraints is not None: raise NotImplementedError( "Constrained decoding with the multilingual_translation task is not supported" ) lang_pair = "%s-%s" % (self.args.source_lang, self.args.target_lang) return RoundRobinZipDatasets( OrderedDict( [ ( lang_pair, self.alter_dataset_langtok( LanguagePairDataset( src_tokens, src_lengths, self.source_dictionary ), src_eos=self.source_dictionary.eos(), src_lang=self.args.source_lang, tgt_eos=self.target_dictionary.eos(), tgt_lang=self.args.target_lang, ), ) ] ), eval_key=lang_pair, ) def build_model(self, args): def check_args(): messages = [] if ( len(set(self.args.lang_pairs).symmetric_difference(args.lang_pairs)) != 0 ): messages.append( "--lang-pairs should include all the language pairs {}.".format( args.lang_pairs ) ) if self.args.encoder_langtok != args.encoder_langtok: messages.append( "--encoder-langtok should be {}.".format(args.encoder_langtok) ) if self.args.decoder_langtok != args.decoder_langtok: messages.append( "--decoder-langtok should {} be set.".format( "" if args.decoder_langtok else "not" ) ) if len(messages) > 0: raise ValueError(" ".join(messages)) # Update args -> the fact that the constructor here # changes the args object doesn't mean you get the same one here self.update_args(args) # Check if task args are consistant with model args check_args() from fairseq import models model = models.build_model(args, self) if not isinstance(model, FairseqMultiModel): raise ValueError( "MultilingualTranslationTask requires a FairseqMultiModel architecture" ) return model def _per_lang_pair_train_loss( self, lang_pair, model, update_num, criterion, sample, optimizer, ignore_grad ): loss, sample_size, logging_output = criterion( model.models[lang_pair], sample[lang_pair] ) if ignore_grad: loss *= 0 optimizer.backward(loss) return loss, sample_size, logging_output def train_step( self, sample, model, criterion, optimizer, update_num, ignore_grad=False ): model.train() from collections import defaultdict agg_loss, agg_sample_size, agg_logging_output = 0.0, 0.0, defaultdict(float) curr_lang_pairs = [ lang_pair for lang_pair in self.model_lang_pairs if sample[lang_pair] is not None and len(sample[lang_pair]) != 0 ] for idx, lang_pair in enumerate(curr_lang_pairs): def maybe_no_sync(): if ( self.args.distributed_world_size > 1 and hasattr(model, "no_sync") and idx < len(curr_lang_pairs) - 1 ): return model.no_sync() else: return contextlib.ExitStack() # dummy contextmanager with maybe_no_sync(): loss, sample_size, logging_output = self._per_lang_pair_train_loss( lang_pair, model, update_num, criterion, sample, optimizer, ignore_grad, ) agg_loss += loss.detach().item() # TODO make summing of the sample sizes configurable agg_sample_size += sample_size for k in logging_output: agg_logging_output[k] += logging_output[k] agg_logging_output[f"{lang_pair}:{k}"] += logging_output[k] return agg_loss, agg_sample_size, agg_logging_output def _per_lang_pair_valid_loss(self, lang_pair, model, criterion, sample): return criterion(model.models[lang_pair], sample[lang_pair]) def valid_step(self, sample, model, criterion): model.eval() with torch.no_grad(): from collections import defaultdict agg_loss, agg_sample_size, agg_logging_output = 0.0, 0.0, defaultdict(float) for lang_pair in self.eval_lang_pairs: if ( lang_pair not in sample or sample[lang_pair] is None or len(sample[lang_pair]) == 0 ): continue loss, sample_size, logging_output = self._per_lang_pair_valid_loss( lang_pair, model, criterion, sample ) agg_loss += loss.data.item() # TODO make summing of the sample sizes configurable agg_sample_size += sample_size for k in logging_output: agg_logging_output[k] += logging_output[k] agg_logging_output[f"{lang_pair}:{k}"] += logging_output[k] return agg_loss, agg_sample_size, agg_logging_output def inference_step( self, generator, models, sample, prefix_tokens=None, constraints=None ): with torch.no_grad(): if self.args.decoder_langtok: bos_token = _lang_token_index( self.target_dictionary, self.args.target_lang ) else: bos_token = self.target_dictionary.eos() return generator.generate( models, sample, prefix_tokens=prefix_tokens, constraints=constraints, bos_token=bos_token, ) def reduce_metrics(self, logging_outputs, criterion): with metrics.aggregate(): # pass 'sample_size', 'nsentences', 'ntokens' stats to fairseq_task super().reduce_metrics(logging_outputs, criterion) for k in ["sample_size", "nsentences", "ntokens"]: metrics.log_scalar(k, sum(l[k] for l in logging_outputs)) @property def source_dictionary(self): if self.training: return next(iter(self.dicts.values())) else: return self.dicts[self.args.source_lang] @property def target_dictionary(self): if self.training: return next(iter(self.dicts.values())) else: return self.dicts[self.args.target_lang] def max_positions(self): """Return the max sentence length allowed by the task.""" if len(self.datasets.values()) == 0: return { "%s-%s" % (self.args.source_lang, self.args.target_lang): ( self.args.max_source_positions, self.args.max_target_positions, ) } return OrderedDict( [ (key, (self.args.max_source_positions, self.args.max_target_positions)) for split in self.datasets.keys() for key in self.datasets[split].datasets.keys() ] )
COCO-LM/fairseq/fairseq/tasks/multilingual_translation.py/0
{ "file_path": "COCO-LM/fairseq/fairseq/tasks/multilingual_translation.py", "repo_id": "COCO-LM", "token_count": 8926 }
187
#pragma once #include <assert.h> #include <cfloat> #include <limits> #include <stdint.h> #include <cuda_fp16.h> #include <cuda_bf16.h> #include <cmath> namespace { template <typename Datatype, int ELEMENTS_PER_LDG> __device__ __inline__ void copy_vector(Datatype *dst, const Datatype *src); template <> __device__ __inline__ void copy_vector<__half, 1>(__half *dst, const __half *src) { *dst = *src; } template <> __device__ __inline__ void copy_vector<__nv_bfloat16, 1>(__nv_bfloat16 *dst, const __nv_bfloat16 *src) { *dst = *src; } template <> __device__ __inline__ void copy_vector<float, 1>(float *dst, const float *src) { *dst = *src; } } // namespace anonymous //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Warp Softmax forward //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // WARP_BATCH number of batches. // WARP_ITERATOINS The number of iterations required for one warp to iterate over all data. // WARP_SIZE number of elements working on a single batch, has to be a power of two. // ELEMENTS_PER_LDG_STG has to be 1. template <typename input_t, typename output_t, typename acc_t, int WARP_BATCH, int WARP_ITERATIONS, int WARP_SIZE = 32, int ELEMENTS_PER_LDG_STG=1> __global__ void softmax_warp_forward(input_t *dst, const output_t *src, int batch_size, int stride, int element_count) { assert(ELEMENTS_PER_LDG_STG==1); int first_batch = (blockDim.y * blockIdx.x + threadIdx.y) * WARP_BATCH; // batch_size might not be a multiple of WARP_BATCH. Check how // many batches have to computed within this WARP. int local_batches = batch_size - first_batch; if (local_batches > WARP_BATCH) local_batches = WARP_BATCH; // there might be multiple batches per warp. compute the index within the batch int local_idx = threadIdx.x; src += first_batch * stride + ELEMENTS_PER_LDG_STG * local_idx; dst += first_batch * stride + ELEMENTS_PER_LDG_STG * local_idx; // load data from global memory input_t elements_input[WARP_BATCH][WARP_ITERATIONS]; for (int i = 0;i < WARP_BATCH;++i) { int batch_element_count = (i >= local_batches) ? 0 : element_count; for (int it = 0;it < WARP_ITERATIONS;it += ELEMENTS_PER_LDG_STG) { int element_index = ELEMENTS_PER_LDG_STG * local_idx + it * WARP_SIZE; #pragma unroll for (int element = 0;element < ELEMENTS_PER_LDG_STG;++element) { elements_input[i][it + element] = -std::numeric_limits<float>::infinity(); } if (element_index < batch_element_count) { copy_vector<input_t, ELEMENTS_PER_LDG_STG>(&elements_input[i][it], src + i * element_count + it * WARP_SIZE); } } } // convert input_t to acc_t acc_t elements[WARP_BATCH][WARP_ITERATIONS]; for (int i = 0;i < WARP_BATCH;++i) { for (int it = 0;it < WARP_ITERATIONS;++it) { elements[i][it] = elements_input[i][it]; } } constexpr uint32_t FULL_MASK = 0xffffffff; // compute local max_value // take the max_value of the first element to avoid one max call acc_t max_value[WARP_BATCH]; #pragma unroll for (int i = 0;i < WARP_BATCH;++i) { max_value[i] = elements[i][0]; } #pragma unroll for (int it = 1;it < WARP_ITERATIONS;++it) { for (int i = 0;i < WARP_BATCH;++i) { max_value[i] = (max_value[i] > elements[i][it]) ? max_value[i] : elements[i][it]; } } // reduction max_value #pragma unroll for (int offset = WARP_SIZE / 2; offset > 0; offset /= 2) { float val[WARP_BATCH]; #pragma unroll for (int i = 0;i < WARP_BATCH;++i) { val[i] = __shfl_xor_sync(FULL_MASK, max_value[i], offset, WARP_SIZE); } #pragma unroll for (int i = 0;i < WARP_BATCH;++i) { max_value[i] = max_value[i] > val[i] ? max_value[i] : val[i]; } } // compute local sum acc_t sum[WARP_BATCH] { 0.0f }; #pragma unroll for (int i = 0;i < WARP_BATCH;++i) { for (int it = 0;it < WARP_ITERATIONS;++it) { //elements[i][it] = expf(elements[i][it] - max_value[i]); elements[i][it] = std::exp(elements[i][it] - max_value[i]); sum[i] += elements[i][it]; } } // reduction sum #pragma unroll for (int offset = WARP_SIZE / 2; offset > 0; offset /= 2) { #pragma unroll for (int i = 0;i < WARP_BATCH;++i) { sum[i] += __shfl_xor_sync(FULL_MASK, sum[i], offset, WARP_SIZE); } } // store result #pragma unroll for (int i = 0;i < WARP_BATCH;++i) { if (i >= local_batches) break; #pragma unroll for (int it = 0;it < WARP_ITERATIONS;it += ELEMENTS_PER_LDG_STG) { int element_index = ELEMENTS_PER_LDG_STG * local_idx + it * WARP_SIZE; if (element_index < element_count) { //dst[i * element_count + it * WARP_SIZE] = elements[i][it] / sum[i]; output_t out[ELEMENTS_PER_LDG_STG]; for (int element = 0;element < ELEMENTS_PER_LDG_STG;++element) { out[element] = elements[i][it + element] / sum[i]; } copy_vector<output_t, ELEMENTS_PER_LDG_STG>(dst + i * element_count + it * WARP_SIZE, out); } else { break; } } } } // WARP_BATCH number of batches. // WARP_ITERATOINS The number of iterations required for one warp to iterate over all data. // WARP_SIZE number of elements working on a single batch, has to be a power of two. // ELEMENTS_PER_LDG_STG has to be 1. template <typename input_t, typename output_t> using softmax_forward_func = void(*)(input_t *dst, const output_t *src, int batch_size, int stride, int element_count); template <typename input_t, typename output_t, typename acc_t> bool warp_softmax_kernel(int log2_elements, int &warp_size, int &batches_per_warp, softmax_forward_func<input_t, output_t> &kernel) { // determine size of a warp const int next_power_of_two = 1 << log2_elements; warp_size = (next_power_of_two < 32) ? next_power_of_two : 32; // determine how many batches a warp should process. batches_per_warp = (next_power_of_two <= 128) ? 2 : 1; switch (log2_elements) { case 0: // 1 kernel = &softmax_warp_forward<input_t, output_t, acc_t, 2,1,1,1>; break; case 1: // 2 kernel = &softmax_warp_forward<input_t, output_t, acc_t, 2,1,2,1>; break; case 2: // 4 kernel = &softmax_warp_forward<input_t, output_t, acc_t, 2,1,4,1>; break; case 3: // 8 kernel = &softmax_warp_forward<input_t, output_t, acc_t, 2,1,8,1>; break; case 4: // 16 kernel = &softmax_warp_forward<input_t, output_t, acc_t, 2,1,16,1>; break; case 5: // 32 kernel = &softmax_warp_forward<input_t, output_t, acc_t, 2,1,32,1>; break; case 6: // 64 kernel = &softmax_warp_forward<input_t, output_t, acc_t, 2,2,32,1>; break; case 7: // 128 kernel = &softmax_warp_forward<input_t, output_t, acc_t, 2,4,32,1>; break; case 8: // 256 kernel = &softmax_warp_forward<input_t, output_t, acc_t, 1,8,32,1>; break; case 9: // 512 kernel = &softmax_warp_forward<input_t, output_t, acc_t, 1,16,32,1>; break; case 10: // 1024 kernel = &softmax_warp_forward<input_t, output_t, acc_t, 1,32,32,1>; break; default: return false; } return true; } template<typename input_t, typename output_t, typename acc_t> bool dispatch_softmax(output_t *dst, const input_t *src, int softmax_elements, int softmax_elements_stride, int batch_count) { if (softmax_elements == 0) { return true; } else if (softmax_elements <= 1024) { // compute function index. there's a function for each power of two size up to 1024. int log2_elements = 0; while ((1 << log2_elements) < softmax_elements) ++log2_elements; softmax_forward_func<input_t, output_t> kernel; int warp_size, batches_per_warp; if (!warp_softmax_kernel<input_t, output_t, acc_t>(log2_elements, warp_size, batches_per_warp, kernel)) { return false; } // use 128 threads per block to maximimize gpu utilization constexpr int threads_per_block = 128; // compute warps per block. int warps_per_block = (threads_per_block / warp_size); // compute launch size int batches_per_block = warps_per_block * batches_per_warp; int blocks = (batch_count + batches_per_block - 1) / batches_per_block; dim3 threads(warp_size, warps_per_block, 1); // launch kernel<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(dst, src, batch_count, softmax_elements_stride, softmax_elements); return true; } return false; } int log2_ceil_native(int value) { int log2_value = 0; while ((1 << log2_value) < value) ++log2_value; return log2_value; } template <typename T> __device__ __forceinline__ T WARP_SHFL_XOR_NATIVE(T value, int laneMask, int width = warpSize, unsigned int mask = 0xffffffff) { #if CUDA_VERSION >= 9000 return __shfl_xor_sync(mask, value, laneMask, width); #else return __shfl_xor(value, laneMask, width); #endif } template <typename acc_t, int WARP_BATCH, int WARP_SIZE> __device__ __forceinline__ void warp_reduce_sum(acc_t* sum) { #pragma unroll for (int offset = WARP_SIZE / 2; offset > 0; offset /= 2) { #pragma unroll for (int i = 0; i < WARP_BATCH; ++i) { acc_t b = WARP_SHFL_XOR_NATIVE(sum[i], offset, WARP_SIZE); sum[i] = sum[i] + b; } } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Warp softmax backward functions as fused variants of at::softmax_backward_data function //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// template <typename input_t, typename output_t, typename acc_t, int log2_elements, bool is_log_softmax> __global__ void masked_scale_softmax_warp_backward(output_t *gradInput, const input_t *grad, const input_t *output, const uint8_t *mask, acc_t scale, int batch_size, int stride, int element_count) { // WARP_SIZE and WARP_BATCH must match the return values batches_per_warp and warp_size of method warp_softmax_backward_kernel. constexpr int next_power_of_two = 1 << log2_elements; constexpr int WARP_SIZE = (next_power_of_two < C10_WARP_SIZE) ? next_power_of_two : C10_WARP_SIZE; constexpr int WARP_ITERATIONS = next_power_of_two / WARP_SIZE; constexpr int WARP_BATCH = (next_power_of_two <= 128) ? 2 : 1; int first_batch = (blockDim.y * blockIdx.x + threadIdx.y) * WARP_BATCH; // batch_size might not be a multiple of WARP_BATCH. Check how // many batches have to computed within this WARP. int local_batches = batch_size - first_batch; if (local_batches > WARP_BATCH) local_batches = WARP_BATCH; // there might be multiple batches per warp. compute the index within the batch int local_idx = threadIdx.x % WARP_SIZE; // the first element to process by the current thread int thread_offset = first_batch * stride + local_idx; grad += thread_offset; output += thread_offset; gradInput += thread_offset; mask += thread_offset; // The nested loops over WARP_BATCH and then WARP_ITERATIONS can be simplified to one loop, // but I think doing so would obfuscate the logic of the algorithm, thus I chose to keep // the nested loops. // This should have no impact on performance because the loops are unrolled anyway. // load data from global memory acc_t grad_reg[WARP_BATCH][WARP_ITERATIONS] ; acc_t output_reg[WARP_BATCH][WARP_ITERATIONS] ; for (int i = 0; i < WARP_BATCH; ++i) { int batch_element_count = (i >= local_batches) ? 0 : element_count; for (int it = 0; it < WARP_ITERATIONS; ++it) { int element_index = local_idx + it * WARP_SIZE; if (element_index < batch_element_count) { grad_reg[i][it] = (input_t)((acc_t)mask[i*element_count+it*WARP_SIZE] * (acc_t)grad[i*element_count+it*WARP_SIZE] * (acc_t)scale )*output[i*element_count+it*WARP_SIZE]; output_reg[i][it] = output[i*element_count+it*WARP_SIZE]; } else { grad_reg[i][it] = acc_t(0); output_reg[i][it] = acc_t(0); } } } acc_t sum[WARP_BATCH]; #pragma unroll for (int i = 0; i < WARP_BATCH; ++i) { sum[i] = grad_reg[i][0]; #pragma unroll for (int it = 1; it < WARP_ITERATIONS; ++it) { sum[i] += grad_reg[i][it]; } } warp_reduce_sum<acc_t, WARP_BATCH, WARP_SIZE>(sum); // store result #pragma unroll for (int i = 0; i < WARP_BATCH; ++i) { if (i >= local_batches) break; #pragma unroll for (int it = 0; it < WARP_ITERATIONS; ++it) { int element_index = local_idx + it * WARP_SIZE; if (element_index < element_count) { // compute gradients if (is_log_softmax) { gradInput[i*element_count+it*WARP_SIZE] = (grad_reg[i][it] - std::exp(output_reg[i][it]) * sum[i]); } else { gradInput[i*element_count+it*WARP_SIZE] = (grad_reg[i][it] - output_reg[i][it] * sum[i]); } } } } } template<typename input_t, typename output_t, typename acc_t, bool is_log_softmax> void dispatch_masked_scale_softmax_backward(output_t *grad_input, const input_t *grad, const input_t *output, const uint8_t *mask, acc_t scale, int softmax_elements, int softmax_elements_stride, int batch_count) { TORCH_INTERNAL_ASSERT( softmax_elements >= 0 && softmax_elements <= 1024 ); if (softmax_elements == 0) { return; } else { int log2_elements = log2_ceil_native(softmax_elements); const int next_power_of_two = 1 << log2_elements; // This value must match the WARP_SIZE constexpr value computed inside softmax_warp_backward. int warp_size = (next_power_of_two < C10_WARP_SIZE) ? next_power_of_two : C10_WARP_SIZE; // This value must match the WARP_BATCH constexpr value computed inside softmax_warp_backward. int batches_per_warp = (next_power_of_two <= 128) ? 2 : 1; // use 128 threads per block to maximimize gpu utilization constexpr int threads_per_block = 128; int warps_per_block = (threads_per_block / warp_size); int batches_per_block = warps_per_block * batches_per_warp; int blocks = (batch_count + batches_per_block - 1) / batches_per_block; dim3 threads(warp_size, warps_per_block, 1); // Launch code would be more elegant if C++ supported FOR CONSTEXPR switch (log2_elements) { case 0: // 1 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 0, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 1: // 2 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 1, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 2: // 4 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 2, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 3: // 8 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 3, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 4: // 16 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 4, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 5: // 32 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 5, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 6: // 64 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 6, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 7: // 128 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 7, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 8: // 256 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 8, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 9: // 512 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 9, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 10: // 1024 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 10, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; default: break; } } } template<typename input_t, typename output_t, typename acc_t, bool is_log_softmax> void dispatch_masked_scale_softmax_backward_stream(output_t *grad_input, const input_t *grad, const input_t *output, const uint8_t *mask, acc_t scale, int softmax_elements, int softmax_elements_stride, int batch_count, cudaStream_t streamid) { TORCH_INTERNAL_ASSERT( softmax_elements >= 0 && softmax_elements <= 1024 ); if (softmax_elements == 0) { return; } else { int log2_elements = log2_ceil_native(softmax_elements); const int next_power_of_two = 1 << log2_elements; // This value must match the WARP_SIZE constexpr value computed inside softmax_warp_backward. int warp_size = (next_power_of_two < C10_WARP_SIZE) ? next_power_of_two : C10_WARP_SIZE; // This value must match the WARP_BATCH constexpr value computed inside softmax_warp_backward. int batches_per_warp = (next_power_of_two <= 128) ? 2 : 1; // use 128 threads per block to maximimize gpu utilization constexpr int threads_per_block = 128; int warps_per_block = (threads_per_block / warp_size); int batches_per_block = warps_per_block * batches_per_warp; int blocks = (batch_count + batches_per_block - 1) / batches_per_block; dim3 threads(warp_size, warps_per_block, 1); // Launch code would be more elegant if C++ supported FOR CONSTEXPR switch (log2_elements) { case 0: // 1 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 0, is_log_softmax> <<<blocks, threads, 0, streamid>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 1: // 2 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 1, is_log_softmax> <<<blocks, threads, 0, streamid>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 2: // 4 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 2, is_log_softmax> <<<blocks, threads, 0, streamid>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 3: // 8 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 3, is_log_softmax> <<<blocks, threads, 0, streamid>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 4: // 16 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 4, is_log_softmax> <<<blocks, threads, 0, streamid>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 5: // 32 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 5, is_log_softmax> <<<blocks, threads, 0, streamid>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 6: // 64 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 6, is_log_softmax> <<<blocks, threads, 0, streamid>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 7: // 128 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 7, is_log_softmax> <<<blocks, threads, 0, streamid>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 8: // 256 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 8, is_log_softmax> <<<blocks, threads, 0, streamid>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 9: // 512 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 9, is_log_softmax> <<<blocks, threads, 0, streamid>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; case 10: // 1024 masked_scale_softmax_warp_backward<input_t, output_t, acc_t, 10, is_log_softmax> <<<blocks, threads, 0, streamid>>>(grad_input, grad, output, mask, scale, batch_count, softmax_elements_stride, softmax_elements); break; default: break; } } } // elementwise multiplication called in at::softmax_backward_data is fused inside softmax dgrad kernel // as a result of fusion, intermediate multiplication result is stored in fp32 in registers, instead of fp16 template <typename input_t, typename output_t, typename acc_t, int log2_elements, bool is_log_softmax> __global__ void softmax_warp_backward_fused_native(output_t *gradInput, const input_t *grad, const input_t *output, int batch_size, int stride, int element_count) { // WARP_SIZE and WARP_BATCH must match the return values batches_per_warp and warp_size of method warp_softmax_backward_kernel. constexpr int next_power_of_two = 1 << log2_elements; constexpr int WARP_SIZE = (next_power_of_two < C10_WARP_SIZE) ? next_power_of_two : C10_WARP_SIZE; constexpr int WARP_ITERATIONS = next_power_of_two / WARP_SIZE; constexpr int WARP_BATCH = (next_power_of_two <= 128) ? 2 : 1; int first_batch = (blockDim.y * blockIdx.x + threadIdx.y) * WARP_BATCH; // batch_size might not be a multiple of WARP_BATCH. Check how // many batches have to computed within this WARP. int local_batches = batch_size - first_batch; if (local_batches > WARP_BATCH) local_batches = WARP_BATCH; // there might be multiple batches per warp. compute the index within the batch int local_idx = threadIdx.x % WARP_SIZE; // the first element to process by the current thread int thread_offset = first_batch * stride + local_idx; grad += thread_offset; output += thread_offset; gradInput += thread_offset; // The nested loops over WARP_BATCH and then WARP_ITERATIONS can be simplified to one loop, // but I think doing so would obfuscate the logic of the algorithm, thus I chose to keep // the nested loops. // This should have no impact on performance because the loops are unrolled anyway. // load data from global memory acc_t grad_reg[WARP_BATCH][WARP_ITERATIONS] ; acc_t output_reg[WARP_BATCH][WARP_ITERATIONS] ; for (int i = 0; i < WARP_BATCH; ++i) { int batch_element_count = (i >= local_batches) ? 0 : element_count; for (int it = 0; it < WARP_ITERATIONS; ++it) { int element_index = local_idx + it * WARP_SIZE; if (element_index < batch_element_count) { grad_reg[i][it] = grad[i*element_count+it*WARP_SIZE]*output[i*element_count+it*WARP_SIZE]; output_reg[i][it] = output[i*element_count+it*WARP_SIZE]; } else { grad_reg[i][it] = acc_t(0); output_reg[i][it] = acc_t(0); } } } acc_t sum[WARP_BATCH]; #pragma unroll for (int i = 0; i < WARP_BATCH; ++i) { sum[i] = grad_reg[i][0]; //* output_reg[i][0]; #pragma unroll for (int it = 1; it < WARP_ITERATIONS; ++it) { sum[i] += grad_reg[i][it];// * output_reg[i][it]; } } warp_reduce_sum<acc_t, WARP_BATCH, WARP_SIZE>(sum); // store result #pragma unroll for (int i = 0; i < WARP_BATCH; ++i) { if (i >= local_batches) break; #pragma unroll for (int it = 0; it < WARP_ITERATIONS; ++it) { int element_index = local_idx + it * WARP_SIZE; if (element_index < element_count) { // compute gradients if (is_log_softmax) { gradInput[i*element_count+it*WARP_SIZE] = (grad_reg[i][it] - std::exp(output_reg[i][it]) * sum[i]); } else { gradInput[i*element_count+it*WARP_SIZE] = (grad_reg[i][it] - output_reg[i][it] * sum[i]); } } } } } template<typename input_t, typename output_t, typename acc_t, bool is_log_softmax> void dispatch_softmax_backward_fused_native(output_t *grad_input, const input_t *grad, const input_t *output, int softmax_elements, int softmax_elements_stride, int batch_count) { TORCH_INTERNAL_ASSERT( softmax_elements >= 0 && softmax_elements <= 1024 ); if (softmax_elements == 0) { return; } else { int log2_elements = log2_ceil_native(softmax_elements); const int next_power_of_two = 1 << log2_elements; // This value must match the WARP_SIZE constexpr value computed inside softmax_warp_backward. int warp_size = (next_power_of_two < C10_WARP_SIZE) ? next_power_of_two : C10_WARP_SIZE; // This value must match the WARP_BATCH constexpr value computed inside softmax_warp_backward. int batches_per_warp = (next_power_of_two <= 128) ? 2 : 1; // use 128 threads per block to maximimize gpu utilization constexpr int threads_per_block = 128; int warps_per_block = (threads_per_block / warp_size); int batches_per_block = warps_per_block * batches_per_warp; int blocks = (batch_count + batches_per_block - 1) / batches_per_block; dim3 threads(warp_size, warps_per_block, 1); // Launch code would be more elegant if C++ supported FOR CONSTEXPR switch (log2_elements) { case 0: // 1 softmax_warp_backward_fused_native<input_t, output_t, acc_t, 0, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, batch_count, softmax_elements_stride, softmax_elements); break; case 1: // 2 softmax_warp_backward_fused_native<input_t, output_t, acc_t, 1, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, batch_count, softmax_elements_stride, softmax_elements); break; case 2: // 4 softmax_warp_backward_fused_native<input_t, output_t, acc_t, 2, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, batch_count, softmax_elements_stride, softmax_elements); break; case 3: // 8 softmax_warp_backward_fused_native<input_t, output_t, acc_t, 3, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, batch_count, softmax_elements_stride, softmax_elements); break; case 4: // 16 softmax_warp_backward_fused_native<input_t, output_t, acc_t, 4, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, batch_count, softmax_elements_stride, softmax_elements); break; case 5: // 32 softmax_warp_backward_fused_native<input_t, output_t, acc_t, 5, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, batch_count, softmax_elements_stride, softmax_elements); break; case 6: // 64 softmax_warp_backward_fused_native<input_t, output_t, acc_t, 6, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, batch_count, softmax_elements_stride, softmax_elements); break; case 7: // 128 softmax_warp_backward_fused_native<input_t, output_t, acc_t, 7, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, batch_count, softmax_elements_stride, softmax_elements); break; case 8: // 256 softmax_warp_backward_fused_native<input_t, output_t, acc_t, 8, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, batch_count, softmax_elements_stride, softmax_elements); break; case 9: // 512 softmax_warp_backward_fused_native<input_t, output_t, acc_t, 9, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, batch_count, softmax_elements_stride, softmax_elements); break; case 10: // 1024 softmax_warp_backward_fused_native<input_t, output_t, acc_t, 10, is_log_softmax> <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, batch_count, softmax_elements_stride, softmax_elements); break; default: break; } } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Warp softmax backward //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// template <typename input_t, typename output_t, typename acc_t, int WARP_BATCH, int WARP_ITERATIONS, int WARP_SIZE=32, int ELEMENTS_PER_LDG_STG=1> __global__ void softmax_warp_backward(__half *gradInput, const __half *grad, const __half *output, int batch_size, int stride, int element_count) { int first_batch = (blockDim.y * blockIdx.x + threadIdx.y) * WARP_BATCH; // batch_size might not be a multiple of WARP_BATCH. Check how // many batches have to computed within this WARP. int local_batches = batch_size - first_batch; if (local_batches > WARP_BATCH) local_batches = WARP_BATCH; // there might be multiple batches per warp. compute the index within the batch int local_idx = threadIdx.x; // the first element to process by the current thread int thread_offset = first_batch * stride + ELEMENTS_PER_LDG_STG * local_idx; grad += thread_offset; output += thread_offset; gradInput += thread_offset; // load data from global memory input_t grad_reg_input[WARP_BATCH][WARP_ITERATIONS] = {0.0f}; input_t output_reg_input[WARP_BATCH][WARP_ITERATIONS] = {0.0f}; #pragma unroll for (int i = 0;i < WARP_BATCH;++i) { int batch_element_count = (i >= local_batches) ? 0 : element_count; #pragma unroll for (int it = 0;it < WARP_ITERATIONS;it += ELEMENTS_PER_LDG_STG) { int element_index = ELEMENTS_PER_LDG_STG * local_idx + it * WARP_SIZE; if (element_index < batch_element_count) { copy_vector<input_t, ELEMENTS_PER_LDG_STG>(&grad_reg_input[i][it], grad + i * element_count + it * WARP_SIZE); copy_vector<input_t,ELEMENTS_PER_LDG_STG>(&output_reg_input[i][it], output + i * element_count + it * WARP_SIZE); } } } // convert half to floating point acc_t grad_reg[WARP_BATCH][WARP_ITERATIONS]; acc_t output_reg[WARP_BATCH][WARP_ITERATIONS]; #pragma unroll for (int i = 0;i < WARP_BATCH;++i) { for (int it = 0;it < WARP_ITERATIONS;++it) { grad_reg[i][it] = grad_reg_input[i][it]; output_reg[i][it] = output_reg_input[i][it]; } } // compute thread local sum acc_t sum[WARP_BATCH] = {0}; #pragma unroll for (int it = 0;it < WARP_ITERATIONS;++it) { for (int i = 0;i < WARP_BATCH;++i) { sum[i] += grad_reg[i][it] * output_reg[i][it]; } } // reduction sum constexpr uint32_t FULL_MASK = 0xffffffff; #pragma unroll for (int offset = WARP_SIZE / 2; offset > 0; offset /= 2) { #pragma unroll for (int i = 0;i < WARP_BATCH;++i) { sum[i] += __shfl_xor_sync(FULL_MASK, sum[i], offset, WARP_SIZE); } } // store result #pragma unroll for (int i = 0;i < WARP_BATCH;++i) { if (i >= local_batches) break; #pragma unroll for (int it = 0;it < WARP_ITERATIONS;it += ELEMENTS_PER_LDG_STG) { int element_index = ELEMENTS_PER_LDG_STG * local_idx + it * WARP_SIZE; if (element_index < element_count) { // compute gradients output_t out[ELEMENTS_PER_LDG_STG]; for (int element = 0;element < ELEMENTS_PER_LDG_STG;++element) { out[element] = (output_reg[i][it+element] * (grad_reg[i][it+element] - sum[i])); } // store them in global memory copy_vector<output_t, ELEMENTS_PER_LDG_STG>(gradInput + i * element_count + it * WARP_SIZE, out); } } } } // WARP_BATCH number of batches. // WARP_ITERATOINS The number of iterations required for one warp to iterate over all data. // WARP_SIZE number of elements working on a single batch, has to be a power of two. // ELEMENTS_PER_LDG_STG has to be 1. template <typename input_t, typename output_t> using softmax_backward_func = void(*)(output_t *gradInput, const input_t *grad, const input_t *output, int batch_size, int stride, int element_count); template <typename input_t, typename output_t, typename acc_t> bool warp_softmax_backward_kernel(int log2_elements, int &warp_size, int &batches_per_warp, softmax_backward_func<input_t, output_t> &kernel) { // determine size of a warp const int next_power_of_two = 1 << log2_elements; warp_size = (next_power_of_two < 32) ? next_power_of_two : 32; // determine how many batches a warp should process. batches_per_warp = (next_power_of_two <= 128) ? 2 : 1; switch (log2_elements) { case 0: // 1 kernel = &softmax_warp_backward<input_t, output_t, acc_t, 2,1,1,1>; break; case 1: // 2 kernel = &softmax_warp_backward<input_t, output_t, acc_t, 2,1,2,1>; break; case 2: // 4 kernel = &softmax_warp_backward<input_t, output_t, acc_t, 2,1,4,1>; break; case 3: // 8 kernel = &softmax_warp_backward<input_t, output_t, acc_t, 2,1,8,1>; break; case 4: // 16 kernel = &softmax_warp_backward<input_t, output_t, acc_t, 2,1,16,1>; break; case 5: // 32 kernel = &softmax_warp_backward<input_t, output_t, acc_t, 2,1,32,1>; break; case 6: // 64 kernel = &softmax_warp_backward<input_t, output_t, acc_t, 2,2,32,1>; break; case 7: // 128 kernel = &softmax_warp_backward<input_t, output_t, acc_t, 2,4,32,1>; break; case 8: // 256 kernel = &softmax_warp_backward<input_t, output_t, acc_t, 1,8,32,1>; break; case 9: // 512 kernel = &softmax_warp_backward<input_t, output_t, acc_t, 1,16,32,1>; break; case 10: // 1024 kernel = &softmax_warp_backward<input_t, output_t, acc_t, 1,32,32,1>; break; default: return false; } return true; } template<typename input_t, typename output_t, typename acc_t> bool dispatch_softmax_backward(output_t *grad_input, const input_t *grad, const input_t *output, int softmax_elements, int softmax_elements_stride, int batch_count) { if (softmax_elements == 0) { return true; } else if (softmax_elements <= 1024) { // compute function index. there's a function for each power of two size up to 1024. int log2_elements = 0; while ((1 << log2_elements) < softmax_elements) ++log2_elements; softmax_backward_func<input_t, output_t> kernel; int warp_size, batches_per_warp; if (!warp_softmax_backward_kernel<input_t, output_t, acc_t>(log2_elements, warp_size, batches_per_warp, kernel)) { return false; } // use 128 threads per block to maximimize gpu utilization constexpr int threads_per_block = 128; // compute warps per block. int warps_per_block = (threads_per_block / warp_size); // compute launch size int batches_per_block = warps_per_block * batches_per_warp; int blocks = (batch_count + batches_per_block - 1) / batches_per_block; dim3 threads(warp_size, warps_per_block, 1); // launch kernel<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(grad_input, grad, output, batch_count, softmax_elements_stride, softmax_elements); return true; } return false; } template<typename input_t, typename output_t, typename acc_t> bool dispatch_softmax_backward_stream(output_t *grad_input, const input_t *grad, const input_t *output, int softmax_elements, int softmax_elements_stride, int batch_count, cudaStream_t streamid) { if (softmax_elements == 0) { return true; } else if (softmax_elements <= 1024) { // compute function index. there's a function for each power of two size up to 1024. int log2_elements = 0; while ((1 << log2_elements) < softmax_elements) ++log2_elements; softmax_backward_func<input_t, output_t> kernel; int warp_size, batches_per_warp; if (!warp_softmax_backward_kernel<input_t, output_t, acc_t>(log2_elements, warp_size, batches_per_warp, kernel)) { return false; } // use 128 threads per block to maximimize gpu utilization constexpr int threads_per_block = 128; // compute warps per block. int warps_per_block = (threads_per_block / warp_size); // compute launch size int batches_per_block = warps_per_block * batches_per_warp; int blocks = (batch_count + batches_per_block - 1) / batches_per_block; dim3 threads(warp_size, warps_per_block, 1); // launch kernel<<<blocks, threads, 0, streamid>>>(grad_input, grad, output, batch_count, softmax_elements_stride, softmax_elements); return true; } return false; }
COCO-LM/fairseq/fused_ops/csrc/softmax_dropout/softmax.h/0
{ "file_path": "COCO-LM/fairseq/fused_ops/csrc/softmax_dropout/softmax.h", "repo_id": "COCO-LM", "token_count": 18693 }
188
# Copyright 2020 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import json import os, collections from functools import partial from multiprocessing import Pool, cpu_count import pickle import logging import numpy as np import six from tqdm import tqdm import argparse from fairseq.data.encoders.sentencepiece_bpe import SentencepieceBPE from fairseq.data.squad.squad_extractor import read_squad_examples, squad_convert_examples_to_features parser = argparse.ArgumentParser() parser.add_argument('--sentencepiece-model', type=str) parser.add_argument('--vocab', type=str) parser.add_argument('--input', type=str) parser.add_argument('--output', type=str) parser.add_argument('--is-training', action='store_true') parser.add_argument('--version-2-with-negative', action='store_true') parser.add_argument('--max-query-length', type=int, default=64) parser.add_argument('--max-seq-length', type=int, default=384) parser.add_argument('--doc-stride', type=int, default=128) args = parser.parse_args() tokenizer = SentencepieceBPE(args) examples = read_squad_examples(args.input, args.is_training, args.version_2_with_negative) features = squad_convert_examples_to_features(examples, tokenizer, args.max_seq_length, args.doc_stride, args.max_query_length, args.is_training) print('size of examples {}, size of features {}'.format(len(examples), len(features))) pickle.dump(examples, open(args.output+'_examples.pkl', 'wb')) pickle.dump(features, open(args.output+'_features.pkl', 'wb'))
COCO-LM/fairseq/preprocess/squad/squad_process.py/0
{ "file_path": "COCO-LM/fairseq/preprocess/squad/squad_process.py", "repo_id": "COCO-LM", "token_count": 635 }
189
#!/bin/bash if [ $# -ne 4 ]; then echo "usage: $0 TESTSET SRCLANG TGTLANG GEN" exit 1 fi TESTSET=$1 SRCLANG=$2 TGTLANG=$3 GEN=$4 if ! command -v sacremoses &> /dev/null then echo "sacremoses could not be found, please install with: pip install sacremoses" exit fi grep ^H $GEN \ | sed 's/^H\-//' \ | sort -n -k 1 \ | cut -f 3 \ | sacremoses detokenize \ > $GEN.sorted.detok sacrebleu --test-set $TESTSET --language-pair "${SRCLANG}-${TGTLANG}" < $GEN.sorted.detok
COCO-LM/fairseq/scripts/sacrebleu.sh/0
{ "file_path": "COCO-LM/fairseq/scripts/sacrebleu.sh", "repo_id": "COCO-LM", "token_count": 217 }
190
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import contextlib import logging import os import tempfile import unittest from io import StringIO import torch from fairseq import options from fairseq_cli import train from tests.utils import ( create_dummy_data, generate_main, preprocess_lm_data, preprocess_translation_data, train_translation_model, ) class TestTranslationGPU(unittest.TestCase): def setUp(self): logging.disable(logging.CRITICAL) def tearDown(self): logging.disable(logging.NOTSET) @unittest.skipIf(not torch.cuda.is_available(), "test requires a GPU") def test_fp16(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory("test_fp16") as data_dir: create_dummy_data(data_dir) preprocess_translation_data(data_dir) train_translation_model(data_dir, "fconv_iwslt_de_en", ["--fp16"]) generate_main(data_dir) @unittest.skipIf(not torch.cuda.is_available(), "test requires a GPU") def test_memory_efficient_fp16(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory("test_memory_efficient_fp16") as data_dir: create_dummy_data(data_dir) preprocess_translation_data(data_dir) train_translation_model( data_dir, "fconv_iwslt_de_en", ["--memory-efficient-fp16"] ) generate_main(data_dir) @unittest.skipIf(not torch.cuda.is_available(), "test requires a GPU") def test_transformer_fp16(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory("test_transformer") as data_dir: create_dummy_data(data_dir) preprocess_translation_data(data_dir) train_translation_model( data_dir, "transformer_iwslt_de_en", [ "--encoder-layers", "2", "--decoder-layers", "2", "--encoder-embed-dim", "64", "--decoder-embed-dim", "64", "--fp16", ], run_validation=True, ) generate_main(data_dir) @unittest.skipIf(not torch.cuda.is_available(), "test requires a GPU") def test_levenshtein_transformer(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory( "test_levenshtein_transformer" ) as data_dir: create_dummy_data(data_dir) preprocess_translation_data(data_dir, ["--joined-dictionary"]) train_translation_model( data_dir, "levenshtein_transformer", [ "--apply-bert-init", "--early-exit", "6,6,6", "--criterion", "nat_loss", ], task="translation_lev", ) gen_config = [ "--task", "translation_lev", "--iter-decode-max-iter", "9", "--iter-decode-eos-penalty", "0", "--print-step", ] # non-ensemble generation generate_main(data_dir, gen_config) # ensemble generation generate_main( data_dir, gen_config, path=os.pathsep.join([ os.path.join(data_dir, "checkpoint_last.pt"), os.path.join(data_dir, "checkpoint_last.pt"), ]), ) def _quantize_language_model(data_dir, arch, extra_flags=None, run_validation=False): train_parser = options.get_training_parser() train_args = options.parse_args_and_arch( train_parser, [ "--task", "language_modeling", data_dir, "--arch", arch, "--optimizer", "adam", "--lr", "0.0001", "--criterion", "adaptive_loss", "--adaptive-softmax-cutoff", "5,10,15", "--max-tokens", "500", "--tokens-per-sample", "500", "--save-dir", data_dir, "--max-epoch", "1", "--no-progress-bar", "--distributed-world-size", "1", "--ddp-backend", "no_c10d", "--num-workers", "0", ] + (extra_flags or []), ) train.main(train_args) # try scalar quantization scalar_quant_train_parser = options.get_training_parser() scalar_quant_train_args = options.parse_args_and_arch( scalar_quant_train_parser, [ "--task", "language_modeling", data_dir, "--arch", arch, "--optimizer", "adam", "--lr", "0.0001", "--criterion", "adaptive_loss", "--adaptive-softmax-cutoff", "5,10,15", "--max-tokens", "500", "--tokens-per-sample", "500", "--save-dir", data_dir, "--max-update", "3", "--no-progress-bar", "--distributed-world-size", "1", "--ddp-backend", "no_c10d", "--num-workers", "0", "--quant-noise-scalar", "0.5", ] + (extra_flags or []), ) train.main(scalar_quant_train_args) # try iterative PQ quantization quantize_parser = options.get_training_parser() quantize_args = options.parse_args_and_arch( quantize_parser, [ "--task", "language_modeling", data_dir, "--arch", arch, "--optimizer", "adam", "--lr", "0.0001", "--criterion", "adaptive_loss", "--adaptive-softmax-cutoff", "5,10,15", "--max-tokens", "50", "--tokens-per-sample", "50", "--max-update", "6", "--no-progress-bar", "--distributed-world-size", "1", "--ddp-backend", "no_c10d", "--num-workers", "0", "--restore-file", os.path.join(data_dir, "checkpoint_last.pt"), "--reset-optimizer", "--quantization-config-path", os.path.join( os.path.dirname(__file__), "transformer_quantization_config.yaml" ), ] + (extra_flags or []), ) train.main(quantize_args) class TestQuantization(unittest.TestCase): def setUp(self): logging.disable(logging.CRITICAL) def tearDown(self): logging.disable(logging.NOTSET) @unittest.skipIf(not torch.cuda.is_available(), "test requires a GPU") def test_quantization(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory("test_quantization") as data_dir: create_dummy_data(data_dir) preprocess_lm_data(data_dir) # tests both scalar and iterative PQ quantization _quantize_language_model(data_dir, "transformer_lm") class TestOptimizersGPU(unittest.TestCase): def setUp(self): logging.disable(logging.CRITICAL) def tearDown(self): logging.disable(logging.NOTSET) @unittest.skipIf(not torch.cuda.is_available(), "test requires a GPU") def test_flat_grads(self): with contextlib.redirect_stdout(StringIO()): with tempfile.TemporaryDirectory("test_flat_grads") as data_dir: # Use just a bit of data and tiny model to keep this test runtime reasonable create_dummy_data(data_dir, num_examples=10, maxlen=5) preprocess_translation_data(data_dir) with self.assertRaises(RuntimeError): # adafactor isn't compatible with flat grads, which # are used by default with --fp16 train_translation_model( data_dir, "lstm", [ "--required-batch-size-multiple", "1", "--encoder-layers", "1", "--encoder-hidden-size", "32", "--decoder-layers", "1", "--optimizer", "adafactor", "--fp16", ], ) # but it should pass once we set --fp16-no-flatten-grads train_translation_model( data_dir, "lstm", [ "--required-batch-size-multiple", "1", "--encoder-layers", "1", "--encoder-hidden-size", "32", "--decoder-layers", "1", "--optimizer", "adafactor", "--fp16", "--fp16-no-flatten-grads", ], ) if __name__ == "__main__": unittest.main()
COCO-LM/fairseq/tests/gpu/test_binaries_gpu.py/0
{ "file_path": "COCO-LM/fairseq/tests/gpu/test_binaries_gpu.py", "repo_id": "COCO-LM", "token_count": 6032 }
191
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import unittest import torch import torch.nn as nn from fairseq.modules import ConvTBC class TestConvTBC(unittest.TestCase): def test_convtbc(self): # ksz, in_channels, out_channels conv_tbc = ConvTBC(4, 5, kernel_size=3, padding=1) # out_channels, in_channels, ksz conv1d = nn.Conv1d(4, 5, kernel_size=3, padding=1) conv_tbc.weight.data.copy_(conv1d.weight.data.transpose(0, 2)) conv_tbc.bias.data.copy_(conv1d.bias.data) input_tbc = torch.randn(7, 2, 4, requires_grad=True) input1d = input_tbc.data.transpose(0, 1).transpose(1, 2) input1d.requires_grad = True output_tbc = conv_tbc(input_tbc) output1d = conv1d(input1d) self.assertAlmostEqual( output_tbc.data.transpose(0, 1).transpose(1, 2), output1d.data ) grad_tbc = torch.randn(output_tbc.size()) grad1d = grad_tbc.transpose(0, 1).transpose(1, 2).contiguous() output_tbc.backward(grad_tbc) output1d.backward(grad1d) self.assertAlmostEqual( conv_tbc.weight.grad.data.transpose(0, 2), conv1d.weight.grad.data ) self.assertAlmostEqual(conv_tbc.bias.grad.data, conv1d.bias.grad.data) self.assertAlmostEqual( input_tbc.grad.data.transpose(0, 1).transpose(1, 2), input1d.grad.data ) def assertAlmostEqual(self, t1, t2): self.assertEqual(t1.size(), t2.size(), "size mismatch") self.assertLess((t1 - t2).abs().max(), 1e-4) if __name__ == "__main__": unittest.main()
COCO-LM/fairseq/tests/test_convtbc.py/0
{ "file_path": "COCO-LM/fairseq/tests/test_convtbc.py", "repo_id": "COCO-LM", "token_count": 820 }
192
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import unittest from collections import OrderedDict import numpy as np import torch from fairseq.data import LanguagePairDataset, TokenBlockDataset from fairseq.data.multi_corpus_sampled_dataset import MultiCorpusSampledDataset from tests.test_train import mock_dict class TestMultiCorpusSampledDataset(unittest.TestCase): def setUp(self): d = mock_dict() tokens_1 = torch.LongTensor([1]).view(1, -1) tokens_ds1 = TokenBlockDataset( tokens_1, sizes=[tokens_1.size(-1)], block_size=1, pad=0, eos=1, include_targets=False, ) self.dataset_1 = LanguagePairDataset( tokens_ds1, tokens_ds1.sizes, d, shuffle=False ) tokens_2 = torch.LongTensor([2]).view(1, -1) tokens_ds2 = TokenBlockDataset( tokens_2, sizes=[tokens_2.size(-1)], block_size=1, pad=0, eos=1, include_targets=False, ) self.dataset_2 = LanguagePairDataset( tokens_ds2, tokens_ds2.sizes, d, shuffle=False ) def _test_sample_helper( self, expected_sample_from_first_ds_percentage, num_samples=1000, sampling_func=None, ): # To make sure test is not flaky np.random.seed(0) if sampling_func is None: m = MultiCorpusSampledDataset( OrderedDict({0: self.dataset_1, 1: self.dataset_2}), ) else: m = MultiCorpusSampledDataset( OrderedDict({0: self.dataset_1, 1: self.dataset_2}), sampling_func=sampling_func, ) m.ordered_indices() count_sample_from_first_dataset = 0 for _ in range(num_samples): if m.collater([m[0], m[1]])["net_input"]["src_tokens"][0] == 1: count_sample_from_first_dataset += 1 sample_from_first_ds_percentage = ( 1.0 * count_sample_from_first_dataset / num_samples ) self.assertLess( abs( sample_from_first_ds_percentage - expected_sample_from_first_ds_percentage ), 0.01, ) def test_multi_corpus_sampled_dataset_uniform_sample(self): self._test_sample_helper(expected_sample_from_first_ds_percentage=0.5) def test_multi_corpus_sampled_dataset_weighted_sample(self): def naive_weighted_sample(weights): def f(l): v = np.random.random() agg = 0 for i, weight in enumerate(weights): agg += weight if agg > v: return i return f self._test_sample_helper( expected_sample_from_first_ds_percentage=0.9, sampling_func=naive_weighted_sample(weights=[0.9, 0.1]), )
COCO-LM/fairseq/tests/test_multi_corpus_sampled_dataset.py/0
{ "file_path": "COCO-LM/fairseq/tests/test_multi_corpus_sampled_dataset.py", "repo_id": "COCO-LM", "token_count": 1624 }
193
# Copyright (c) Microsoft Corporation. # Licensed under the MIT license. # The script is largely adapted from the huggingface transformers library import torch import logging from transformers.modeling_utils import cached_path, WEIGHTS_NAME, TF2_WEIGHTS_NAME, TF_WEIGHTS_NAME logger = logging.getLogger(__name__) def get_checkpoint_from_transformer_cache( archive_file, pretrained_model_name_or_path, pretrained_model_archive_map, cache_dir, force_download, proxies, resume_download, ): try: resolved_archive_file = cached_path(archive_file, cache_dir=cache_dir, force_download=force_download, proxies=proxies, resume_download=resume_download) except EnvironmentError: if pretrained_model_name_or_path in pretrained_model_archive_map: msg = "Couldn't reach server at '{}' to download pretrained weights.".format( archive_file) else: msg = "Model name '{}' was not found in model name list ({}). " \ "We assumed '{}' was a path or url to model weight files named one of {} but " \ "couldn't find any such file at this path or url.".format( pretrained_model_name_or_path, ', '.join(pretrained_model_archive_map.keys()), archive_file, [WEIGHTS_NAME, TF2_WEIGHTS_NAME, TF_WEIGHTS_NAME]) raise EnvironmentError(msg) if resolved_archive_file == archive_file: logger.info("loading weights file {}".format(archive_file)) else: logger.info("loading weights file {} from cache at {}".format( archive_file, resolved_archive_file)) return torch.load(resolved_archive_file, map_location='cpu')
COCO-LM/huggingface/cocolm/convert_state_dict.py/0
{ "file_path": "COCO-LM/huggingface/cocolm/convert_state_dict.py", "repo_id": "COCO-LM", "token_count": 713 }
194
datadir: /data/CMIP6/AWI-ESM name: temperature cmip_name: ta era_name: t run: r1i1p1f1 res: - 1.40625 # - 5.625
ClimaX/snakemake_configs/AWI-ESM/config_temperature.yml/0
{ "file_path": "ClimaX/snakemake_configs/AWI-ESM/config_temperature.yml", "repo_id": "ClimaX", "token_count": 61 }
195
datadir: /data/CMIP6/HAMMOZ name: v_component_of_wind cmip_name: va era_name: v run: r1i1p1f1 version: v20190628 res: - 1.40625 # - 5.625
ClimaX/snakemake_configs/HAMMOZ/config_v_component_of_wind.yml/0
{ "file_path": "ClimaX/snakemake_configs/HAMMOZ/config_v_component_of_wind.yml", "repo_id": "ClimaX", "token_count": 73 }
196
datadir: /data/CMIP6/TaiESM1 server_prefix: https://esgf.ceda.ac.uk/thredds/fileServer/esg_cmip6/CMIP6/CMIP name: v_component_of_wind cmip_name: va era_name: v run: r1i1p1f1 res: - 1.40625 # - 5.625
ClimaX/snakemake_configs/TaiESM1/config_v_component_of_wind.yml/0
{ "file_path": "ClimaX/snakemake_configs/TaiESM1/config_v_component_of_wind.yml", "repo_id": "ClimaX", "token_count": 105 }
197
# Copyright (c) Microsoft Corporation. # Licensed under the MIT license. import math import os import random import numpy as np import torch from torch.utils.data import IterableDataset class NpyReader(IterableDataset): def __init__( self, file_list, start_idx, end_idx, variables, out_variables, shuffle: bool = False, multi_dataset_training=False, ) -> None: super().__init__() start_idx = int(start_idx * len(file_list)) end_idx = int(end_idx * len(file_list)) file_list = file_list[start_idx:end_idx] self.file_list = [f for f in file_list if "climatology" not in f] self.variables = variables self.out_variables = out_variables if out_variables is not None else variables self.shuffle = shuffle self.multi_dataset_training = multi_dataset_training def __iter__(self): if self.shuffle: random.shuffle(self.file_list) worker_info = torch.utils.data.get_worker_info() if worker_info is None: iter_start = 0 iter_end = len(self.file_list) else: if not torch.distributed.is_initialized(): rank = 0 world_size = 1 else: rank = torch.distributed.get_rank() world_size = torch.distributed.get_world_size() num_workers_per_ddp = worker_info.num_workers if self.multi_dataset_training: num_nodes = int(os.environ.get("NODES", None)) num_gpus_per_node = int(world_size / num_nodes) num_shards = num_workers_per_ddp * num_gpus_per_node rank = rank % num_gpus_per_node else: num_shards = num_workers_per_ddp * world_size per_worker = int(math.floor(len(self.file_list) / float(num_shards))) worker_id = rank * num_workers_per_ddp + worker_info.id iter_start = worker_id * per_worker iter_end = iter_start + per_worker for idx in range(iter_start, iter_end): path = self.file_list[idx] data = np.load(path) yield {k: data[k] for k in self.variables}, self.variables, self.out_variables class Forecast(IterableDataset): def __init__( self, dataset: NpyReader, max_predict_range: int = 6, random_lead_time: bool = False, hrs_each_step: int = 1 ) -> None: super().__init__() self.dataset = dataset self.max_predict_range = max_predict_range self.random_lead_time = random_lead_time self.hrs_each_step = hrs_each_step def __iter__(self): for data, variables, out_variables in self.dataset: x = np.concatenate([data[k].astype(np.float32) for k in data.keys()], axis=1) x = torch.from_numpy(x) y = np.concatenate([data[k].astype(np.float32) for k in out_variables], axis=1) y = torch.from_numpy(y) inputs = x[: -self.max_predict_range] # N, C, H, W if self.random_lead_time: predict_ranges = torch.randint(low=1, high=self.max_predict_range, size=(inputs.shape[0],)) else: predict_ranges = torch.ones(inputs.shape[0]).to(torch.long) * self.max_predict_range lead_times = self.hrs_each_step * predict_ranges / 100 lead_times = lead_times.to(inputs.dtype) output_ids = torch.arange(inputs.shape[0]) + predict_ranges outputs = y[output_ids] yield inputs, outputs, lead_times, variables, out_variables class IndividualForecastDataIter(IterableDataset): def __init__(self, dataset, transforms: torch.nn.Module, output_transforms: torch.nn.Module, region_info = None): super().__init__() self.dataset = dataset self.transforms = transforms self.output_transforms = output_transforms self.region_info = region_info def __iter__(self): for (inp, out, lead_times, variables, out_variables) in self.dataset: assert inp.shape[0] == out.shape[0] for i in range(inp.shape[0]): if self.region_info is not None: yield self.transforms(inp[i]), self.output_transforms(out[i]), lead_times[i], variables, out_variables, self.region_info else: yield self.transforms(inp[i]), self.output_transforms(out[i]), lead_times[i], variables, out_variables class ShuffleIterableDataset(IterableDataset): def __init__(self, dataset, buffer_size: int) -> None: super().__init__() assert buffer_size > 0 self.dataset = dataset self.buffer_size = buffer_size def __iter__(self): buf = [] for x in self.dataset: if len(buf) == self.buffer_size: idx = random.randint(0, self.buffer_size - 1) yield buf[idx] buf[idx] = x else: buf.append(x) random.shuffle(buf) while buf: yield buf.pop()
ClimaX/src/climax/pretrain/dataset.py/0
{ "file_path": "ClimaX/src/climax/pretrain/dataset.py", "repo_id": "ClimaX", "token_count": 2475 }
198
import torch from climax.arch import ClimaX def test_parallel_patch_embed(): vars = tuple(["a", "b", "c"]) x = torch.rand(4, len(vars), 32, 64) serial_model = ClimaX(vars, img_size=[32, 64], patch_size=4, embed_dim=128, parallel_patch_embed=False) parallel_model = ClimaX(vars, img_size=[32, 64], patch_size=4, embed_dim=128, parallel_patch_embed=True) assert serial_model.token_embeds[0].num_patches == parallel_model.num_patches assert len(serial_model.token_embeds) == len(parallel_model.token_embeds.proj_weights) for i in range(len(serial_model.token_embeds)): serial_model.token_embeds[i].proj.weight.data = parallel_model.token_embeds.proj_weights[i].data serial_model.token_embeds[i].proj.bias.data = parallel_model.token_embeds.proj_biases[i].data test_vars = [["a"], ["b"], ["c"], ["a", "b"], ["a", "c"], ["b", "c"], ["a", "b", "c"]] for vars in test_vars: vars = tuple(vars) serial_var_ids = serial_model.get_var_ids(vars, x.device) parallel_var_ids = parallel_model.get_var_ids(vars, x.device) assert all(serial_var_ids == parallel_var_ids) x = torch.rand(4, len(vars), 32, 64) parallel_embed = parallel_model.token_embeds(x, parallel_var_ids) embeds = [] for i in range(len(serial_var_ids)): id = serial_var_ids[i] embeds.append(serial_model.token_embeds[id](x[:, i : i + 1, :, :])) serial_embed = torch.stack(embeds, dim=1) assert parallel_embed.shape == serial_embed.shape assert torch.allclose(serial_embed, parallel_embed) if __name__ == "__main__": test_parallel_patch_embed()
ClimaX/tests/test_parallel_patch_embed.py/0
{ "file_path": "ClimaX/tests/test_parallel_patch_embed.py", "repo_id": "ClimaX", "token_count": 726 }
199
# Copyright (c) Microsoft Corporation. # Licensed under the MIT License. import torch import torch.nn as nn import torch.nn.functional as F import util.util as util from models.networks.base_network import BaseNetwork from models.networks.architecture import ResidualBlock from models.networks.normalization import get_nonspade_norm_layer from models.networks.architecture import SPADEResnetBlock """patch match""" from models.networks.patch_match import PatchMatchGRU from models.networks.ops import * def match_kernel_and_pono_c(feature, match_kernel, PONO_C, eps=1e-10): b, c, h, w = feature.size() if match_kernel == 1: feature = feature.view(b, c, -1) else: feature = F.unfold(feature, kernel_size=match_kernel, padding=int(match_kernel//2)) dim_mean = 1 if PONO_C else -1 feature = feature - feature.mean(dim=dim_mean, keepdim=True) feature_norm = torch.norm(feature, 2, 1, keepdim=True) + eps feature = torch.div(feature, feature_norm) return feature.view(b, -1, h, w) """512x512""" class AdaptiveFeatureGenerator(BaseNetwork): @staticmethod def modify_commandline_options(parser, is_train): return parser def __init__(self, opt): super().__init__() self.opt = opt kw = opt.featEnc_kernel pw = int((kw-1)//2) nf = opt.nef norm_layer = get_nonspade_norm_layer(opt, opt.norm_E) self.layer1 = norm_layer(nn.Conv2d(opt.spade_ic, nf, 3, stride=1, padding=pw)) self.layer2 = nn.Sequential( norm_layer(nn.Conv2d(nf * 1, nf * 2, 3, 1, 1)), ResidualBlock(nf * 2, nf * 2), ) self.layer3 = nn.Sequential( norm_layer(nn.Conv2d(nf * 2, nf * 4, kw, stride=2, padding=pw)), ResidualBlock(nf * 4, nf * 4), ) self.layer4 = nn.Sequential( norm_layer(nn.Conv2d(nf * 4, nf * 4, kw, stride=2, padding=pw)), ResidualBlock(nf * 4, nf * 4), ) self.layer5 = nn.Sequential( norm_layer(nn.Conv2d(nf * 4, nf * 4, kw, stride=2, padding=pw)), ResidualBlock(nf * 4, nf * 4), ) self.head_0 = SPADEResnetBlock(nf * 4, nf * 4, opt) self.G_middle_0 = SPADEResnetBlock(nf * 4, nf * 4, opt) self.G_middle_1 = SPADEResnetBlock(nf * 4, nf * 2, opt) self.G_middle_2 = SPADEResnetBlock(nf * 2, nf * 1, opt) self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True) def forward(self, input, seg): # 512 x1 = self.layer1(input) # 512 x2 = self.layer2(self.actvn(x1)) # 256 x3 = self.layer3(self.actvn(x2)) # 128 x4 = self.layer4(self.actvn(x3)) # 64 x5 = self.layer5(self.actvn(x4)) # bottleneck x6 = self.head_0(x5, seg) # 128 x7 = self.G_middle_0(self.up(x6) + x4, seg) # 256 x8 = self.G_middle_1(self.up(x7) + x3, seg) # 512 x9 = self.G_middle_2(self.up(x8) + x2, seg) return [x6, x7, x8, x9] def actvn(self, x): return F.leaky_relu(x, 2e-1) class NoVGGHPMCorrespondence(BaseNetwork): def __init__(self, opt): self.opt = opt super().__init__() opt.spade_ic = opt.semantic_nc self.adaptive_model_seg = AdaptiveFeatureGenerator(opt) opt.spade_ic = 3 + opt.semantic_nc self.adaptive_model_img = AdaptiveFeatureGenerator(opt) del opt.spade_ic self.batch_size = opt.batchSize """512x512""" feature_channel = opt.nef self.phi_0 = nn.Conv2d(in_channels=feature_channel*4, out_channels=feature_channel, kernel_size=1, stride=1, padding=0) self.phi_1 = nn.Conv2d(in_channels=feature_channel*4, out_channels=feature_channel, kernel_size=1, stride=1, padding=0) self.phi_2 = nn.Conv2d(in_channels=feature_channel*2, out_channels=feature_channel, kernel_size=1, stride=1, padding=0) self.phi_3 = nn.Conv2d(in_channels=feature_channel, out_channels=feature_channel, kernel_size=1, stride=1, padding=0) self.theta_0 = nn.Conv2d(in_channels=feature_channel*4, out_channels=feature_channel, kernel_size=1, stride=1, padding=0) self.theta_1 = nn.Conv2d(in_channels=feature_channel*4, out_channels=feature_channel, kernel_size=1, stride=1, padding=0) self.theta_2 = nn.Conv2d(in_channels=feature_channel*2, out_channels=feature_channel, kernel_size=1, stride=1, padding=0) self.theta_3 = nn.Conv2d(in_channels=feature_channel, out_channels=feature_channel, kernel_size=1, stride=1, padding=0) self.patch_match = PatchMatchGRU(opt) """512x512""" def multi_scale_patch_match(self, f1, f2, ref, hierarchical_scale, pre=None, real_img=None): if hierarchical_scale == 0: y_cycle = None scale = 64 batch_size, channel, feature_height, feature_width = f1.size() ref = F.avg_pool2d(ref, 8, stride=8) ref = ref.view(batch_size, 3, scale * scale) f1 = f1.view(batch_size, channel, scale * scale) f2 = f2.view(batch_size, channel, scale * scale) matmul_result = torch.matmul(f1.permute(0, 2, 1), f2)/self.opt.temperature mat = F.softmax(matmul_result, dim=-1) y = torch.matmul(mat, ref.permute(0, 2, 1)) if self.opt.phase is 'train' and self.opt.weight_warp_cycle > 0: mat_cycle = F.softmax(matmul_result.transpose(1, 2), dim=-1) y_cycle = torch.matmul(mat_cycle, y) y_cycle = y_cycle.permute(0, 2, 1).view(batch_size, 3, scale, scale) y = y.permute(0, 2, 1).view(batch_size, 3, scale, scale) return mat, y, y_cycle if hierarchical_scale == 1: scale = 128 with torch.no_grad(): batch_size, channel, feature_height, feature_width = f1.size() topk_num = 1 search_window = 4 centering = 1 dilation = 2 total_candidate_num = topk_num * (search_window ** 2) topk_inds = torch.topk(pre, topk_num, dim=-1)[-1] inds = topk_inds.permute(0, 2, 1).view(batch_size, topk_num, (scale//2), (scale//2)).float() offset_x, offset_y = inds_to_offset(inds) dx = torch.arange(search_window, dtype=topk_inds.dtype, device=topk_inds.device).unsqueeze_(dim=1).expand(-1, search_window).contiguous().view(-1) - centering dy = torch.arange(search_window, dtype=topk_inds.dtype, device=topk_inds.device).unsqueeze_(dim=0).expand(search_window, -1).contiguous().view(-1) - centering dx = dx.view(1, search_window ** 2, 1, 1) * dilation dy = dy.view(1, search_window ** 2, 1, 1) * dilation offset_x_up = F.interpolate((2 * offset_x + dx), scale_factor=2) offset_y_up = F.interpolate((2 * offset_y + dy), scale_factor=2) ref = F.avg_pool2d(ref, 4, stride=4) ref = ref.view(batch_size, 3, scale * scale) mat, y = self.patch_match(f1, f2, ref, offset_x_up, offset_y_up) y = y.view(batch_size, 3, scale, scale) return mat, y if hierarchical_scale == 2: scale = 256 with torch.no_grad(): batch_size, channel, feature_height, feature_width = f1.size() topk_num = 1 search_window = 4 centering = 1 dilation = 2 total_candidate_num = topk_num * (search_window ** 2) topk_inds = pre[:, :, :topk_num] inds = topk_inds.permute(0, 2, 1).view(batch_size, topk_num, (scale//2), (scale//2)).float() offset_x, offset_y = inds_to_offset(inds) dx = torch.arange(search_window, dtype=topk_inds.dtype, device=topk_inds.device).unsqueeze_(dim=1).expand(-1, search_window).contiguous().view(-1) - centering dy = torch.arange(search_window, dtype=topk_inds.dtype, device=topk_inds.device).unsqueeze_(dim=0).expand(search_window, -1).contiguous().view(-1) - centering dx = dx.view(1, search_window ** 2, 1, 1) * dilation dy = dy.view(1, search_window ** 2, 1, 1) * dilation offset_x_up = F.interpolate((2 * offset_x + dx), scale_factor=2) offset_y_up = F.interpolate((2 * offset_y + dy), scale_factor=2) ref = F.avg_pool2d(ref, 2, stride=2) ref = ref.view(batch_size, 3, scale * scale) mat, y = self.patch_match(f1, f2, ref, offset_x_up, offset_y_up) y = y.view(batch_size, 3, scale, scale) return mat, y if hierarchical_scale == 3: scale = 512 with torch.no_grad(): batch_size, channel, feature_height, feature_width = f1.size() topk_num = 1 search_window = 4 centering = 1 dilation = 2 total_candidate_num = topk_num * (search_window ** 2) topk_inds = pre[:, :, :topk_num] inds = topk_inds.permute(0, 2, 1).view(batch_size, topk_num, (scale//2), (scale//2)).float() offset_x, offset_y = inds_to_offset(inds) dx = torch.arange(search_window, dtype=topk_inds.dtype, device=topk_inds.device).unsqueeze_(dim=1).expand(-1, search_window).contiguous().view(-1) - centering dy = torch.arange(search_window, dtype=topk_inds.dtype, device=topk_inds.device).unsqueeze_(dim=0).expand(search_window, -1).contiguous().view(-1) - centering dx = dx.view(1, search_window ** 2, 1, 1) * dilation dy = dy.view(1, search_window ** 2, 1, 1) * dilation offset_x_up = F.interpolate((2 * offset_x + dx), scale_factor=2) offset_y_up = F.interpolate((2 * offset_y + dy), scale_factor=2) ref = ref.view(batch_size, 3, scale * scale) mat, y = self.patch_match(f1, f2, ref, offset_x_up, offset_y_up) y = y.view(batch_size, 3, scale, scale) return mat, y def forward(self, ref_img, real_img, seg_map, ref_seg_map): corr_out = {} seg_input = seg_map adaptive_feature_seg = self.adaptive_model_seg(seg_input, seg_input) ref_input = torch.cat((ref_img, ref_seg_map), dim=1) adaptive_feature_img = self.adaptive_model_img(ref_input, ref_input) for i in range(len(adaptive_feature_seg)): adaptive_feature_seg[i] = util.feature_normalize(adaptive_feature_seg[i]) adaptive_feature_img[i] = util.feature_normalize(adaptive_feature_img[i]) if self.opt.isTrain and self.opt.weight_novgg_featpair > 0: real_input = torch.cat((real_img, seg_map), dim=1) adaptive_feature_img_pair = self.adaptive_model_img(real_input, real_input) loss_novgg_featpair = 0 weights = [1.0, 1.0, 1.0, 1.0] for i in range(len(adaptive_feature_img_pair)): adaptive_feature_img_pair[i] = util.feature_normalize(adaptive_feature_img_pair[i]) loss_novgg_featpair += F.l1_loss(adaptive_feature_seg[i], adaptive_feature_img_pair[i]) * weights[i] corr_out['loss_novgg_featpair'] = loss_novgg_featpair * self.opt.weight_novgg_featpair cont_features = adaptive_feature_seg ref_features = adaptive_feature_img theta = [] phi = [] """512x512""" theta.append(match_kernel_and_pono_c(self.theta_0(cont_features[0]), self.opt.match_kernel, self.opt.PONO_C)) theta.append(match_kernel_and_pono_c(self.theta_1(cont_features[1]), self.opt.match_kernel, self.opt.PONO_C)) theta.append(match_kernel_and_pono_c(self.theta_2(cont_features[2]), self.opt.match_kernel, self.opt.PONO_C)) theta.append(match_kernel_and_pono_c(self.theta_3(cont_features[3]), self.opt.match_kernel, self.opt.PONO_C)) phi.append(match_kernel_and_pono_c(self.phi_0(ref_features[0]), self.opt.match_kernel, self.opt.PONO_C)) phi.append(match_kernel_and_pono_c(self.phi_1(ref_features[1]), self.opt.match_kernel, self.opt.PONO_C)) phi.append(match_kernel_and_pono_c(self.phi_2(ref_features[2]), self.opt.match_kernel, self.opt.PONO_C)) phi.append(match_kernel_and_pono_c(self.phi_3(ref_features[3]), self.opt.match_kernel, self.opt.PONO_C)) ref = ref_img ys = [] m = None for i in range(len(theta)): if i == 0: m, y, y_cycle = self.multi_scale_patch_match(theta[i], phi[i], ref, i, pre=m) if y_cycle is not None: corr_out['warp_cycle'] = y_cycle else: m, y = self.multi_scale_patch_match(theta[i], phi[i], ref, i, pre=m) ys.append(y) corr_out['warp_out'] = ys return corr_out
CoCosNet-v2/models/networks/correspondence.py/0
{ "file_path": "CoCosNet-v2/models/networks/correspondence.py", "repo_id": "CoCosNet-v2", "token_count": 6521 }
200
# Copyright (c) Microsoft Corporation. # Licensed under the MIT License. import os import copy import sys import torch from models.pix2pix_model import Pix2PixModel try: from torch.cuda.amp import GradScaler except: # dummy GradScaler for PyTorch < 1.6 class GradScaler: def __init__(self, enabled): pass def scale(self, loss): return loss def unscale_(self, optimizer): pass def step(self, optimizer): optimizer.step() def update(self): pass class Pix2PixTrainer(): """ Trainer creates the model and optimizers, and uses them to updates the weights of the network while reporting losses and the latest visuals to visualize the progress in training. """ def __init__(self, opt, resume_epoch=0): self.opt = opt self.pix2pix_model = Pix2PixModel(opt) if len(opt.gpu_ids) > 1: self.pix2pix_model = torch.nn.DataParallel(self.pix2pix_model, device_ids=opt.gpu_ids) self.pix2pix_model_on_one_gpu = self.pix2pix_model.module else: self.pix2pix_model.to(opt.gpu_ids[0]) self.pix2pix_model_on_one_gpu = self.pix2pix_model self.generated = None if opt.isTrain: self.optimizer_G, self.optimizer_D = self.pix2pix_model_on_one_gpu.create_optimizers(opt) self.old_lr = opt.lr if opt.continue_train and opt.which_epoch == 'latest': try: load_path = os.path.join(opt.checkpoints_dir, opt.name, 'optimizer.pth') checkpoint = torch.load(load_path) self.optimizer_G.load_state_dict(checkpoint['G']) self.optimizer_D.load_state_dict(checkpoint['D']) except FileNotFoundError as err: print(err) print('Not find optimizer state dict: ' + load_path + '. Do not load optimizer!') self.last_data, self.last_netCorr, self.last_netG, self.last_optimizer_G = None, None, None, None self.g_losses = {} self.d_losses = {} self.scaler = GradScaler(enabled=self.opt.amp) def run_generator_one_step(self, data): self.optimizer_G.zero_grad() g_losses, out = self.pix2pix_model(data, mode='generator') g_loss = sum(g_losses.values()).mean() # g_loss.backward() self.scaler.scale(g_loss).backward() self.scaler.unscale_(self.optimizer_G) # self.optimizer_G.step() self.scaler.step(self.optimizer_G) self.scaler.update() self.g_losses = g_losses self.out = out def run_discriminator_one_step(self, data): self.optimizer_D.zero_grad() GforD = {} GforD['fake_image'] = self.out['fake_image'] GforD['adaptive_feature_seg'] = self.out['adaptive_feature_seg'] GforD['adaptive_feature_img'] = self.out['adaptive_feature_img'] d_losses = self.pix2pix_model(data, mode='discriminator', GforD=GforD) d_loss = sum(d_losses.values()).mean() # d_loss.backward() self.scaler.scale(d_loss).backward() self.scaler.unscale_(self.optimizer_D) # self.optimizer_D.step() self.scaler.step(self.optimizer_D) self.scaler.update() self.d_losses = d_losses def get_latest_losses(self): return {**self.g_losses, **self.d_losses} def get_latest_generated(self): return self.out['fake_image'] def update_learning_rate(self, epoch): self.update_learning_rate(epoch) def save(self, epoch): self.pix2pix_model_on_one_gpu.save(epoch) if epoch == 'latest': torch.save({'G': self.optimizer_G.state_dict(), \ 'D': self.optimizer_D.state_dict(), \ 'lr': self.old_lr,}, \ os.path.join(self.opt.checkpoints_dir, self.opt.name, 'optimizer.pth')) def update_learning_rate(self, epoch): if epoch > self.opt.niter: lrd = self.opt.lr / self.opt.niter_decay new_lr = self.old_lr - lrd else: new_lr = self.old_lr if new_lr != self.old_lr: new_lr_G = new_lr new_lr_D = new_lr else: new_lr_G = self.old_lr new_lr_D = self.old_lr for param_group in self.optimizer_D.param_groups: param_group['lr'] = new_lr_D for param_group in self.optimizer_G.param_groups: param_group['lr'] = new_lr_G print('update learning rate: %f -> %f' % (self.old_lr, new_lr)) self.old_lr = new_lr
CoCosNet-v2/trainers/pix2pix_trainer.py/0
{ "file_path": "CoCosNet-v2/trainers/pix2pix_trainer.py", "repo_id": "CoCosNet-v2", "token_count": 2322 }
201
""" Copyright (C) 2019 NVIDIA Corporation. All rights reserved. Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). """ from .base_options import BaseOptions class TrainOptions(BaseOptions): def initialize(self, parser): BaseOptions.initialize(self, parser) # for displays parser.add_argument('--display_freq', type=int, default=2000, help='frequency of showing training results on screen') parser.add_argument('--print_freq', type=int, default=100, help='frequency of showing training results on console') parser.add_argument('--save_latest_freq', type=int, default=5000, help='frequency of saving the latest results') parser.add_argument('--save_epoch_freq', type=int, default=10, help='frequency of saving checkpoints at the end of epochs') # for training parser.add_argument('--continue_train', action='store_true', help='continue training: load the latest model') parser.add_argument('--which_epoch', type=str, default='latest', help='which epoch to load? set to latest to use latest cached model') parser.add_argument('--niter', type=int, default=100, help='# of iter at starting learning rate. This is NOT the total #epochs. Totla #epochs is niter + niter_decay') parser.add_argument('--niter_decay', type=int, default=100, help='# of iter to linearly decay learning rate to zero') parser.add_argument('--optimizer', type=str, default='adam') parser.add_argument('--beta1', type=float, default=0.5, help='momentum term of adam') parser.add_argument('--beta2', type=float, default=0.999, help='momentum term of adam') parser.add_argument('--lr', type=float, default=0.0002, help='initial learning rate for adam') parser.add_argument('--D_steps_per_G', type=int, default=1, help='number of discriminator iterations per generator iterations.') # for discriminators parser.add_argument('--ndf', type=int, default=64, help='# of discrim filters in first conv layer') parser.add_argument('--lambda_feat', type=float, default=10.0, help='weight for feature matching loss') parser.add_argument('--lambda_vgg', type=float, default=10.0, help='weight for vgg loss') parser.add_argument('--no_ganFeat_loss', action='store_true', help='if specified, do *not* use discriminator feature matching loss') parser.add_argument('--gan_mode', type=str, default='hinge', help='(ls|original|hinge)') parser.add_argument('--netD', type=str, default='multiscale', help='(n_layers|multiscale|image)') parser.add_argument('--no_TTUR', action='store_true', help='Use TTUR training scheme') parser.add_argument('--which_perceptual', type=str, default='5_2', help='relu5_2 or relu4_2') parser.add_argument('--weight_perceptual', type=float, default=0.01) parser.add_argument('--weight_mask', type=float, default=0.0, help='weight of warped mask loss, used in direct/cycle') parser.add_argument('--real_reference_probability', type=float, default=0.7, help='self-supervised training probability') parser.add_argument('--hard_reference_probability', type=float, default=0.2, help='hard reference training probability') parser.add_argument('--weight_gan', type=float, default=10.0, help='weight of all loss in stage1') parser.add_argument('--novgg_featpair', type=float, default=10.0, help='in no vgg setting, use pair feat loss in domain adaptation') parser.add_argument('--D_cam', type=float, default=0.0, help='weight of CAM loss in D') parser.add_argument('--warp_self_w', type=float, default=0.0, help='push warp self to ref') parser.add_argument('--fm_ratio', type=float, default=0.1, help='vgg fm loss weight comp with ctx loss') parser.add_argument('--use_22ctx', action='store_true', help='if true, also use 2-2 in ctx loss') parser.add_argument('--ctx_w', type=float, default=1.0, help='ctx loss weight') parser.add_argument('--mask_epoch', type=int, default=-1, help='useful when noise_for_mask is true, first train mask_epoch with mask, the rest epoch with noise') self.isTrain = True return parser
CoCosNet/options/train_options.py/0
{ "file_path": "CoCosNet/options/train_options.py", "repo_id": "CoCosNet", "token_count": 1501 }
202
# coding=utf-8 # Copyright 2018 The Google AI Language Team Authors and The HuggingFace Inc. team. # Copyright (c) 2018, NVIDIA CORPORATION. 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. """ Fine-tuning the library models for language modeling on a text file (GPT, GPT-2, BERT, RoBERTa). GPT and GPT-2 are fine-tuned using a causal language modeling (CLM) loss while BERT and RoBERTa are fine-tuned using a masked language modeling (MLM) loss. """ from __future__ import absolute_import import os import sys import bleu import pickle import torch import json import random import logging import argparse import numpy as np from io import open from itertools import cycle import torch.nn as nn from model import Seq2Seq from tqdm import tqdm, trange from torch.utils.data import DataLoader, Dataset, SequentialSampler, RandomSampler,TensorDataset from torch.utils.data.distributed import DistributedSampler from transformers import (WEIGHTS_NAME, AdamW, get_linear_schedule_with_warmup, RobertaConfig, RobertaModel, RobertaTokenizer) MODEL_CLASSES = {'roberta': (RobertaConfig, RobertaModel, RobertaTokenizer)} logging.basicConfig(format = '%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt = '%m/%d/%Y %H:%M:%S', level = logging.INFO) logger = logging.getLogger(__name__) class Example(object): """A single training/test example.""" def __init__(self, idx, source, target, ): self.idx = idx self.source = source self.target = target def read_examples(filename): """Read examples from filename.""" examples=[] with open(filename,encoding="utf-8") as f: for idx, line in enumerate(f): line=line.strip() js=json.loads(line) if 'idx' not in js: js['idx']=idx code=' '.join(js['code_tokens']).replace('\n',' ') code=' '.join(code.strip().split()) nl=' '.join(js['docstring_tokens']).replace('\n','') nl=' '.join(nl.strip().split()) examples.append( Example( idx = idx, source=code, target = nl, ) ) return examples class InputFeatures(object): """A single training/test features for a example.""" def __init__(self, example_id, source_ids, target_ids, source_mask, target_mask, ): self.example_id = example_id self.source_ids = source_ids self.target_ids = target_ids self.source_mask = source_mask self.target_mask = target_mask def convert_examples_to_features(examples, tokenizer, args,stage=None): features = [] for example_index, example in enumerate(examples): #source source_tokens = tokenizer.tokenize(example.source)[:args.max_source_length-2] source_tokens =[tokenizer.cls_token]+source_tokens+[tokenizer.sep_token] source_ids = tokenizer.convert_tokens_to_ids(source_tokens) source_mask = [1] * (len(source_tokens)) padding_length = args.max_source_length - len(source_ids) source_ids+=[tokenizer.pad_token_id]*padding_length source_mask+=[0]*padding_length #target if stage=="test": target_tokens = tokenizer.tokenize("None") else: target_tokens = tokenizer.tokenize(example.target)[:args.max_target_length-2] target_tokens = [tokenizer.cls_token]+target_tokens+[tokenizer.sep_token] target_ids = tokenizer.convert_tokens_to_ids(target_tokens) target_mask = [1] *len(target_ids) padding_length = args.max_target_length - len(target_ids) target_ids+=[tokenizer.pad_token_id]*padding_length target_mask+=[0]*padding_length if example_index < 5: if stage=='train': logger.info("*** Example ***") logger.info("idx: {}".format(example.idx)) logger.info("source_tokens: {}".format([x.replace('\u0120','_') for x in source_tokens])) logger.info("source_ids: {}".format(' '.join(map(str, source_ids)))) logger.info("source_mask: {}".format(' '.join(map(str, source_mask)))) logger.info("target_tokens: {}".format([x.replace('\u0120','_') for x in target_tokens])) logger.info("target_ids: {}".format(' '.join(map(str, target_ids)))) logger.info("target_mask: {}".format(' '.join(map(str, target_mask)))) features.append( InputFeatures( example_index, source_ids, target_ids, source_mask, target_mask, ) ) return features def set_seed(args): """set random seed.""" random.seed(args.seed) np.random.seed(args.seed) torch.manual_seed(args.seed) if args.n_gpu > 0: torch.cuda.manual_seed_all(args.seed) def main(): parser = argparse.ArgumentParser() ## Required parameters parser.add_argument("--model_type", default=None, type=str, required=True, help="Model type: e.g. roberta") parser.add_argument("--model_name_or_path", default=None, type=str, required=True, help="Path to pre-trained model: e.g. roberta-base" ) parser.add_argument("--output_dir", default=None, type=str, required=True, help="The output directory where the model predictions and checkpoints will be written.") parser.add_argument("--load_model_path", default=None, type=str, help="Path to trained model: Should contain the .bin files" ) ## Other parameters parser.add_argument("--train_filename", default=None, type=str, help="The train filename. Should contain the .jsonl files for this task.") parser.add_argument("--dev_filename", default=None, type=str, help="The dev filename. Should contain the .jsonl files for this task.") parser.add_argument("--test_filename", default=None, type=str, help="The test filename. Should contain the .jsonl files for this task.") parser.add_argument("--config_name", default="", type=str, help="Pretrained config name or path if not the same as model_name") parser.add_argument("--tokenizer_name", default="", type=str, help="Pretrained tokenizer name or path if not the same as model_name") parser.add_argument("--max_source_length", default=64, type=int, help="The maximum total source sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded.") parser.add_argument("--max_target_length", default=32, type=int, help="The maximum total target sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded.") parser.add_argument("--do_train", action='store_true', help="Whether to run training.") parser.add_argument("--do_eval", action='store_true', help="Whether to run eval on the dev set.") parser.add_argument("--do_test", action='store_true', help="Whether to run eval on the dev set.") parser.add_argument("--do_lower_case", action='store_true', help="Set this flag if you are using an uncased model.") parser.add_argument("--no_cuda", action='store_true', help="Avoid using CUDA when available") parser.add_argument("--train_batch_size", default=8, type=int, help="Batch size per GPU/CPU for training.") parser.add_argument("--eval_batch_size", default=8, type=int, help="Batch size per GPU/CPU for evaluation.") parser.add_argument('--gradient_accumulation_steps', type=int, default=1, help="Number of updates steps to accumulate before performing a backward/update pass.") parser.add_argument("--learning_rate", default=5e-5, type=float, help="The initial learning rate for Adam.") parser.add_argument("--beam_size", default=10, type=int, help="beam size for beam search") parser.add_argument("--weight_decay", default=0.0, type=float, help="Weight deay if we apply some.") parser.add_argument("--adam_epsilon", default=1e-8, type=float, help="Epsilon for Adam optimizer.") parser.add_argument("--max_grad_norm", default=1.0, type=float, help="Max gradient norm.") parser.add_argument("--num_train_epochs", default=3.0, type=float, help="Total number of training epochs to perform.") parser.add_argument("--max_steps", default=-1, type=int, help="If > 0: set total number of training steps to perform. Override num_train_epochs.") parser.add_argument("--eval_steps", default=-1, type=int, help="") parser.add_argument("--train_steps", default=-1, type=int, help="") parser.add_argument("--warmup_steps", default=0, type=int, help="Linear warmup over warmup_steps.") parser.add_argument("--local_rank", type=int, default=-1, help="For distributed training: local_rank") parser.add_argument('--seed', type=int, default=42, help="random seed for initialization") # print arguments args = parser.parse_args() logger.info(args) # Setup CUDA, GPU & distributed training if args.local_rank == -1 or args.no_cuda: device = torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu") args.n_gpu = torch.cuda.device_count() else: # Initializes the distributed backend which will take care of sychronizing nodes/GPUs torch.cuda.set_device(args.local_rank) device = torch.device("cuda", args.local_rank) torch.distributed.init_process_group(backend='nccl') args.n_gpu = 1 logger.warning("Process rank: %s, device: %s, n_gpu: %s, distributed training: %s", args.local_rank, device, args.n_gpu, bool(args.local_rank != -1)) args.device = device # Set seed set_seed(args) # make dir if output_dir not exist if os.path.exists(args.output_dir) is False: os.makedirs(args.output_dir) config_class, model_class, tokenizer_class = MODEL_CLASSES[args.model_type] config = config_class.from_pretrained(args.config_name if args.config_name else args.model_name_or_path) tokenizer = tokenizer_class.from_pretrained(args.tokenizer_name if args.tokenizer_name else args.model_name_or_path,do_lower_case=args.do_lower_case) #build model encoder = model_class.from_pretrained(args.model_name_or_path,config=config) decoder_layer = nn.TransformerDecoderLayer(d_model=config.hidden_size, nhead=config.num_attention_heads) decoder = nn.TransformerDecoder(decoder_layer, num_layers=6) model=Seq2Seq(encoder=encoder,decoder=decoder,config=config, beam_size=args.beam_size,max_length=args.max_target_length, sos_id=tokenizer.cls_token_id,eos_id=tokenizer.sep_token_id) if args.load_model_path is not None: logger.info("reload model from {}".format(args.load_model_path)) model.load_state_dict(torch.load(args.load_model_path)) model.to(device) if args.local_rank != -1: # Distributed training try: from apex.parallel import DistributedDataParallel as DDP except ImportError: raise ImportError("Please install apex from https://www.github.com/nvidia/apex to use distributed and fp16 training.") model = DDP(model) elif args.n_gpu > 1: # multi-gpu training model = torch.nn.DataParallel(model) if args.do_train: # Prepare training data loader train_examples = read_examples(args.train_filename) train_features = convert_examples_to_features(train_examples, tokenizer,args,stage='train') all_source_ids = torch.tensor([f.source_ids for f in train_features], dtype=torch.long) all_source_mask = torch.tensor([f.source_mask for f in train_features], dtype=torch.long) all_target_ids = torch.tensor([f.target_ids for f in train_features], dtype=torch.long) all_target_mask = torch.tensor([f.target_mask for f in train_features], dtype=torch.long) train_data = TensorDataset(all_source_ids,all_source_mask,all_target_ids,all_target_mask) if args.local_rank == -1: train_sampler = RandomSampler(train_data) else: train_sampler = DistributedSampler(train_data) train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=args.train_batch_size//args.gradient_accumulation_steps) num_train_optimization_steps = args.train_steps # Prepare optimizer and schedule (linear warmup and decay) no_decay = ['bias', 'LayerNorm.weight'] optimizer_grouped_parameters = [ {'params': [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)], 'weight_decay': args.weight_decay}, {'params': [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay': 0.0} ] optimizer = AdamW(optimizer_grouped_parameters, lr=args.learning_rate, eps=args.adam_epsilon) scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=args.warmup_steps, num_training_steps=num_train_optimization_steps) #Start training logger.info("***** Running training *****") logger.info(" Num examples = %d", len(train_examples)) logger.info(" Batch size = %d", args.train_batch_size) logger.info(" Num epoch = %d", num_train_optimization_steps*args.train_batch_size//len(train_examples)) model.train() dev_dataset={} nb_tr_examples, nb_tr_steps,tr_loss,global_step,best_bleu,best_loss = 0, 0,0,0,0,1e6 bar = tqdm(range(num_train_optimization_steps),total=num_train_optimization_steps) train_dataloader=cycle(train_dataloader) eval_flag = True for step in bar: batch = next(train_dataloader) batch = tuple(t.to(device) for t in batch) source_ids,source_mask,target_ids,target_mask = batch loss,_,_ = model(source_ids=source_ids,source_mask=source_mask,target_ids=target_ids,target_mask=target_mask) if args.n_gpu > 1: loss = loss.mean() # mean() to average on multi-gpu. if args.gradient_accumulation_steps > 1: loss = loss / args.gradient_accumulation_steps tr_loss += loss.item() train_loss=round(tr_loss*args.gradient_accumulation_steps/(nb_tr_steps+1),4) bar.set_description("loss {}".format(train_loss)) nb_tr_examples += source_ids.size(0) nb_tr_steps += 1 loss.backward() if (nb_tr_steps + 1) % args.gradient_accumulation_steps == 0: #Update parameters optimizer.step() optimizer.zero_grad() scheduler.step() global_step += 1 eval_flag = True if args.do_eval and ((global_step + 1) %args.eval_steps == 0) and eval_flag: #Eval model with dev dataset tr_loss = 0 nb_tr_examples, nb_tr_steps = 0, 0 eval_flag=False if 'dev_loss' in dev_dataset: eval_examples,eval_data=dev_dataset['dev_loss'] else: eval_examples = read_examples(args.dev_filename) eval_features = convert_examples_to_features(eval_examples, tokenizer, args,stage='dev') all_source_ids = torch.tensor([f.source_ids for f in eval_features], dtype=torch.long) all_source_mask = torch.tensor([f.source_mask for f in eval_features], dtype=torch.long) all_target_ids = torch.tensor([f.target_ids for f in eval_features], dtype=torch.long) all_target_mask = torch.tensor([f.target_mask for f in eval_features], dtype=torch.long) eval_data = TensorDataset(all_source_ids,all_source_mask,all_target_ids,all_target_mask) dev_dataset['dev_loss']=eval_examples,eval_data eval_sampler = SequentialSampler(eval_data) eval_dataloader = DataLoader(eval_data, sampler=eval_sampler, batch_size=args.eval_batch_size) logger.info("\n***** Running evaluation *****") logger.info(" Num examples = %d", len(eval_examples)) logger.info(" Batch size = %d", args.eval_batch_size) #Start Evaling model model.eval() eval_loss,tokens_num = 0,0 for batch in eval_dataloader: batch = tuple(t.to(device) for t in batch) source_ids,source_mask,target_ids,target_mask = batch with torch.no_grad(): _,loss,num = model(source_ids=source_ids,source_mask=source_mask, target_ids=target_ids,target_mask=target_mask) eval_loss += loss.sum().item() tokens_num += num.sum().item() #Pring loss of dev dataset model.train() eval_loss = eval_loss / tokens_num result = {'eval_ppl': round(np.exp(eval_loss),5), 'global_step': global_step+1, 'train_loss': round(train_loss,5)} for key in sorted(result.keys()): logger.info(" %s = %s", key, str(result[key])) logger.info(" "+"*"*20) #save last checkpoint last_output_dir = os.path.join(args.output_dir, 'checkpoint-last') if not os.path.exists(last_output_dir): os.makedirs(last_output_dir) model_to_save = model.module if hasattr(model, 'module') else model # Only save the model it-self output_model_file = os.path.join(last_output_dir, "pytorch_model.bin") torch.save(model_to_save.state_dict(), output_model_file) if eval_loss<best_loss: logger.info(" Best ppl:%s",round(np.exp(eval_loss),5)) logger.info(" "+"*"*20) best_loss=eval_loss # Save best checkpoint for best ppl output_dir = os.path.join(args.output_dir, 'checkpoint-best-ppl') if not os.path.exists(output_dir): os.makedirs(output_dir) model_to_save = model.module if hasattr(model, 'module') else model # Only save the model it-self output_model_file = os.path.join(output_dir, "pytorch_model.bin") torch.save(model_to_save.state_dict(), output_model_file) #Calculate bleu if 'dev_bleu' in dev_dataset: eval_examples,eval_data=dev_dataset['dev_bleu'] else: eval_examples = read_examples(args.dev_filename) eval_examples = random.sample(eval_examples,min(1000,len(eval_examples))) eval_features = convert_examples_to_features(eval_examples, tokenizer, args,stage='test') all_source_ids = torch.tensor([f.source_ids for f in eval_features], dtype=torch.long) all_source_mask = torch.tensor([f.source_mask for f in eval_features], dtype=torch.long) eval_data = TensorDataset(all_source_ids,all_source_mask) dev_dataset['dev_bleu']=eval_examples,eval_data eval_sampler = SequentialSampler(eval_data) eval_dataloader = DataLoader(eval_data, sampler=eval_sampler, batch_size=args.eval_batch_size) model.eval() p=[] for batch in eval_dataloader: batch = tuple(t.to(device) for t in batch) source_ids,source_mask= batch with torch.no_grad(): preds = model(source_ids=source_ids,source_mask=source_mask) for pred in preds: t=pred[0].cpu().numpy() t=list(t) if 0 in t: t=t[:t.index(0)] text = tokenizer.decode(t,clean_up_tokenization_spaces=False) p.append(text) model.train() predictions=[] with open(os.path.join(args.output_dir,"dev.output"),'w') as f, open(os.path.join(args.output_dir,"dev.gold"),'w') as f1: for ref,gold in zip(p,eval_examples): predictions.append(str(gold.idx)+'\t'+ref) f.write(str(gold.idx)+'\t'+ref+'\n') f1.write(str(gold.idx)+'\t'+gold.target+'\n') (goldMap, predictionMap) = bleu.computeMaps(predictions, os.path.join(args.output_dir, "dev.gold")) dev_bleu=round(bleu.bleuFromMaps(goldMap, predictionMap)[0],2) logger.info(" %s = %s "%("bleu-4",str(dev_bleu))) logger.info(" "+"*"*20) if dev_bleu>best_bleu: logger.info(" Best bleu:%s",dev_bleu) logger.info(" "+"*"*20) best_bleu=dev_bleu # Save best checkpoint for best bleu output_dir = os.path.join(args.output_dir, 'checkpoint-best-bleu') if not os.path.exists(output_dir): os.makedirs(output_dir) model_to_save = model.module if hasattr(model, 'module') else model # Only save the model it-self output_model_file = os.path.join(output_dir, "pytorch_model.bin") torch.save(model_to_save.state_dict(), output_model_file) if args.do_test: files=[] if args.dev_filename is not None: files.append(args.dev_filename) if args.test_filename is not None: files.append(args.test_filename) for idx,file in enumerate(files): logger.info("Test file: {}".format(file)) eval_examples = read_examples(file) eval_features = convert_examples_to_features(eval_examples, tokenizer, args,stage='test') all_source_ids = torch.tensor([f.source_ids for f in eval_features], dtype=torch.long) all_source_mask = torch.tensor([f.source_mask for f in eval_features], dtype=torch.long) eval_data = TensorDataset(all_source_ids,all_source_mask) # Calculate bleu eval_sampler = SequentialSampler(eval_data) eval_dataloader = DataLoader(eval_data, sampler=eval_sampler, batch_size=args.eval_batch_size) model.eval() p=[] for batch in tqdm(eval_dataloader,total=len(eval_dataloader)): batch = tuple(t.to(device) for t in batch) source_ids,source_mask= batch with torch.no_grad(): preds = model(source_ids=source_ids,source_mask=source_mask) for pred in preds: t=pred[0].cpu().numpy() t=list(t) if 0 in t: t=t[:t.index(0)] text = tokenizer.decode(t,clean_up_tokenization_spaces=False) p.append(text) model.train() predictions=[] with open(os.path.join(args.output_dir,"test_{}.output".format(str(idx))),'w') as f, open(os.path.join(args.output_dir,"test_{}.gold".format(str(idx))),'w') as f1: for ref,gold in zip(p,eval_examples): predictions.append(str(gold.idx)+'\t'+ref) f.write(str(gold.idx)+'\t'+ref+'\n') f1.write(str(gold.idx)+'\t'+gold.target+'\n') (goldMap, predictionMap) = bleu.computeMaps(predictions, os.path.join(args.output_dir, "test_{}.gold".format(idx))) dev_bleu=round(bleu.bleuFromMaps(goldMap, predictionMap)[0],2) logger.info(" %s = %s "%("bleu-4",str(dev_bleu))) logger.info(" "+"*"*20) if __name__ == "__main__": main()
CodeBERT/CodeBERT/code2nl/run.py/0
{ "file_path": "CodeBERT/CodeBERT/code2nl/run.py", "repo_id": "CodeBERT", "token_count": 12905 }
203
import torch import torch.nn as nn import torch from torch.autograd import Variable import copy import torch.nn.functional as F from torch.nn import CrossEntropyLoss, MSELoss import random class Model(nn.Module): def __init__(self, encoder,config,tokenizer,args): super(Model, self).__init__() self.encoder = encoder self.config = config self.tokenizer = tokenizer self.args = args self.lm_head = nn.Linear(config.hidden_size,config.vocab_size) self.qa_outputs = nn.Linear(config.hidden_size, config.hidden_size) self.lm_head.weight = self.encoder.embeddings.word_embeddings.weight self.register_buffer( "bias", torch.tril(torch.ones((args.block_size, args.block_size), dtype=torch.uint8)).view(1, args.block_size, args.block_size) ) self.weights = torch.full([len(self.tokenizer)], 10.0).to(self.args.device) easy_ids = self.tokenizer.convert_tokens_to_ids(["<state>","</state>", "<dictsep>", ":"]) for i in easy_ids: self.weights[i] = 1.0 def forward(self, dual_gen_ids, dual_gen_type_ids): dual_loss,align_loss,contras_loss = 0,0,0 # Encoder-Decoder for Cross-modal Generation source_ids = dual_gen_ids type_ids = dual_gen_type_ids attention_mask = self.bias attention_mask = attention_mask | (type_ids.eq(1)[:,:,None]*type_ids.eq(1)[:,None,:]) outputs = self.encoder(source_ids,attention_mask=attention_mask) encoder_outputs = outputs.last_hidden_state[:,:-1] labels_mask = type_ids.eq(2)[:,1:] encoder_outputs = encoder_outputs.reshape(-1,encoder_outputs.size(-1))[labels_mask.reshape(-1)] prediction_scores = self.lm_head(encoder_outputs) lm_labels = source_ids[:,1:].reshape(-1)[labels_mask.reshape(-1)] loss_fct = CrossEntropyLoss(reduction='none') lm_loss = loss_fct(prediction_scores, lm_labels) lm_loss = self.weights[lm_labels] * lm_loss lm_loss = lm_loss.sum()/len(lm_labels) dual_loss = lm_loss.item() return lm_loss, dual_loss, align_loss, contras_loss
CodeBERT/CodeExecutor/pretrain/model.py/0
{ "file_path": "CodeBERT/CodeExecutor/pretrain/model.py", "repo_id": "CodeBERT", "token_count": 973 }
204
# Code Refinement ## Task Definition Code refinement aims to automatically fix bugs in the code, which can contribute to reducing the cost of bug-fixes for developers. In CodeXGLUE, given a piece of Java code with bugs, the task is to remove the bugs to output the refined code. Models are evaluated by BLEU scores and accuracy (exactly match). ## Dataset We use the dataset released by this paper(https://arxiv.org/pdf/1812.08693.pdf). The source side is a Java function with bugs and the target side is the refined one. All the function and variable names are normalized. Their dataset contains two subsets ( i.e.small and medium) based on the function length. ### Data Format The dataset is in the "data" folder. Each line of the files is a function. You can get data using the following command: ``` unzip data.zip ``` ### Data Statistics Data statistics of this dataset are shown in the below table: | | #Examples | #Examples | | ------- | :-------: | :-------: | | | Small | Medium | | Train | 46,680 | 52,364 | | Valid | 5,835 | 6,545 | | Test | 5,835 | 6,545 | ## Pipeline-GraphCodeBERT ### Dependency - pip install torch - pip install transformers - pip install tree_sitter ### Tree-sitter (optional) If the built file "parser/my-languages.so" doesn't work for you, please rebuild as the following command: ```shell cd parser bash build.sh cd .. ``` ### Fine-tune We use 4*V100-16G to fine-tune. Taking the "small" subset as example: ```shell scale=small lr=1e-4 batch_size=32 beam_size=10 source_length=320 target_length=256 output_dir=saved_models/$scale/ train_file=data/$scale/train.buggy-fixed.buggy,data/$scale/train.buggy-fixed.fixed dev_file=data/$scale/valid.buggy-fixed.buggy,data/$scale/valid.buggy-fixed.fixed epochs=50 pretrained_model=microsoft/graphcodebert-base mkdir -p $output_dir python run.py --do_train --do_eval --model_type roberta --model_name_or_path $pretrained_model --tokenizer_name microsoft/graphcodebert-base --config_name microsoft/graphcodebert-base --train_filename $train_file --dev_filename $dev_file --output_dir $output_dir --max_source_length $source_length --max_target_length $target_length --beam_size $beam_size --train_batch_size $batch_size --eval_batch_size $batch_size --learning_rate $lr --num_train_epochs $epochs 2>&1| tee $output_dir/train.log ``` ### Inference We use full test data for inference. ```shell batch_size=64 dev_file=data/$scale/valid.buggy-fixed.buggy,data/$scale/valid.buggy-fixed.fixed test_file=data/$scale/test.buggy-fixed.buggy,data/$scale/test.buggy-fixed.fixed load_model_path=$output_dir/checkpoint-best-bleu/pytorch_model.bin #checkpoint for test python run.py --do_test --model_type roberta --model_name_or_path $pretrained_model --tokenizer_name microsoft/graphcodebert-base --config_name microsoft/graphcodebert-base --load_model_path $load_model_path --dev_filename $dev_file --test_filename $test_file --output_dir $output_dir --max_source_length $source_length --max_target_length $target_length --beam_size $beam_size --eval_batch_size $batch_size 2>&1| tee $output_dir/test.log ``` ## Result The results on the test set are shown as below: Small: | Method | BLEU | Acc (100%) | | ------------- | :-------: | :--------: | | Naive copy | 78.06 | 0.0 | | LSTM | 76.76 | 10.0 | | Transformer | 77.21 | 14.7 | | CodeBERT | 77.42 | 16.4 | | GraphCodeBERT | **80.02** | **17.3** | Medium: | Method | BLEU | Acc (100%) | | ------------- | :-------: | :--------: | | Naive copy | 90.91 | 0.0 | | LSTM | 72.08 | 2.5 | | Transformer | 89.25 | 3.7 | | CodeBERT | 91.07 | 5.16 | | GraphCodeBERT | **91.31** | **9.1** |
CodeBERT/GraphCodeBERT/refinement/README.md/0
{ "file_path": "CodeBERT/GraphCodeBERT/refinement/README.md", "repo_id": "CodeBERT", "token_count": 1434 }
205
# coding=utf-8 # Copyright 2018 The Google AI Language Team Authors and The HuggingFace Inc. team. # Copyright (c) 2018, NVIDIA CORPORATION. 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. """ Fine-tuning the library models for language modeling on a text file (GPT, GPT-2, BERT, RoBERTa). GPT and GPT-2 are fine-tuned using a causal language modeling (CLM) loss while BERT and RoBERTa are fine-tuned using a masked language modeling (MLM) loss. """ from __future__ import absolute_import import os import sys import nltk import pickle import torch import json import random import logging import argparse import numpy as np from fuzzywuzzy import fuzz from io import open from itertools import cycle import torch.nn as nn import re from model import Seq2Seq from torch.utils.data import DataLoader, Dataset, SequentialSampler, RandomSampler,TensorDataset from torch.utils.data.distributed import DistributedSampler from transformers import (WEIGHTS_NAME, AdamW, get_linear_schedule_with_warmup, LongformerConfig, RobertaTokenizer) from longcoder import LongcoderModel from parser import (remove_comments_and_docstrings, tree_to_token_index, index_to_code_token, tree_to_variable_index) from tree_sitter import Language, Parser from datasets import load_dataset #load parsers parsers={} for lang in ['python','ruby','java','go','javascript','php','c','cpp','c_sharp']: LANGUAGE = Language('parser/my-languages.so', lang) parser = Parser() parser.set_language(LANGUAGE) if lang == "c_sharp": parsers["csharp"]= parser else: parsers[lang]= parser def tokenize_code(parser,context): root_node = parser.parse(bytes(context,'utf8')).root_node tokens_index=tree_to_token_index(root_node) code=context.split('\n') code_tokens=[index_to_code_token(x,code) for x in tokens_index] return " ".join(code_tokens) def extract_global_statement(node,lang=None): indexs = [] for child in node.children: indexs+=extract_global_statement(child,lang) if lang=="java": if any([x in node.type for x in ["class","package_declaration"]]): indexs.append(node.end_point[0]) if any([x in node.type for x in ["method_declaration"]]): indexs.append(node.start_point[0]) elif lang=="python": if any([x in node.type for x in ["import_statement","function_definition","class_definition"]]): indexs.append(node.start_point[0]) elif lang=="csharp": if any([x in node.type for x in ["using_directive","class_declaration","method_declaration"]]): indexs.append(node.start_point[0]) return indexs logging.basicConfig(format = '%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt = '%m/%d/%Y %H:%M:%S', level = logging.INFO) logger = logging.getLogger(__name__) class TextDataset(Dataset): def __init__(self, tokenizer, args, split): self.examples = load_dataset(args.filename)[split] self.args = args self.tokenizer = tokenizer self.split = split def __len__(self): return len(self.examples) def __getitem__(self, i): source = self.examples[i]["context"] source = re.sub("\n+", "\n", source) root_node = parsers[self.args.lang].parse(bytes(source,'utf8')).root_node try: statement_indexs = sorted(list(set(extract_global_statement(root_node,self.args.lang)))) except: statement_indexs = [i for i in range(200)] target = self.examples[i]["gt"] source_tokens = self.tokenizer.tokenize(source.strip()) + ["Ċ"] statement_masks = [] index = 0 for x in source_tokens: if "Ċ" in x: if index in statement_indexs: statement_masks.append(True) else: statement_masks.append(False) index += 1 else: statement_masks.append(False) if self.split == "train": max_length = self.args.max_source_length + self.args.max_target_length if len(source_tokens) <= max_length-3: index = 0 else: index = random.randint((len(source_tokens)-(max_length-3))//2,len(source_tokens)-(max_length-3)) source_tokens = source_tokens[index:index+max_length-3] statement_masks = statement_masks[index:index+max_length-3] else: max_length = self.args.max_source_length source_tokens = source_tokens[-(max_length-3):] statement_masks = statement_masks[-(max_length-3):] source_tokens = ["<s>","<decoder-only>","</s>"]+source_tokens statement_masks = [False,False,False] + statement_masks cont = 0 global_mask = [] for x in statement_masks: if len(source_tokens) - len(global_mask) < self.args.window_size: global_mask.append(False) elif x is False: global_mask.append(False) elif cont == self.args.max_global_length: global_mask.append(False) else: global_mask.append(True) cont += 1 for i in range(len(source_tokens)): if cont == self.args.max_global_length: continue elif source_tokens[i] == "Ċ" and global_mask[i] is False: global_mask[i] = True cont += 1 if sum(global_mask) == 0: global_mask[0] = True source_ids = self.tokenizer.convert_tokens_to_ids(source_tokens) source_ids += [self.tokenizer.pad_token_id]* (max_length - len(source_ids)) global_mask = global_mask + [False] * (max_length - len(global_mask)) return torch.tensor(source_ids),torch.tensor(global_mask) def train(args, model, tokenizer): """ Train the model """ #get training dataset train_dataset = TextDataset(tokenizer, args,"train") train_dataloader = DataLoader(train_dataset, sampler=RandomSampler(train_dataset), batch_size=args.train_batch_size,num_workers=4) #get optimizer and scheduler optimizer = AdamW(model.parameters(), lr=args.learning_rate, eps=1e-8) scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps = 0, num_training_steps = len(train_dataloader) * args.num_train_epochs) # Train! logger.info("***** Running training *****") logger.info(" Num examples = %d", len(train_dataset)) logger.info(" Num Epochs = %d", args.num_train_epochs) logger.info(" Instantaneous batch size per GPU = %d", args.train_batch_size//args.n_gpu) logger.info(" Total train batch size = %d", args.train_batch_size) logger.info(" Optimization steps per epoch = %d", len(train_dataloader)) # model.resize_token_embeddings(len(tokenizer)) model.zero_grad() model.train() losses, best_acc = [], 0.0 for idx in range(args.num_train_epochs): losses = [] for step,batch in enumerate(train_dataloader): #get inputs source_ids,global_mask = [x.to(args.device) for x in batch] # forward and return loss loss = model(source_ids,global_mask,True) if args.n_gpu > 1: loss = loss.mean() # mean() to average on multi-gpu. #report loss losses.append(loss.item()) if len(losses)%100 == 0: logger.info("epoch {} step {} loss {}".format(idx,step+1,round(np.mean(losses[-100:]),5))) #backward loss.backward() torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm) optimizer.step() optimizer.zero_grad() scheduler.step() #evaluate results = evaluate(args, model, tokenizer,"validation", output_file_prefix = "dev") for key, value in results.items(): logger.info(" %s = %s", key, round(value,4)) #save best model if results['eval_acc']>best_acc: best_acc = results['eval_acc'] logger.info(" "+"*"*20) logger.info(" Best acc:%s",best_acc) logger.info(" "+"*"*20) checkpoint_prefix = 'checkpoint-best-acc' output_dir = os.path.join(args.output_dir, '{}'.format(checkpoint_prefix)) if not os.path.exists(output_dir): os.makedirs(output_dir) model_to_save = model.module if hasattr(model,'module') else model output_dir = os.path.join(output_dir, '{}'.format('model.bin')) torch.save(model_to_save.state_dict(), output_dir) logger.info("Saving model checkpoint to %s", output_dir) def evaluate(args, model, tokenizer,split, test_number = 100000000, output_file_prefix = "test"): test_dataset = TextDataset(tokenizer, args, split) test_sampler = SequentialSampler(test_dataset) test_dataloader = DataLoader(test_dataset, sampler=test_sampler, batch_size=args.eval_batch_size,num_workers=4) # Eval! logger.info("***** Running evaluation *****") logger.info(" Num examples = %d", len(test_dataset)) logger.info(" Batch size = %d", args.eval_batch_size) model.eval() losses = [] gts = [] preds = [] for batch in test_dataloader: source_ids,global_mask = [x.to(args.device) for x in batch] with torch.no_grad(): loss = model(source_ids,global_mask,True) if args.n_gpu > 1: loss = loss.mean() losses.append(loss.item()) outs = model(source_ids,global_mask) for pred in outs: t=pred[0].cpu().numpy() t=list(t) if 0 in t: t=t[:t.index(0)] text = tokenizer.decode(t,clean_up_tokenization_spaces=False) if "\n" in text: text = text[:text.index("\n")] preds.append(text) if len(preds) >= test_number: break preds = preds[:test_number] gts = [test_dataset.examples[i]['gt'] for i in range(min(len(test_dataset.examples),test_number))] with open(os.path.join(args.output_dir,"{}.output".format(output_file_prefix)),'w') as f, open(os.path.join(args.output_dir,"{}.gold".format(output_file_prefix)),'w') as f1: for ref,gold in zip(preds,gts): f.write(ref+'\n') f1.write(gold+'\n') f.close() f1.close() EM = [] edit_sim = [] for ref,gold in zip(preds,gts): pred = tokenize_code(parser,ref) gt = tokenize_code(parser,gold) edit_sim.append(fuzz.ratio(pred, gt)) EM.append(pred.split() == gt.split()) model.train() result = { "eval_loss":round(np.mean(losses),4), "eval_acc":round(np.mean(EM)*100,2), "eval_edit_sim":round(np.mean(edit_sim),2), } return result def set_seed(seed=42): random.seed(seed) os.environ['PYHTONHASHSEED'] = str(seed) np.random.seed(seed) torch.manual_seed(seed) torch.cuda.manual_seed(seed) torch.backends.cudnn.deterministic = True def main(): parser = argparse.ArgumentParser() ## Required parameters parser.add_argument("--model_name_or_path", default=None, type=str, required=True, help="Path to pre-trained model: e.g. roberta-base" ) parser.add_argument("--output_dir", default=None, type=str, required=True, help="The output directory where the model predictions and checkpoints will be written.") parser.add_argument("--load_model_path", default=None, type=str, help="Path to trained model: Should contain the .bin files" ) parser.add_argument("--lang", default=None, type=str,) ## Other parameters parser.add_argument("--filename", default=None, type=str, help="The train filename. Should contain the .jsonl files for this task.") parser.add_argument("--max_source_length", default=64, type=int, help="The maximum total source sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded.") parser.add_argument("--max_target_length", default=32, type=int, help="The maximum total target sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded.") parser.add_argument("--max_global_length", default=32, type=int, help="The maximum total target sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded.") parser.add_argument("--window_size", default=512, type=int) parser.add_argument("--do_train", action='store_true', help="Whether to run training.") parser.add_argument("--do_eval", action='store_true', help="Whether to run eval on the dev set.") parser.add_argument("--do_test", action='store_true', help="Whether to run eval on the dev set.") parser.add_argument("--train_batch_size", default=8, type=int, help="Batch size per GPU/CPU for training.") parser.add_argument("--eval_batch_size", default=8, type=int, help="Batch size per GPU/CPU for evaluation.") parser.add_argument("--learning_rate", default=5e-5, type=float, help="The initial learning rate for Adam.") parser.add_argument("--beam_size", default=10, type=int, help="beam size for beam search") parser.add_argument("--weight_decay", default=0.0, type=float, help="Weight deay if we apply some.") parser.add_argument("--adam_epsilon", default=1e-8, type=float, help="Epsilon for Adam optimizer.") parser.add_argument("--max_grad_norm", default=1.0, type=float, help="Max gradient norm.") parser.add_argument("--num_train_epochs", default=3, type=int, help="Total number of training epochs to perform.") parser.add_argument('--seed', type=int, default=42, help="random seed for initialization") # print arguments args = parser.parse_args() logger.info(args) # Setup CUDA, GPU device = torch.device("cuda" if torch.cuda.is_available() else "cpu") args.n_gpu = torch.cuda.device_count() logger.warning("device: %s, n_gpu: %s", device, args.n_gpu) args.device = device # Set seed set_seed(args.seed) # make dir if output_dir not exist if os.path.exists(args.output_dir) is False: os.makedirs(args.output_dir) config = LongformerConfig.from_pretrained(args.model_name_or_path) tokenizer = RobertaTokenizer.from_pretrained(args.model_name_or_path) config.attention_window = [args.window_size for x in config.attention_window] config.is_decoder_only = True #budild model encoder = LongcoderModel.from_pretrained(args.model_name_or_path,config=config) eos_ids = [tokenizer.convert_tokens_to_ids('Ċ')] model=Seq2Seq(encoder=encoder,decoder=encoder,config=config,tokenizer=tokenizer, beam_size=args.beam_size,max_length=args.max_target_length, sos_id=tokenizer.cls_token_id,eos_id=eos_ids) if args.load_model_path is not None: logger.info("reload model from {}".format(args.load_model_path)) model.load_state_dict(torch.load(args.load_model_path)) model.to(device) if args.n_gpu > 1: # multi-gpu training model = torch.nn.DataParallel(model) if args.do_train: train(args, model, tokenizer) if args.do_test: results = evaluate(args, model, tokenizer,"test", test_number = 10000000, output_file_prefix = "test") for key, value in results.items(): logger.info(" %s = %s", key, round(value,4)) if __name__ == "__main__": main()
CodeBERT/LongCoder/run.py/0
{ "file_path": "CodeBERT/LongCoder/run.py", "repo_id": "CodeBERT", "token_count": 7765 }
206
# Copyright (c) Microsoft Corporation. # Licensed under the MIT license. import torch import torch.nn as nn import torch from torch.autograd import Variable import copy class Seq2Seq(nn.Module): """ Build Seqence-to-Sequence. Parameters: * `encoder`- encoder of seq2seq model. e.g. roberta * `decoder`- decoder of seq2seq model. e.g. transformer * `config`- configuration of encoder model. * `beam_size`- beam size for beam search. * `max_length`- max length of target for beam search. * `sos_id`- start of symbol ids in target for beam search. * `eos_id`- end of symbol ids in target for beam search. """ def __init__(self, encoder,decoder,config,beam_size=None,max_length=None,sos_id=None,eos_id=None): super(Seq2Seq, self).__init__() self.encoder = encoder self.decoder=decoder self.config=config self.register_buffer( "bias", torch.tril(torch.ones((1024, 1024), dtype=torch.uint8)).view(1,1024, 1024) ) self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False) self.lm_head.weight=self.encoder.embeddings.word_embeddings.weight self.lsm = nn.LogSoftmax(dim=-1) self.beam_size=beam_size self.max_length=max_length self.sos_id=sos_id self.eos_id=eos_id def tie_weights(self): """ Make sure we are sharing the input and output embeddings. Export to TorchScript can't handle parameter sharing so we are cloning them instead. """ self._tie_or_clone_weights(self.lm_head, self.encoder.embeddings.word_embeddings) def forward(self, source_ids,train=False): max_length = source_ids.ne(1).sum(-1).max() source_ids = source_ids[:,:max_length] if train: length = source_ids.size(-1) out = self.decoder(source_ids,attention_mask=self.bias[:,:length,:length]).last_hidden_state lm_logits = self.lm_head(out) # Shift so that tokens < n predict n active_loss = source_ids[..., 1:].ne(1).view(-1) shift_logits = lm_logits[..., :-1, :].contiguous() shift_labels = source_ids[..., 1:].contiguous() # Flatten the tokens loss_fct = nn.CrossEntropyLoss(ignore_index=-1) loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1))[active_loss], shift_labels.view(-1)[active_loss]) outputs = loss,loss*active_loss.sum(),active_loss.sum() return outputs else: #Predict preds=[] zero=torch.cuda.LongTensor(1).fill_(0) source_len = list(source_ids.ne(1).sum(-1).cpu().numpy()) length = source_ids.size(-1) encoder_output = self.decoder(source_ids,attention_mask=self.bias[:,:length,:length]) for i in range(source_ids.shape[0]): context=[[x[i:i+1,:,:source_len[i]].repeat(self.beam_size,1,1,1) for x in y] for y in encoder_output.past_key_values] beam = Beam(self.beam_size,self.sos_id,self.eos_id) input_ids=beam.getCurrentState() context_ids = source_ids[i:i+1,:source_len[i]].repeat(self.beam_size,1) out = encoder_output.last_hidden_state[i:i+1,:source_len[i]].repeat(self.beam_size,1,1) for _ in range(self.max_length): if beam.done(): break if _ == 0: hidden_states=out[:,-1,:] out = self.lsm(self.lm_head(hidden_states)).data beam.advance(out) input_ids.data.copy_(input_ids.data.index_select(0, beam.getCurrentOrigin())) input_ids=beam.getCurrentState() else: length = context_ids.size(-1)+input_ids.size(-1) out = self.decoder(input_ids,attention_mask=self.bias[:,context_ids.size(-1):length,:length], past_key_values=context).last_hidden_state hidden_states=out[:,-1,:] out = self.lsm(self.lm_head(hidden_states)).data beam.advance(out) input_ids.data.copy_(input_ids.data.index_select(0, beam.getCurrentOrigin())) input_ids=torch.cat((input_ids,beam.getCurrentState()),-1) hyp= beam.getHyp(beam.getFinal()) pred=beam.buildTargetTokens(hyp)[:self.beam_size] pred=[torch.cat([x.view(-1) for x in p]+[zero]*(self.max_length-len(p))).view(1,-1) for p in pred] preds.append(torch.cat(pred,0).unsqueeze(0)) preds=torch.cat(preds,0) return preds class Beam(object): def __init__(self, size, sos, eos): self.size = size self.tt = torch.cuda # The score for each translation on the beam. self.scores = self.tt.FloatTensor(size).zero_() # The backpointers at each time-step. self.prevKs = [] # The outputs at each time-step. self.nextYs = [self.tt.LongTensor(size) .fill_(0)] self.nextYs[0][:] = sos # Has EOS topped the beam yet. self._eos = eos self.eosTop = False # Time and k pair for finished. self.finished = [] def getCurrentState(self): "Get the outputs for the current timestep." batch = self.tt.LongTensor(self.nextYs[-1]).view(-1, 1) return batch def getCurrentOrigin(self): "Get the backpointers for the current timestep." return self.prevKs[-1] def advance(self, wordLk): """ Given prob over words for every last beam `wordLk` and attention `attnOut`: Compute and update the beam search. Parameters: * `wordLk`- probs of advancing from the last step (K x words) * `attnOut`- attention at the last step Returns: True if beam search is complete. """ numWords = wordLk.size(1) # Sum the previous scores. if len(self.prevKs) > 0: beamLk = wordLk + self.scores.unsqueeze(1).expand_as(wordLk) # Don't let EOS have children. for i in range(self.nextYs[-1].size(0)): if self.nextYs[-1][i] in self._eos: beamLk[i] = -1e20 else: beamLk = wordLk[0] flatBeamLk = beamLk.view(-1) bestScores, bestScoresId = flatBeamLk.topk(self.size, 0, True, True) self.scores = bestScores # bestScoresId is flattened beam x word array, so calculate which # word and beam each score came from prevK = bestScoresId // numWords self.prevKs.append(prevK) self.nextYs.append((bestScoresId - prevK * numWords)) for i in range(self.nextYs[-1].size(0)): if self.nextYs[-1][i] in self._eos: s = self.scores[i] self.finished.append((s, len(self.nextYs) - 1, i)) # End condition is when top-of-beam is EOS and no global score. if self.nextYs[-1][0] in self._eos: self.eosTop = True def done(self): return self.eosTop and len(self.finished) >=self.size def getFinal(self): if len(self.finished) == 0: self.finished.append((self.scores[0], len(self.nextYs) - 1, 0)) self.finished.sort(key=lambda a: -a[0]) if len(self.finished) != self.size: unfinished=[] for i in range(self.nextYs[-1].size(0)): if self.nextYs[-1][i] not in self._eos: s = self.scores[i] unfinished.append((s, len(self.nextYs) - 1, i)) unfinished.sort(key=lambda a: -a[0]) self.finished+=unfinished[:self.size-len(self.finished)] return self.finished[:self.size] def getHyp(self, beam_res): """ Walk back to construct the full hypothesis. """ hyps=[] for _,timestep, k in beam_res: hyp = [] for j in range(len(self.prevKs[:timestep]) - 1, -1, -1): hyp.append(self.nextYs[j+1][k]) k = self.prevKs[j][k] hyps.append(hyp[::-1]) return hyps def buildTargetTokens(self, preds): sentence=[] for pred in preds: tokens = [] for tok in pred: tokens.append(tok) if tok in self._eos: break sentence.append(tokens) return sentence
CodeBERT/UniXcoder/downstream-tasks/code-completion/model.py/0
{ "file_path": "CodeBERT/UniXcoder/downstream-tasks/code-completion/model.py", "repo_id": "CodeBERT", "token_count": 4662 }
207
import json for lang,suffix in [("Java",".java"),("Ruby",".rb"),("Python",".py")]: with open("{}.jsonl".format(lang.lower())) as f, open("{}_with_func.jsonl".format(lang.lower()),"w") as f1: for line in f: js = json.loads(line.strip()) problem_id = str(js["label"]) problem_id = "p" + "0" * (5-len(problem_id)) + problem_id language = lang submission_id = js["index"] func = open("Project_CodeNet/data/{}/{}/{}{}".format(problem_id,language,submission_id,suffix)).read() js["func"] = func f1.write(json.dumps(js)+"\n")
CodeBERT/UniXcoder/downstream-tasks/zero-shot-search/dataset/preprocess.py/0
{ "file_path": "CodeBERT/UniXcoder/downstream-tasks/zero-shot-search/dataset/preprocess.py", "repo_id": "CodeBERT", "token_count": 305 }
208
# Copyright (c) Microsoft Corporation. # Licensed under the MIT license. from typing import Optional, Dict import contextlib import faulthandler import io import os import multiprocessing import platform import signal import tempfile def _pack_test_cases(test_cases, timeout): blank_4 = ' ' * 4 blank_8 = ' ' * 8 blank_12 = ' ' * 12 result = f'def check():\n pass_result = []\n' for idx, tc in enumerate(test_cases): multi_line_assertion = tc.strip().replace('\n', f'\n{blank_12}') result += f'\n{blank_4}try:\n{blank_8}with time_limit({timeout}):\n{blank_12}{multi_line_assertion}\ \n{blank_12}pass_result.append(True)\n{blank_4}except Exception as e:\n{blank_8}pass_result.append(False)\n' result += '\n return pass_result\n' result += f'\nglobal final_result\nfinal_result = check()' return result def check_correctness_with_test_cases(task_id, prompt, completion, test_cases, timeout): """ Evaluates the functional correctness of a solution_content by running the test suite provided in the problem. """ extend_timeout = timeout*len(test_cases) def unsafe_execute(): with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil rmtree = shutil.rmtree rmdir = os.rmdir chdir = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Construct the check program and run it. check_program = ( prompt + completion + "\n" + _pack_test_cases(test_cases, timeout) ) try: exec_globals = {'time_limit': time_limit} with swallow_io(): exec(check_program, exec_globals) result.append(exec_globals['final_result']) except TimeoutException: result.append("timed out") except BaseException as e: result.append(f"failed: {e}") # Needed for cleaning up. shutil.rmtree = rmtree os.rmdir = rmdir os.chdir = chdir manager = multiprocessing.Manager() result = manager.list() p = multiprocessing.Process(target=unsafe_execute) p.start() p.join(timeout=extend_timeout + 0.1) if p.is_alive(): p.kill() if not result: result.append("timed out") return dict( task_id=task_id, test_cases=test_cases, completion=completion, passed=(type(result[0]) == list) and len(result[0]) > 0, result=result[0] ) def check_correctness(task_id: str, prompt: str, completion: str, test: str, entry_point: str, timeout: float) -> Dict: """ Evaluates the functional correctness of a completion by running the test suite provided in the problem. """ def unsafe_execute(): with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil rmtree = shutil.rmtree rmdir = os.rmdir chdir = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Construct the check program and run it. check_program = ( prompt + completion + "\n" + test + "\n" + f'check({entry_point})' ) try: exec_globals = {} with swallow_io(): with time_limit(timeout): exec(check_program, exec_globals) result.append("passed") except TimeoutException: result.append("timed out") except BaseException as e: result.append(f"failed: {e}") # Needed for cleaning up. shutil.rmtree = rmtree os.rmdir = rmdir os.chdir = chdir manager = multiprocessing.Manager() result = manager.list() p = multiprocessing.Process(target=unsafe_execute) p.start() p.join(timeout=timeout+1) if p.is_alive(): p.kill() if not result: result.append("timed out") return dict( task_id=task_id, passed=result[0] == "passed", result=result[0], completion=completion, ) @contextlib.contextmanager def time_limit(seconds: float): def signal_handler(signum, frame): raise TimeoutException("Timed out!") signal.setitimer(signal.ITIMER_REAL, seconds) signal.signal(signal.SIGALRM, signal_handler) try: yield finally: signal.setitimer(signal.ITIMER_REAL, 0) @contextlib.contextmanager def swallow_io(): stream = WriteOnlyStringIO() with contextlib.redirect_stdout(stream): with contextlib.redirect_stderr(stream): with redirect_stdin(stream): yield @contextlib.contextmanager def create_tempdir(): with tempfile.TemporaryDirectory() as dirname: with chdir(dirname): yield dirname class TimeoutException(Exception): pass class WriteOnlyStringIO(io.StringIO): """ StringIO that throws an exception when it's read from """ def read(self, *args, **kwargs): raise IOError def readline(self, *args, **kwargs): raise IOError def readlines(self, *args, **kwargs): raise IOError def readable(self, *args, **kwargs): """ Returns True if the IO object can be read. """ return False class redirect_stdin(contextlib._RedirectStream): # type: ignore _stream = 'stdin' @contextlib.contextmanager def chdir(root): if root == ".": yield return cwd = os.getcwd() os.chdir(root) try: yield except BaseException as exc: raise exc finally: os.chdir(cwd) def reliability_guard(maximum_memory_bytes: Optional[int] = None): """ This disables various destructive functions and prevents the generated code from interfering with the test (e.g. fork bomb, killing other processes, removing filesystem files, etc.) WARNING This function is NOT a security sandbox. Untrusted code, including, model- generated code, should not be blindly executed outside of one. See the Codex paper for more information about OpenAI's code sandbox, and proceed with caution. """ if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS, (maximum_memory_bytes, maximum_memory_bytes)) resource.setrlimit(resource.RLIMIT_DATA, (maximum_memory_bytes, maximum_memory_bytes)) if not platform.uname().system == 'Darwin': resource.setrlimit(resource.RLIMIT_STACK, (maximum_memory_bytes, maximum_memory_bytes)) faulthandler.disable() import builtins builtins.exit = None builtins.quit = None import os os.environ['OMP_NUM_THREADS'] = '1' os.kill = None os.system = None os.putenv = None os.remove = None os.removedirs = None os.rmdir = None os.fchdir = None os.setuid = None os.fork = None os.forkpty = None os.killpg = None os.rename = None os.renames = None os.truncate = None os.replace = None os.unlink = None os.fchmod = None os.fchown = None os.chmod = None os.chown = None os.chroot = None os.fchdir = None os.lchflags = None os.lchmod = None os.lchown = None os.getcwd = None os.chdir = None import shutil shutil.rmtree = None shutil.move = None shutil.chown = None import subprocess subprocess.Popen = None # type: ignore __builtins__['help'] = None import sys sys.modules['ipdb'] = None sys.modules['joblib'] = None sys.modules['resource'] = None sys.modules['psutil'] = None sys.modules['tkinter'] = None
CodeT/CodeT/src/_execution.py/0
{ "file_path": "CodeT/CodeT/src/_execution.py", "repo_id": "CodeT", "token_count": 3491 }
209
import os def get_file(path): if os.path.isdir(path): return os.path.join(path, os.listdir(path)[0]) else: return path
CodeT/DIVERSE/code/src/utils_io.py/0
{ "file_path": "CodeT/DIVERSE/code/src/utils_io.py", "repo_id": "CodeT", "token_count": 67 }
210
### Codex CLI setup - start $nl_cli_script = "{{codex_query_path}}" # this function takes the input from the buffer and passes it to codex_query.py function create_completion() { param ( [Parameter (Mandatory = $true)] [string] $buffer ) if ($nl_cli_script -eq "" -or !(Test-Path($nl_cli_script))) { Write-Output "# Please update the nl_cli_script path in the profile!" return "`nnotepad $profile" } $output = echo -n $buffer | python $nl_cli_script return $output } Set-PSReadLineKeyHandler -Key Ctrl+g ` -BriefDescription NLCLI ` -LongDescription "Calls Codex CLI tool on the current buffer" ` -ScriptBlock { param($key, $arg) $line = $null $cursor = $null [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor) # get response from create_completion function $output = create_completion($line) # check if output is not null if ($output -ne $null) { foreach ($str in $output) { if ($str -ne $null -and $str -ne "") { [Microsoft.PowerShell.PSConsoleReadLine]::AddLine() [Microsoft.PowerShell.PSConsoleReadLine]::Insert($str) } } } } ### Codex CLI setup - end
Codex-CLI/scripts/powershell_plugin.ps1/0
{ "file_path": "Codex-CLI/scripts/powershell_plugin.ps1", "repo_id": "Codex-CLI", "token_count": 587 }
211
#!/usr/bin/env python # -*- coding: utf-8 -*- """ File: large_face_list.py Description: Large Face List section of the Cognitive Face API. """ from . import util def create(large_face_list_id, name=None, user_data=None): """Create an empty large face list with user-specified `large_face_list_id`, `name` and an optional `user_data`. Args: large_face_list_id: Valid character is letter in lower case or digit or '-' or '_', maximum length is 64. name: Name of the created large face list, maximum length is 128. user_data: Optional parameter. User-defined data for the large face list. Length should not exceed 16KB. Returns: An empty response body. """ name = name or large_face_list_id url = 'largefacelists/{}'.format(large_face_list_id) json = { 'name': name, 'userData': user_data, } return util.request('PUT', url, json=json) def delete(large_face_list_id): """Delete an existing large face list according to `large_face_list_id`. Persisted face images in the large face list will also be deleted. Args: large_face_list_id: Valid character is letter in lower case or digit or '-' or '_', maximum length is 64. Returns: An empty response body. """ url = 'largefacelists/{}'.format(large_face_list_id) return util.request('DELETE', url) def get(large_face_list_id): """Retrieve a large face list's information, including `large_face_list_id`, `name`, `user_data`. Large face list simply represents a list of faces, and could be treated as a searchable data source in `face.find_similars`. Args: large_face_list_id: Valid character is letter in lower case or digit or '-' or '_', maximum length is 64. Returns: The large face list's information. """ url = 'largefacelists/{}'.format(large_face_list_id) return util.request('GET', url) def get_status(large_face_list_id): """Retrieve the training status of a large face list (completed or ongoing). Training can be triggered by `large_face_list.train`. The training will process for a while on the server side. Args: large_face_list_id: `large_face_list_id` of the target large face list. Returns: The large face list's training status. """ url = 'largefacelists/{}/training'.format(large_face_list_id) return util.request('GET', url) def list(start=None, top=None): """Retrieve information about all existing large face lists. Only `large_face_list_id`, `name` and `user_data` will be returned. Args: start: Optional parameter. List large face lists from the least `large_face_list_id` greater than the "start". It contains no more than 64 characters. Default is empty. top: The number of large face lists to list, ranging in [1, 1000]. Default is 1000. Returns: An array of large face lists. """ url = 'largefacelists' params = { 'start': start, 'top': top, } return util.request('GET', url, params=params) def train(large_face_list_id): """Queue a large face list training task, the training task may not be started immediately. Args: large_face_list_id: Target large face list to be trained. Returns: An empty JSON body. """ url = 'largefacelists/{}/train'.format(large_face_list_id) return util.request('POST', url) def update(large_face_list_id, name=None, user_data=None): """Update information of a large face list, including `name` and `user_data`. Args: large_face_list_id: Valid character is letter in lower case or digit or '-' or '_', maximum length is 64. name: Name of the created large face list, maximum length is 128. user_data: Optional parameter. User-defined data for the large face list. Length should not exceed 16KB. Returns: An empty response body. """ url = 'largefacelists/{}'.format(large_face_list_id) json = { 'name': name, 'userData': user_data, } return util.request('PATCH', url, json=json)
Cognitive-Face-Python/cognitive_face/large_face_list.py/0
{ "file_path": "Cognitive-Face-Python/cognitive_face/large_face_list.py", "repo_id": "Cognitive-Face-Python", "token_count": 1598 }
212
#!/usr/bin/env python # -*- coding: utf-8 -*- """ File: test_person.py Description: Unittests for Person section of the Cognitive Face API. """ import unittest import cognitive_face as CF from . import util class TestPerson(unittest.TestCase): """Unittests for Person section.""" def test_face(self): """Unittests for `person.add_face`, `person.update_face` and `person.delete_face`. """ image = '{}PersonGroup/Family1-Dad/Family1-Dad3.jpg'.format( util.BASE_URL_IMAGE) res = CF.person.add_face(image, util.DataStore.person_group_id, util.DataStore.person_id['Dad']) print(res) self.assertIsInstance(res, dict) util.wait() persisted_face_id = res['persistedFaceId'] res = CF.person.update_face(util.DataStore.person_group_id, util.DataStore.person_id['Dad'], persisted_face_id, 'TempUserData') print(res) self.assertIsInstance(res, dict) util.wait() res = CF.person.delete_face(util.DataStore.person_group_id, util.DataStore.person_id['Dad'], persisted_face_id) print(res) self.assertIsInstance(res, dict) util.wait() def test_person(self): """Unittests for `person.create`, `person.update` and `person.delete`. """ res = CF.person.create(util.DataStore.person_group_id, 'TempPerson') print(res) self.assertIsInstance(res, dict) util.wait() person_id = res['personId'] res = CF.person.update(util.DataStore.person_group_id, person_id, 'TP') print(res) self.assertIsInstance(res, dict) util.wait() res = CF.person.delete(util.DataStore.person_group_id, person_id) print(res) self.assertIsInstance(res, dict) util.wait() def test_get(self): """Unittest for `person.get`.""" res = CF.person.get(util.DataStore.person_group_id, util.DataStore.person_id['Dad']) print(res) self.assertIsInstance(res, dict) util.wait() def test_get_face(self): """Unittest for `person.get_face`.""" res = CF.person.get_face( util.DataStore.person_group_id, util.DataStore.person_id['Dad'], util.DataStore.person_persisted_face_id['Dad'][0]) print(res) self.assertIsInstance(res, dict) util.wait() def test_lists(self): """Unittest for `person.lists`.""" res = CF.person.lists(util.DataStore.person_group_id) print(res) self.assertIsInstance(res, list) util.wait()
Cognitive-Face-Python/cognitive_face/tests/test_person.py/0
{ "file_path": "Cognitive-Face-Python/cognitive_face/tests/test_person.py", "repo_id": "Cognitive-Face-Python", "token_count": 1375 }
213
#!/usr/bin/env python # -*- coding: utf-8 -*- """ File: panel_verification.py Description: Verification Panel for Python SDK sample. """ import os import uuid import wx import wx.lib.scrolledpanel as scrolled import util import model from view import base class VerificationPanel(base.MyPanel): """Verification Panel.""" def __init__(self, parent): super(VerificationPanel, self).__init__(parent) self.face_ids = { 'face_id': None, 'another_face_id': None, 'person_face_id': None, } self.large_person_group_id = str(uuid.uuid1()) self.person_name = None self.person_id = None self.face_paths = [] self.detected_face_paths = [] self.vsizer = wx.BoxSizer(wx.VERTICAL) self.panel = scrolled.ScrolledPanel(self) self.hsizer = wx.BoxSizer() self.hsizer.AddStretchSpacer() self.hvsizer = wx.BoxSizer(wx.VERTICAL) self.hvsizer.SetMinSize((util.INNER_PANEL_WIDTH, -1)) label = ("Demo 1: Face-to-face verification determines whether " "two faces belong to the same person. Choose two images " "with a single face each. Then click 'Verify' to get " "the verification result.") self.static_text = wx.StaticText(self.panel, label=label) self.static_text.Wrap(util.INNER_PANEL_WIDTH) self.hvsizer.Add(self.static_text, 0, wx.ALL, 0) self.vhsizer1 = wx.BoxSizer() self.lsizer1 = wx.BoxSizer(wx.VERTICAL) self.lsizer1.SetMinSize((util.MAX_IMAGE_SIZE, -1)) flag = wx.EXPAND | wx.ALIGN_CENTER | wx.ALL self.btn_face2face_1 = wx.Button(self.panel, label='Choose Image') self.lsizer1.Add(self.btn_face2face_1, 0, flag, 5) flag = wx.ALIGN_CENTER | wx.ALL self.bitmap_face2face_1 = base.MyStaticBitmap(self.panel) self.lsizer1.Add(self.bitmap_face2face_1, 0, flag, 5) self.btn_face2face_1.Bind( wx.EVT_BUTTON, lambda evt: self.OnChooseImage( evt, self.bitmap_face2face_1, 'face_id') ) self.vhsizer1.Add(self.lsizer1, 1, wx.EXPAND) self.msizer1 = wx.BoxSizer(wx.VERTICAL) self.msizer1.SetMinSize((90, -1)) self.btn_verify_1 = wx.Button(self.panel, label='Verify') self.msizer1.Add(self.btn_verify_1, 0, wx.EXPAND | wx.ALL, 5) self.result_1 = wx.StaticText(self.panel, label='Results:') self.msizer1.Add(self.result_1, 0, wx.EXPAND | wx.ALL, 5) self.btn_verify_1.Bind( wx.EVT_BUTTON, lambda evt: self.OnVerify(evt, 'face2face', self.result_1)) self.vhsizer1.Add(self.msizer1, 0, wx.ALIGN_CENTER | wx.ALL, 5) self.rsizer1 = wx.BoxSizer(wx.VERTICAL) self.rsizer1.SetMinSize((util.MAX_IMAGE_SIZE, -1)) flag = wx.EXPAND | wx.ALIGN_CENTER | wx.ALL self.btn_face2face_2 = wx.Button(self.panel, label='Choose Image') self.rsizer1.Add(self.btn_face2face_2, 0, flag, 5) flag = wx.ALIGN_CENTER | wx.ALL self.bitmap_face2face_2 = base.MyStaticBitmap(self.panel) self.rsizer1.Add(self.bitmap_face2face_2, 0, flag, 5) self.btn_face2face_2.Bind( wx.EVT_BUTTON, lambda evt: self.OnChooseImage( evt, self.bitmap_face2face_2, 'another_face_id') ) self.vhsizer1.Add(self.rsizer1, 1, wx.EXPAND) self.hvsizer.Add(self.vhsizer1) label = ("Demo 2: Face-to-person verification determines whether a " "face belongs to a given person. Click 'Load Person' to " "pick a folder containing the images of one person's face. " "Next, click 'Choose Image' to pick a face image of the " "same person (or of a different person). Finally, click " "'Verify' to see the verification result.") self.static_text = wx.StaticText(self.panel, label=label) self.static_text.Wrap(util.INNER_PANEL_WIDTH) self.hvsizer.Add(self.static_text, 0, wx.ALL, 0) self.vhsizer2 = wx.BoxSizer() self.lsizer2 = wx.BoxSizer(wx.VERTICAL) self.lsizer2.SetMinSize((util.MAX_IMAGE_SIZE, -1)) flag = wx.EXPAND | wx.ALIGN_CENTER | wx.ALL self.btn_face2person_1 = wx.Button(self.panel, label='Load Person') self.lsizer2.Add(self.btn_face2person_1, 0, flag, 5) self.Bind(wx.EVT_BUTTON, self.OnChooseFolder, self.btn_face2person_1) flag = wx.ALIGN_CENTER | wx.ALL self.grid = base.MyGridStaticBitmap(self.panel, 0, 4, 0, 0) self.lsizer2.Add(self.grid, 0, flag, 5) self.vhsizer2.Add(self.lsizer2, 1, wx.EXPAND) self.msizer2 = wx.BoxSizer(wx.VERTICAL) self.msizer2.SetMinSize((90, -1)) self.btn_verify_2 = wx.Button(self.panel, label='Verify') self.msizer2.Add(self.btn_verify_2, 0, wx.EXPAND | wx.ALL, 5) self.result_2 = wx.StaticText(self.panel, label='Results:') self.msizer2.Add(self.result_2, 0, wx.EXPAND | wx.ALL, 5) self.btn_verify_2.Bind( wx.EVT_BUTTON, lambda evt: self.OnVerify(evt, 'face2person', self.result_2)) self.vhsizer2.Add(self.msizer2, 0, wx.ALIGN_CENTER | wx.ALL, 5) self.rsizer2 = wx.BoxSizer(wx.VERTICAL) self.rsizer2.SetMinSize((util.MAX_IMAGE_SIZE, -1)) flag = wx.EXPAND | wx.ALIGN_CENTER | wx.ALL self.btn_face2person_2 = wx.Button(self.panel, label='Choose Image') self.rsizer2.Add(self.btn_face2person_2, 0, flag, 5) flag = wx.ALIGN_CENTER | wx.ALL self.bitmap_face2person = base.MyStaticBitmap(self.panel) self.rsizer2.Add(self.bitmap_face2person, 0, flag, 5) self.btn_face2person_2.Bind( wx.EVT_BUTTON, lambda evt: self.OnChooseImage( evt, self.bitmap_face2person, 'person_face_id') ) self.vhsizer2.Add(self.rsizer2, 1, wx.EXPAND) self.hvsizer.Add(self.vhsizer2) self.hsizer.Add(self.hvsizer) self.hsizer.AddStretchSpacer() self.hsizer.Layout() self.panel.SetSizer(self.hsizer) self.panel.Layout() self.panel.SetupScrolling(scroll_x=False) self.vsizer.Add(self.panel, 3, wx.EXPAND) self.log = base.MyLog(self) self.vsizer.Add(self.log, 1, wx.EXPAND) self.SetSizerAndFit(self.vsizer) self.btn_verify_1.Disable() self.btn_verify_2.Disable() def OnChooseImage(self, evt, bitmap, face_id): """Choose Image""" dlg = wx.FileDialog(self, wildcard=util.IMAGE_WILDCARD) if dlg.ShowModal() != wx.ID_OK: return path = dlg.GetPath() self.log.log('Request: Detecting {}'.format(path)) res = util.CF.face.detect(path) faces = [model.Face(face, path) for face in res] self.log.log('Response: Success. Detected {} face(s) in {}'.format( len(res), path)) if len(faces) > 1: text = ( 'Verification accepts two faces as input, please pick images ' 'with only one detectable face in it.') title = 'Warning' style = wx.OK | wx.ICON_WARNING wx.MessageBox(text, title, style) return bitmap.set_path(path) util.draw_bitmap_rectangle(bitmap, faces) self.face_ids[face_id] = faces[0].id self.check_btn_verify() def OnChooseFolder(self, evt): """Choose Folder.""" self.log.log(( 'Request: Group {0} will be used to build a person database. ' 'Checking whether the group exists.').format( self.large_person_group_id)) try: util.CF.large_person_group.get(self.large_person_group_id) self.log.log( 'Response: Group {0} exists.'.format( self.large_person_group_id)) text = ( 'Requires a clean up for group "{0}" before setting up a new ' 'person database. Click YES to proceed, group "{0}" will be ' 'cleared.').format(self.large_person_group_id) title = 'Warning' style = wx.YES_NO | wx.ICON_WARNING result = wx.MessageBox(text, title, style) if result == wx.YES: util.CF.large_person_group.delete(self.large_person_group_id) self.large_person_group_id = str(uuid.uuid1()) self.person_id = None else: return except util.CF.CognitiveFaceException as exp: if exp.code != 'LargePersonGroupNotFound': self.log.log('Response: {}. {}'.format(exp.code, exp.msg)) return else: self.log.log('Response: Group {0} does not exist previously.'. format(self.large_person_group_id)) dlg = wx.DirDialog(self) if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() self.person_name = os.path.basename(path) del self.face_paths[:] for root, dirs, files in os.walk(path): if files: self.face_paths.extend( [os.path.join(root, filename) for filename in files]) self.log.log( 'Request: Creating group "{0}"'.format( self.large_person_group_id)) util.CF.large_person_group.create(self.large_person_group_id) self.log.log( 'Response: Success. Group "{0}" created'.format( self.large_person_group_id)) self.log.log(( 'Preparing person for verification, detecting faces in ' 'chosen folder.')) self.log.log( 'Request: Creating person "{0}"'.format(self.person_name)) res = util.CF.large_person_group_person.create( self.large_person_group_id, self.person_name) self.person_id = res['personId'] self.log.log( 'Response: Success. Person "{0}" (PersonID:{1}) created'. format(self.person_name, self.person_id)) del self.detected_face_paths[:] for path in self.face_paths: res = util.CF.large_person_group_person_face.add( path, self.large_person_group_id, self.person_id) if res.get('persistedFaceId'): self.detected_face_paths.append(path) self.log.log('Response: Success. Total {0} faces are detected.'. format(len(self.detected_face_paths))) res = util.CF.large_person_group.train(self.large_person_group_id) self.grid.set_paths(self.detected_face_paths) self.panel.SetupScrolling(scroll_x=False) self.check_btn_verify() def OnVerify(self, evt, mode, result): """Verify the faces.""" if mode == 'face2face': self.log.log('Request: Verifying face {0} and {1}'.format( self.face_ids['face_id'], self.face_ids['another_face_id'])) res = util.CF.face.verify(self.face_ids['face_id'], self.face_ids['another_face_id']) if res['isIdentical']: self.log.log(( 'Response: Success. Face {0} and {1} belong to the same ' 'person').format( self.face_ids['face_id'], self.face_ids['another_face_id'])) text = ( 'Results: \nConfidence = {0}, two faces belong to the ' 'same person').format(res['confidence']) else: self.log.log(( 'Response: Success. Face {0} and {1} do not belong to ' 'the same person').format( self.face_ids['face_id'], self.face_ids['another_face_id'])) text = ( 'Results: \nConfidence = {0}, two faces do not belong to ' 'the same person').format(res['confidence']) else: util.CF.util.wait_for_large_person_group_training( self.large_person_group_id) self.log.log('Request: Verifying face {0} and person {1}'.format( self.face_ids['person_face_id'], self.person_id)) res = util.CF.face.verify( self.face_ids['person_face_id'], large_person_group_id=self.large_person_group_id, person_id=self.person_id) if res['isIdentical']: self.log.log( 'Response: Success. Face {0} belongs to person {1}'.format( self.face_ids['person_face_id'], self.person_name)) text = ( 'Results: \nConfidence = {0}, the face belongs to the ' 'person').format(res['confidence']) else: self.log.log(( 'Response: Success. Face {0} does not belong to person ' '{1}').format( self.face_ids['person_face_id'], self.person_name)) text = ( 'Results: \nConfidence = {0}, the face does not belong to ' 'the person').format(res['confidence']) result.SetLabelText(text) result.Wrap(88) def check_btn_verify(self): """Check whether the verify button is valid.""" if self.face_ids['face_id'] and self.face_ids['another_face_id']: self.btn_verify_1.Enable() if self.person_id and self.face_ids['person_face_id']: self.btn_verify_2.Enable()
Cognitive-Face-Python/sample/view/panel_verification.py/0
{ "file_path": "Cognitive-Face-Python/sample/view/panel_verification.py", "repo_id": "Cognitive-Face-Python", "token_count": 7171 }
214
# :mailbox: Paper Code Collection (MSRA DKI Group) <img src="https://img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RE1Mu3b?ver=5c31" height="25" align=center> [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) This repo hosts multiple open-source codes of the [Microsoft Research Asia DKI Group](https://www.microsoft.com/en-us/research/opportunity/data-analytics-intern-msra-dki/). You could find the corresponding code as below: ## News - December, 2022: our paper [MultiSpider: Towards Benchmarking Multilingual Text-to-SQL Semantic Parsing](https://arxiv.org/abs/2212.13492) was accepted by AAAI 2023. - October, 2022: our paper [Towards Knowledge-Intensive Text-to-SQL Semantic Parsing with Formulaic Knowledge](https://arxiv.org/abs/2301.01067) was accepted by EMNLP 2022. - October, 2022: our paper [Reasoning Like Program Executors](https://arxiv.org/abs/2201.11473) was accepted by EMNLP 2022. - October, 2022: our paper [LEMON: Language-Based Environment Manipulation via Execution-guided Pre-training](https://arxiv.org/abs/2201.08081) was accepted by EMNLP 2022 Findings. - October, 2022: our paper [AdapterShare: Task Correlation Modeling with Adapter Differentiation](https://aclanthology.org/2022.emnlp-main.728.pdf) was accepted by EMNLP 2022. - Septempter, 2022: our paper [LogiGAN: Learning Logical Reasoning via Adversarial Pre-training](https://arxiv.org/abs/2205.08794) was accepted by NeurIPS 2022. - March, 2022: Our paper [Towards Robustness of Text-to-SQL Models Against Natural and Realistic Adversarial Table Perturbation](https://aclanthology.org/2022.acl-long.142.pdf) was accepted by ACL 2022. - August, 2021: Our paper [Awakening Latent Grounding from Pretrained Language Models for Semantic Parsing](https://aclanthology.org/2021.findings-acl.100.pdf) was accepted by ACL 2021 Findings. - August, 2021: Our paper [Learning Algebraic Recombination for Compositional Generalization](https://arxiv.org/abs/2107.06516) was accepted by ACL 2021 Findings. - September, 2020: Our paper ["What Do You Mean by That?" A Parser-Independent Interactive Approach for Enhancing Text-to-SQL](https://arxiv.org/abs/2011.04151) was accepted by EMNLP 2020. - September, 2020: Our paper [Incomplete Utterance Rewriting as Semantic Segmentation](https://arxiv.org/abs/2009.13166) was accepted by EMNLP 2020. - September, 2020: Our paper [Hierarchical Poset Decoding for Compositional Generalization in Language](https://arxiv.org/abs/2010.07792) was accepted by NeurIPS 2020. - September, 2020: Our paper [Compositional Generalization by Learning Analytical Expressions](https://arxiv.org/abs/2006.10627) was accepted by NeurIPS 2020 as *Spotlight*. - April, 2020: Our paper [How Far are We from Effective Context Modeling ? An Exploratory Study on Semantic Parsing in Context](https://arxiv.org/abs/2002.00652) was accepted by IJCAI 2020. ## Code Release (Click Title to Locate the Code) ### Reasoning > **[Reasoning Like Program Executors](poet)** > Xinyu Pi*, Qian Liu*, Bei Chen, Morteza Ziyadi, Zeqi Lin, Qiang Fu, Yan Gao, Jian-Guang Lou, Weizhu Chen, EMNLP 2022. > **[LEMON: Language-Based Environment Manipulation via Execution-guided Pre-training](lemon)** > Qi Shi, Qian Liu, Bei Chen, Yu Zhang, Ting Liu, Jian-Guang Lou, EMNLP 2022 Findings. > **[LogiGAN: Learning Logical Reasoning via Adversarial Pre-training](logigan)** > Xinyu Pi*, Wanjun Zhong*, Yan Gao, Nan Duan, Jian-Guang Lou, NeurIPS 2022. ### Text-to-SQL > **[MultiSpider: Towards Benchmarking Multilingual Text-to-SQL Semantic Parsing](multilingual_text_to_sql)** > Longxu Dou, Yan Gao, Mingyang Pan, Dingzirui Wang, Wanxiang Che, Dechen Zhan, Jian-Guang Lou, AAAI 2023. > > **[Towards Knowledge-Intensive Text-to-SQL Semantic Parsing with Formulaic Knowledge](knowledge_intensive_text_to_sql)** > Longxu Dou, Yan Gao, Xuqi Liu, Mingyang Pan, Dingzirui Wang, Wanxiang Che, Min-Yen Kan, Dechen Zhan, Jian-Guang Lou, EMNLP 2022. > **[UniSAr: A Unified Structure-Aware Autoregressive Language Model for Text-to-SQL](unified_parser_text_to_sql)** > Longxu Dou, Yan Gao, Mingyang Pan, Dingzirui Wang, Wanxiang Che, Dechen Zhan, Jian-Guang Lou, arxiv 2022. > **[Towards Robustness of Text-to-SQL Models Against Natural and Realistic Adversarial Table Perturbation](robustness_of_text_to_sql)** > Xinyu Pi*, Bing Wang*, Yan Gao, Jiaqi Guo, Zhoujun Li, Jian-Guang Lou, ACL 2022. > **[Awakening Latent Grounding from Pretrained Language Models for Semantic Parsing](awakening_latent_grounding)** > Qian Liu*, Dejian Yang*, Jiahui Zhang*, Jiaqi Guo, Bin Zhou, Jian-Guang Lou, ACL 2021 Findings. > ### Compositional Generalization > **[Learning Algebraic Recombination for Compositional Generalization](https://github.com/thousfeet/LEAR)** > Chenyao Liu*, Shengnan An*, Zeqi Lin, Qian Liu, Bei Chen, Jian-Guang Lou, Lijie Wen, Nanning Zheng, Dongmei Zhang, ACL 2021 Findings. > **[Hierarchical Poset Decoding for Compositional Generalization in Language](poset_decoding)** > Yinuo Guo, Zeqi Lin, Jian-Guang Lou, Dongmei Zhang, NeurIPS 2020. > **[Compositional Generalization by Learning Analytical Expressions](compositional_generalization)** > Qian Liu*, Shengnan An*, Jian-Guang Lou, Bei Chen, Zeqi Lin, Yan Gao, Bin Zhou, Nanning Zheng, Dongmei Zhang, NeurIPS 2020. ### Conversation > **["What Do You Mean by That?" A Parser-Independent Interactive Approach for Enhancing Text-to-SQL](interactive_text_to_sql)** > Yuntao Li, Bei Chen, Qian Liu, Yan Gao, Jian-Guang Lou, Yan Zhang, Dongmei Zhang, EMNLP 2020 > **[Incomplete Utterance Rewriting as Semantic Segmentation](incomplete_utterance_rewriting)** > Qian Liu, Bei Chen, Jian-Guang Lou, Bin Zhou, Dongmei Zhang, EMNLP 2020 > **[How Far are We from Effective Context Modeling ? An Exploratory Study on Semantic Parsing in Context](semantic_parsing_in_context)** > Qian Liu, Bei Chen, Jiaqi Guo, Jian-Guang Lou, Bin Zhou, Dongmei Zhang, IJCAI 2020 ## Contributing This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com. When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA. This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments. ## Question If you have any question or find any bug, please go ahead and [open an issue](https://github.com/microsoft/ContextualSP/issues). Issues are an acceptable discussion forum as well. If you want to concat the author, please email: `qian DOT liu AT buaa.edu.cn`.
ContextualSP/README.md/0
{ "file_path": "ContextualSP/README.md", "repo_id": "ContextualSP", "token_count": 2215 }
215
# coding=utf-8 # Copyright (c) Microsoft. All rights reserved. """Extract feature vectors. """ import os import argparse import torch import json from transformers import AutoTokenizer from torch.utils.data import DataLoader from data_utils.log_wrapper import create_logger from data_utils.utils import set_environment from mt_dnn.batcher import Collater, SingleTaskDataset from mt_dnn.model import MTDNNModel from data_utils.task_def import DataFormat, EncoderModelType logger = create_logger(__name__, to_disk=True, log_file="mt_dnn_feature_extractor.log") def load_data(file): rows = [] cnt = 0 is_single_sentence = False with open(file, encoding="utf8") as f: for line in f: blocks = line.strip().split("|||") if len(blocks) == 2: sample = { "uid": str(cnt), "premise": blocks[0], "hypothesis": blocks[1], "label": 0, } else: is_single_sentence = True sample = {"uid": str(cnt), "premise": blocks[0], "label": 0} rows.append(sample) cnt += 1 return rows, is_single_sentence def build_data(data, max_seq_len, is_train=True, tokenizer=None): """Build data of sentence pair tasks""" rows = [] for idx, sample in enumerate(data): ids = sample["uid"] label = sample["label"] inputs = tokenizer( sample["premise"], sample["hypothesis"], add_special_tokens=True, max_length=max_seq_len, truncation=True, padding=False, ) input_ids = inputs["input_ids"] token_type_ids = ( inputs["token_type_ids"] if "token_type_ids" in inputs else [0] * len(input_ids) ) # The mask has 1 for real tokens and 0 for padding tokens. Only real # tokens are attended to. attention_mask = inputs["attention_mask"] feature = { "uid": ids, "label": label, "token_id": input_ids, "type_id": token_type_ids, #"tokens": ["[CLS]"] + sample["premise"] + ["[SEP]"] + sample["hypothesis"] + ["[SEP]"], } rows.append(feature) return rows def build_data_single(data, max_seq_len, tokenizer=None): """Build data of single sentence tasks""" rows = [] for idx, sample in enumerate(data): ids = sample["uid"] label = sample["label"] inputs = tokenizer( sample["premise"], None, add_special_tokens=True, max_length=max_seq_len, truncation=True, padding=False, ) input_ids = inputs["input_ids"] token_type_ids = ( inputs["token_type_ids"] if "token_type_ids" in inputs else [0] * len(input_ids) ) # The mask has 1 for real tokens and 0 for padding tokens. Only real # tokens are attended to. attention_mask = inputs["attention_mask"] features = { "uid": ids, "label": label, "token_id": input_ids, "type_id": token_type_ids, } rows.append(features) return rows def model_config(parser): parser.add_argument("--update_bert_opt", default=0, type=int) parser.add_argument("--multi_gpu_on", action="store_true") parser.add_argument( "--mem_cum_type", type=str, default="simple", help="bilinear/simple/defualt" ) parser.add_argument("--answer_num_turn", type=int, default=5) parser.add_argument("--answer_mem_drop_p", type=float, default=0.1) parser.add_argument("--answer_att_hidden_size", type=int, default=128) parser.add_argument( "--answer_att_type", type=str, default="bilinear", help="bilinear/simple/defualt", ) parser.add_argument( "--answer_rnn_type", type=str, default="gru", help="rnn/gru/lstm" ) parser.add_argument( "--answer_sum_att_type", type=str, default="bilinear", help="bilinear/simple/defualt", ) parser.add_argument("--answer_merge_opt", type=int, default=1) parser.add_argument("--answer_mem_type", type=int, default=1) parser.add_argument("--answer_dropout_p", type=float, default=0.1) parser.add_argument("--answer_weight_norm_on", action="store_true") parser.add_argument("--dump_state_on", action="store_true") parser.add_argument("--answer_opt", type=int, default=0, help="0,1") parser.add_argument("--label_size", type=str, default="3") parser.add_argument("--mtl_opt", type=int, default=0) parser.add_argument("--ratio", type=float, default=0) parser.add_argument("--mix_opt", type=int, default=0) parser.add_argument("--init_ratio", type=float, default=1) return parser def train_config(parser): parser.add_argument( "--cuda", type=bool, default=torch.cuda.is_available(), help="whether to use GPU acceleration.", ) parser.add_argument( "--optimizer", default="adamax", help="supported optimizer: adamax, sgd, adadelta, adam", ) parser.add_argument("--grad_clipping", type=float, default=0) parser.add_argument("--global_grad_clipping", type=float, default=1.0) parser.add_argument("--weight_decay", type=float, default=0) parser.add_argument("--learning_rate", type=float, default=5e-5) parser.add_argument("--momentum", type=float, default=0) parser.add_argument("--warmup", type=float, default=0.1) parser.add_argument("--warmup_schedule", type=str, default="warmup_linear") parser.add_argument("--vb_dropout", action="store_false") parser.add_argument("--dropout_p", type=float, default=0.1) parser.add_argument("--dropout_w", type=float, default=0.000) parser.add_argument("--bert_dropout_p", type=float, default=0.1) parser.add_argument("--ema_opt", type=int, default=0) parser.add_argument("--ema_gamma", type=float, default=0.995) # scheduler parser.add_argument( "--have_lr_scheduler", dest="have_lr_scheduler", action="store_false" ) parser.add_argument("--multi_step_lr", type=str, default="10,20,30") parser.add_argument("--freeze_layers", type=int, default=-1) parser.add_argument("--embedding_opt", type=int, default=0) parser.add_argument("--lr_gamma", type=float, default=0.5) parser.add_argument("--bert_l2norm", type=float, default=0.0) parser.add_argument("--scheduler_type", type=str, default="ms", help="ms/rop/exp") parser.add_argument("--output_dir", default="checkpoint") parser.add_argument( "--seed", type=int, default=2018, help="random seed for data shuffling, embedding init, etc.", ) parser.add_argument("--encoder_type", type=int, default=EncoderModelType.BERT) # fp 16 parser.add_argument( "--fp16", action="store_true", help="Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit", ) parser.add_argument( "--fp16_opt_level", type=str, default="O1", help="For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3']." "See details at https://nvidia.github.io/apex/amp.html", ) return parser def set_config(parser): parser.add_argument("--finput", default=None, type=str, required=True) parser.add_argument("--foutput", default=None, type=str, required=True) parser.add_argument( "--bert_model", default=None, type=str, required=True, help="Bert model: bert-base-uncased", ) parser.add_argument( "--checkpoint", default=None, type=str, required=True, help="model parameters" ) parser.add_argument( "--do_lower_case", action="store_true", help="Set this flag if you are using an uncased model.", ) parser.add_argument("--layers", default="10,11", type=str) parser.add_argument("--max_seq_length", default=512, type=int, help="") parser.add_argument("--batch_size", default=4, type=int) parser.add_argument("--transformer_cache", default=".cache", type=str) def process_data(args): tokenizer = AutoTokenizer.from_pretrained( args.bert_model, cache_dir=args.transformer_cache ) path = args.finput data, is_single_sentence = load_data(path) if is_single_sentence: tokened_data = build_data_single( data, max_seq_len=args.max_seq_length, tokenizer=tokenizer ) else: tokened_data = build_data( data, max_seq_len=args.max_seq_length, tokenizer=tokenizer ) return tokened_data, is_single_sentence def dump_data(data, path): with open(path, "w", encoding="utf-8") as writer: for sample in data: writer.write("{}\n".format(json.dumps(sample))) def main(): parser = argparse.ArgumentParser() model_config(parser) set_config(parser) train_config(parser) args = parser.parse_args() encoder_type = args.encoder_type layer_indexes = [int(x) for x in args.layers.split(",")] set_environment(args.seed) # process data data, is_single_sentence = process_data(args) data_type = ( DataFormat.PremiseOnly if is_single_sentence else DataFormat.PremiseAndOneHypothesis ) fout_temp = "{}.tmp".format(args.finput) dump_data(data, fout_temp) collater = Collater(is_train=False, encoder_type=encoder_type) dataset = SingleTaskDataset( fout_temp, False, maxlen=args.max_seq_length, ) batcher = DataLoader( dataset, batch_size=args.batch_size, collate_fn=collater.collate_fn, pin_memory=args.cuda, ) opt = vars(args) # load model if os.path.exists(args.checkpoint): state_dict = torch.load(args.checkpoint) config = state_dict["config"] config["dump_feature"] = True opt.update(config) else: logger.error("#" * 20) logger.error( "Could not find the init model!\n The parameters will be initialized randomly!" ) logger.error("#" * 20) return num_all_batches = len(batcher) model = MTDNNModel(opt, state_dict=state_dict, num_train_step=num_all_batches) if args.cuda: model.cuda() features_dict = {} for batch_meta, batch_data in batcher: batch_meta, batch_data = Collater.patch_data(args.cuda, batch_meta, batch_data) all_encoder_layers, _ = model.extract(batch_meta, batch_data) embeddings = [ all_encoder_layers[idx].detach().cpu().numpy() for idx in layer_indexes ] uids = batch_meta["uids"] masks = batch_data[batch_meta["mask"]].detach().cpu().numpy().tolist() for idx, uid in enumerate(uids): slen = sum(masks[idx]) features = {} for yidx, layer in enumerate(layer_indexes): features[layer] = str(embeddings[yidx][idx][:slen].tolist()) features_dict[uid] = features # save features with open(args.foutput, "w", encoding="utf-8") as writer: for sample in data: uid = sample["uid"] feature = features_dict[uid] feature["uid"] = uid writer.write("{}\n".format(json.dumps(feature))) if __name__ == "__main__": main()
ContextualSP/adaptershare/experiments/dump_embedding/extractor.py/0
{ "file_path": "ContextualSP/adaptershare/experiments/dump_embedding/extractor.py", "repo_id": "ContextualSP", "token_count": 5126 }
216
squad: data_format: CLUE_SPAN dropout_p: 0.1 enable_san: false metric_meta: - EmF12 task_type: Span loss: SpanCeCriterion kd_loss: MseCriterion adv_loss: SymKlCriterion n_class: 2 split_names: - train - dev squad-v2: data_format: CLUE_SPAN dropout_p: 0.1 enable_san: false metric_meta: - EmF12 task_type: SpanYN loss: SpanCeCriterion kd_loss: MseCriterion adv_loss: SymKlCriterion n_class: 2 split_names: - train - dev record: data_format: CLUE_SPAN dropout_p: 0.1 enable_san: false metric_meta: - EmF1 n_class: 2 task_type: Span loss: SpanCeCriterion kd_loss: MseCriterion adv_loss: SymKlCriterion split_names: - train - dev
ContextualSP/adaptershare/experiments/squad/squad_task_def.yml/0
{ "file_path": "ContextualSP/adaptershare/experiments/squad/squad_task_def.yml", "repo_id": "ContextualSP", "token_count": 317 }
217
# coding=utf-8 # Copyright (c) Microsoft. All rights reserved. import torch import torch.nn as nn import torch.nn.functional as F import numpy from torch.nn.utils import weight_norm from torch.nn.parameter import Parameter from .common import activation, init_wrapper from .dropout_wrapper import DropoutWrapper class DotProduct(nn.Module): def __init__(self, x1_dim, x2_dim, prefix="sim", opt={}, dropout=None): super(DotProduct, self).__init__() assert x1_dim == x2_dim self.opt = opt self.prefix = prefix self.scale_on = opt.get("{}_scale".format(self.prefix), False) self.scalor = 1.0 / numpy.power(x2_dim, 0.5) def forward(self, x1, x2): assert x1.size(2) == x2.size(2) scores = x1.bmm(x2.transpose(1, 2)) if self.scale_on: scores *= self.scalor return scores class DotProductProject(nn.Module): def __init__(self, x1_dim, x2_dim, prefix="sim", opt={}, dropout=None): super(DotProductProject, self).__init__() self.prefix = prefix self.opt = opt self.hidden_size = opt.get("{}_hidden_size".format(self.prefix), 64) self.residual_on = opt.get("{}_residual_on".format(self.prefix), False) self.layer_norm_on = opt.get("{}_norm_on".format(self.prefix), False) self.share = opt.get("{}_share".format(self.prefix), False) self.f = activation(opt.get("{}_activation".format(self.prefix), "relu")) self.scale_on = opt.get("{}_scale_on".format(self.prefix), False) self.dropout = dropout x1_in_dim = x1_dim x2_in_dim = x2_dim out_dim = self.hidden_size self.proj_1 = nn.Linear(x1_in_dim, out_dim, bias=False) if self.layer_norm_on: self.proj_1 = weight_norm(self.proj_1) if self.share and x1_in_dim == x2_in_dim: self.proj_2 = self.proj_1 else: self.proj_2 = nn.Linear(x2_in_dim, out_dim) if self.layer_norm_on: self.proj_2 = weight_norm(self.proj_2) if self.scale_on: self.scalar = Parameter( torch.ones(1, 1, 1) / (self.hidden_size**0.5), requires_grad=False ) else: self.sclalar = Parameter( torch.ones(1, 1, self.hidden_size), requires_grad=True ) def forward(self, x1, x2): assert x1.size(2) == x2.size(2) if self.dropout: x1 = self.dropout(x1) x2 = self.dropout(x2) x1_flat = x1.contiguous().view(-1, x1.size(2)) x2_flat = x2.contiguous().view(-1, x2.size(2)) x1_o = self.f(self.proj_1(x1_flat)).view(x1.size(0), x1.size(1), -1) # x2_o = self.f(self.proj_1(x2_flat)).view(x2.size(0), x2.size(1), -1) x2_o = self.f(self.proj_2(x2_flat)).view(x2.size(0), x2.size(1), -1) if self.scale_on: scalar = self.scalar.expand_as(x2_o) x2_o = scalar * x2_o scores = x1_o.bmm(x2_o.transpose(1, 2)) return scores class Bilinear(nn.Module): def __init__(self, x1_dim, x2_dim, prefix="sim", opt={}, dropout=None): super(Bilinear, self).__init__() self.opt = opt self.layer_norm_on = opt.get("{}_norm_on".format(self.prefix), False) self.transform_on = opt.get("{}_proj_on".format(self.prefix), False) # self.init = init_wrapper(opt.get('{}_init'.format(self.prefix), '')) self.dropout = dropout if self.transform_on: self.proj = nn.Linear(x1_dim, x2_dim) # self.init(self.proj.weight) if self.layer_norm_on: self.proj = weight_norm(self.proj) def forward(self, x, y): """ x = batch * len * h1 y = batch * h2 x_mask = batch * len """ if self.dropout: x = self.dropout(x) y = self.dropout(y) proj = self.proj(y) if self.transform_on else y if self.dropout: proj = self.dropout(proj) scores = x.bmm(proj.unsqueeze(2)).squeeze(2) return scores class BilinearSum(nn.Module): def __init__(self, x1_dim, x2_dim, prefix="sim", opt={}, dropout=None): super(BilinearSum, self).__init__() self.x_linear = nn.Linear(x1_dim, 1, bias=False) self.y_linear = nn.Linear(x2_dim, 1, bias=False) self.layer_norm_on = opt.get("{}_norm_on".format(self.prefix), False) self.init = init_wrapper(opt.get("{}_init".format(self.prefix), False)) if self.layer_norm_on: self.x_linear = weight_norm(self.x_linear) self.y_linear = weight_norm(self.y_linear) self.init(self.x_linear.weight) self.init(self.y_linear.weight) self.dropout = dropout def forward(self, x1, x2): """ x1: batch * len1 * input_size x2: batch * len2 * input_size score: batch * len1 * len2 """ if self.dropout: x1 = self.dropout(x1) x2 = self.dropout(x2) x1_logits = self.x_linear(x1.contiguous().view(-1, x1.size(-1))).view( x1.size(0), -1, 1 ) x2_logits = self.y_linear(x2.contiguous().view(-1, x2.size(-1))).view( x2.size(0), 1, -1 ) shape = (x1.size(0), x1.size(1), x2.size()) scores = x1_logits.expand_as(shape) + x2_logits.expand_as(shape) return scores class Trilinear(nn.Module): """Function used in BiDAF""" def __init__(self, x1_dim, x2_dim, prefix="sim", opt={}, dropout=None): super(Trilinear, self).__init__() self.prefix = prefix self.x_linear = nn.Linear(x1_dim, 1, bias=False) self.x_dot_linear = nn.Linear(x1_dim, 1, bias=False) self.y_linear = nn.Linear(x2_dim, 1, bias=False) self.layer_norm_on = opt.get("{}_norm_on".format(self.prefix), False) self.init = init_wrapper( opt.get("{}_init".format(self.prefix), "xavier_uniform") ) if self.layer_norm_on: self.x_linear = weight_norm(self.x_linear) self.x_dot_linear = weight_norm(self.x_dot_linear) self.y_linear = weight_norm(self.y_linear) self.init(self.x_linear.weight) self.init(self.x_dot_linear.weight) self.init(self.y_linear.weight) self.dropout = dropout def forward(self, x1, x2): """ x1: batch * len1 * input_size x2: batch * len2 * input_size score: batch * len1 * len2 """ if self.dropout: x1 = self.dropout(x1) x2 = self.dropout(x2) x1_logits = self.x_linear(x1.contiguous().view(-1, x1.size(-1))).view( x1.size(0), -1, 1 ) x2_logits = self.y_linear(x2.contiguous().view(-1, x2.size(-1))).view( x2.size(0), 1, -1 ) x1_dot = ( self.x_dot_linear(x1.contiguous().view(-1, x1.size(-1))) .view(x1.size(0), -1, 1) .expand_as(x1) ) x1_dot = x1 * x1_dot scores = x1_dot.bmm(x2.transpose(1, 2)) scores += x1_logits.expand_as(scores) + x2_logits.expand_as(scores) return scores class SimilarityWrapper(nn.Module): def __init__(self, x1_dim, x2_dim, prefix="attention", opt={}, dropout=None): super(SimilarityWrapper, self).__init__() self.score_func_str = opt.get( "{}_sim_func".format(prefix), "dotproductproject" ).lower() self.score_func = None if self.score_func_str == "dotproduct": self.score_func = DotProduct( x1_dim, x2_dim, prefix=prefix, opt=opt, dropout=dropout ) elif self.score_func_str == "dotproductproject": self.score_func = DotProductProject( x1_dim, x2_dim, prefix=prefix, opt=opt, dropout=dropout ) elif self.score_func_str == "bilinear": self.score_func = Bilinear( x1_dim, x2_dim, prefix=prefix, opt=opt, dropout=dropout ) elif self.score_func_str == "bilinearsum": self.score_func = BilinearSum( x1_dim, x2_dim, prefix=prefix, opt=opt, dropout=dropout ) elif self.score_func_str == "trilinear": self.score_func = Trilinear( x1_dim, x2_dim, prefix=prefix, opt=opt, dropout=dropout ) else: raise NotImplementedError def forward(self, x1, x2): scores = self.score_func(x1, x2) return scores class AttentionWrapper(nn.Module): def __init__( self, x1_dim, x2_dim, x3_dim=None, prefix="attention", opt={}, dropout=None ): super(AttentionWrapper, self).__init__() self.prefix = prefix self.att_dropout = opt.get("{}_att_dropout".format(self.prefix), 0) self.score_func = SimilarityWrapper( x1_dim, x2_dim, prefix=prefix, opt=opt, dropout=dropout ) self.drop_diagonal = opt.get("{}_drop_diagonal".format(self.prefix), False) self.output_size = x2_dim if x3_dim is None else x3_dim def forward(self, query, key, value, key_padding_mask=None, return_scores=False): logits = self.score_func(query, key) key_mask = key_padding_mask.unsqueeze(1).expand_as(logits) logits.data.masked_fill_(key_mask.data, -float("inf")) if self.drop_diagonal: assert logits.size(1) == logits.size(2) diag_mask = ( torch.diag(logits.data.new(logits.size(1)).zero_() + 1) .byte() .unsqueeze(0) .expand_as(logits) ) logits.data.masked_fill_(diag_mask, -float("inf")) prob = F.softmax(logits.view(-1, key.size(1)), 1) prob = prob.view(-1, query.size(1), key.size(1)) if self.att_dropout > 0: prob = self.dropout(prob) if value is None: value = key attn = prob.bmm(value) if return_scores: return attn, prob, logits else: return attn class LinearSelfAttn(nn.Module): """Self attention over a sequence: * o_i = softmax(Wx_i) for x_i in X. """ def __init__(self, input_size, dropout=None): super(LinearSelfAttn, self).__init__() self.linear = nn.Linear(input_size, 1) self.dropout = dropout def forward(self, x, x_mask): x = self.dropout(x) x_flat = x.contiguous().view(-1, x.size(-1)) scores = self.linear(x_flat).view(x.size(0), x.size(1)) scores.data.masked_fill_(x_mask.data, -float("inf")) alpha = F.softmax(scores, 1) return alpha.unsqueeze(1).bmm(x).squeeze(1) class MLPSelfAttn(nn.Module): def __init__(self, input_size, opt={}, prefix="attn_sum", dropout=None): super(MLPSelfAttn, self).__init__() self.prefix = prefix self.FC = nn.Linear(input_size, input_size) self.linear = nn.Linear(input_size, 1) self.layer_norm_on = opt.get("{}_norm_on".format(self.prefix), False) self.f = activation(opt.get("{}_activation".format(self.prefix), "relu")) if dropout is None: self.dropout = DropoutWrapper( opt.get("{}_dropout_p".format(self.prefix), 0) ) else: self.dropout = dropout if self.layer_norm_on: self.FC = weight_norm(self.FC) def forward(self, x, x_mask): x = self.dropout(x) x_flat = x.contiguous().view(-1, x.size(-1)) scores = self.linear(self.f(self.FC(x_flat))).view(x.size(0), x.size(1)) scores.data.masked_fill_(x_mask.data, -float("inf")) alpha = F.softmax(scores) return alpha.unsqueeze(1).bmm(x).squeeze(1) class SelfAttnWrapper(nn.Module): def __init__(self, input_size, prefix="attn_sum", opt={}, dropout=None): super(SelfAttnWrapper, self).__init__() """ Self att wrapper, support linear and MLP """ attn_type = opt.get("{}_type".format(prefix), "linear") if attn_type == "mlp": self.att = MLPSelfAttn(input_size, prefix, opt, dropout) else: self.att = LinearSelfAttn(input_size, dropout) def forward(self, x, x_mask): return self.att(x, x_mask) class DeepAttentionWrapper(nn.Module): def __init__( self, x1_dim, x2_dim, x3_dims, att_cnt, prefix="deep_att", opt=None, dropout=None, ): super(DeepAttentionWrapper, self).__init__() self.opt = {} if opt is None else opt self.prefix = prefix self.x1_dim = x1_dim self.x2_dim = x2_dim self.x3_dims = x3_dims if dropout is None: self.dropout = DropoutWrapper( opt.get("{}_dropout_p".format(self.prefix), 0) ) else: self.dropout = dropout self.attn_list = nn.ModuleList() for i in range(0, att_cnt): if opt["multihead_on"]: attention = MultiheadAttentionWrapper( self.x1_dim, self.x2_dim, self.x3_dims[i], prefix, opt, dropout=dropout, ) else: attention = AttentionWrapper( self.x1_dim, self.x2_dim, self.x3_dims[i], prefix, opt, self.dropout ) self.attn_list.append(attention) def forward(self, x1, x2, x3, x2_mask): rvl = [] for i in range(0, len(x3)): hiddens = self.attn_list[i](x1, x2, x3[i], x2_mask) rvl.append(hiddens) return torch.cat(rvl, 2) class BilinearFlatSim(nn.Module): """A bilinear attention layer over a sequence X w.r.t y: * o_i = x_i'Wy for x_i in X. """ def __init__(self, x_size, y_size, opt={}, prefix="seqatt", dropout=None): super(BilinearFlatSim, self).__init__() self.opt = opt self.weight_norm_on = opt.get("{}_weight_norm_on".format(prefix), False) self.linear = nn.Linear(y_size, x_size) if self.weight_norm_on: self.linear = weight_norm(self.linear) if dropout is None: self.dropout = DropoutWrapper( opt.get("{}_dropout_p".format(self.prefix), 0) ) else: self.dropout = dropout def forward(self, x, y, x_mask): """ x = batch * len * h1 y = batch * h2 x_mask = batch * len """ x = self.dropout(x) y = self.dropout(y) Wy = self.linear(y) xWy = x.bmm(Wy.unsqueeze(2)).squeeze(2) xWy.data.masked_fill_(x_mask.data, -float("inf")) return xWy class SimpleFlatSim(nn.Module): def __init__(self, x_size, y_size, opt={}, prefix="seqatt", dropout=None): super(SimpleFlatSim, self).__init__() self.opt = opt self.weight_norm_on = opt.get("{}_norm_on".format(prefix), False) self.linear = nn.Linear(y_size + x_size, 1) if self.weight_norm_on: self.linear = weight_norm(self.linear) if dropout is None: self.dropout = DropoutWrapper( opt.get("{}_dropout_p".format(self.prefix), 0) ) else: self.dropout = dropout def forward(self, x, y, x_mask): """ x = batch * len * h1 y = batch * h2 x_mask = batch * len """ x = self.dropout(x) y = self.dropout(y) y = y.unsqueeze(1).expand_as(x) flat_x = torch.cat([x, y], 2).contiguous().view(x.size(0) * x.size(1), -1) flat_scores = self.linear(flat_x) scores = flat_scores.contiguous().view(x.size(0), -1) scores.data.masked_fill_(x_mask.data, -float("inf")) return scores class FlatSim(nn.Module): def __init__(self, x_size, y_size, opt={}, prefix="seqatt", dropout=None): super(FlatSim, self).__init__() assert x_size == y_size self.opt = opt self.weight_norm_on = opt.get("{}_weight_norm_on".format(prefix), False) self.linear = nn.Linear(x_size * 3, 1) if self.weight_norm_on: self.linear = weight_norm(self.linear) if dropout is None: self.dropout = DropoutWrapper( opt.get("{}_dropout_p".format(self.prefix), 0) ) else: self.dropout = dropout def forward(self, x, y, x_mask): """ x = batch * len * h1 y = batch * h2 x_mask = batch * len """ x = self.dropout(x) y = self.dropout(y) y = y.unsqueeze(1).expand_as(x) flat_x = ( torch.cat([x, y, x * y], 2).contiguous().view(x.size(0) * x.size(1), -1) ) flat_scores = self.linear(flat_x) scores = flat_scores.contiguous().view(x.size(0), -1) scores.data.masked_fill_(x_mask.data, -float("inf")) return scores class FlatSimV2(nn.Module): def __init__(self, x_size, y_size, opt={}, prefix="seqatt", dropout=None): super(FlatSimV2, self).__init__() assert x_size == y_size self.opt = opt self.weight_norm_on = opt.get("{}_weight_norm_on".format(prefix), False) self.linear = nn.Linear(x_size * 4, 1) if self.weight_norm_on: self.linear = weight_norm(self.linear) if dropout is None: self.dropout = DropoutWrapper( opt.get("{}_dropout_p".format(self.prefix), 0) ) else: self.dropout = dropout def forward(self, x, y, x_mask): """ x = batch * len * h1 y = batch * h2 x_mask = batch * len """ x = self.dropout(x) y = self.dropout(y) y = y.unsqueeze(1).expand_as(x) flat_x = ( torch.cat([x, y, x * y, torch.abs(x - y)], 2) .contiguous() .view(x.size(0) * x.size(1), -1) ) flat_scores = self.linear(flat_x) scores = flat_scores.contiguous().view(x.size(0), -1) scores.data.masked_fill_(x_mask.data, -float("inf")) return scores class FlatSimilarityWrapper(nn.Module): def __init__(self, x1_dim, x2_dim, prefix="attention", opt={}, dropout=None): super(FlatSimilarityWrapper, self).__init__() self.score_func_str = opt.get("{}_att_type".format(prefix), "none").lower() self.att_dropout = DropoutWrapper(opt.get("{}_att_dropout".format(prefix), 0)) self.score_func = None if self.score_func_str == "bilinear": self.score_func = BilinearFlatSim( x1_dim, x2_dim, prefix=prefix, opt=opt, dropout=dropout ) elif self.score_func_str == "simple": self.score_func = SimpleFlatSim( x1_dim, x2_dim, prefix=prefix, opt=opt, dropout=dropout ) elif self.score_func_str == "flatsim": self.score_func = FlatSim( x1_dim, x2_dim, prefix=prefix, opt=opt, dropout=dropout ) else: self.score_func = FlatSimV2( x1_dim, x2_dim, prefix=prefix, opt=opt, dropout=dropout ) def forward(self, x1, x2, mask): scores = self.score_func(x1, x2, mask) return scores class MultiheadAttentionWrapper(nn.Module): """Multi-headed attention. See "Attention Is All You Need" for more details. """ def __init__( self, query_dim, key_dim, value_dim, prefix="attention", opt={}, dropout=None ): super().__init__() self.prefix = prefix self.num_heads = opt.get("{}_head".format(self.prefix), 1) self.dropout = ( DropoutWrapper(opt.get("{}_dropout".format(self.prefix), 0)) if dropout is None else dropout ) self.qkv_dim = [query_dim, key_dim, value_dim] assert query_dim == key_dim, "query dim must equal with key dim" self.hidden_size = opt.get("{}_hidden_size".format(self.prefix), 64) self.proj_on = opt.get("{}_proj_on".format(prefix), False) self.share = opt.get("{}_share".format(self.prefix), False) self.layer_norm_on = opt.get("{}_norm_on".format(self.prefix), False) self.scale_on = opt.get("{}_scale_on".format(self.prefix), False) if self.proj_on: self.proj_modules = nn.ModuleList( [nn.Linear(dim, self.hidden_size) for dim in self.qkv_dim[0:2]] ) if self.layer_norm_on: for proj in self.proj_modules: proj = weight_norm(proj) if self.share and self.qkv_dim[0] == self.qkv_dim[1]: self.proj_modules[1] = self.proj_modules[0] self.f = activation(opt.get("{}_activation".format(self.prefix), "relu")) self.qkv_head_dim = [self.hidden_size // self.num_heads] * 3 self.qkv_head_dim[2] = value_dim // self.num_heads assert ( self.qkv_head_dim[0] * self.num_heads == self.hidden_size ), "hidden size must be divisible by num_heads" assert ( self.qkv_head_dim[2] * self.num_heads == value_dim ), "value size must be divisible by num_heads" else: self.qkv_head_dim = [emb // self.num_heads for emb in self.qkv_dim] # import pdb; pdb.set_trace() assert ( self.qkv_head_dim[0] * self.num_heads == self.qkv_dim[0] ), "query size must be divisible by num_heads" assert ( self.qkv_head_dim[1] * self.num_heads == self.qkv_dim[1] ), "key size must be divisible by num_heads" assert ( self.qkv_head_dim[2] * self.num_heads == self.qkv_dim[2] ), "value size must be divisible by num_heads" if self.scale_on: self.scaling = self.qkv_head_dim[0] ** -0.5 self.drop_diagonal = opt.get("{}_drop_diagonal".format(self.prefix), False) self.output_size = self.qkv_dim[2] def forward(self, query, key, value, key_padding_mask=None): query = query.transpose(0, 1) key = key.transpose(0, 1) value = value.transpose(0, 1) tgt_len, bsz, embed_dim = query.size() assert embed_dim == self.qkv_dim[0] q, k, v = query, key, value if self.proj_on: if self.dropout: q, k = self.dropout(q), self.dropout(k) q, k = [ self.f(proj(input)) for input, proj in zip([query, key], self.proj_modules) ] src_len = k.size(0) if key_padding_mask is not None: assert key_padding_mask.size(0) == bsz assert key_padding_mask.size(1) == src_len if self.scale_on: q *= self.scaling q = ( q.contiguous() .view(tgt_len, bsz * self.num_heads, self.qkv_head_dim[0]) .transpose(0, 1) ) k = ( k.contiguous() .view(src_len, bsz * self.num_heads, self.qkv_head_dim[1]) .transpose(0, 1) ) v = ( v.contiguous() .view(src_len, bsz * self.num_heads, self.qkv_head_dim[2]) .transpose(0, 1) ) attn_weights = torch.bmm(q, k.transpose(1, 2)) assert list(attn_weights.size()) == [bsz * self.num_heads, tgt_len, src_len] if key_padding_mask is not None: # don't attend to padding symbols attn_weights = attn_weights.view(bsz, self.num_heads, tgt_len, src_len) attn_weights = ( attn_weights.float() .masked_fill( key_padding_mask.unsqueeze(1).unsqueeze(2), float("-inf"), ) .type_as(attn_weights) ) # FP16 support: cast to float and back attn_weights = attn_weights.view(bsz * self.num_heads, tgt_len, src_len) if self.drop_diagonal: assert attn_weights.size(1) == attn_weights.size(2) diag_mask = ( torch.diag(attn_weights.data.new(attn_weights.size(1)).zero_() + 1) .byte() .unsqueeze(0) .expand_as(attn_weights) ) attn_weights.data.masked_fill_(diag_mask, -float("inf")) attn_weights = F.softmax(attn_weights.float(), dim=-1).type_as(attn_weights) attn_weights = self.dropout(attn_weights) attn = torch.bmm(attn_weights, v) assert list(attn.size()) == [ bsz * self.num_heads, tgt_len, self.qkv_head_dim[2], ] attn = attn.transpose(0, 1).contiguous().view(tgt_len, bsz, -1) # output_shape: Batch * Time * Channel attn = attn.transpose(0, 1) return attn
ContextualSP/adaptershare/module/similarity.py/0
{ "file_path": "ContextualSP/adaptershare/module/similarity.py", "repo_id": "ContextualSP", "token_count": 13168 }
218
#!/usr/bin/env bash ############################### # Training a mt-dnn model # Note that this is a toy setting and please refer our paper for detailed hyper-parameters. ############################### # cook GLUE data bash experiments/glue/prepro.sh # FT on rte export CUDA_VISIBLE_DEVICES=0,; bash experiments/glue/run_glue_finetuning.sh data/canonical_data/ bert base rte 16 1
ContextualSP/adaptershare/run_toy.sh/0
{ "file_path": "ContextualSP/adaptershare/run_toy.sh", "repo_id": "ContextualSP", "token_count": 120 }
219
python eval.py \ --checkpoint checkpoints/spider_grounding_model/model.pt \ --data_path data/spider_grounding/dev \ --threshold 0.4
ContextualSP/awakening_latent_grounding/eval_spider_ground.sh/0
{ "file_path": "ContextualSP/awakening_latent_grounding/eval_spider_ground.sh", "repo_id": "ContextualSP", "token_count": 54 }
220
#%% import sys sys.path.append("..") from typing import List from utils.data_types import * @dataclass class AlignmentExample: question: str schema: List[str] sql: str identify_results: List[str] gold_align_lables: List[AlignmentLabel] pred_align_labels: List[AlignmentLabel] def get_wrong_tokens_count(self): count = 0 for gold_align, pred_align in zip(self.gold_align_lables, self.pred_align_labels): if not gold_align == pred_align: count += 1 return count def to_string(self): out_string = '' out_string += "Q: {}\n".format(self.question) out_string += "\n".join(self.schema) + '\n' out_string += "Gold SQL: {}\n".format(self.sql) if len(self.identify_results) > 0: out_string += "\n".join(self.identify_results) + '\n' out_string += "Gold Align: {}\n".format(" ".join([str(x) for x in self.gold_align_lables])) out_string += "Pred Align: {}\n".format(" ".join([str(x) for x in self.pred_align_labels])) out_string += "Alignment Wrong: {}\n".format(self.get_wrong_tokens_count()) out_string += '\n' return out_string def parse_align_labels_from_line(align_str: str) -> List[AlignmentLabel]: items = align_str.split(' ') align_labels = [] for i, item in enumerate(items): ss = item.split('/') if item == '/': token = Token(index=i, token=item, lemma=item, pieces=item) else: token = Token(index=i, token=ss[0], lemma=ss[0].lower(), pieces=[ss[0].lower()]) if len(ss) == 1 or item == '/': align_labels.append(AlignmentLabel(token=token, align_type=SQLTokenType.null, align_value=None, confidence=1.0)) continue assert len(ss) == 3, "{}\t{}".format(item, align_str) align_type = SQLTokenType.column if len(ss[1].split('.')) == 1: align_type = SQLTokenType.table score = float(ss[2]) align_labels.append(AlignmentLabel(token=token, align_type=align_type, align_value=ss[1], confidence=score)) return align_labels def load_saved_alignment_results(path: str): align_examples = [] with open(path, 'r', encoding='utf-8') as fr: example: AlignmentExample = None for line in fr: line = line[:-1] if line.startswith('Q: '): if example is not None: if len(example.gold_align_lables) != len(example.pred_align_labels): print(example.question) align_examples.append(example) example = AlignmentExample( question=line.replace('Q: ', ""), schema=[], identify_results=[], sql=None, gold_align_lables=None, pred_align_labels=None ) continue if line.startswith("db id: "): example.schema.append(line.strip()) if line.startswith("T ") or line.startswith("C ") or line.startswith("V ") or line.startswith("Alignment: "): example.identify_results.append(line.strip()) if line.startswith("Gold SQL: "): example.sql = line.replace("Gold SQL: ", "") if line.startswith("Gold Align: "): example.gold_align_lables = parse_align_labels_from_line(line.replace("Gold Align: ", "")) if line.startswith("Pred Align: "): example.pred_align_labels = parse_align_labels_from_line(line.replace("Pred Align: ", "")) if example is not None: align_examples.append(example) print("load {} alignment examples from {} over".format(len(align_examples), path)) return align_examples #%% path = r'../pt/spider_alignment_old_202012261608/spider_alignment/SpiderAlignmentModel_202012261610/SpiderAlignmentModel.step_51000.acc_0.634dev.threshold0.30.tbl_0.837.col_0.830.val_0.917.results.txt' align_examples = load_saved_alignment_results(path) sorted_align_examples = sorted(align_examples, key=lambda x: x.get_wrong_tokens_count(), reverse=True) saved_path = path.replace(".txt", '.sorted.txt') saved_align_lines = [x.to_string() for x in sorted_align_examples] open(saved_path, 'w', encoding='utf-8').writelines(saved_align_lines) print('sort and save alignment results over') # %%
ContextualSP/awakening_latent_grounding/scripts/results_post_process.slsql.py/0
{ "file_path": "ContextualSP/awakening_latent_grounding/scripts/results_post_process.slsql.py", "repo_id": "ContextualSP", "token_count": 2099 }
221
#!/usr/bin/env bash wget https://github.com/terryqj0107/GECOR/raw/master/CamRest676_for_coreference_and_ellipsis_resolution/CamRest676_annotated.json python ../../preprocess.py --dataset Task
ContextualSP/incomplete_utterance_rewriting/dataset/Task/download.sh/0
{ "file_path": "ContextualSP/incomplete_utterance_rewriting/dataset/Task/download.sh", "repo_id": "ContextualSP", "token_count": 72 }
222
# Copyright (c) Microsoft Corporation. # Licensed under the MIT license. # Author: Qian Liu (SivilTaram) # Original Repo: https://github.com/microsoft/ContextualSP from overrides import overrides from allennlp.common.util import JsonDict from allennlp.data import Instance from allennlp.predictors.predictor import Predictor @Predictor.register("rewrite") class RewritePredictor(Predictor): @overrides def _json_to_instance(self, json_dict: JsonDict) -> Instance: """ Expects JSON that looks like `{"source": "..."}`. """ context = json_dict["context"] current = json_dict["current"] # placeholder restate = "hi" return self._dataset_reader.text_to_instance(context, current, restate, training=False)
ContextualSP/incomplete_utterance_rewriting/src/predictor.py/0
{ "file_path": "ContextualSP/incomplete_utterance_rewriting/src/predictor.py", "repo_id": "ContextualSP", "token_count": 284 }
223
# coding: utf-8 import json import logging import os import numpy as np import torch import torch.nn as nn import torch.optim as optim from transformers import BertModel from src.data import SpiderAlignDataset, BertUtil, SpiderSemQLConverter from src.loss import HingeLoss from src.utils.schema_linker import SchemaLinker from src.utils.semql_tree_util import Node as SemQLTree class BatchCosineSimilarity(nn.Module): def __init__(self): super(BatchCosineSimilarity, self).__init__() def forward(self, input1, input2): """ input1 & input2 are both embedding sequences from rnn encoder """ batch_dot_product = torch.bmm(input1, input2.transpose(1, 2)) # print('bdp', batch_dot_product) norm_1, norm_2 = torch.norm(input1, p=2, dim=-1), torch.norm(input2, p=2, dim=-1) # print('norm1', norm_1) # print('norm2', norm_2) norm_matrix = torch.bmm(torch.unsqueeze(norm_1, -1), torch.unsqueeze(norm_2, 1)) + 1e-8 # print('norm_matrix', norm_matrix) assert norm_matrix.size() == batch_dot_product.size() cosine_similarity = torch.div(batch_dot_product, norm_matrix) return cosine_similarity class BertAlignerModel(torch.nn.Module): def __init__(self, hidden_dim=100, use_autoencoder=False): self.use_autoencoder = use_autoencoder super().__init__() bert_pretrained_weights_shortcut = 'bert-base-uncased' bert_output_dim = 768 self.hidden_dim = hidden_dim # self.hidden_dim = bert_output_dim self.bert_chosen_layer = -2 self.bert_model = BertModel.from_pretrained(bert_pretrained_weights_shortcut, output_hidden_states=True) self.dropout = nn.Dropout(p=0.5) self.fc = nn.Sequential( nn.Linear(bert_output_dim, hidden_dim, bias=True), nn.Sigmoid() ) # fc_weight = np.eye(bert_output_dim)[:hidden_dim] # self.fc[0].weight = nn.Parameter(torch.from_numpy(fc_weight).float()) if self.use_autoencoder: self.defc = nn.Linear(hidden_dim, bert_output_dim, bias=True) # using autoencoder for better supervision self.similarity_layer = BatchCosineSimilarity() self.criterion = HingeLoss(l1_norm_weight=0.1) self.optimizer = optim.Adam([ {'params': self.bert_model.parameters(), 'lr': 0}, {'params': self.fc.parameters(), 'lr': 1e-3} ]) def forward(self, positive_tensor, positive_lengths, positive_weight_matrix=None, negative_tensor=None, negative_lengths=None, negative_weight_matrix=None, ques_max_len=None, pos_max_len=None, neg_max_len=None, mode='train'): assert mode in ('train', 'eval') positive_lengths = positive_lengths.permute(1, 0) positive_bert_output_all = self.bert_model(positive_tensor) if self.bert_chosen_layer == -1: positive_bert_output = positive_bert_output_all[0].detach()[:, 1:-1] else: positive_bert_hidden_all = positive_bert_output_all[2] positive_bert_output = positive_bert_hidden_all[self.bert_chosen_layer][:, 1:-1] positive_output = self.fc(self.dropout(positive_bert_output)) # positive_output = positive_bert_output if self.use_autoencoder: rebuild_positive_bert_output = self.defc(positive_output) positive_diff = (rebuild_positive_bert_output - positive_bert_output).sum() batch_size = positive_output.size(0) question_max_len = ques_max_len[0] if ques_max_len is not None else positive_lengths[0].max() positive_max_len = pos_max_len[0] if pos_max_len is not None else positive_lengths[1].max() _DeviceTensor = torch.cuda.FloatTensor if next(self.parameters()).is_cuda else torch.FloatTensor positive_question_matrix = _DeviceTensor(batch_size, question_max_len, self.hidden_dim).zero_() positive_bert_question_matrix = _DeviceTensor(batch_size, question_max_len, 768).zero_() positive_matrix = _DeviceTensor(batch_size, positive_max_len, self.hidden_dim).zero_() positive_bert_matrix = _DeviceTensor(batch_size, positive_max_len, 768).zero_() if negative_tensor is not None: negative_lengths = negative_lengths.permute(1, 0) negative_bert_output_all = self.bert_model(negative_tensor) if self.bert_chosen_layer == -1: negative_bert_output = negative_bert_output_all[0][:, 1:-1] else: negative_bert_hidden_all = negative_bert_output_all[2] negative_bert_output = negative_bert_hidden_all[self.bert_chosen_layer].detach()[:, 1:-1] # negative_output = negative_bert_output negative_output = self.fc(self.dropout(negative_bert_output)) if self.use_autoencoder: rebuild_negative_bert_output = self.defc(negative_output) negative_diff = (rebuild_negative_bert_output - negative_bert_output).sum() negative_max_len = neg_max_len[0] if neg_max_len is not None else negative_lengths[1].max() negative_question_matrix = _DeviceTensor(batch_size, question_max_len, self.hidden_dim).zero_() negative_bert_question_matrix = _DeviceTensor(batch_size, question_max_len, 768).zero_() negative_matrix = _DeviceTensor(batch_size, negative_max_len, self.hidden_dim).zero_() negative_bert_matrix = _DeviceTensor(batch_size, negative_max_len, 768).zero_() for batch_idx in range(batch_size): positive_question_matrix[batch_idx, :positive_lengths[0][batch_idx]] = positive_output[batch_idx, :positive_lengths[0][batch_idx]] positive_matrix[batch_idx, :positive_lengths[1][batch_idx]] = \ positive_output[batch_idx, positive_lengths[0][batch_idx] + 1: positive_lengths[0][batch_idx] + positive_lengths[1][batch_idx] + 1] positive_bert_question_matrix[batch_idx, :positive_lengths[0][batch_idx]] = positive_bert_output[batch_idx, :positive_lengths[0][batch_idx]] positive_bert_matrix[batch_idx, :positive_lengths[1][batch_idx]] = \ positive_bert_output[batch_idx, positive_lengths[0][batch_idx] + 1: positive_lengths[0][batch_idx] + positive_lengths[1][batch_idx] + 1] if negative_tensor is not None: negative_question_matrix[batch_idx, :negative_lengths[0][batch_idx]] = negative_output[batch_idx, :negative_lengths[0][batch_idx]] negative_matrix[batch_idx, :negative_lengths[1][batch_idx]] = \ negative_output[batch_idx, negative_lengths[0][batch_idx] + 1: negative_lengths[0][batch_idx] + negative_lengths[1][batch_idx] + 1] negative_bert_question_matrix[batch_idx, :negative_lengths[0][batch_idx]] = negative_bert_output[batch_idx, :negative_lengths[0][batch_idx]] negative_bert_matrix[batch_idx, :negative_lengths[1][batch_idx]] = \ negative_bert_output[batch_idx, negative_lengths[0][batch_idx] + 1: negative_lengths[0][batch_idx] + negative_lengths[1][batch_idx] + 1] if mode == 'train': positive_similarity_matrix = self.similarity_layer(positive_question_matrix, positive_matrix) else: positive_similarity_matrix = (self.similarity_layer(positive_question_matrix, positive_matrix) + self.similarity_layer(positive_bert_question_matrix, positive_bert_matrix)) / 2 if mode == 'eval' and positive_weight_matrix is not None: positive_similarity_matrix *= positive_weight_matrix if negative_tensor is not None: if mode == 'train': negative_similarity_matrix = self.similarity_layer(positive_question_matrix, negative_matrix) else: negative_similarity_matrix = (self.similarity_layer(positive_question_matrix, negative_matrix) + self.similarity_layer(negative_bert_question_matrix, negative_bert_matrix)) / 2 if mode == 'eval' and negative_weight_matrix is not None: negative_similarity_matrix *= negative_weight_matrix if self.use_autoencoder: if negative_tensor is not None: return positive_similarity_matrix, negative_similarity_matrix, positive_diff + negative_diff else: return positive_similarity_matrix, positive_diff else: if negative_tensor is not None: return positive_similarity_matrix, negative_similarity_matrix else: return positive_similarity_matrix class BertAligner: def __init__(self, aligner_model: BertAlignerModel = None): if aligner_model is not None: self.aligner_model = aligner_model else: self.aligner_model = BertAlignerModel() if os.path.exists('saved/spider/model.pt'): self.aligner_model.load_state_dict(torch.load('saved/spider/model.pt')) else: logging.warning("No pretrined aligned model loaded!!!") if torch.cuda.is_available(): self.aligner_model = self.aligner_model.cuda() self.bert_util = BertUtil() self.semql_converter = SpiderSemQLConverter() self.schema_linker = SchemaLinker() def calculate_alignment(self, nl, restatement): assert nl and restatement tokens, lengths = self.bert_util.join_sentences([nl, restatement]) ids = torch.LongTensor(self.bert_util.tokens_to_ids(tokens)).unsqueeze(0) lengths_tensor = torch.LongTensor(lengths).unsqueeze(0) if torch.cuda.is_available(): ids = ids.cuda() lengths_tensor = lengths_tensor.cuda() if self.aligner_model.use_autoencoder: alignment_matrix, autoencoder_diff = self.aligner_model(ids, lengths_tensor, mode='eval') else: alignment_matrix = self.aligner_model(ids, lengths_tensor, mode='eval') return alignment_matrix, ids, tokens, lengths def split_tokens(self, tokens, lengths): assert len(tokens) == sum(lengths) + 3 tokens1 = tokens[1:1 + lengths[0]] tokens2 = tokens[2 + lengths[0]:-1] return tokens1, tokens2 def load_example(self, example): nl = example['question'] semql = self.semql_converter.convert_example(example) semql_tree = SemQLTree.from_statements([str(_) for _ in semql]) restatement = semql_tree.restatement() return nl, restatement def link_example_schema(self, example): ret = self.schema_linker.link_example(example) return ret if __name__ == '__main__': bert_aligner = BertAligner() examples = json.load(open('data/spider/train_spider.json', 'r', encoding='utf-8')) nl, restatement = bert_aligner.load_example(examples[0]) alignment_matrix = bert_aligner.calculate_alignment(nl, restatement)
ContextualSP/interactive_text_to_sql/src/aligner_model.py/0
{ "file_path": "ContextualSP/interactive_text_to_sql/src/aligner_model.py", "repo_id": "ContextualSP", "token_count": 4832 }
224
import json import requests def complex_rephrase(query, span, target_span): url = 'http://172.16.13.25:9009' headers = {'apikey': '9212c3f48ad1766bcf63535710686d07'} params = {'query': query, 'span': span, 'target': target_span} r = requests.get(url, params=params, headers=headers) return json.loads(r.text['modified_query'])
ContextualSP/interactive_text_to_sql/src/utils/external.py/0
{ "file_path": "ContextualSP/interactive_text_to_sql/src/utils/external.py", "repo_id": "ContextualSP", "token_count": 164 }
225
import sys import hashlib import random # random.seed(0) def gen_md5(data): """ 生成md5 :param data: 字符串数据 :return: """ md5 = hashlib.md5() md5.update(data.encode('utf-8')) return md5.hexdigest() def big_file_remove_same(input_file, output_file): """ 针对大文件文件去重(将文件文件写在一行的,没有办法去重) :param input_file: :param output_file: :return: """ finger_print_set = set() with open(input_file, 'r', encoding='utf-8') as f, open(output_file, 'w', encoding='utf-8') as ff,open('/tmp/dumple.txt', 'w', encoding='utf-8') as fff: f = f.readlines() # random.shuffle(f) for line in f: line_string = line.strip() finger_print = gen_md5(line_string) if finger_print not in finger_print_set: finger_print_set.add(finger_print) ff.write(line) else: fff.write(line) if __name__ == "__main__": # big_file_remove_same(sys.argv[1],sys.argv[2]) input_file = './table-evidence.txt' output_file = './result.txt' big_file_remove_same(input_file, output_file)
ContextualSP/lemon/corpus_generation/remove_same.py/0
{ "file_path": "ContextualSP/lemon/corpus_generation/remove_same.py", "repo_id": "ContextualSP", "token_count": 617 }
226
import numpy as np import tensorflow as tf from gtd.ml.framework import Feedable from gtd.ml.utils import expand_dims_for_broadcast, broadcast class SequenceBatch(object): """Represent a batch of sequences as a Tensor.""" def __init__(self, values, mask, name='SequenceBatch'): with tf.name_scope(name): # check that dimensions are correct values_shape = tf.shape(values) mask_shape = tf.shape(mask) values_shape_prefix = tf.slice(values_shape, [0], [2]) max_rank = max(values.get_shape().ndims, mask.get_shape().ndims) assert_op = tf.assert_equal(values_shape_prefix, mask_shape, data=[values_shape_prefix, mask_shape], summarize=max_rank, name="assert_shape_prefix") with tf.control_dependencies([assert_op]): self._values = tf.identity(values, name='values') self._mask = tf.identity(mask, name='mask') @property def values(self): """A Tensor holding the values of the sequence batch, of shape [batch_size, seq_length, :, ..., :]. Each row represents one sequence. """ return self._values @property def mask(self): """A boolean mask of shape [batch_size, seq_length], indicating which entries of self.values are padding. mask[i, j] = 0 if the entry is padding, 1 otherwise. Returns: A Tensor of shape (batch_size, seq_length) """ return self._mask def with_pad_value(self, val): """Return a new SequenceBatch, with pad values set to the specified value.""" return SequenceBatch(change_pad_value(self.values, self.mask, val), self.mask) def change_pad_value(values, mask, pad_val): """Given a set of values and a pad mask, change the value of all pad entries. Args: values (Tensor): of shape [batch_size, seq_length, :, ..., :]. mask (Tensor): binary float tensor of shape [batch_size, seq_length] pad_val (float): value to set all pad entries to Returns: Tensor: a new Tensor of same shape as values """ # broadcast the mask to match shape of values mask = expand_dims_for_broadcast(mask, values) # (batch_size, seq_length, 1, ..., 1) mask = broadcast(mask, values) mask = tf.cast(mask, tf.bool) # cast to bool # broadcast val broadcast_val = pad_val * tf.ones(tf.shape(values)) new_values = tf.select(mask, values, broadcast_val) return new_values class FeedSequenceBatch(Feedable, SequenceBatch): """A SequenceBatch that is fed into TensorFlow from the outside. The SequenceBatch is represented by a Tensor of shape [batch_size, seq_length] - batch_size is dynamically determined by the # sequences fed - seq_length is dynamically set to the length of the longest sequence fed, or the statically specified value. """ def __init__(self, align='left', seq_length=None, dtype=tf.int32, name='FeedSequenceBatch'): """Create a Feedable SequenceBatch. Args: align (str): can be 'left' or 'right'. If 'left', values will be left-aligned, with padding on the right. If 'right', values will be right-aligned, with padding on the left. Default is 'left'. seq_length (int): the Tensor representing the SequenceBatch will have exactly this many columns. Default is None. If None, seq_length will be dynamically determined. dtype: data type of the SequenceBatch values array. Defaults to int32. name (str): namescope for the Tensors created inside this Model. """ if align not in ('left', 'right'): raise ValueError("align must be either 'left' or 'right'.") self._align_right = (align == 'right') self._seq_length = seq_length with tf.name_scope(name): values = tf.placeholder(dtype, shape=[None, None], name='values') # (batch_size, seq_length) mask = tf.placeholder(tf.float32, shape=[None, None], name='mask') # (batch_size, seq_length) if self._seq_length is not None: # add static shape information batch_dim, _ = values.get_shape() new_shape = tf.TensorShape([batch_dim, tf.Dimension(seq_length)]) values.set_shape(new_shape) mask.set_shape(new_shape) super(FeedSequenceBatch, self).__init__(values, mask) def inputs_to_feed_dict(self, sequences, vocab=None): """Convert sequences into a feed_dict. Args: sequences (list[list[unicode]]): a list of unicode sequences vocab (Vocab): a vocab mapping tokens to integers. If vocab is None, sequences are directly passed into TensorFlow, without performing any token-to-integer lookup. Returns: a feed_dict """ batch_size = len(sequences) if batch_size == 0: seq_length = 0 if self._seq_length is None else self._seq_length empty = np.empty((0, seq_length)) return {self.values: empty, self.mask: empty} # dynamic seq_length if none specified if self._seq_length is None: seq_length = max(len(tokens) for tokens in sequences) else: seq_length = self._seq_length # if no vocab, just pass the raw value if vocab is None: tokens_to_values = lambda words: words else: tokens_to_values = vocab.words2indices if self._align_right: truncate = lambda tokens: tokens[-seq_length:] indices = [[(seq_length - n) + i for i in range(n)] for n in range(seq_length + 1)] else: truncate = lambda tokens: tokens[:seq_length] indices = list(map(range, list(range(seq_length + 1)))) values_arr = np.zeros((batch_size, seq_length), dtype=np.float32) mask_arr = np.zeros((batch_size, seq_length), dtype=np.float32) for row_idx, tokens in enumerate(sequences): num_tokens = len(tokens) if num_tokens == 0: continue if num_tokens > seq_length: truncated_tokens = truncate(tokens) else: truncated_tokens = tokens inds = indices[len(truncated_tokens)] vals = tokens_to_values(truncated_tokens) values_arr[row_idx][inds] = vals mask_arr[row_idx][inds] = 1.0 return {self.values: values_arr, self.mask: mask_arr} def embed(sequence_batch, embeds): mask = sequence_batch.mask embedded_values = tf.gather(embeds, sequence_batch.values) embedded_values = tf.verify_tensor_all_finite(embedded_values, 'embedded_values') # set all pad embeddings to zero broadcasted_mask = expand_dims_for_broadcast(mask, embedded_values) embedded_values *= broadcasted_mask return SequenceBatch(embedded_values, mask) def reduce_mean(seq_batch, allow_empty=False): """Compute the mean of each sequence in a SequenceBatch. Args: seq_batch (SequenceBatch): a SequenceBatch with the following attributes: values (Tensor): a Tensor of shape (batch_size, seq_length, :, ..., :) mask (Tensor): if the mask values are arbitrary floats (rather than binary), the mean will be a weighted average. allow_empty (bool): allow computing the average of an empty sequence. In this case, we assume 0/0 == 0, rather than NaN. Default is False, causing an error to be thrown. Returns: Tensor: of shape (batch_size, :, ..., :) """ values, mask = seq_batch.values, seq_batch.mask # compute weights for the average sums = tf.reduce_sum(mask, 1, keep_dims=True) # (batch_size, 1) if allow_empty: asserts = [] # no assertion sums = tf.select(tf.equal(sums, 0), tf.ones(tf.shape(sums)), sums) # replace 0's with 1's else: asserts = [tf.assert_positive(sums)] # throw error if 0's exist with tf.control_dependencies(asserts): weights = mask / sums # (batch_size, seq_length) return weighted_sum(seq_batch, weights) def reduce_sum(seq_batch): weights = tf.ones(shape=tf.shape(seq_batch.mask)) return weighted_sum(seq_batch, weights) def weighted_sum(seq_batch, weights): """Compute the weighted sum of each sequence in a SequenceBatch. Args: seq_batch (SequenceBatch): a SequenceBatch. weights (Tensor): a Tensor of shape (batch_size, seq_length). Determines the weights. Weights outside the seq_batch's mask are ignored. Returns: Tensor: of shape (batch_size, :, ..., :) """ values, mask = seq_batch.values, seq_batch.mask weights = weights * mask # ignore weights outside the mask weights = expand_dims_for_broadcast(weights, values) weighted_array = values * weights # (batch_size, seq_length, X) return tf.reduce_sum(weighted_array, 1) # (batch_size, X) def reduce_max(seq_batch): sums = tf.reduce_sum(seq_batch.mask, 1, keep_dims=True) # (batch_size, 1) with tf.control_dependencies([tf.assert_positive(sums)]): # avoid dividing by zero seq_batch = seq_batch.with_pad_value(float('-inf')) # set pad values to -inf result = tf.reduce_max(seq_batch.values, 1) return result
ContextualSP/lemon/executor/gtd/ml/seq_batch.py/0
{ "file_path": "ContextualSP/lemon/executor/gtd/ml/seq_batch.py", "repo_id": "ContextualSP", "token_count": 3873 }
227
from collections import Counter from numpy.testing import assert_approx_equal from gtd.lm import last_k, CountLM, LMSampler, normalize_counts import pytest @pytest.fixture def lm(): return CountLM(3) @pytest.fixture def lm_sampler(lm): return LMSampler(lm) def test_last_k(): tokens = [1, 2, 3, 4] assert last_k(tokens, 2) == (3, 4) assert last_k(tokens, 4) == (1, 2, 3, 4) assert last_k(tokens, 0) == tuple() def test_get_contexts(lm): tokens = [1, 2, 3, 4, 5] assert list(lm._get_contexts(tokens)) == [tuple(), (5,), (4, 5), (3, 4, 5)] assert list(lm._get_contexts([1, 2])) == [tuple(), (2,), (1, 2)] def test_largest_known_context(lm): contexts = {tuple(), (3,), (2, 3), (1, 2)} assert lm._largest_context([1, 2, 3], contexts) == (2, 3) assert lm._largest_context([2, 3, 0], contexts) == tuple() def test_normalize_counts(): c = Counter([1, 1, 2, 2, 3]) assert normalize_counts(c) == Counter({1: .4, 2: .4, 3: .2}) @pytest.mark.skip def test_sample_from_distribution(lm_sampler): distr = {'a': 0.3, 'b': 0.7} ctr = Counter() # law of large numbers test for k in range(100000): ctr[lm_sampler._sample_from_distribution(distr)] += 1 empirical = normalize_counts(ctr) for key in list(distr.keys()) + list(empirical.keys()): assert_approx_equal(empirical[key], distr[key], significant=2) def test_sequence_probability(lm): lm = CountLM(3) lines = ['apple pear banana', 'pear banana apple', 'banana pear banana'] for line in lines: tokens = line.split() lm.record_counts(tokens, append_end=True) probs = lm.sequence_probability(['pear', 'apple', 'pear']) assert probs == [('pear', 0.3333333333333333), ('apple', 0.0), ('pear', 0.5)]
ContextualSP/lemon/executor/gtd/tests/test_lm.py/0
{ "file_path": "ContextualSP/lemon/executor/gtd/tests/test_lm.py", "repo_id": "ContextualSP", "token_count": 777 }
228
import os import random import numpy as np import tensorflow as tf import gtd.ml.experiment from dependency.data_directory import DataDirectory from gtd.chrono import verboserate from gtd.ml.model import TokenEmbedder from gtd.ml.utils import guarantee_initialized_variables from gtd.utils import cached_property, as_batches, random_seed, sample_if_large from strongsup.decoder import Decoder from strongsup.domain import get_domain from strongsup.embeddings import ( StaticPredicateEmbeddings, GloveEmbeddings, TypeEmbeddings, RLongPrimitiveEmbeddings) from strongsup.evaluation import Evaluation, BernoulliSequenceStat from strongsup.example import Example, DelexicalizedContext from strongsup.parse_case import ParsePath from strongsup.parse_model import ( UtteranceEmbedder, CombinedPredicateEmbedder, DynamicPredicateEmbedder, PositionalPredicateEmbedder, DelexicalizedDynamicPredicateEmbedder, HistoryEmbedder, SimplePredicateScorer, AttentionPredicateScorer, SoftCopyPredicateScorer, PredicateScorer, CrossEntropyLossModel, LogitLossModel, ParseModel, TrainParseModel, ExecutionStackEmbedder, RLongObjectEmbedder) from strongsup.utils import OptimizerOptions from strongsup.value_function import ValueFunctionExample from strongsup.visualizer import Visualizer class Experiments(gtd.ml.experiment.Experiments): def __init__(self, check_commit=True): """Create Experiments. If check_commit is true, this will not allow you to run old experiments without being on the correct commit number, or old experiments where the working directory was not clean. """ data_dir = DataDirectory.experiments src_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) default_config = os.path.join(src_dir, 'configs', 'debug.txt') super(Experiments, self).__init__(data_dir, src_dir, Experiment, default_config, check_commit=check_commit) class Experiment(gtd.ml.experiment.TFExperiment): """Encapsulates the elements of a training run.""" def __init__(self, config, save_dir): super(Experiment, self).__init__(config, save_dir) self.workspace.add_file('train_visualize', 'train_visualizer.txt') self.workspace.add_file('valid_visualize', 'valid_visualizer.txt') self.workspace.add_file('full_eval', 'full_eval_at_{step}.txt') self.workspace.add_file('codalab', 'codalab.json') self._domain = get_domain(config) self._train_parse_model = self._build_train_parse_model() self._decoder = self._build_decoder(self.train_parse_model) self._train_visualizer = Visualizer(self.decoder, self.workspace.train_visualize, 'train', train=True) self._valid_visualizer = Visualizer(self.decoder, self.workspace.valid_visualize, 'valid', train=False) # Reload weights if they exist. Otherwise, initialize weights. try: self.saver.restore() print('Successfully reloaded the weights') except IOError: # NOTE: use this instead of tf.initialize_all_variables()! # That op will overwrite Keras initializations. sess = tf.get_default_session() guarantee_initialized_variables(sess) print('Weights initialized') @property def train_parse_model(self): return self._train_parse_model @property def parse_model(self): return self.train_parse_model.parse_model @property def decoder(self): return self._decoder @property def path_checker(self): return self._domain.path_checker @property def train_visualizer(self): return self._train_visualizer @property def valid_visualizer(self): return self._valid_visualizer def _build_train_parse_model(self): """Construct the TrainParseModel. If weights have been saved to disk, restore those weights. Returns: TrainParseModel """ config = self.config.parse_model delexicalized = self.config.delexicalized # Glove embeddings have embed_dim 100 glove_embeddings = GloveEmbeddings(vocab_size=20000) type_embeddings = TypeEmbeddings(embed_dim=50, all_types=self._domain.all_types) # set up word embeddings word_embedder = TokenEmbedder(glove_embeddings, 'word_embeds', trainable=config.train_word_embeddings) type_embedder = TokenEmbedder(type_embeddings, 'type_embeds') # build utterance embedder utterance_embedder = UtteranceEmbedder(word_embedder, lstm_dim=config.utterance_embedder.lstm_dim, utterance_length=config.utterance_embedder.utterance_length) # build predicate embedder # dynamic if delexicalized: dyn_pred_embedder = DelexicalizedDynamicPredicateEmbedder(utterance_embedder.hidden_states_by_utterance, type_embedder) else: dyn_pred_embedder = DynamicPredicateEmbedder(word_embedder, type_embedder) if config.predicate_positions: dyn_pred_embedder = PositionalPredicateEmbedder(dyn_pred_embedder) # static static_pred_embeddings = StaticPredicateEmbeddings( dyn_pred_embedder.embed_dim, # matching dim self._domain.fixed_predicates) static_pred_embedder = TokenEmbedder(static_pred_embeddings, 'static_pred_embeds') # combined pred_embedder = CombinedPredicateEmbedder(static_pred_embedder, dyn_pred_embedder) # build history embedder if config.condition_on_history: history_embedder = HistoryEmbedder(pred_embedder, config.history_length) else: history_embedder = None # build execution stack embedder if config.condition_on_stack: max_stack_size = self.config.decoder.prune.max_stack_size max_list_size = config.stack_embedder.max_list_size primitive_dim = config.stack_embedder.primitive_dim object_dim = config.stack_embedder.object_dim primitive_embeddings = RLongPrimitiveEmbeddings(primitive_dim) stack_primitive_embedder = TokenEmbedder(primitive_embeddings, 'primitive_embeds', trainable=True) # TODO(kelvin): pull this out as its own method assert self.config.dataset.domain == 'rlong' sub_domain = self.config.dataset.name attrib_extractors = [lambda obj: obj.position] if sub_domain == 'scene': attrib_extractors.append(lambda obj: obj.shirt) attrib_extractors.append(lambda obj: obj.hat) pass elif sub_domain == 'alchemy': # skipping chemicals attribute for now, because it is actually a list attrib_extractors.append(lambda obj: obj.color if obj.color is not None else 'color-na') attrib_extractors.append(lambda obj: obj.amount) elif sub_domain == 'tangrams': attrib_extractors.append(lambda obj: obj.shape) elif sub_domain == 'undograms': attrib_extractors.append(lambda obj: obj.shape) else: raise ValueError('No stack embedder available for sub-domain: {}.'.format(sub_domain)) stack_object_embedder = RLongObjectEmbedder(attrib_extractors, stack_primitive_embedder, max_stack_size, max_list_size) stack_embedder = ExecutionStackEmbedder(stack_primitive_embedder, stack_object_embedder, max_stack_size=max_stack_size, max_list_size=max_list_size, project_object_embeds=True, abstract_objects=False) else: stack_embedder = None def scorer_factory(query_tensor): simple_scorer = SimplePredicateScorer(query_tensor, pred_embedder) attention_scorer = AttentionPredicateScorer(query_tensor, pred_embedder, utterance_embedder) soft_copy_scorer = SoftCopyPredicateScorer(attention_scorer.attention_on_utterance.logits, disable=not config.soft_copy ) # note that if config.soft_copy is None, then soft_copy is disabled scorer = PredicateScorer(simple_scorer, attention_scorer, soft_copy_scorer) return scorer parse_model = ParseModel(pred_embedder, history_embedder, stack_embedder, utterance_embedder, scorer_factory, config.h_dims, self._domain, delexicalized) if self.config.decoder.normalization == 'local': loss_model_factory = CrossEntropyLossModel else: loss_model_factory = LogitLossModel train_parse_model = TrainParseModel(parse_model, loss_model_factory, self.config.learning_rate, OptimizerOptions(self.config.optimizer), self.config.get('train_batch_size')) return train_parse_model def _build_decoder(self, train_parse_model): return Decoder(train_parse_model, self.config.decoder, self._domain) @cached_property def _examples(self): train, valid, final = self._domain.load_datasets() def delexicalize_examples(examples): delex_examples = [] for ex in examples: delex_context = DelexicalizedContext(ex.context) delex_ex = Example(delex_context, answer=ex.answer, logical_form=ex.logical_form) delex_examples.append(delex_ex) return delex_examples if self.config.delexicalized: train = delexicalize_examples(train) valid = delexicalize_examples(valid) final = delexicalize_examples(final) return train, valid, final @property def train_examples(self): return self._examples[0] @property def valid_examples(self): return self._examples[1] @property def final_examples(self): return self._examples[2] def train(self): decoder = self.decoder eval_steps = self.config.timing.eval big_eval_steps = self.config.timing.big_eval save_steps = self.config.timing.save self.evaluate(step=decoder.step) # evaluate once before training begins while True: train_examples = random.sample(self.train_examples, k=len(self.train_examples)) # random shuffle train_examples = verboserate(train_examples, desc='Streaming training Examples') for example_batch in as_batches(train_examples, self.config.batch_size): decoder.train_step(example_batch) step = decoder.step self.report_cache_stats(step) if (step + 1) % save_steps == 0: self.saver.save(step) if (step + 1) % eval_steps == 0: self.evaluate(step) if (step + 1) % big_eval_steps == 0: self.big_evaluate(step) if step >= self.config.max_iters: self.evaluate(step) self.saver.save(step) return # def supervised_train(self): # train_parse_model = self.train_parse_model # eval_time = Pulse(self.config.timing.eval) # supervised_eval_time = Pulse(self.config.timing.supervised_eval) # cases = examples_to_supervised_cases(self.train_examples) # while True: # for case_batch in as_batches(cases, self.config.batch_size): # weights = [1.0 / len(case_batch)] * len(case_batch) # train_parse_model.train_step(case_batch, weights, self.config.decoder.inputs_caching) # step = train_parse_model.step # self.report_cache_stats(step) # self.saver.interval_save(step, self.config.timing.save) # if eval_time(): # self.evaluate(step) # eval_time.reset() # if supervised_eval_time(): # self.supervised_evaluate(step) # supervised_eval_time.reset() # if step >= self.config.max_iters: # self.evaluate(step) # self.saver.save(step) # return def report_cache_stats(self, step): return # Don't log these parse_model = self.parse_model scorer_cache = parse_model._scorer.inputs_to_feed_dict_cached pred_cache = parse_model._pred_embedder.inputs_to_feed_dict_cached self.tb_logger.log('cache_scorer_size', scorer_cache.cache_size, step) self.tb_logger.log('cache_predEmbedder_size', pred_cache.cache_size, step) self.tb_logger.log('cache_scorer_hitRate', scorer_cache.hit_rate, step) self.tb_logger.log('cache_predEmbedder_hitRate', pred_cache.hit_rate, step) def supervised_evaluate(self, step): train_parse_model = self.train_parse_model def case_sample(examples): """Get a random sample of supervised ParseCases.""" with random_seed(0): example_sample = sample_if_large(examples, 30) return list(examples_to_supervised_cases(example_sample)) def report_loss(cases, name): weights = [1.0] * len(cases) loss = train_parse_model.compute(train_parse_model.loss, cases, weights, caching=self.config.decoder.inputs_caching) self.tb_logger.log(name, loss, step) report_loss(case_sample(self.train_examples), 'loss_train') report_loss(case_sample(self.valid_examples), 'loss_val') def evaluate(self, step): print('Evaluate at step {}'.format(step)) num_examples = self.config.num_evaluate_examples with random_seed(0): train_sample = sample_if_large(self.train_examples, num_examples, replace=False) with random_seed(0): valid_sample = sample_if_large(self.valid_examples, num_examples, replace=False) train_eval = self.evaluate_on_examples(step, train_sample, self.train_visualizer) valid_eval = self.evaluate_on_examples(step, valid_sample, self.valid_visualizer) # Log to TensorBoard train_eval.json_summarize(self.workspace.codalab, step) train_eval.tboard_summarize(self.tb_logger, step) valid_eval.json_summarize(self.workspace.codalab, step) valid_eval.tboard_summarize(self.tb_logger, step) def evaluate_on_examples(self, step, examples, visualizer): evaluation = Evaluation() examples = verboserate(examples, desc='Decoding {} examples'.format(visualizer.group_name)) visualizer.reset(step=step) for ex_batch in as_batches(examples, self.config.batch_size): beams, batch_evaluation = visualizer.predictions(ex_batch) evaluation.add_evaluation(batch_evaluation) # collect value function examples value_function = self.decoder._value_function vf_examples = [] for example, beam in zip(ex_batch, beams): vf_examples.extend(ValueFunctionExample.examples_from_paths(beam, example)) # compute ValueFunction metrics vf_loss = value_function.loss(vf_examples) predicted_values = value_function.values([ex.case for ex in vf_examples]) avg_predicted_value = np.mean(predicted_values) evaluation.add('valueFunctionLoss', vf_loss) evaluation.add('avgPredictedValue', avg_predicted_value) return evaluation def big_evaluate(self, step, num_samples=None): """Run more comprehensive evaluation of the model. How this differs from `self.evaluate`: - Compute confidence intervals for denotational accuracy estimates - Option to evaluate on a custom/larger number of samples - Due to the larger number of samples, don't print everything out to a visualizer. - Save stats to a file. Args: step (int) num_samples (# samples to evaluate on, for both train and test) """ if num_samples is None: num_samples = self._config.num_evaluate_examples_big full_eval_path = self.workspace.full_eval.format(step=step) silent_visualizer = Visualizer(self.decoder, '/dev/null', 'silent', train=False) def evaluate_helper(examples, prefix): with random_seed(0): sample = sample_if_large(examples, num_samples, replace=False) eval = self.evaluate_on_examples(step=step, examples=sample, visualizer=silent_visualizer) # wrap with BernoulliSequenceStat, for conf intervals for name, stat in list(eval.stats.items()): if name.startswith('denoAcc'): eval.stats[name] = BernoulliSequenceStat(stat) with open(full_eval_path, 'a') as f: eval.summarize(f, prefix=prefix) eval.tboard_summarize(self.tb_logger, step, prefix=prefix) eval.json_summarize(self.workspace.codalab, step, prefix=prefix) evaluate_helper(self.final_examples, 'FINAL') evaluate_helper(self.valid_examples, 'VALID') def example_to_supervised_cases(example): """Convert Example to a list of supervised ParseCases. Only possible if example.logical_form is known. Args: example (Example) Returns: list[ParseCase] """ path = ParsePath([], context=example.context) predicates = example.logical_form cases = [] for pred in predicates: case = path.extend() if pred not in case.choices: case.choices.append(pred) case.decision = pred cases.append(case) path = case.path return cases def examples_to_supervised_cases(examples): """Return a Generator of supervised ParseCases.""" for example in verboserate(examples, desc='Streaming supervised ParseCases'): for case in example_to_supervised_cases(example): yield case
ContextualSP/lemon/executor/strongsup/experiment.py/0
{ "file_path": "ContextualSP/lemon/executor/strongsup/experiment.py", "repo_id": "ContextualSP", "token_count": 8236 }
229
from codecs import open from strongsup.example import Context, Example from strongsup.example_factory import ExampleFactory from strongsup.rlong.state import RLongAlchemyState, RLongSceneState, RLongTangramsState, RLongUndogramsState from strongsup.rlong.value import RLongStateValue from strongsup.rlong.world import RLongAlchemyWorld, RLongSceneWorld, RLongTangramsWorld, RLongUndogramsWorld ################################ # RLongExampleFactory class RLongExampleFactory(ExampleFactory): def __init__(self, filename, domain_name, num_steps_list, slice_steps_from_middle): """Read RLongDataset. Args: domain_name (str): 'alchemy', 'scene', 'tangrams', or 'undograms' filename (str): TSV File to load data from. The file format is <id> <initstate> <sentence1> <state1> <sentence2> <state2> ... num_steps_list (list[int]): Number of sentences for each example. E.g., [2, 3] creates examples from the first 2 or 3 sentences. num_steps of -1 will take all utterances. slice_steps_from_middle (bool): Whether to also get the sentences from the middle of the stories. Setting this to False will only get the sentences from the beginning of the stories. """ self._filename = filename self._domain_name = domain_name if domain_name == 'alchemy': self._state_class = RLongAlchemyState self._world_class = RLongAlchemyWorld elif domain_name == 'scene': self._state_class = RLongSceneState self._world_class = RLongSceneWorld elif domain_name == 'tangrams': self._state_class = RLongTangramsState self._world_class = RLongTangramsWorld elif domain_name == 'undograms': self._state_class = RLongUndogramsState self._world_class = RLongUndogramsWorld else: raise ValueError('Unknown rlong domain name: {}'.format(domain_name)) # Parse num_steps if not isinstance(num_steps_list, list): assert isinstance(num_steps_list, int) num_steps_list = list([num_steps_list]) self._num_steps_list = num_steps_list self._slice_steps_from_middle = slice_steps_from_middle @property def examples(self): with open(self._filename, 'r', 'utf8') as fin: for line in fin: line = line.rstrip('\n').split('\t') assert len(line) % 2 == 0 for num_steps in self._num_steps_list: if num_steps == -1: # Maximum number of steps num_steps = len(line) / 2 - 1 start_idx = 1 while start_idx + 2 * num_steps < len(line): utterances = [utterance.split() for utterance in line[start_idx+1:start_idx+2*num_steps:2]] init_state = self._state_class.from_raw_string( line[start_idx]) target_state = self._state_class.from_raw_string( line[start_idx+2*num_steps]) world = self._world_class(init_state) context = Context(world, utterances) example = Example(context, answer=[RLongStateValue(target_state)]) yield example if not self._slice_steps_from_middle: break start_idx += 2
ContextualSP/lemon/executor/strongsup/rlong/example_factory.py/0
{ "file_path": "ContextualSP/lemon/executor/strongsup/rlong/example_factory.py", "repo_id": "ContextualSP", "token_count": 1757 }
230
from strongsup.predicate import Predicate from strongsup.tables.executor import is_unary, is_binary, ALL_BUILT_INS from strongsup.tables.graph import ALL_GRAPH_BUILT_INS from strongsup.utils import EOU class WikiTablePredicate(Predicate): def __init__(self, name, original_string=None): types = self._compute_types(name) super(WikiTablePredicate, self).__init__(name, original_string, types=types) def _compute_types(self, name): """Get the types (and a few features) of a predicate. Args: name (unicode): name of the predicate Return: tuple[string] """ types = [] if is_unary(name): types.append(WikiTablePredicateType.UNARY) if is_binary(name): types.append(WikiTablePredicateType.BINARY) if name in FIXED_PREDICATE_NAMES: types.append(WikiTablePredicateType.BUILTIN) if name.startswith('fb:cell.') and not name.startswith('fb:cell.cell.'): types.append(WikiTablePredicateType.CELL) elif name.startswith('fb:part.'): types.append(WikiTablePredicateType.PART) elif name.startswith('fb:row.row.'): types.append(WikiTablePredicateType.COLUMN) elif name.startswith('!fb:row.row.'): types.append(WikiTablePredicateType.RCOLUMN) elif name.startswith('N'): types.append(WikiTablePredicateType.NUMBER) elif name.startswith('D'): types.append(WikiTablePredicateType.DATE) return tuple(types) @property def types_vector(self): """Return the types as a k-hot vector. Returns: list[boolean] """ return [x in self.types for x in WikiTablePredicateType.ALL_TYPES] @property def words(self): """Get the words from the ID. Returns: list[unicode] """ return self.name.split('.')[-1].split('_') @property def delexicalized_name(self): """A placeholder used in a delexicalized utterance. Returns: unicode """ if WikiTablePredicateType.COLUMN in self.types: return 'COL' if WikiTablePredicateType.CELL in self.types: return 'ENT' return None class WikiTablePredicateType(object): UNARY = 'unary' BINARY = 'binary' BUILTIN = 'builtin' CELL = 'cell' PART = 'part' COLUMN = 'column' RCOLUMN = '!column' NUMBER = 'number' DATE = 'date' ALL_TYPES = (UNARY, BINARY, BUILTIN, CELL, PART, COLUMN, RCOLUMN, NUMBER, DATE) @classmethod def is_relation(cls, pred): return (WikiTablePredicateType.BINARY in pred.types) and not cls.is_builtin(pred) @classmethod def is_entity(cls, pred): return WikiTablePredicateType.UNARY in pred.types @classmethod def is_builtin(cls, pred): return WikiTablePredicateType.BUILTIN in pred.types FIXED_PREDICATE_NAMES = (EOU,) + ALL_BUILT_INS + ALL_GRAPH_BUILT_INS FIXED_PREDICATES = [WikiTablePredicate(name) for name in FIXED_PREDICATE_NAMES]
ContextualSP/lemon/executor/strongsup/tables/predicate.py/0
{ "file_path": "ContextualSP/lemon/executor/strongsup/tables/predicate.py", "repo_id": "ContextualSP", "token_count": 1415 }
231
from gtd.utils import Bunch from strongsup.example import Utterance class TestUtterance(object): def test_eq(self): ctx1 = Bunch() ctx2 = Bunch() utt1 = Utterance(('a', 'b', 'c'), ctx1, 0) utt2 = Utterance(('AA', 'B', 'CCC'), ctx1, 0) utt3 = Utterance(('a', 'b', 'c'), ctx2, 0) assert utt1 == utt2 assert utt1 != utt3
ContextualSP/lemon/executor/strongsup/tests/test_example.py/0
{ "file_path": "ContextualSP/lemon/executor/strongsup/tests/test_example.py", "repo_id": "ContextualSP", "token_count": 195 }
232
DATASET_PATH=path_to_dataset MODEL_PATH=path_to_bart_large python -m bpe_encoder \ --encoder-json $MODEL_PATH/encoder.json \ --vocab-bpe $MODEL_PATH/vocab.bpe \ --inputs $DATASET_PATH/train.src \ --outputs $DATASET_PATH/train.bpe.src \ --workers 20 \ --keep-empty python -m bpe_encoder \ --encoder-json $MODEL_PATH/encoder.json \ --vocab-bpe $MODEL_PATH/vocab.bpe \ --inputs $DATASET_PATH/train.tgt \ --outputs $DATASET_PATH/train.bpe.tgt \ --workers 20 \ --keep-empty python -m bpe_encoder \ --encoder-json $MODEL_PATH/encoder.json \ --vocab-bpe $MODEL_PATH/vocab.bpe \ --inputs $DATASET_PATH/dev.src \ --outputs $DATASET_PATH/dev.bpe.src \ --workers 20 \ --keep-empty python -m bpe_encoder \ --encoder-json $MODEL_PATH/encoder.json \ --vocab-bpe $MODEL_PATH/vocab.bpe \ --inputs $DATASET_PATH/dev.tgt \ --outputs $DATASET_PATH/dev.bpe.tgt \ --workers 20 \ --keep-empty fairseq-preprocess --source-lang "src" --target-lang "tgt" \ --trainpref $DATASET_PATH/train.bpe \ --validpref $DATASET_PATH/dev.bpe \ --destdir $DATASET_PATH/bin_large \ --workers 20 \ --srcdict $MODEL_PATH/dict.txt \ --tgtdict $MODEL_PATH/dict.txt
ContextualSP/lemon/lemon/preprocess_pretrain.bat/0
{ "file_path": "ContextualSP/lemon/lemon/preprocess_pretrain.bat", "repo_id": "ContextualSP", "token_count": 786 }
233
#!/bin/bash set -euo pipefail if [[ ! -f OpenBookQA-V1-Sep2018.zip ]]; then echo Missing file OpenBookQA-V1-Sep2018.zip echo echo Download it first: https://s3-us-west-2.amazonaws.com/ai2-website/data/OpenBookQA-V1-Sep2018.zip exit 1 fi unzip -p OpenBookQA-V1-Sep2018.zip OpenBookQA-V1-Sep2018/Data/Main/test.jsonl | jq -r -c '[.id, .question.choices[0].label] | @csv' | tr -d '"' > dummy-predictions.csv
ContextualSP/lemon/propara_evaluator/aristo-leaderboard/openbookqa/data/build-dummy-predictions.sh/0
{ "file_path": "ContextualSP/lemon/propara_evaluator/aristo-leaderboard/openbookqa/data/build-dummy-predictions.sh", "repo_id": "ContextualSP", "token_count": 181 }
234
import sys def corrupted_action_file(filename: str, details: str, line_num: int = None): if line_num is None: print(f"Corrupted or empty action file {filename} ({details})") else: print(f"Corrupted action file {filename} on line {line_num} ({details})") sys.exit(2) def corrupted_sentences_file(filename: str, details: str): print(f"Corrupted or empty sentences file {filename} ({details})") sys.exit(2)
ContextualSP/lemon/propara_evaluator/aristo-leaderboard/propara/evaluator/errors/errors.py/0
{ "file_path": "ContextualSP/lemon/propara_evaluator/aristo-leaderboard/propara/evaluator/errors/errors.py", "repo_id": "ContextualSP", "token_count": 159 }
235
import unittest from process import ProcessSummary, Conversion, Move, Input, Output from scoring import question, QuestionScores class TestScoring(unittest.TestCase): def test_compare_locations(self): self.assertEquals(question._compare_locations('', ''), 1.0) self.assertEquals(question._compare_locations('', '-'), 0.0) self.assertEquals(question._compare_locations('?', '?'), 1.0) self.assertEquals(question._compare_locations('plant OR leaf', 'leaf'), 1.0) self.assertEquals(question._compare_locations('', 'leaf'), 0.0) self.assertEquals(question._compare_locations('-', 'leaf'), 0.0) self.assertEquals(question._compare_locations('plant OR leaf', 'leaf'), 1.0) self.assertEquals(question._compare_locations('dew OR rain', 'water OR dew'), 1.0) self.assertEquals(question._compare_locations('dew', 'dew AND sun'), 0.5) self.assertEquals(question._compare_locations('dew AND sun', 'dew'), 0.5) self.assertEquals(question._compare_locations('dew AND sun', 'dew AND blah1 AND blah2'), 0.25) self.assertEquals(question._compare_locations('dew AND rain', 'water OR dew'), 0.5) self.assertEquals(question._compare_locations('water OR dew AND sun', 'dew OR rain'), 0.5) def test_score_tuple_question(self): answers = [ Move(participants='plant OR leaf', location_before='root', location_after='earth', step_id='event2'), Move(participants='leaf', location_before='soil', location_after='mud', step_id='event2'), ] predictions = [ Move(participants='plants OR leaf', location_before='root', location_after='earth', step_id='event2'), Move(participants='plant', location_before='mud OR plant', location_after='room OR earth', step_id='event2') ] predictions_longer = [ Move(participants='plants OR leaf', location_before='root', location_after='earth', step_id='event2'), Move(participants='plant', location_before='mud OR plant', location_after='room OR earth', step_id='event2'), Move(participants='tree', location_before='monkey', location_after='earth', step_id='event2'), ] predictions_shorter = [ Move(participants='plants OR leaf', location_before='root', location_after='earth', step_id='event2'), ] self.assertEquals(question._score_moves(answers, predictions).F1(), 0.8333333333333333) self.assertEquals(question._score_moves(answers, predictions_shorter).F1(), 0.8) self.assertEquals(question._score_moves(answers, predictions_longer).F1(), 0.6666666666666666) self.assertEquals(question._score_moves(answers, []).F1(), 0.0) self.assertEquals(question._score_moves([], predictions).F1(), 0.0) self.assertEquals(question._score_moves([], []).F1(), 1.0) def test_score_conversion_pair(self): self.assertEquals(question._score_conversion_pair( Conversion(destroyed='animal OR monkey', created='tree', locations='branch', step_id='event1'), Conversion(destroyed='animal', created='tree', locations='branch', step_id='event1') ), 1.0) self.assertEquals(question._score_conversion_pair( Conversion(destroyed='plant OR leaf', created='root', locations='earth', step_id='event2'), Conversion(destroyed='leaf', created='root OR plant', locations='soil OR earth', step_id='event2'), ), 1.0) # plants should match plant. self.assertEquals(question._score_conversion_pair( Conversion(destroyed='plants OR leaf', created='root', locations='earth', step_id='event2'), Conversion(destroyed='leaf', created='root OR plant', locations='soil OR earth', step_id='event2'), ), 1.0) # identical conversion, but mismatching step_ids self.assertEquals(question._score_conversion_pair( Conversion(destroyed='foo', created='bar', locations='baz', step_id='eventX'), Conversion(destroyed='foo', created='bar', locations='baz', step_id='eventY'), ), 0.0) def test_score_input_pair(self): self.assertEquals(question._score_input_pair( Input(participants=''), Input(participants='-') ), 0) self.assertEquals(question._score_input_pair( Input(participants='plant OR leaf'), Input(participants='leaf') ), 1) self.assertEquals(question._score_input_pair( Input(participants='?'), Input(participants='?') ), 1) def test_calculate(self): score = QuestionScores.from_summaries( answer=ProcessSummary( 1, inputs=[Input(participants='plant')], outputs=[Output(participants='plant OR leaf'), Output(participants='soil')], conversions=[], moves=[ Move(participants="plant OR leaf", location_before="root", location_after="event2", step_id="event2") ] ), prediction=ProcessSummary( 1, inputs=[Input(participants='tree')], outputs=[Output(participants='leaf'), Output(participants='mud')], conversions=[Conversion(destroyed='tree', created='root', locations='soil', step_id='event1')], moves=[Move(participants='plant', location_before='root', location_after='soil', step_id='event2')] ), ) self.assertEquals(score.conversions.F1(), 0.0) score = QuestionScores.from_summaries( answer=ProcessSummary( 1, inputs=[Input(participants='monkey'), Input(participants='ape')], outputs=[Output(participants='langur OR langer')], conversions=[ Conversion(destroyed='animal OR monkey', created='tree', locations='branch', step_id='event1')], moves=[], ), prediction=ProcessSummary( 1, inputs=[Input(participants='langur'), Input(participants='ape')], outputs=[Output(participants='monkey')], conversions=[Conversion(destroyed='animal', created='tree', locations='branch', step_id='event1')], moves=[], ), ) self.assertEquals(score.conversions.F1(), 1.0) def test_score_empty_answers(self): score = QuestionScores.from_summaries( answer=ProcessSummary(process_id=1, inputs=[], outputs=[], conversions=[], moves=[]), prediction=ProcessSummary(process_id=1, inputs=[], outputs=[], conversions=[], moves=[]) ) self.assertEquals(score.inputs.F1(), 1.0) self.assertEquals(score.outputs.F1(), 1.0) self.assertEquals(score.conversions.F1(), 1.0) def test_score(self): i1 = Input(participants='xxx') i2 = Input(participants='yyy') i3 = Input(participants='zzz') answers = [i1] predictions = [i2, i3] def scoring_function(answer: Input, prediction: Input) -> float: return 1.0 score = question._score(answers, predictions, scoring_function) self.assertEqual(score.precision, 1.0) self.assertEqual(score.recall, 1.0) self.assertEqual(score.F1(), 1.0) def test_score2(self): i1 = Input(participants='xxx') i2 = Input(participants='yyy') i3 = Input(participants='zzz') answers = [i1] predictions = [i2, i3] def scoring_function(answer: Input, prediction: Input) -> float: if (answer, prediction) == (i1, i2): return 1.0 return 0.0 score = question._score(answers, predictions, scoring_function) self.assertEqual(score.precision, 0.5) self.assertEqual(score.recall, 1.0) self.assertEqual(score.F1(), 2 / 3) if __name__ == '__main__': unittest.main()
ContextualSP/lemon/propara_evaluator/aristo-leaderboard/propara/evaluator/scoring/test_scoring.py/0
{ "file_path": "ContextualSP/lemon/propara_evaluator/aristo-leaderboard/propara/evaluator/scoring/test_scoring.py", "repo_id": "ContextualSP", "token_count": 3528 }
236
#!/bin/bash set -euo pipefail if [[ ! -f qasc_dataset.tar.gz ]]; then echo Missing file qasc_dataset.tar.gz echo echo Download it first: http://data.allenai.org/downloads/qasc/qasc_dataset.tar.gz exit 1 fi # Questions with correct answers for train and dev (test set is hidden) tar -zxvOf qasc_dataset.tar.gz QASC_Dataset/train.jsonl > train.jsonl tar -zxvOf qasc_dataset.tar.gz QASC_Dataset/dev.jsonl > dev.jsonl # Predicted answers for train, dev and test (always "A"). tar -zxvOf qasc_dataset.tar.gz QASC_Dataset/train.jsonl | jq -r '[.id, "A"] | @csv' > train-predictions.csv tar -zxvOf qasc_dataset.tar.gz QASC_Dataset/dev.jsonl | jq -r '[.id, "A"] | @csv' > dev-predictions.csv tar -zxvOf qasc_dataset.tar.gz QASC_Dataset/test.jsonl | jq -r '[.id, "A"] | @csv' > test-predictions.csv
ContextualSP/lemon/propara_evaluator/aristo-leaderboard/qasc/data/build-files.sh/0
{ "file_path": "ContextualSP/lemon/propara_evaluator/aristo-leaderboard/qasc/data/build-files.sh", "repo_id": "ContextualSP", "token_count": 340 }
237