python_code
stringlengths
0
992k
repo_name
stringlengths
8
46
file_path
stringlengths
5
162
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] from io import open from setuptools import find_packages, setup setup( name="torchscale", version="0.1.1", author="TorchScale Team", author_email="[email protected]", description="Transformers at any scale", long_description=open("README.md", "r", encoding="utf-8").read(), long_description_content_type="text/markdown", keywords="Transformers at any scale", license="MIT", url="https://github.com/msranlp/torchscale", packages=find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"]), install_requires=["apex", "torch>=1.8", "fairscale==0.4.0", "timm==0.4.12"], python_requires=">=3.8.0", classifiers=[ "Programming Language :: Python :: 3", ], )
KosmosX-API-main
kosmosX/torchscale/setup.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details]
KosmosX-API-main
kosmosX/torchscale/torchscale/__init__.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import torch import torch.nn as nn def fixed_pos_embedding(x): seq_len, dim = x.shape inv_freq = 1.0 / (10000 ** (torch.arange(0, dim) / dim)) sinusoid_inp = ( torch.einsum("i , j -> i j", torch.arange(0, seq_len, dtype=torch.float), inv_freq).to(x) ) return torch.sin(sinusoid_inp), torch.cos(sinusoid_inp) class SoPE(nn.Module): def __init__( self, head_dim, scale_base = 512 ): super().__init__() self.head_dim = head_dim self.scale_base = scale_base self.register_buffer( "scale", (torch.arange(0, head_dim, 2) + 0.4 * head_dim) / (1.4 * head_dim) ) def forward(self, len): # min = -(len + offset) // 2 # max = len + offset + min # scale = self.scale ** torch.arange(min, max, 1).to(self.scale).div(self.scale_base)[:, None] scale = self.scale ** (torch.arange(0, len, 1) - len // 2).to(self.scale).div(self.scale_base)[:, None] sin, cos = fixed_pos_embedding(scale) return (sin, cos, scale)
KosmosX-API-main
kosmosX/torchscale/torchscale/component/sope_relative_position.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import copy import torch import torch.nn as nn def MultiwayWrapper(args, module, dim=0): if args.multiway: return MultiwayNetwork(module, dim=dim) return module def set_split_position(position): def apply_fn(module): if hasattr(module, "split_position"): module.split_position = position return apply_fn class MultiwayNetwork(nn.Module): def __init__(self, module, dim=0): super().__init__() self.dim = dim self.A = module self.B = copy.deepcopy(module) self.B.reset_parameters() self.split_position = -1 def forward(self, x, **kwargs): if self.split_position == -1: return self.A(x, **kwargs) if self.split_position == 0: return self.B(x, **kwargs) x1, x2 = torch.split( x, [self.split_position, x.size(self.dim) - self.split_position], dim=self.dim, ) # x1, x2 = x[:self.split_position], x[self.split_position:] y1, y2 = self.A(x1, **kwargs), self.B(x2, **kwargs) return torch.cat([y1, y2], dim=self.dim)
KosmosX-API-main
kosmosX/torchscale/torchscale/component/multiway_network.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import math import torch import torch.nn.functional as F from apex.normalization import FusedLayerNorm as LayerNorm from torch import nn from .multiway_network import MultiwayWrapper from xformers.ops import memory_efficient_attention, LowerTriangularMask, MemoryEfficientAttentionCutlassOp def rotate_every_two(x): x1 = x[:, :, ::2] x2 = x[:, :, 1::2] x = torch.stack((-x2, x1), dim=-1) return x.flatten(-2) # in einsum notation: rearrange(x, '... d j -> ... (d j)')\ def duplicate_interleave(m): """ A simple version of `torch.repeat_interleave` for duplicating a matrix while interleaving the copy. """ dim0 = m.shape[0] m = m.view(-1, 1) # flatten the matrix m = m.repeat(1, 2) # repeat all elements into the 2nd dimension m = m.view(dim0, -1) # reshape into a matrix, interleaving the copy return m def apply_rotary_pos_emb(x, sin, cos, scale=1): sin, cos = map(lambda t: duplicate_interleave(t * scale), (sin, cos)) # einsum notation for lambda t: repeat(t[offset:x.shape[1]+offset,:], "n d -> () n () (d j)", j=2) return (x * cos) + (rotate_every_two(x) * sin) class MultiheadAttention(nn.Module): def __init__( self, args, embed_dim, num_heads, dropout=0.0, self_attention=False, encoder_decoder_attention=False, subln=False, ): super().__init__() self.args = args self.embed_dim = embed_dim self.num_heads = num_heads self.head_dim = embed_dim // num_heads self.scaling = self.head_dim**-0.5 self.scale_length = args.scale_length self.self_attention = self_attention self.encoder_decoder_attention = encoder_decoder_attention assert self.self_attention ^ self.encoder_decoder_attention self.k_proj = MultiwayWrapper(args, nn.Linear(embed_dim, embed_dim, bias=True)) self.v_proj = MultiwayWrapper(args, nn.Linear(embed_dim, embed_dim, bias=True)) self.q_proj = MultiwayWrapper(args, nn.Linear(embed_dim, embed_dim, bias=True)) self.out_proj = MultiwayWrapper( args, nn.Linear(embed_dim, embed_dim, bias=True) ) self.inner_attn_ln = ( MultiwayWrapper(args, LayerNorm(self.embed_dim)) if subln and self.self_attention else None ) self.dropout_module = torch.nn.Dropout(dropout, inplace=True) def reset_parameters(self): nn.init.xavier_uniform_(self.k_proj.weight, gain=1 / math.sqrt(2)) nn.init.xavier_uniform_(self.v_proj.weight, gain=1 / math.sqrt(2)) nn.init.xavier_uniform_(self.q_proj.weight, gain=1 / math.sqrt(2)) nn.init.xavier_uniform_(self.out_proj.weight) nn.init.constant_(self.out_proj.bias, 0.0) def forward( self, query, key, value, incremental_state=None, key_padding_mask=None, attn_mask=None, rel_pos=None, sope_rel_pos=None, ): tgt_len, bsz, embed_dim = query.size() src_len = tgt_len assert embed_dim == self.embed_dim, f"query dim {embed_dim} != {self.embed_dim}" assert list(query.size()) == [tgt_len, bsz, embed_dim] src_len, key_bsz, _ = key.size() assert key_bsz == bsz, f"{query.size(), key.size()}" assert value is not None assert src_len, bsz == value.shape[:2] q = self.q_proj(query) k = self.k_proj(key) v = self.v_proj(value) q = q.view(tgt_len, bsz * self.num_heads, self.head_dim).transpose(0, 1).contiguous() k = k.view(-1, bsz * self.num_heads, self.head_dim).transpose(0, 1).contiguous() v = v.view(-1, bsz * self.num_heads, self.head_dim).transpose(0, 1).contiguous() if incremental_state is not None: if "prev_key" in incremental_state: prev_key = incremental_state["prev_key"].view( bsz * self.num_heads, -1, self.head_dim ) prev_value = incremental_state["prev_value"].view( bsz * self.num_heads, -1, self.head_dim ) k = torch.cat([prev_key, k], dim=1) v = torch.cat([prev_value, v], dim=1) incremental_state["prev_key"] = k.view( bsz, self.num_heads, -1, self.head_dim ) incremental_state["prev_value"] = v.view( bsz, self.num_heads, -1, self.head_dim ) src_len = k.size(1) if sope_rel_pos is not None: assert rel_pos is None sin, cos, scale = sope_rel_pos if self.self_attention: k = apply_rotary_pos_emb(k, sin, cos, scale = 1 / scale) q = apply_rotary_pos_emb(q, sin[-q.shape[1]:], cos[-q.shape[1]:], scale = scale[-q.shape[1]:]) else: k = apply_rotary_pos_emb(k, sin[:k.shape[1]], cos[:k.shape[1]], scale = 1 / scale[:k.shape[1]]) q = apply_rotary_pos_emb(q, sin[k.shape[1]:], cos[k.shape[1]:], scale = scale[k.shape[1]:]) if k.shape[1] > self.scale_length: scale_attention = torch.maximum(torch.ones(q.shape[1]), torch.arange(k.shape[1] - q.shape[1], k.shape[1], 1).log() / math.log(self.scale_length)).to(q) q = q * scale_attention.unsqueeze(-1) if self.args.flash_attention and rel_pos is None and attn_mask is not None: attn_bias = LowerTriangularMask() attn = memory_efficient_attention(q, k, v, attn_bias, op=MemoryEfficientAttentionCutlassOp) attn_weights = None else: q *= self.scaling attn_weights = torch.bmm(q, k.transpose(1, 2)) if attn_mask is not None: attn_weights = torch.nan_to_num(attn_weights) attn_mask = attn_mask.unsqueeze(0) attn_weights += attn_mask if key_padding_mask is not None: attn_weights = attn_weights.view(bsz, self.num_heads, tgt_len, src_len) attn_weights = attn_weights.masked_fill( key_padding_mask.unsqueeze(1).unsqueeze(2).to(torch.bool), float("-inf"), ) attn_weights = attn_weights.view(bsz * self.num_heads, tgt_len, src_len) if rel_pos is not None: rel_pos = rel_pos.view(attn_weights.size()) attn_weights = attn_weights + rel_pos attn_weights = F.softmax(attn_weights, dim=-1, dtype=torch.float32).type_as( attn_weights ) attn_probs = self.dropout_module(attn_weights) attn = torch.bmm(attn_probs, v) attn = attn.transpose(0, 1).contiguous().view(tgt_len, bsz, embed_dim) if self.inner_attn_ln is not None: attn = self.inner_attn_ln(attn) attn = self.out_proj(attn) if attn_weights is not None: attn_weights = attn_weights.view( bsz, self.num_heads, tgt_len, src_len ).transpose(1, 0) return attn, attn_weights
KosmosX-API-main
kosmosX/torchscale/torchscale/component/multihead_attention.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import math import torch import torch.nn as nn class RelativePositionBias(nn.Module): def __init__( self, bidirectional=True, num_buckets=32, max_distance=128, n_heads=12 ): super().__init__() self.bidirectional = bidirectional self.num_buckets = num_buckets self.max_distance = max_distance self.n_heads = n_heads self.relative_attention_bias = nn.Embedding(self.num_buckets, self.n_heads) @staticmethod def _relative_position_bucket( relative_position, bidirectional=True, num_buckets=32, max_distance=128 ): ret = 0 n = -relative_position if bidirectional: num_buckets //= 2 ret += (n < 0).to(torch.long) * num_buckets n = torch.abs(n) else: n = torch.max(n, torch.zeros_like(n)) max_exact = num_buckets // 2 is_small = n < max_exact val_if_large = max_exact + ( torch.log(n.float() / max_exact) / math.log(max_distance / max_exact) * (num_buckets - max_exact) ).to(torch.long) val_if_large = torch.min( val_if_large, torch.full_like(val_if_large, num_buckets - 1) ) ret += torch.where(is_small, n, val_if_large) return ret def compute_bias(self, qlen, klen, step=None): step = 0 if step is None else step context_position = torch.arange( step, step + qlen, dtype=torch.long, device=self.relative_attention_bias.weight.device, )[:, None] memory_position = torch.arange( klen, dtype=torch.long, device=self.relative_attention_bias.weight.device )[None, :] relative_position = memory_position - context_position # shape (qlen, klen) rp_bucket = self._relative_position_bucket( relative_position, # shape (qlen, klen) bidirectional=self.bidirectional, num_buckets=self.num_buckets, ) rp_bucket = rp_bucket.to(self.relative_attention_bias.weight.device) values = self.relative_attention_bias( rp_bucket ) # shape (qlen, klen, num_heads) values = values.permute([2, 0, 1]).unsqueeze( 0 ) # shape (1, num_heads, qlen, klen) return values def forward(self, batch_size, qlen, klen, step=None): # shape (batch * num_heads, qlen, klen) return ( self.compute_bias(qlen, klen, step) .repeat(batch_size, 1, 1, 1) .view(-1, qlen, klen) )
KosmosX-API-main
kosmosX/torchscale/torchscale/component/relative_position_bias.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import torch import torch.nn as nn import torch.nn.functional as F class VisionLanguageEmbedding(nn.Module): def __init__(self, text_embed, vision_embed): super().__init__() self.text_embed = text_embed self.vision_embed = vision_embed def forward(self, textual_tokens, visual_tokens, **kwargs): if textual_tokens is None: return self.vision_embed(visual_tokens) if visual_tokens is None: return self.text_embed(textual_tokens) x1 = self.vision_embed(visual_tokens) x2 = self.text_embed(textual_tokens) return torch.cat([x1, x2], dim=1) class VisionEmbedding(nn.Module): """Image to Patch Embedding""" def __init__( self, img_size=224, patch_size=16, in_chans=3, embed_dim=768, contain_mask_token=False, prepend_cls_token=False, ): super().__init__() img_size = (img_size, img_size) patch_size = (patch_size, patch_size) num_patches = (img_size[1] // patch_size[1]) * (img_size[0] // patch_size[0]) self.patch_shape = (img_size[0] // patch_size[0], img_size[1] // patch_size[1]) self.img_size = img_size self.patch_size = patch_size self.num_patches = num_patches self.proj = nn.Conv2d( in_chans, embed_dim, kernel_size=patch_size, stride=patch_size ) if contain_mask_token: self.mask_token = nn.Parameter(torch.zeros(1, 1, embed_dim)) else: self.mask_token = None if prepend_cls_token: self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim)) else: self.cls_token = None def forward(self, x, masked_position=None, **kwargs): B, C, H, W = x.shape assert ( H == self.img_size[0] and W == self.img_size[1] ), f"Input image size ({H}*{W}) doesn't match model ({self.img_size[0]}*{self.img_size[1]})." x = self.proj(x).flatten(2).transpose(1, 2) batch_size, seq_len, _ = x.size() if masked_position is not None: assert self.mask_token is not None mask_token = self.mask_token.expand(batch_size, seq_len, -1) w = masked_position.unsqueeze(-1).type_as(mask_token) x = x * (1 - w) + mask_token * w if self.cls_token is not None: cls_tokens = self.cls_token.expand( batch_size, -1, -1 ) # stole cls_tokens impl from Phil Wang, thanks x = torch.cat((cls_tokens, x), dim=1) return x class TextEmbedding(nn.Embedding): def reset_parameters(self): nn.init.normal_(self.weight, mean=0, std=self.embedding_dim**-0.5) self._fill_padding_idx_with_zero() class PositionalEmbedding(nn.Embedding): def forward( self, x, positions=None, **kwargs, ): if positions is None: # being consistent with Fairseq, which starts from 2. positions = ( torch.arange(2, x.size(1) + 2, device=x.device).long().unsqueeze(0) ) return F.embedding( positions, self.weight, self.padding_idx, self.max_norm, self.norm_type, self.scale_grad_by_freq, self.sparse, )
KosmosX-API-main
kosmosX/torchscale/torchscale/component/embedding.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import torch.nn as nn from timm.models.layers import drop_path class DropPath(nn.Module): """Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).""" def __init__(self, drop_prob=None): super(DropPath, self).__init__() self.drop_prob = drop_prob def forward(self, x): return drop_path(x, self.drop_prob, self.training) def extra_repr(self): return "p={}".format(self.drop_prob)
KosmosX-API-main
kosmosX/torchscale/torchscale/component/droppath.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details]
KosmosX-API-main
kosmosX/torchscale/torchscale/component/__init__.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import torch import torch.nn as nn import torch.nn.functional as F from apex.normalization import FusedLayerNorm as LayerNorm class set_torch_seed(object): def __init__(self, seed): assert isinstance(seed, int) self.rng_state = self.get_rng_state() torch.manual_seed(seed) if torch.cuda.is_available(): torch.cuda.manual_seed(seed) def get_rng_state(self): state = {"torch_rng_state": torch.get_rng_state()} if torch.cuda.is_available(): state["cuda_rng_state"] = torch.cuda.get_rng_state() return state def set_rng_state(self, state): torch.set_rng_state(state["torch_rng_state"]) if torch.cuda.is_available(): torch.cuda.set_rng_state(state["cuda_rng_state"]) def __enter__(self): return self def __exit__(self, *exc): self.set_rng_state(self.rng_state) def make_experts(args, embed_dim, expert_ffn_dim): world_size = ( 1 if not torch.distributed.is_initialized() else torch.distributed.get_world_size() ) expert_list = [] ddp_rank = args.ddp_rank start_seed = torch.randint(1000000, (1,)).item() # at least as many experts than gpus if args.moe_expert_count >= world_size: assert ( args.moe_expert_count % world_size == 0 ), f"{args.moe_expert_count}, {world_size}" local_moe_expert_count = args.moe_expert_count // world_size for i in range(local_moe_expert_count): with set_torch_seed(start_seed + ddp_rank * local_moe_expert_count + i): expert_list.append( FeedForwardNetwork( embed_dim, expert_ffn_dim, args.activation_fn, args.dropout, args.activation_dropout, args.subln, ) ) else: assert ( world_size % args.moe_expert_count == 0 ), f"{world_size}, {args.moe_expert_count}" with set_torch_seed(start_seed + ddp_rank % args.moe_expert_count): expert_list.append( FeedForwardNetwork( embed_dim, expert_ffn_dim, args.activation_fn, args.dropout, args.activation_dropout, args.subln, ) ) experts = nn.ModuleList(expert_list) return experts def get_activation_fn(activation): if activation == "relu": return F.relu elif activation == "gelu": return F.gelu else: raise NotImplementedError class FeedForwardNetwork(nn.Module): def __init__( self, embed_dim, ffn_dim, activation_fn, dropout, activation_dropout, subln=False, ): super().__init__() self.embed_dim = embed_dim self.activation_fn = get_activation_fn(activation=str(activation_fn)) self.activation_dropout_module = torch.nn.Dropout( activation_dropout, inplace=True ) self.dropout_module = torch.nn.Dropout(dropout, inplace=True) self.fc1 = nn.Linear(self.embed_dim, ffn_dim) self.fc2 = nn.Linear(ffn_dim, self.embed_dim) self.ffn_layernorm = LayerNorm(ffn_dim) if subln else None def reset_parameters(self): self.fc1.reset_parameters() self.fc2.reset_parameters() if self.ffn_layernorm is not None: self.ffn_layernorm.reset_parameters() def forward(self, x): x_shape = x.shape x = x.reshape(-1, x.size(-1)) x = self.fc1(x) x = self.activation_fn(x.float()).type_as(x) x = self.activation_dropout_module(x) if self.ffn_layernorm is not None: x = self.ffn_layernorm(x) x = self.fc2(x) x = x.view(x_shape) x = self.dropout_module(x) return x
KosmosX-API-main
kosmosX/torchscale/torchscale/component/feedforward_network.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details]
KosmosX-API-main
kosmosX/torchscale/torchscale/component/xmoe/__init__.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] # Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. # # This source code is licensed under the BSD license found in the # LICENSE file in the root directory of this source tree. # NOTE: This is a mirror of the code in # https://github.com/facebookresearch/fairscale/tree/master/fairscale/nn/moe import logging import time from typing import Any, Tuple, cast import torch import torch.distributed as dist from torch import Tensor from torch.nn import Module, ModuleList try: from fairseq.modules.moe import MOELayer has_fairseq = True Base = MOELayer except ModuleNotFoundError: Base = Module has_fairseq = False try: # To enable Tutel MoE optimizations: # python3 -m pip install --user --upgrade git+https://github.com/microsoft/[email protected] from tutel import moe as tutel_moe has_tutel, fused_cumsum_sub_one = True, tutel_moe.fast_cumsum_sub_one except ModuleNotFoundError: has_tutel, fused_cumsum_sub_one = False, lambda mask: torch.cumsum(mask, dim=0) - 1 logger = logging.getLogger(__name__) # einsum dimensions: (g)roup, (s)equence, (e)xpert, (m)odel, (c)apacity # See https://arxiv.org/pdf/2006.16668.pdf for details. # Based on https://github.com/pytorch/pytorch/pull/40762 class _AllToAll(torch.autograd.Function): @staticmethod def forward(ctx: Any, group: dist.ProcessGroup, input: Tensor) -> Tensor: # type: ignore ctx.group = group input = input.contiguous() output = torch.empty_like(input) if torch.distributed.is_initialized(): dist.all_to_all_single(output, input, group=group) else: assert group is None output = input return output @staticmethod def backward(ctx: Any, *grad_output: Tensor) -> Tuple[None, Tensor]: return (None, _AllToAll.apply(ctx.group, *grad_output)) def _find_my_group_index(grouped_ranks): my_rank = dist.get_rank() for i, group in enumerate(grouped_ranks): if my_rank in group: return i raise RuntimeError def get_moe_group(moe_expert_count): if dist.is_initialized(): if not hasattr(get_moe_group, "_moe_groups"): world_size = dist.get_world_size() if world_size <= moe_expert_count: assert moe_expert_count % world_size == 0 moe_groups = [[i] for i in range(world_size)] else: assert world_size % moe_expert_count == 0 ranks_per_group = world_size // moe_expert_count moe_groups = [ [i + j * moe_expert_count for j in range(ranks_per_group)] for i in range(moe_expert_count) ] get_moe_group._moe_group_idx = moe_groups get_moe_group._moe_groups = [dist.new_group(g) for g in moe_groups] my_group_idx = _find_my_group_index(get_moe_group._moe_group_idx) return get_moe_group._moe_groups[my_group_idx] def get_all2all_group(moe_expert_count): if dist.is_initialized(): if not hasattr(get_all2all_group, "_all2all_groups"): world_size = dist.get_world_size() # more experts than world size if world_size <= moe_expert_count: assert moe_expert_count % world_size == 0 all2all_groups = [[i for i in range(world_size)]] # larger world than num experts else: assert world_size % moe_expert_count == 0 ranks_per_group = world_size // moe_expert_count all2all_groups = [ [i * moe_expert_count + j for j in range(moe_expert_count)] for i in range(ranks_per_group) ] get_all2all_group._all2all_group_idx = all2all_groups get_all2all_group._all2all_groups = [ dist.new_group(g) for g in all2all_groups ] my_group_idx = _find_my_group_index(get_all2all_group._all2all_group_idx) return get_all2all_group._all2all_groups[my_group_idx] class MOELayer(Base): """MOELayer module which implements MixtureOfExperts as described in Gshard_. :: gate = Top2Gate(model_dim, num_experts) moe = MOELayer(gate, expert) output = moe(input) l_aux = moe.l_aux .. Gshard_: https://arxiv.org/pdf/2006.16668.pdf Args: gate (torch.nn.Module): gate network expert (torch.nn.Module): expert network """ def __init__(self, gate, experts, args): if has_fairseq: super(Base, self).__init__() else: super().__init__() self.gate = gate if type(experts) == ModuleList: self.experts = cast(ModuleList, experts) else: self.experts = ModuleList([experts]) self.expert_group = get_moe_group(args.moe_expert_count) self.all2all_group = get_all2all_group(args.moe_expert_count) self.world_size = dist.get_world_size(group=self.expert_group) self.all2all_size = dist.get_world_size(group=self.all2all_group) for p in experts.parameters(): p.expert = True # type: ignore self.num_local_experts = len(self.experts) self.args = args self.in_generation = False self.a2a_cuda_event_intervals = [] self.a2a_cpu_time_ms = 0.0 def forward(self, *input: Tensor, input_padding_mask=None, **kwargs: Any) -> Tensor: assert len(input) == 1, "only single input Tensor supported" input = input[0] assert ( len(input.shape) == 3 ), "input Tensor must have dimensions: (s)equence, (t)oken, (m)odel" if input_padding_mask is not None: assert ( len(input_padding_mask.shape) == 2 ), "input Tensor must have dimensions: (s)equence, (t)oken" assert input_padding_mask.shape[0] == input.shape[0] assert input_padding_mask.shape[1] == input.shape[1] # assert input.shape[0] % len(self.experts) == 0, "num tokens must be order of number of local experts" # Implement Algorithm 2 from GShard paper. d_model = input.shape[2] # Pad to expected batch size input_shape = list(input.shape) expected_bsz = ( getattr(self.args, "batch_size", 0) if self.training else getattr(self.args, "batch_size_valid", 0) ) # This indicates that --batch-size or --max-sentences is not specified if expected_bsz is None: expected_bsz = 0 # Note: Padding is not necessary at generation time at present # because all DDP workers process the same batch. Also, batch size at generation time # can be different from that present in the checkpoint state if ( not self.in_generation and expected_bsz != 0 and input_shape[0] != expected_bsz ): logger.warning( f"padding batch with unexpected size {input_shape[0]} (expected: {expected_bsz})" ) assert input_shape[0] < expected_bsz, f"{input_shape[0]} < {expected_bsz}" padded_input = torch.zeros( (expected_bsz, input_shape[1], input_shape[2]), dtype=input.dtype, layout=input.layout, device=input.device, ) padded_input[: input_shape[0], :, :] = input input = padded_input padded_input_padding_mask = torch.ones( ( expected_bsz, input_shape[1], ), dtype=torch.bool, device=input.device, ) if input_padding_mask is not None: padded_input_padding_mask[: input_shape[0], :] = input_padding_mask else: padded_input_padding_mask[: input_shape[0], :] = False input_padding_mask = padded_input_padding_mask # Reshape into S tokens by dropping sequence dimension. reshaped_input = input.reshape(-1, d_model) reshaped_input_shape = reshaped_input.shape reshaped_input_padding_mask = ( input_padding_mask.reshape(-1) if input_padding_mask is not None else None ) # Doing padding here when --max-tokens is specified and not --batch-size or --max-sentences # Pro of --max-tokens: more flexible for MT variable sequence lengths # Con of --max-tokens: extra all-reduce needed to figure out optimal padding without running OOM if expected_bsz == 0: expected_dim = reshaped_input_shape[0] * torch.ones( (1,), dtype=torch.long, device=input.device ) dist.all_reduce(expected_dim, group=dist.group.WORLD, op=dist.ReduceOp.MAX) expected_dim = int(expected_dim.item()) padded_input = torch.zeros( (expected_dim, reshaped_input_shape[1]), dtype=input.dtype, layout=input.layout, device=input.device, ) padded_input[: reshaped_input_shape[0], :] = reshaped_input reshaped_input = padded_input padded_input_padding_mask = torch.ones( (expected_dim,), dtype=torch.bool, device=padded_input.device ) if reshaped_input_padding_mask is not None: padded_input_padding_mask[ : reshaped_input_shape[0] ] = reshaped_input_padding_mask else: padded_input_padding_mask[: reshaped_input_shape[0]] = False reshaped_input_padding_mask = padded_input_padding_mask if has_tutel: l_aux, self.metadata, C, E, indices_, locations_, gates_ = self.gate( reshaped_input, reshaped_input_padding_mask ) S, M = reshaped_input.size(0), reshaped_input.size(1) if not hasattr(self, "_tutel_dispatcher"): self._tutel_dispatcher = tutel_moe.fast_dispatcher( E, C, M, dispatch_dtype=reshaped_input.dtype ) self._tutel_dispatcher.update(indices_, locations_, gates_, capacity=C) dispatched_input = self._tutel_dispatcher.encode(reshaped_input) else: l_aux, combine_weights, dispatch_mask, self.metadata = self.gate( reshaped_input, reshaped_input_padding_mask ) dispatch_mask = dispatch_mask.to(input.dtype).permute( 1, 2, 0 ) # S,E,C -> E,C,S E, C, S = dispatch_mask.size() M = reshaped_input.size(1) assert reshaped_input.size() == (S, M) # einsum("sec,sm->ecm") dispatched_input = torch.mm( dispatch_mask.view(E * C, S), reshaped_input ) # -> (E*C),M if self.all2all_size > 1: dispatched_input = self.all_to_all_wrapper(dispatched_input) # Re-shape after all-to-all: ecm -> gecm dispatched_input = dispatched_input.reshape( self.all2all_size, self.num_local_experts, -1, d_model ) chunks = dispatched_input.chunk(self.num_local_experts, dim=1) expert_outputs = [] for chunk, expert in zip(chunks, self.experts): expert_outputs += [expert(chunk)] expert_output = torch.cat(expert_outputs, dim=1) if self.all2all_size > 1: expert_output = self.all_to_all_wrapper(expert_output) # Re-shape back: gecm -> ecm expert_output = expert_output.reshape( self.all2all_size * self.num_local_experts, -1, d_model ) if has_tutel: combined_output = self._tutel_dispatcher.decode( expert_output.view(E * C, M) ) else: # einsum("sec,ecm->sm") combined_output = combine_weights.view(S, E * C).mm( expert_output.view(E * C, M) ) # Remove padding here when --max-tokens is specified and not --batch-size or --max-sentences combined_output = combined_output[: reshaped_input_shape[0], :] combined_output = combined_output.reshape(input.shape) combined_output = combined_output[: input_shape[0], :, :] self.record_all_to_all_stats() return combined_output, l_aux def prepare_for_inference_(self): self.in_generation = True def all_to_all_wrapper(self, input: Tensor): dummy_a2a = getattr(self.args, "dummy_a2a", False) if dummy_a2a: input = input.contiguous() output = input.detach().clone() return input # always record times, since it is not a lot of overhead # if we do not log it we simply clear it off in record_all_to_all_stats cuda_start = torch.cuda.Event(enable_timing=True) cuda_end = torch.cuda.Event(enable_timing=True) cpu_start = time.time() * 1000 cuda_start.record() output = _AllToAll.apply(self.all2all_group, input) cuda_end.record() cpu_end = time.time() * 1000 self.a2a_cpu_time_ms += cpu_end - cpu_start self.a2a_cuda_event_intervals.append((cuda_start, cuda_end)) return output def record_all_to_all_stats(self): # controlled via an argument as we want to minimize any impact from torch.cuda.synchronize() record_a2a_perf_stats = getattr(self.args, "record_a2a_perf_stats", False) if record_a2a_perf_stats: torch.cuda.synchronize() self.metadata["all_to_all_cpu_time_ms"] = self.a2a_cpu_time_ms a2a_cuda_time_ms = 0.0 for ev_start, ev_end in self.a2a_cuda_event_intervals: a2a_cuda_time_ms += ev_start.elapsed_time(ev_end) self.metadata["all_to_all_cuda_time_ms"] = a2a_cuda_time_ms # reset stats self.a2a_cpu_time_ms = 0.0 self.a2a_cuda_event_intervals = []
KosmosX-API-main
kosmosX/torchscale/torchscale/component/xmoe/moe_layer.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] # Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. # # This source code is licensed under the BSD license found in the # LICENSE file in the root directory of this source tree. # Implementation of Top2Gating described in https://arxiv.org/pdf/2006.16668.pdf # Code is inspired by Top2GatingOnLogits from lingvo: # https://github.com/tensorflow/lingvo/blob/21b8106c5f1d30a196c98eedc441d4fd70833b11/lingvo/core/moe_layers.py#L477 # NOTE: This is a mirror of the code in # https://github.com/facebookresearch/fairscale/tree/master/fairscale/nn/moe import math from typing import Callable, Dict, Optional, Tuple import torch import torch.nn.functional as F from torch import Tensor from .moe_layer import fused_cumsum_sub_one, has_tutel # use a fixed temperature to compute balance loss TEMPERATURE_FOR_L_UAX = 0.07 # maximum capacity of 1 expert as a fraction of number of tokens in the batch # Note: setting this to 1.0 causes inference to significantly slow down EVAL_CAPACITY_TOKEN_FRACTION = 0.25 # logging SAMPLE_FRACTION = 0.2 def top1gating( logits: torch.Tensor, input_mask: Optional[torch.Tensor] = None, use_fp32=False, capacity_factor=1.0, eval_mode=False, moe_eval_capacity_token_fraction=EVAL_CAPACITY_TOKEN_FRACTION, use_xmoe=False, gate_obj=None, ) -> Tuple[Tensor, Tensor, Tensor, Dict]: """Implements Top2Gating on logits.""" metadata = {} if use_fp32: orig_dtype = logits.dtype logits = logits.float() gates = F.softmax(logits, dim=1) metadata["entropy_gating"] = entropy(probs=gates).mean().detach() # gates has shape of SE num_tokens = gates.shape[0] num_experts = gates.shape[1] if moe_eval_capacity_token_fraction > 0.0 and eval_mode: capacity = math.ceil(moe_eval_capacity_token_fraction * num_tokens) else: # capacity = capacity_factor * S/E capacity = int(capacity_factor * math.ceil(num_tokens / num_experts)) # Create a mask for 1st's expert per token indices1_s = torch.argmax(gates, dim=1) mask1 = one_hot(indices1_s, num_classes=num_experts, unsqueeze_indices=True) if input_mask is not None and input_mask.any(): nonpadding = ~input_mask mask1 = mask1 * nonpadding.unsqueeze(-1).to(mask1.dtype) # for logging (percent of tokens routed to each expert) expert1_hist = ( 100 * torch.histc( (indices1_s.squeeze() + 1), bins=num_experts, min=1, max=num_experts ) / num_tokens ) metadata["unused_expert1_count"] = (expert1_hist == 0).sum() expert1_hist = ( torch.sort(expert1_hist, dim=0, descending=True).values + torch.finfo(torch.float32).tiny ) sample_count = max(math.ceil(num_experts * SAMPLE_FRACTION), 1) metadata["expert1_balance_top"] = expert1_hist[:sample_count].sum() metadata["expert1_balance_bottom"] = expert1_hist[-sample_count:].sum() gates1_s = (gates * mask1).sum(dim=1) # Compute locations in capacity buffer locations1 = fused_cumsum_sub_one(mask1) # Compute l_aux me = torch.mean(gates, dim=0) ce = torch.mean(mask1.to(gates.dtype), dim=0) l_aux = torch.mean(me * ce) l_aux = l_aux * num_experts * num_experts if has_tutel: locations1_s = torch.sum(locations1 * mask1, dim=1) return ( l_aux, metadata, capacity, num_experts, [ indices1_s, ], [ locations1_s, ], [ gates1_s, ], ) # Remove locations outside capacity from mask mask1 = mask1 * torch.lt(locations1, capacity) # Store the capacity location for each token locations1_s = torch.sum(locations1 * mask1, dim=1) # Calculate combine_weights and dispatch_mask gates1 = gates1_s.unsqueeze(-1) * mask1.to(gates1_s.dtype) # einsum("s,se->se") # locations1_sc = num_tokens * capacity locations1_sc = one_hot(locations1_s, num_classes=capacity, unsqueeze_indices=True) combine1_sec = torch.bmm( # einsum("se,sc->sec") gates1.unsqueeze(-1), locations1_sc.to(gates1.dtype).unsqueeze(1), ) dispatch_mask = combine1_sec.bool() if use_fp32: return l_aux, combine1_sec.to(orig_dtype), dispatch_mask, metadata else: return l_aux, combine1_sec, dispatch_mask, metadata class Top1Gate(torch.nn.Module): """Gate module which implements Top2Gating as described in Gshard_. :: gate = Top2Gate(model_dim, num_experts) l_aux, combine_weights, dispatch_mask = gate(input) .. Gshard_: https://arxiv.org/pdf/2006.16668.pdf Args: model_dim (int): size of model embedding dimension num_experts (ints): number of experts in model """ wg: torch.nn.Linear def __init__( self, model_dim: int, num_experts: int, use_fp32=False, input_noise_type=None, capacity_factor=1.0, moe_eval_capacity_token_fraction=EVAL_CAPACITY_TOKEN_FRACTION, use_xmoe=False, ) -> None: # TODO: merge this to top2gate.py # super().__init__() if not use_xmoe: self.wg = torch.nn.Linear(model_dim, num_experts, bias=False) else: self.wg_reduction = torch.nn.Linear(model_dim, 16, bias=False) wg = torch.empty(num_experts, 16) torch.nn.init.orthogonal_(wg, gain=0.32) self.register_parameter("wg", torch.nn.Parameter(wg)) self.use_xmoe = use_xmoe self.use_fp32 = use_fp32 self.input_noise_type = input_noise_type self.capacity_factor = capacity_factor self.moe_eval_capacity_token_fraction = moe_eval_capacity_token_fraction def forward(self, input, mask=None): # type: ignore if self.use_xmoe: input = self.wg_reduction(input) with torch.no_grad(): wg_norm = self.wg.norm(p=2.0, dim=1, keepdim=True) self.wg.mul_(1.5 / wg_norm) logits = self._cosine(input, self.wg) logits = self._make_finite(logits) else: logits = self.wg(input) return top1gating( logits, mask, use_fp32=self.use_fp32, capacity_factor=self.capacity_factor, eval_mode=not self.training, moe_eval_capacity_token_fraction=self.moe_eval_capacity_token_fraction, use_xmoe=self.use_xmoe, gate_obj=self, ) def _make_finite(self, scores): ok = scores.isfinite() if not ok.all(): # NaNs here can break the assignment algorithm scores[~ok] = scores[ok].min() return scores def _get_gating_temperature(self, eps=1e-4): if self.gating_t.data.item() < eps: return eps return self.gating_t def _cosine(self, mat1, mat2, eps=1e-4): assert mat1.dim() == 2 assert mat2.dim() == 2 # mat1 = F.normalize(mat1, p=2.0, dim=1, eps=eps) mat2 = F.normalize(mat2.float(), p=2.0, dim=1, eps=eps) return mat1.float().matmul(mat2.transpose(0, 1)).type_as(mat1) gumbel_map: Dict[torch.device, Callable] = {} def gumbel_rsample(shape: Tuple, device: torch.device) -> Tensor: gumbel = gumbel_map.get(device) if gumbel is None: one = torch.tensor(1.0, device=device) zero = torch.tensor(0.0, device=device) gumbel = torch.distributions.gumbel.Gumbel(zero, one).rsample # type: ignore gumbel_map[device] = gumbel return gumbel(shape) def one_hot(indices: torch.Tensor, num_classes: int, unsqueeze_indices=False) -> Tensor: if unsqueeze_indices: indices = indices.unsqueeze(-1) assert indices.shape[-1] == 1, "last dimension of indices must be have size 1" output = torch.zeros( indices.shape[:-1] + (num_classes,), device=indices.device, dtype=indices.dtype ) output.scatter_(len(output.shape) - 1, indices, 1) return output def entropy(probs): logits = torch.distributions.utils.probs_to_logits(probs) p_log_p = probs * logits return -p_log_p.sum(-1) def top2gating( logits: torch.Tensor, input_mask: Optional[torch.Tensor] = None, use_fp32=False, second_expert_policy="sampling", normalize_gate_prob_before_dropping=False, eval_mode=False, moe_eval_capacity_token_fraction=0.25, batch_prioritized_routing=False, ) -> Tuple[Tensor, Tensor, Tensor]: """Implements Top2Gating on logits.""" metadata = {} if use_fp32: orig_dtype = logits.dtype logits = logits.float() gates = F.softmax(logits, dim=1) metadata["entropy_gating"] = entropy(probs=gates).mean().detach() # gates has shape of SE num_tokens = gates.shape[0] num_experts = gates.shape[1] if moe_eval_capacity_token_fraction > 0.0 and eval_mode: capacity = math.ceil(moe_eval_capacity_token_fraction * num_tokens) else: # capacity = 2S/E capacity = 2 * math.ceil(num_tokens / num_experts) # Create a mask for 1st's expert per token indices1_s = torch.argmax(gates, dim=1, keepdim=True) mask1 = one_hot(indices1_s, num_experts) if second_expert_policy == "sampling": # Create a mask for 2nd's expert per token using Gumbel-max trick # https://timvieira.github.io/blog/post/2014/07/31/gumbel-max-trick/ logits_w_noise = logits + gumbel_rsample(logits.shape, device=logits.device) else: logits_w_noise = logits # Replace top-expert with min value logits_except1 = logits_w_noise.masked_fill(mask1.bool(), float("-inf")) indices2_s = torch.argmax(logits_except1, dim=1, keepdim=True) mask2 = one_hot(indices2_s, num_experts) gates1_s = (gates * mask1).sum(dim=1) gates2_s = (gates * mask2).sum(dim=1) if normalize_gate_prob_before_dropping: # Normalize gate probabilities denom_s = gates1_s + gates2_s # Avoid divide-by-zero denom_s = torch.clamp(denom_s, min=torch.finfo(denom_s.dtype).eps) gates1_s = gates1_s / denom_s gates2_s = gates2_s / denom_s if second_expert_policy == "random": sampled = (2 * gates2_s) > torch.rand_like(gates2_s) mask2 = mask2 * sampled.repeat(num_experts, 1).transpose(1, 0) # Compute locations in capacity buffer if input_mask is not None and input_mask.any(): nonpadding = ~input_mask mask1 = mask1 * nonpadding.unsqueeze(-1).to(mask1.dtype) mask2 = mask2 * nonpadding.unsqueeze(-1).to(mask1.dtype) if batch_prioritized_routing: # if batch_prioritized_routing: importance_scores = -1 * gates.max(dim=1)[0] sorted_mask1 = mask1[importance_scores.argsort(dim=0)] sorted_cumsum1 = fused_cumsum_sub_one(sorted_mask1) * sorted_mask1 importance_sorted_locations1 = sorted_cumsum1[ importance_scores.argsort(dim=0).argsort(dim=0) ] sorted_mask2 = mask2[importance_scores.argsort(dim=0)] sorted_cumsum2 = fused_cumsum_sub_one(sorted_mask2) * sorted_mask2 importance_sorted_locations2 = sorted_cumsum2[ importance_scores.argsort(dim=0).argsort(dim=0) ] importance_sorted_locations2 += torch.sum(mask1, dim=0, keepdim=True) locations1, locations2 = ( importance_sorted_locations1, importance_sorted_locations2, ) else: locations1 = fused_cumsum_sub_one(mask1) locations2 = fused_cumsum_sub_one(mask2) # Update 2nd's location by accounting for locations of 1st locations2 += torch.sum(mask1, dim=0, keepdim=True) # Compute l_aux me = torch.mean(gates, dim=0) ce = torch.mean(mask1.to(gates.dtype), dim=0) l_aux = torch.mean(me * ce) l_aux = l_aux * num_experts * num_experts # for logging purposes metadata["overflow_expert1"] = ( 100 * torch.sum(mask1 * torch.ge(locations1, capacity)) / torch.sum(mask1) ) metadata["overflow_expert2"] = ( 100 * torch.sum(mask2 * torch.ge(locations2, capacity)) / torch.sum(mask2) ) # Remove locations outside capacity from mask mask1_, mask2_ = mask1, mask2 mask1 = mask1 * torch.lt(locations1, capacity) mask2 = mask2 * torch.lt(locations2, capacity) # for logging (percent of tokens routed to each expert) expert1_hist = ( 100 * torch.histc( (indices1_s.squeeze() + 1), bins=num_experts, min=1, max=num_experts ) / num_tokens ) metadata["unused_expert1_count"] = (expert1_hist == 0).sum() expert1_hist = ( torch.sort(expert1_hist, dim=0, descending=True).values + torch.finfo(torch.float32).tiny ) expert2_hist = ( 100 * torch.histc( (indices2_s.squeeze() + 1), bins=num_experts, min=1, max=num_experts ) / num_tokens ) metadata["unused_expert2_count"] = (expert2_hist == 0).sum() expert2_hist = ( torch.sort(expert2_hist, dim=0, descending=True).values + torch.finfo(torch.float32).tiny ) sample_count = max(math.ceil(num_experts * SAMPLE_FRACTION), 1) metadata["expert1_balance_top"] = expert1_hist[:sample_count].sum() metadata["expert1_balance_bottom"] = expert1_hist[-sample_count:].sum() metadata["expert2_balance_top"] = expert2_hist[:sample_count].sum() metadata["expert2_balance_bottom"] = expert2_hist[-sample_count:].sum() if not normalize_gate_prob_before_dropping: # Normalize gate probabilities gates1_s = (gates * mask1).sum(dim=1) gates2_s = (gates * mask2).sum(dim=1) denom_s = gates1_s + gates2_s # Avoid divide-by-zero denom_s = torch.clamp(denom_s, min=torch.finfo(denom_s.dtype).eps) gates1_s /= denom_s gates2_s /= denom_s if has_tutel: locations1_s = torch.sum(locations1 * mask1_, dim=1) locations2_s = torch.sum(locations2 * mask2_, dim=1) return ( l_aux, metadata, capacity, num_experts, [indices1_s, indices2_s], [locations1_s, locations2_s], [gates1_s, gates2_s], ) # Store the capacity location for each token locations1_s = torch.sum(locations1 * mask1, dim=1) locations2_s = torch.sum(locations2 * mask2, dim=1) # Calculate combine_weights and dispatch_mask gates1 = gates1_s.unsqueeze(-1) * mask1.to(gates1_s.dtype) # einsum("s,se->se") gates2 = gates2_s.unsqueeze(-1) * mask2.to(gates2_s.dtype) # einsum("s,se->se") locations1_sc = one_hot(locations1_s, num_classes=capacity, unsqueeze_indices=True) locations2_sc = one_hot(locations2_s, num_classes=capacity, unsqueeze_indices=True) combine1_sec = torch.bmm( # einsum("se,sc->sec") gates1.unsqueeze(-1), locations1_sc.to(gates1.dtype).unsqueeze(1), ) combine2_sec = torch.bmm( # einsum("se,sc->sec") gates2.unsqueeze(-1), locations2_sc.to(gates2.dtype).unsqueeze(1), ) combine_weights = combine1_sec + combine2_sec dispatch_mask = combine_weights.bool() if use_fp32: return l_aux, combine_weights.to(orig_dtype), dispatch_mask, metadata else: return l_aux, combine_weights, dispatch_mask, metadata class Top2Gate(torch.nn.Module): """Gate module which implements Top2Gating as described in Gshard_. :: gate = Top2Gate(model_dim, num_experts) l_aux, combine_weights, dispatch_mask = gate(input) .. Gshard_: https://arxiv.org/pdf/2006.16668.pdf Args: model_dim (int): size of model embedding dimension num_experts (ints): number of experts in model """ wg: torch.nn.Linear def __init__( self, model_dim: int, num_experts: int, use_fp32=False, second_expert_policy="sampling", normalize_gate_prob_before_dropping=False, moe_eval_capacity_token_fraction=0.25, batch_prioritized_routing=False, use_xmoe=False, ) -> None: super().__init__() if not use_xmoe: self.wg = torch.nn.Linear(model_dim, num_experts, bias=False) else: self.wg_reduction = torch.nn.Linear(model_dim, 16, bias=False) wg = torch.empty(num_experts, 16) torch.nn.init.orthogonal_(wg, gain=0.32) self.register_parameter("wg", torch.nn.Parameter(wg)) self.use_fp32 = use_fp32 self.second_expert_policy = second_expert_policy self.normalize_gate_prob_before_dropping = normalize_gate_prob_before_dropping self.moe_eval_capacity_token_fraction = moe_eval_capacity_token_fraction self.batch_prioritized_routing = batch_prioritized_routing self.use_xmoe = use_xmoe def forward(self, input, mask=None): # type: ignore if self.use_xmoe: input = self.wg_reduction(input) with torch.no_grad(): wg_norm = self.wg.norm(p=2.0, dim=1, keepdim=True) self.wg.mul_(1.5 / wg_norm) logits = self._cosine(input, self.wg) logits = self._make_finite(logits) else: logits = self.wg(input) return top2gating( logits, mask, use_fp32=self.use_fp32, second_expert_policy=self.second_expert_policy, normalize_gate_prob_before_dropping=self.normalize_gate_prob_before_dropping, eval_mode=not self.training, moe_eval_capacity_token_fraction=self.moe_eval_capacity_token_fraction, batch_prioritized_routing=self.batch_prioritized_routing, ) def _cosine(self, mat1, mat2, eps=1e-4): assert mat1.dim() == 2 assert mat2.dim() == 2 # mat1 = F.normalize(mat1, p=2.0, dim=1, eps=eps) mat2 = F.normalize(mat2.float(), p=2.0, dim=1, eps=eps) return mat1.float().matmul(mat2.transpose(0, 1)).type_as(mat1) def _make_finite(self, scores): ok = scores.isfinite() if not ok.all(): # NaNs here can break the assignment algorithm scores[~ok] = scores[ok].min() return scores
KosmosX-API-main
kosmosX/torchscale/torchscale/component/xmoe/routing.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import math import numpy as np import torch import torch.nn as nn from apex.normalization import FusedLayerNorm as LayerNorm from fairscale.nn import checkpoint_wrapper, wrap from torchscale.architecture.utils import init_bert_params from torchscale.component.droppath import DropPath from torchscale.component.feedforward_network import FeedForwardNetwork, make_experts from torchscale.component.multihead_attention import MultiheadAttention from torchscale.component.relative_position_bias import RelativePositionBias from torchscale.component.xmoe.moe_layer import MOELayer from torchscale.component.xmoe.routing import Top1Gate, Top2Gate from torchscale.component.sope_relative_position import SoPE class DecoderLayer(nn.Module): def __init__( self, args, depth, is_moe_layer=False, is_encoder_decoder=False, ): super().__init__() self.args = args self.embed_dim = args.decoder_embed_dim self.dropout_module = torch.nn.Dropout(args.dropout, inplace=True) if args.drop_path_rate > 0: drop_path_prob = np.linspace(0, args.drop_path_rate, args.decoder_layers)[ depth ] self.drop_path = DropPath(drop_path_prob) else: self.drop_path = None self.self_attn = self.build_self_attention(self.embed_dim, args) self.normalize_before = args.decoder_normalize_before self.self_attn_layer_norm = LayerNorm(self.embed_dim) if not is_encoder_decoder: self.encoder_attn = None self.encoder_attn_layer_norm = None else: self.encoder_attn = self.build_encoder_attention(self.embed_dim, args) self.encoder_attn_layer_norm = LayerNorm(self.embed_dim) self.is_moe_layer = is_moe_layer self.ffn_dim = args.decoder_ffn_embed_dim if not self.is_moe_layer: self.ffn = self.build_ffn( self.embed_dim, self.args, ) else: if args.moe_top1_expert: gate = Top1Gate( self.embed_dim, args.moe_expert_count, use_fp32=args.moe_gating_use_fp32, moe_eval_capacity_token_fraction=args.moe_eval_capacity_token_fraction, use_xmoe=args.use_xmoe, ) else: gate = Top2Gate( self.embed_dim, args.moe_expert_count, args.moe_gating_use_fp32, args.moe_second_expert_policy, args.moe_normalize_gate_prob_before_dropping, args.moe_eval_capacity_token_fraction, use_xmoe=args.use_xmoe, ) experts = make_experts(args, self.embed_dim, self.ffn_dim) self.moe_layer = MOELayer(gate, experts, args) self.final_layer_norm = LayerNorm(self.embed_dim) if args.deepnorm: if is_encoder_decoder: self.alpha = math.pow(3.0 * args.decoder_layers, 0.25) else: self.alpha = math.pow(2.0 * args.decoder_layers, 0.25) else: self.alpha = 1.0 def build_ffn(self, embed_dim, args): return FeedForwardNetwork( embed_dim, self.ffn_dim, args.activation_fn, args.dropout, args.activation_dropout, args.subln, ) def build_self_attention(self, embed_dim, args): return MultiheadAttention( args, embed_dim, args.decoder_attention_heads, dropout=args.attention_dropout, self_attention=True, encoder_decoder_attention=False, subln=args.subln, ) def build_encoder_attention(self, embed_dim, args): return MultiheadAttention( args, embed_dim, args.decoder_attention_heads, dropout=args.attention_dropout, self_attention=False, encoder_decoder_attention=True, subln=args.subln, ) def residual_connection(self, x, residual): return residual * self.alpha + x def forward( self, x, encoder_out=None, encoder_padding_mask=None, incremental_state=None, self_attn_mask=None, self_attn_padding_mask=None, self_attn_rel_pos=None, cross_attn_rel_pos=None, self_attn_sope_rel_pos=None, cross_attn_sope_rel_pos=None, ): residual = x if self.normalize_before: x = self.self_attn_layer_norm(x) x, attn = self.self_attn( query=x, key=x, value=x, key_padding_mask=self_attn_padding_mask, incremental_state=incremental_state, attn_mask=self_attn_mask, rel_pos=self_attn_rel_pos, sope_rel_pos=self_attn_sope_rel_pos, ) x = self.dropout_module(x) if self.drop_path is not None: x = self.drop_path(x) x = self.residual_connection(x, residual) if not self.normalize_before: x = self.self_attn_layer_norm(x) if self.encoder_attn is not None and encoder_out is not None: residual = x if self.normalize_before: x = self.encoder_attn_layer_norm(x) x, attn = self.encoder_attn( query=x, key=encoder_out, value=encoder_out, key_padding_mask=encoder_padding_mask, incremental_state=None, rel_pos=cross_attn_rel_pos, sope_rel_pos=cross_attn_sope_rel_pos, ) x = self.dropout_module(x) if self.drop_path is not None: x = self.drop_path(x) x = self.residual_connection(x, residual) if not self.normalize_before: x = self.encoder_attn_layer_norm(x) residual = x if self.normalize_before: x = self.final_layer_norm(x) if not self.is_moe_layer: x = self.ffn(x) l_aux = None else: x = x.transpose(0, 1) x, l_aux = self.moe_layer(x) x = x.transpose(0, 1) if self.drop_path is not None: x = self.drop_path(x) x = self.residual_connection(x, residual) if not self.normalize_before: x = self.final_layer_norm(x) return x, attn, None, l_aux class Decoder(nn.Module): def __init__( self, args, embed_tokens=None, embed_positions=None, output_projection=None, is_encoder_decoder=False, **kwargs ): super().__init__(**kwargs) self.args = args self.dropout_module = torch.nn.Dropout(args.dropout, inplace=True) embed_dim = args.decoder_embed_dim self.embed_dim = embed_dim self.embed_scale = 1.0 if args.no_scale_embedding else math.sqrt(embed_dim) self.embed_tokens = embed_tokens self.embed_positions = embed_positions if ( output_projection is None and not args.no_output_layer and args.vocab_size > 0 ): self.output_projection = self.build_output_projection(args) else: self.output_projection = output_projection if args.layernorm_embedding: self.layernorm_embedding = LayerNorm(embed_dim) else: self.layernorm_embedding = None self.layers = nn.ModuleList([]) moe_freq = args.moe_freq for i in range(args.decoder_layers): is_moe_layer = moe_freq != 0 and (i + 1) % moe_freq == 0 self.layers.append( self.build_decoder_layer( args, depth=i, is_moe_layer=is_moe_layer, is_encoder_decoder=is_encoder_decoder, ) ) self.num_layers = len(self.layers) if args.decoder_normalize_before: self.layer_norm = LayerNorm(embed_dim) else: self.layer_norm = None self.output_projection = output_projection self.self_attn_relative_position = None self.cross_attn_relative_position = None self.self_attn_sope = None self.cross_attn_sope = None if args.sope_rel_pos: assert args.rel_pos_buckets == 0 and args.max_rel_pos == 0 self.self_attn_sope = SoPE( args.decoder_embed_dim // args.decoder_attention_heads ) if is_encoder_decoder: self.cross_attn_sope = SoPE( args.decoder_embed_dim // args.decoder_attention_heads ) if args.rel_pos_buckets > 0 and args.max_rel_pos > 0: self.self_attn_relative_position = RelativePositionBias( num_buckets=args.rel_pos_buckets, max_distance=args.max_rel_pos, n_heads=args.decoder_attention_heads, ) if is_encoder_decoder: self.cross_attn_relative_position = RelativePositionBias( num_buckets=args.rel_pos_buckets, max_distance=args.max_rel_pos, n_heads=args.decoder_attention_heads, ) if args.bert_init: self.apply(init_bert_params) if args.deepnorm: if is_encoder_decoder: init_scale = math.pow(12.0 * args.decoder_layers, 0.25) else: init_scale = math.pow(8.0 * args.decoder_layers, 0.25) for name, p in self.named_parameters(): if ( "fc1" in name or "fc2" in name or "out_proj" in name or "v_proj" in name ): p.data.div_(init_scale) if args.subln: if is_encoder_decoder: init_scale = math.sqrt(math.log(args.decoder_layers * 3)) else: init_scale = math.sqrt(math.log(args.decoder_layers * 2)) for name, p in self.named_parameters(): if "encoder_attn" in name: continue if ( "fc1" in name or "fc2" in name or "out_proj" in name or "v_proj" in name ): p.data.mul_(init_scale) def build_output_projection( self, args, ): if args.share_decoder_input_output_embed: output_projection = torch.nn.Linear( self.embed_tokens.weight.shape[1], self.embed_tokens.weight.shape[0], bias=False, ) output_projection.weight = self.embed_tokens.weight else: output_projection = torch.nn.Linear( args.decoder_embed_dim, args.vocab_size, bias=False ) torch.nn.init.normal_( output_projection.weight, mean=0, std=args.decoder_embed_dim**-0.5 ) return output_projection def build_decoder_layer( self, args, depth, is_moe_layer=False, is_encoder_decoder=False ): layer = DecoderLayer( args, depth, is_moe_layer=is_moe_layer, is_encoder_decoder=is_encoder_decoder, ) if args.checkpoint_activations: layer = checkpoint_wrapper(layer) if args.fsdp: layer = wrap(layer) return layer def forward_embedding( self, tokens, token_embedding=None, incremental_state=None, ): positions = None if self.embed_positions is not None: positions = self.embed_positions( tokens, incremental_state=incremental_state ) if incremental_state is not None: tokens = tokens[:, -1:] if positions is not None: positions = positions[:, -1:] if token_embedding is None: token_embedding = self.embed_tokens(tokens) x = embed = self.embed_scale * token_embedding if positions is not None: x += positions if self.layernorm_embedding is not None: x = self.layernorm_embedding(x) x = self.dropout_module(x) return x, embed def forward( self, prev_output_tokens, self_attn_padding_mask=None, encoder_out=None, incremental_state=None, features_only=False, return_all_hiddens=False, token_embeddings=None, **kwargs ): # embed tokens and positions x, _ = self.forward_embedding( prev_output_tokens, token_embeddings, incremental_state ) x = x.transpose(0, 1) # relative postion self_attn_rel_pos_bias = None self_attn_sope_rel_pos = None slen = prev_output_tokens.size(1) if self.self_attn_sope is not None: offset = 0 if incremental_state is None else incremental_state[0]["prev_key"].shape[2] self_attn_sope_rel_pos = self.self_attn_sope(slen, offset) if self.self_attn_relative_position is not None: self_attn_rel_pos_bias = self.self_attn_relative_position( batch_size=x.size(1), qlen=slen, klen=slen ) if incremental_state is not None: self_attn_rel_pos_bias = self_attn_rel_pos_bias[:, -1:, :] cross_attn_rel_pos_bias = None cross_attn_sope_rel_pos = None if self.cross_attn_sope is not None: cross_attn_sope_rel_pos = self.cross_attn_sope(slen + encoder_out["encoder_out"].size(0)) if self.cross_attn_relative_position is not None: cross_attn_rel_pos_bias = self.cross_attn_relative_position( batch_size=x.size(1), qlen=slen, klen=encoder_out["encoder_out"].size(0), ) if incremental_state is not None: cross_attn_rel_pos_bias = cross_attn_rel_pos_bias[:, -1:, :] # decoder layers inner_states = [x] if encoder_out is None: l_aux = [] else: l_aux = encoder_out["l_aux"] if "l_aux" in encoder_out else [] for idx, layer in enumerate(self.layers): if incremental_state is None: self_attn_mask = torch.triu( torch.zeros([x.size(0), x.size(0)]) .float() .fill_(float("-inf")) .type_as(x), 1, ) else: self_attn_mask = None if idx not in incremental_state: incremental_state[idx] = {} x, layer_attn, _, l_aux_i = layer( x, encoder_out["encoder_out"] if encoder_out is not None else None, encoder_out["encoder_padding_mask"] if encoder_out is not None else None, incremental_state[idx] if incremental_state is not None else None, self_attn_mask=self_attn_mask, self_attn_padding_mask=self_attn_padding_mask, self_attn_rel_pos=self_attn_rel_pos_bias, cross_attn_rel_pos=cross_attn_rel_pos_bias, self_attn_sope_rel_pos=self_attn_sope_rel_pos, cross_attn_sope_rel_pos=cross_attn_sope_rel_pos, ) l_aux.append(l_aux_i) inner_states.append(x) if self.layer_norm is not None: x = self.layer_norm(x) x = x.transpose(0, 1) if not features_only: x = self.output_layer(x) return x, { "inner_states": inner_states, "l_aux": l_aux, "attn": [layer_attn.mean(dim=0)] if layer_attn is not None else None, } def output_layer(self, features): return self.output_projection(features)
KosmosX-API-main
kosmosX/torchscale/torchscale/architecture/decoder.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] class EncoderConfig(object): def __init__(self, **kwargs): self.encoder_embed_dim = kwargs.pop("encoder_embed_dim", 768) self.encoder_attention_heads = kwargs.pop("encoder_attention_heads", 12) self.encoder_ffn_embed_dim = kwargs.pop("encoder_ffn_embed_dim", 3072) self.encoder_layers = kwargs.pop("encoder_layers", 12) self.encoder_normalize_before = kwargs.pop("encoder_normalize_before", True) self.activation_fn = kwargs.pop("activation_fn", "gelu") self.dropout = kwargs.pop("dropout", 0.0) self.drop_path_rate = kwargs.pop("drop_path_rate", 0.0) self.attention_dropout = kwargs.pop("attention_dropout", 0.0) self.activation_dropout = kwargs.pop("activation_dropout", 0.0) self.no_scale_embedding = kwargs.pop("no_scale_embedding", True) self.layernorm_embedding = kwargs.pop("layernorm_embedding", False) self.moe_freq = kwargs.pop("moe_freq", 0) self.moe_top1_expert = kwargs.pop("moe_top1_expert", False) self.moe_expert_count = kwargs.pop("moe_expert_count", 0) self.moe_gating_use_fp32 = kwargs.pop("moe_gating_use_fp32", True) self.moe_eval_capacity_token_fraction = kwargs.pop( "moe_eval_capacity_token_fraction", 0.25 ) self.moe_second_expert_policy = kwargs.pop("moe_second_expert_policy", "random") self.moe_normalize_gate_prob_before_dropping = kwargs.pop( "moe_normalize_gate_prob_before_dropping", False ) self.use_xmoe = kwargs.pop("use_xmoe", False) self.rel_pos_buckets = kwargs.pop("rel_pos_buckets", 0) self.max_rel_pos = kwargs.pop("max_rel_pos", 0) self.deepnorm = kwargs.pop("deepnorm", False) self.subln = kwargs.pop("subln", True) self.bert_init = kwargs.pop("bert_init", False) self.multiway = kwargs.pop("multiway", False) self.share_encoder_input_output_embed = kwargs.pop( "share_encoder_input_output_embed", False ) self.max_source_positions = kwargs.pop("max_source_positions", 1024) self.no_output_layer = kwargs.pop("no_output_layer", False) # Text self.vocab_size = kwargs.pop("vocab_size", -1) # Vision self.img_size = kwargs.pop("img_size", 224) self.patch_size = kwargs.pop("patch_size", 16) self.in_chans = kwargs.pop("in_chans", 3) # Fairscale self.checkpoint_activations = kwargs.pop("checkpoint_activations", False) self.fsdp = kwargs.pop("fsdp", False) self.ddp_rank = kwargs.pop("ddp_rank", 0) self.flash_attention = kwargs.pop("flash_attention", False) self.scale_length = kwargs.pop("scale_length", 2048) if self.deepnorm: self.encoder_normalize_before = False self.subln = False if self.subln: self.encoder_normalize_before = True self.deepnorm = False if self.use_xmoe: self.moe_normalize_gate_prob_before_dropping = True self.moe_second_expert_policy = "random" assert self.moe_freq > 0 and self.moe_expert_count > 0 def override(self, args): for hp in self.__dict__.keys(): if getattr(args, hp, None) is not None: self.__dict__[hp] = getattr(args, hp, None) class DecoderConfig(object): def __init__(self, **kwargs): self.decoder_embed_dim = kwargs.pop("decoder_embed_dim", 768) self.decoder_attention_heads = kwargs.pop("decoder_attention_heads", 12) self.decoder_ffn_embed_dim = kwargs.pop("decoder_ffn_embed_dim", 3072) self.decoder_layers = kwargs.pop("decoder_layers", 12) self.decoder_normalize_before = kwargs.pop("decoder_normalize_before", True) self.activation_fn = kwargs.pop("activation_fn", "gelu") self.dropout = kwargs.pop("dropout", 0.0) self.drop_path_rate = kwargs.pop("drop_path_rate", 0.0) self.attention_dropout = kwargs.pop("attention_dropout", 0.0) self.activation_dropout = kwargs.pop("activation_dropout", 0.0) self.no_scale_embedding = kwargs.pop("no_scale_embedding", True) self.layernorm_embedding = kwargs.pop("layernorm_embedding", False) self.moe_freq = kwargs.pop("moe_freq", 0) self.moe_top1_expert = kwargs.pop("moe_top1_expert", False) self.moe_expert_count = kwargs.pop("moe_expert_count", 0) self.moe_gating_use_fp32 = kwargs.pop("moe_gating_use_fp32", True) self.moe_eval_capacity_token_fraction = kwargs.pop( "moe_eval_capacity_token_fraction", 0.25 ) self.moe_second_expert_policy = kwargs.pop("moe_second_expert_policy", "random") self.moe_normalize_gate_prob_before_dropping = kwargs.pop( "moe_normalize_gate_prob_before_dropping", False ) self.use_xmoe = kwargs.pop("use_xmoe", False) self.rel_pos_buckets = kwargs.pop("rel_pos_buckets", 0) self.max_rel_pos = kwargs.pop("max_rel_pos", 0) self.deepnorm = kwargs.pop("deepnorm", False) self.subln = kwargs.pop("subln", True) self.bert_init = kwargs.pop("bert_init", False) self.multiway = kwargs.pop("multiway", False) self.share_decoder_input_output_embed = kwargs.pop( "share_decoder_input_output_embed", False ) self.max_target_positions = kwargs.pop("max_target_positions", 1024) self.no_output_layer = kwargs.pop("no_output_layer", False) # Text self.vocab_size = kwargs.pop("vocab_size", -1) # Fairscale self.checkpoint_activations = kwargs.pop("checkpoint_activations", False) self.fsdp = kwargs.pop("fsdp", False) self.ddp_rank = kwargs.pop("ddp_rank", 0) self.flash_attention = kwargs.pop("flash_attention", False) self.sope_rel_pos = kwargs.pop("sope_rel_pos", False) self.scale_length = kwargs.pop("scale_length", 2048) if self.deepnorm: self.decoder_normalize_before = False self.subln = False if self.subln: self.decoder_normalize_before = True self.deepnorm = False if self.use_xmoe: self.moe_normalize_gate_prob_before_dropping = True self.moe_second_expert_policy = "random" assert self.moe_freq > 0 and self.moe_expert_count > 0 def override(self, args): for hp in self.__dict__.keys(): if getattr(args, hp, None) is not None: self.__dict__[hp] = getattr(args, hp, None) class EncoderDecoderConfig(object): def __init__(self, **kwargs): self.encoder_embed_dim = kwargs.pop("encoder_embed_dim", 768) self.encoder_attention_heads = kwargs.pop("encoder_attention_heads", 12) self.encoder_ffn_embed_dim = kwargs.pop("encoder_ffn_embed_dim", 3072) self.encoder_layers = kwargs.pop("encoder_layers", 12) self.encoder_normalize_before = kwargs.pop("encoder_normalize_before", True) self.decoder_embed_dim = kwargs.pop("decoder_embed_dim", 768) self.decoder_attention_heads = kwargs.pop("decoder_attention_heads", 12) self.decoder_ffn_embed_dim = kwargs.pop("decoder_ffn_embed_dim", 3072) self.decoder_layers = kwargs.pop("decoder_layers", 12) self.decoder_normalize_before = kwargs.pop("decoder_normalize_before", True) self.activation_fn = kwargs.pop("activation_fn", "gelu") self.dropout = kwargs.pop("dropout", 0.0) self.drop_path_rate = kwargs.pop("drop_path_rate", 0.0) self.attention_dropout = kwargs.pop("attention_dropout", 0.0) self.activation_dropout = kwargs.pop("activation_dropout", 0.0) self.no_scale_embedding = kwargs.pop("no_scale_embedding", True) self.layernorm_embedding = kwargs.pop("layernorm_embedding", False) self.moe_freq = kwargs.pop("moe_freq", 0) self.moe_top1_expert = kwargs.pop("moe_top1_expert", False) self.moe_expert_count = kwargs.pop("moe_expert_count", 0) self.moe_gating_use_fp32 = kwargs.pop("moe_gating_use_fp32", True) self.moe_eval_capacity_token_fraction = kwargs.pop( "moe_eval_capacity_token_fraction", 0.25 ) self.moe_second_expert_policy = kwargs.pop("moe_second_expert_policy", "random") self.moe_normalize_gate_prob_before_dropping = kwargs.pop( "moe_normalize_gate_prob_before_dropping", False ) self.use_xmoe = kwargs.pop("use_xmoe", False) self.rel_pos_buckets = kwargs.pop("rel_pos_buckets", 0) self.max_rel_pos = kwargs.pop("max_rel_pos", 0) self.deepnorm = kwargs.pop("deepnorm", False) self.subln = kwargs.pop("subln", True) self.bert_init = kwargs.pop("bert_init", False) self.multiway = kwargs.pop("multiway", False) self.share_all_embeddings = kwargs.pop("share_all_embeddings", False) self.share_decoder_input_output_embed = kwargs.pop( "share_decoder_input_output_embed", False ) self.max_source_positions = kwargs.pop("max_source_positions", 1024) self.max_target_positions = kwargs.pop("max_target_positions", 1024) self.no_output_layer = kwargs.pop("no_output_layer", False) # Text self.vocab_size = kwargs.pop("vocab_size", -1) # Fairscale self.checkpoint_activations = kwargs.pop("checkpoint_activations", False) self.fsdp = kwargs.pop("fsdp", False) self.ddp_rank = kwargs.pop("ddp_rank", 0) self.flash_attention = kwargs.pop("flash_attention", False) self.sope_rel_pos = kwargs.pop("sope_rel_pos", False) self.scale_length = kwargs.pop("scale_length", 2048) if self.deepnorm: self.encoder_normalize_before = False self.decoder_normalize_before = False self.subln = False if self.subln: self.encoder_normalize_before = True self.decoder_normalize_before = True self.deepnorm = False if self.use_xmoe: self.moe_normalize_gate_prob_before_dropping = True self.moe_second_expert_policy = "random" assert self.moe_freq > 0 and self.moe_expert_count > 0 def override(self, args): for hp in self.__dict__.keys(): if getattr(args, hp, None) is not None: self.__dict__[hp] = getattr(args, hp, None)
KosmosX-API-main
kosmosX/torchscale/torchscale/architecture/config.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import torch.nn as nn from torchscale.architecture.decoder import Decoder from torchscale.architecture.encoder import Encoder class EncoderDecoder(nn.Module): def __init__( self, args, encoder_embed_tokens=None, encoder_embed_positions=None, decoder_embed_tokens=None, decoder_embed_positions=None, output_projection=None, **kwargs ): super().__init__() self.args = args if args.share_all_embeddings: args.share_decoder_input_output_embed = True self.encoder = Encoder( args, encoder_embed_tokens, encoder_embed_positions, is_encoder_decoder=True, **kwargs ) if args.share_all_embeddings and decoder_embed_tokens is None: decoder_embed_tokens = self.encoder.embed_tokens self.decoder = Decoder( args, decoder_embed_tokens, decoder_embed_positions, output_projection, is_encoder_decoder=True, **kwargs ) def forward( self, src_tokens, prev_output_tokens, return_all_hiddens=False, features_only=False, **kwargs ): encoder_out = self.encoder(src_tokens, return_all_hiddens=return_all_hiddens) decoder_out = self.decoder( prev_output_tokens, encoder_out=encoder_out, features_only=features_only, return_all_hiddens=return_all_hiddens, ) return decoder_out
KosmosX-API-main
kosmosX/torchscale/torchscale/architecture/encoder_decoder.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details]
KosmosX-API-main
kosmosX/torchscale/torchscale/architecture/__init__.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import math import numpy as np import torch import torch.nn as nn from apex.normalization import FusedLayerNorm as LayerNorm from fairscale.nn import checkpoint_wrapper, wrap from torchscale.architecture.utils import init_bert_params from torchscale.component.droppath import DropPath from torchscale.component.feedforward_network import FeedForwardNetwork, make_experts from torchscale.component.multihead_attention import MultiheadAttention from torchscale.component.multiway_network import MultiwayWrapper, set_split_position from torchscale.component.relative_position_bias import RelativePositionBias from torchscale.component.xmoe.moe_layer import MOELayer from torchscale.component.xmoe.routing import Top1Gate, Top2Gate class EncoderLayer(nn.Module): def __init__(self, args, depth, is_moe_layer=False, is_encoder_decoder=False): super().__init__() self.args = args self.embed_dim = args.encoder_embed_dim self.self_attn = self.build_self_attention(self.embed_dim, args) self.self_attn_layer_norm = MultiwayWrapper(args, LayerNorm(self.embed_dim)) self.dropout_module = torch.nn.Dropout(args.dropout, inplace=True) if args.drop_path_rate > 0: drop_path_prob = np.linspace(0, args.drop_path_rate, args.encoder_layers)[ depth ] self.drop_path = DropPath(drop_path_prob) else: self.drop_path = None self.normalize_before = args.encoder_normalize_before self.is_moe_layer = is_moe_layer self.ffn_dim = args.encoder_ffn_embed_dim if not self.is_moe_layer: self.ffn = MultiwayWrapper( args, self.build_ffn( self.embed_dim, self.args, ), ) else: assert not self.args.multiway if args.moe_top1_expert: gate = Top1Gate( self.embed_dim, args.moe_expert_count, use_fp32=args.moe_gating_use_fp32, moe_eval_capacity_token_fraction=args.moe_eval_capacity_token_fraction, use_xmoe=args.use_xmoe, ) else: gate = Top2Gate( self.embed_dim, args.moe_expert_count, args.moe_gating_use_fp32, args.moe_second_expert_policy, args.moe_normalize_gate_prob_before_dropping, args.moe_eval_capacity_token_fraction, use_xmoe=args.use_xmoe, ) experts = make_experts(args, self.embed_dim, self.ffn_dim) self.moe_layer = MOELayer(gate, experts, args) self.final_layer_norm = MultiwayWrapper(args, LayerNorm(self.embed_dim)) if args.deepnorm: if is_encoder_decoder: self.alpha = ( math.pow( math.pow(args.encoder_layers, 4) * args.decoder_layers, 0.0625 ) * 0.81 ) else: self.alpha = math.pow(2.0 * args.encoder_layers, 0.25) else: self.alpha = 1.0 def build_ffn(self, embed_dim, args): return FeedForwardNetwork( embed_dim, self.ffn_dim, args.activation_fn, args.dropout, args.activation_dropout, args.subln, ) def build_self_attention(self, embed_dim, args): return MultiheadAttention( args, embed_dim, args.encoder_attention_heads, dropout=args.attention_dropout, self_attention=True, encoder_decoder_attention=False, subln=args.subln, ) def residual_connection(self, x, residual): return residual * self.alpha + x def forward(self, x, encoder_padding_mask, attn_mask=None, rel_pos=None): if attn_mask is not None: attn_mask = attn_mask.masked_fill(attn_mask.to(torch.bool), -1e8) residual = x if self.normalize_before: x = self.self_attn_layer_norm(x) x, _ = self.self_attn( query=x, key=x, value=x, key_padding_mask=encoder_padding_mask, attn_mask=attn_mask, rel_pos=rel_pos, ) x = self.dropout_module(x) if self.drop_path is not None: x = self.drop_path(x) x = self.residual_connection(x, residual) if not self.normalize_before: x = self.self_attn_layer_norm(x) residual = x if self.normalize_before: x = self.final_layer_norm(x) if not self.is_moe_layer: x = self.ffn(x) l_aux = None else: x = x.transpose(0, 1) x, l_aux = self.moe_layer(x) x = x.transpose(0, 1) if self.drop_path is not None: x = self.drop_path(x) x = self.residual_connection(x, residual) if not self.normalize_before: x = self.final_layer_norm(x) return x, l_aux class Encoder(nn.Module): def __init__( self, args, embed_tokens=None, embed_positions=None, output_projection=None, is_encoder_decoder=False, **kwargs ): self.args = args super().__init__(**kwargs) self.dropout_module = torch.nn.Dropout(args.dropout, inplace=True) embed_dim = args.encoder_embed_dim self.embed_scale = 1.0 if args.no_scale_embedding else math.sqrt(embed_dim) self.embed_tokens = embed_tokens self.embed_positions = embed_positions if ( output_projection is None and not is_encoder_decoder and not args.no_output_layer and args.vocab_size > 0 ): self.output_projection = self.build_output_projection(args) else: self.output_projection = output_projection if args.layernorm_embedding: self.layernorm_embedding = MultiwayWrapper( args, LayerNorm(embed_dim), dim=1 ) else: self.layernorm_embedding = None self.layers = nn.ModuleList([]) moe_freq = args.moe_freq for i in range(args.encoder_layers): is_moe_layer = moe_freq != 0 and (i + 1) % moe_freq == 0 self.layers.append( self.build_encoder_layer( args, depth=i, is_moe_layer=is_moe_layer, is_encoder_decoder=is_encoder_decoder, ) ) self.num_layers = len(self.layers) if args.encoder_normalize_before: self.layer_norm = MultiwayWrapper(args, LayerNorm(embed_dim)) else: self.layer_norm = None if args.rel_pos_buckets > 0 and args.max_rel_pos > 0: self.relative_position = RelativePositionBias( num_buckets=args.rel_pos_buckets, max_distance=args.max_rel_pos, n_heads=args.encoder_attention_heads, ) else: self.relative_position = None if args.bert_init: self.apply(init_bert_params) if args.deepnorm: if is_encoder_decoder: init_scale = ( math.pow( math.pow(args.encoder_layers, 4) * args.decoder_layers, 0.0625 ) / 1.15 ) else: init_scale = math.pow(8.0 * args.encoder_layers, 0.25) for name, p in self.named_parameters(): if ( "fc1" in name or "fc2" in name or "out_proj" in name or "v_proj" in name ): p.data.div_(init_scale) if args.subln: if is_encoder_decoder: init_scale = math.sqrt( math.log(3 * args.decoder_layers) * math.log(2 * args.encoder_layers) / 3 ) else: init_scale = math.sqrt(math.log(args.encoder_layers * 2)) for name, p in self.named_parameters(): if ( "fc1" in name or "fc2" in name or "out_proj" in name or "v_proj" in name ): p.data.mul_(init_scale) def build_output_projection( self, args, ): if args.share_encoder_input_output_embed: assert args.encoder_embedding_type == "language" output_projection = torch.nn.Linear( self.embed_tokens.weight.shape[1], self.embed_tokens.weight.shape[0], bias=False, ) output_projection.weight = self.embed_tokens.weight else: output_projection = torch.nn.Linear( args.encoder_embed_dim, args.vocab_size, bias=False ) torch.nn.init.normal_( output_projection.weight, mean=0, std=args.encoder_embed_dim**-0.5 ) return output_projection def build_encoder_layer( self, args, depth, is_moe_layer=False, is_encoder_decoder=False ): layer = EncoderLayer( args, depth, is_moe_layer=is_moe_layer, is_encoder_decoder=is_encoder_decoder, ) if args.checkpoint_activations: layer = checkpoint_wrapper(layer) if args.fsdp: layer = wrap(layer) return layer def forward_embedding( self, src_tokens, token_embedding=None, ): if token_embedding is None: token_embedding = self.embed_tokens(src_tokens) x = embed = self.embed_scale * token_embedding if self.embed_positions is not None: if src_tokens is not None: x = embed + self.embed_positions(src_tokens) else: x = embed + self.embed_positions(x) if self.layernorm_embedding is not None: x = self.layernorm_embedding(x) x = self.dropout_module(x) return x, embed def forward( self, src_tokens, encoder_padding_mask=None, return_all_hiddens=False, token_embeddings=None, multiway_split_position=None, features_only=False, **kwargs ): assert src_tokens is not None or token_embeddings is not None if encoder_padding_mask is None: if src_tokens is not None: encoder_padding_mask = torch.zeros_like( src_tokens, device=src_tokens.device ).bool() else: encoder_padding_mask = torch.zeros( [token_embeddings.size(0), token_embeddings.size(1)], device=token_embeddings.device, ).bool() if multiway_split_position is not None: assert self.args.multiway self.apply(set_split_position(multiway_split_position)) x, encoder_embedding = self.forward_embedding(src_tokens, token_embeddings) x = x * (1 - encoder_padding_mask.unsqueeze(-1).type_as(x)) x = x.transpose(0, 1) encoder_states = [] if return_all_hiddens: encoder_states.append(x) rel_pos_bias = None if self.relative_position is not None: rel_pos_bias = self.relative_position( batch_size=x.size(1), qlen=x.size(0), klen=x.size(0) ) l_aux = [] for layer in self.layers: x, l_aux_i = layer( x, encoder_padding_mask=encoder_padding_mask, rel_pos=rel_pos_bias ) if return_all_hiddens: assert encoder_states is not None encoder_states.append(x) l_aux.append(l_aux_i) if self.layer_norm is not None: x = self.layer_norm(x) if not features_only and self.output_projection is not None: x = self.output_projection(x) return { "encoder_out": x, "encoder_embedding": encoder_embedding, "encoder_padding_mask": encoder_padding_mask, "encoder_states": encoder_states, "l_aux": l_aux, }
KosmosX-API-main
kosmosX/torchscale/torchscale/architecture/encoder.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import torch.nn as nn from torchscale.component.multihead_attention import MultiheadAttention from torchscale.component.multiway_network import MultiwayNetwork def init_bert_params(module): def normal_(data): data.copy_(data.cpu().normal_(mean=0.0, std=0.02).to(data.device)) if isinstance(module, nn.Linear): normal_(module.weight.data) if module.bias is not None: module.bias.data.zero_() if isinstance(module, nn.Embedding): normal_(module.weight.data) if module.padding_idx is not None: module.weight.data[module.padding_idx].zero_() if isinstance(module, MultiheadAttention): if isinstance(module.q_proj, MultiwayNetwork): normal_(module.q_proj.A.weight.data) normal_(module.q_proj.B.weight.data) normal_(module.k_proj.A.weight.data) normal_(module.k_proj.B.weight.data) normal_(module.v_proj.A.weight.data) normal_(module.v_proj.B.weight.data) else: normal_(module.q_proj.weight.data) normal_(module.k_proj.weight.data) normal_(module.v_proj.weight.data)
KosmosX-API-main
kosmosX/torchscale/torchscale/architecture/utils.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import torch import torch.nn as nn from torchscale.architecture.encoder import Encoder from torchscale.component.embedding import ( PositionalEmbedding, TextEmbedding, VisionEmbedding, ) from torchscale.component.multiway_network import MultiwayWrapper class BEiT3(nn.Module): def __init__(self, args, **kwargs): super().__init__() self.args = args assert args.multiway assert args.vocab_size > 0 assert not args.share_encoder_input_output_embed self.text_embed = TextEmbedding(args.vocab_size, args.encoder_embed_dim) self.vision_embed = VisionEmbedding( args.img_size, args.patch_size, args.in_chans, args.encoder_embed_dim, contain_mask_token=True, prepend_cls_token=True, ) embed_positions = MultiwayWrapper( args, PositionalEmbedding(args.max_source_positions, args.encoder_embed_dim), dim=1, ) self.encoder = Encoder( args, embed_tokens=None, embed_positions=embed_positions, output_projection=None, is_encoder_decoder=False, ) def forward( self, textual_tokens=None, visual_tokens=None, text_padding_position=None, vision_masked_position=None, ): assert textual_tokens is not None or visual_tokens is not None if textual_tokens is None: x = self.vision_embed(visual_tokens, vision_masked_position) encoder_padding_mask = None multiway_split_position = -1 elif visual_tokens is None: x = self.text_embed(textual_tokens) encoder_padding_mask = text_padding_position multiway_split_position = 0 else: x1 = self.vision_embed(visual_tokens, vision_masked_position) multiway_split_position = x1.size(1) x2 = self.text_embed(textual_tokens) x = torch.cat([x1, x2], dim=1) if text_padding_position is not None: encoder_padding_mask = torch.cat( [ torch.zeros(x1.shape[:-1]).to(x1.device).bool(), text_padding_position, ], dim=1, ) else: encoder_padding_mask = None encoder_out = self.encoder( src_tokens=None, encoder_padding_mask=encoder_padding_mask, token_embeddings=x, multiway_split_position=multiway_split_position, ) return encoder_out
KosmosX-API-main
kosmosX/torchscale/torchscale/model/BEiT3.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details]
KosmosX-API-main
kosmosX/torchscale/torchscale/model/__init__.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details]
KosmosX-API-main
kosmosX/torchscale/examples/__init__.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] # flake8: noqa import models import tasks from fairseq_cli.generate import cli_main if __name__ == "__main__": cli_main()
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/generate.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details]
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/__init__.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] # flake8: noqa import models import tasks from fairseq_cli.interactive import cli_main if __name__ == "__main__": cli_main()
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/interactive.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] # flake8: noqa import models import tasks from fairseq_cli.train import cli_main if __name__ == "__main__": cli_main()
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/train.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import json import logging import os from argparse import Namespace # 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 sentencepiece as spm from fairseq import utils from fairseq.data import Dictionary from fairseq.dataclass import ChoiceEnum, FairseqDataclass from fairseq.tasks import FairseqTask, register_task from omegaconf import II, MISSING from .data.mlm_loader import MLMLoader logger = logging.getLogger(__name__) SAMPLE_BREAK_MODE_CHOICES = ChoiceEnum(["none", "complete", "complete_doc", "eos"]) SHORTEN_METHOD_CHOICES = ChoiceEnum(["none", "truncate", "random_crop"]) @dataclass class PretrainingConfig(FairseqDataclass): data: str = field( default=MISSING, metadata={ "help": "colon separated path to data directories list, \ will be iterated upon during epochs in round-robin manner" }, ) sample_break_mode: SAMPLE_BREAK_MODE_CHOICES = field( default="complete", metadata={ "help": 'If omitted or "none", fills each sample with tokens-per-sample ' 'tokens. If set to "complete", splits samples only at the end ' "of sentence, but may include multiple sentences per sample. " '"complete_doc" is similar but respects doc boundaries. ' 'If set to "eos", includes only one sentence per sample.' }, ) tokens_per_sample: int = field( default=1024, metadata={"help": "max number of tokens per sample for LM dataset"}, ) mask_prob: float = field( default=0.15, metadata={"help": "probability of replacing a token with mask"}, ) leave_unmasked_prob: float = field( default=0.1, metadata={"help": "probability that a masked token is unmasked"}, ) random_token_prob: float = field( default=0.1, metadata={"help": "probability of replacing a token with a random token"}, ) freq_weighted_replacement: bool = field( default=False, metadata={"help": "sample random replacement words based on word frequencies"}, ) mask_whole_words: bool = field( default=False, metadata={"help": "mask whole words; you may also want to set --bpe"}, ) mask_multiple_length: int = field( default=1, metadata={"help": "repeat the mask indices multiple times"}, ) mask_stdev: float = field( default=0.0, metadata={"help": "stdev of the mask length"}, ) shorten_method: SHORTEN_METHOD_CHOICES = field( default="none", metadata={ "help": "if not none, shorten sequences that exceed --tokens-per-sample" }, ) shorten_data_split_list: str = field( default="", metadata={ "help": "comma-separated list of dataset splits to apply shortening to, " 'e.g., "train,valid" (default: all dataset splits)' }, ) seed: int = II("common.seed") span_length: float = field( default=3.0, metadata={"help": "average span length for masking"}, ) remove_source_sentinel: bool = field( default=False, metadata={"help": "remove the source sentinel for the span corruption task"}, ) remove_target_sentinel: bool = field( default=False, metadata={"help": "remove the target sentinel for the span corruption task"}, ) batch_read_ahead: int = field( default=100000, metadata={"help": "batch read ahead size for infinibatch"}, ) required_batch_size_multiple: int = II("dataset.required_batch_size_multiple") spm_model: str = field( default="", metadata={"help": "sentencepice model to tokenize the data"}, ) dict_file: str = field( default="", metadata={"help": ""}, ) @register_task("pretraining", dataclass=PretrainingConfig) class PLMTask(FairseqTask): def __init__(self, cfg, dictionary, tokenizer): super().__init__(cfg) self.cfg = cfg self.dictionary = dictionary self.tokenizer = tokenizer self.seed = cfg.seed self.mask_idx = dictionary.index("<mask>") @classmethod def setup_task(cls, cfg, **kwargs): paths = utils.split_paths(cfg.data) assert len(paths) > 0 if cfg.dict_file != "": dictionary = Dictionary.load(cfg.dict_file) else: dictionary = Dictionary.load(os.path.join(paths[0], "dict.txt")) # add mask token dictionary.add_symbol("<mask>") for i in range(100): dictionary.add_symbol(f"<mask_{i}>") dictionary.pad_to_multiple_(cfg.required_batch_size_multiple) logger.info("dictionary: {} types".format(len(dictionary))) # tokenizer = SentencepieceBPE(Namespace(sentencepiece_model=cfg.spm_model)) tokenizer = spm.SentencePieceProcessor() tokenizer.Load(cfg.spm_model) return cls(cfg, dictionary, tokenizer) def load_dataset(self, split, epoch=1, combine=False, **kwargs): self.datasets[split] = { "data": json.load(open(f"{self.cfg.data}/json/{split}.json")), "data_dir": self.cfg.data, "shuffle": True if split == "train" else False, } self.datasets[split] = Namespace(**self.datasets[split]) def dataset(self, split): if split not in self.datasets: raise KeyError("Dataset not loaded: " + split) return self.datasets[split] def get_batch_iterator( self, dataset, max_tokens=None, max_sentences=None, max_positions=None, ignore_invalid_inputs=False, required_batch_size_multiple=1, seed=1, num_shards=1, shard_id=0, num_workers=0, epoch=1, data_buffer_size=0, disable_iterator_cache=False, ): return MLMLoader( self.cfg, dataset, self.dictionary, self.tokenizer, max_tokens=max_tokens, max_sentences=max_sentences, max_positions=max_positions, ignore_invalid_inputs=ignore_invalid_inputs, required_batch_size_multiple=required_batch_size_multiple, seed=seed, num_shards=num_shards, shard_id=shard_id, ) @property def source_dictionary(self): return self.dictionary @property def target_dictionary(self): return self.dictionary
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/tasks/pretraining.py
import os import json from argparse import Namespace import torch from fairseq import utils from fairseq.data import Dictionary from fairseq.tasks import register_task from fairseq.tasks.language_modeling import LanguageModelingTask, LanguageModelingConfig from fairseq.data.encoders.gpt2_bpe import GPT2BPE from dataclasses import dataclass, field import sentencepiece from .data.spm_lm_loader import SpmLmLoader as LMLoader from .data.utils import EOL_SYMBOL DEFAULT_ENCODER_JSON = "https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/encoder.json" DEFAULT_VOCAB_BPE = "https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/vocab.bpe" @dataclass class GPTLanguageModelingConfig(LanguageModelingConfig): spm_model: str = field( default="", metadata={ "help": "sentencepice model to tokenize the data" }, ) gpt2_encoder_json: str = field( default=DEFAULT_ENCODER_JSON, metadata={"help": "path to encoder.json"} ) gpt2_vocab_bpe: str = field( default=DEFAULT_VOCAB_BPE, metadata={"help": "path to vocab.bpe"} ) dict_path: str = field( default="", metadata={ "help": "sentencepice model to tokenize the data" }, ) batch_read_ahead: int = field( default=10000, metadata={"help": "batch read ahead size for infinibatch"}, ) pad_to_max_len: bool = field( default=False, metadata={"help": "pad each sentence to max length"}, ) @register_task('gpt_pretraining', dataclass=GPTLanguageModelingConfig) class GPTPretrainingTask(LanguageModelingTask): def __init__(self, args, dictionary, tokenizer, output_dictionary=None, targets=None): super().__init__(args, dictionary, output_dictionary=output_dictionary, targets=targets) self.cfg = args self.tokenizer = tokenizer @classmethod def setup_task(cls, cfg, **kwargs): """Setup the task (e.g., load dictionaries). Args: args (argparse.Namespace): parsed command-line arguments """ paths = utils.split_paths(cfg.data) assert len(paths) > 0 if len(cfg.dict_path) > 0: dictionary = Dictionary.load(cfg.dict_path) else: dictionary = Dictionary.load(os.path.join(paths[0], "dict.txt")) dictionary.add_symbol(EOL_SYMBOL) output_dictionary = dictionary args = cfg # upgrade old checkpoints if getattr(args, "exclude_self_target", False): args.self_target = False targets = [] if getattr(args, "self_target", False): targets.append("self") if getattr(args, "future_target", False): targets.append("future") if getattr(args, "past_target", False): targets.append("past") if len(targets) == 0: # standard language modeling targets = ["future"] if len(cfg.spm_model) > 0: tokenizer = sentencepiece.SentencePieceProcessor(model_file=cfg.spm_model) else: tokenizer = GPT2BPE(Namespace( gpt2_vocab_bpe=cfg.gpt2_vocab_bpe, gpt2_encoder_json=cfg.gpt2_encoder_json)) return cls(cfg, dictionary, tokenizer, output_dictionary, targets=targets) def load_dataset(self, split, epoch=1, combine=False, **kwargs): if "tnlg" in self.cfg.data and split == "train": self.datasets[split] = { # 'data': json.load(open(f'{self.cfg.data}/json/{split}-nogithub.json')) if split == 'train' else json.load(open(f'{self.cfg.data}/json/{split}.json')), # 'data': json.load(open(f'{self.cfg.data}/json/{split}-nogithub-noarvix-nopubmed.json')) if split == 'train' else json.load(open(f'{self.cfg.data}/json/{split}.json')), 'data': json.load(open(f'{self.cfg.data}/json/{split}-nogithub-noarvix-nopubmed-mtnlg.json')) if split == 'train' else json.load(open(f'{self.cfg.data}/json/{split}.json')), 'data_dir': self.cfg.data, 'shuffle': True if split == 'train' else False, } else: self.datasets[split] = { 'data': json.load(open(f'{self.cfg.data}/json/{split}.json')), 'data_dir': self.cfg.data, 'shuffle': True if split == 'train' else False, } self.datasets[split] = Namespace(**self.datasets[split]) def dataset(self, split): if split not in self.datasets: raise KeyError("Dataset not loaded: " + split) return self.datasets[split] def get_batch_iterator( self, dataset, max_tokens=None, max_sentences=None, max_positions=None, ignore_invalid_inputs=False, required_batch_size_multiple=1, seed=1, num_shards=1, shard_id=0, num_workers=0, epoch=1, data_buffer_size=0, disable_iterator_cache=False, skip_remainder_batch=False, grouped_shuffling=False, update_epoch_batch_itr=False ): disable_prefetching = False if not dataset.shuffle: # for valid and test shard_id = 0 disable_prefetching = True return LMLoader( self.cfg, dataset, self.dictionary, self.tokenizer, max_tokens=max_tokens, max_sentences=max_sentences, max_positions=max_positions, ignore_invalid_inputs=ignore_invalid_inputs, required_batch_size_multiple=required_batch_size_multiple, seed=seed, epoch=epoch, num_shards=num_shards, shard_id=shard_id, disable_prefetching=disable_prefetching, ) @property def source_dictionary(self): return self.dictionary @property def target_dictionary(self): return self.dictionary def train_step( self, sample, model, criterion, optimizer, update_num, ignore_grad=False ): """ Do forward and backward, and return the loss as computed by *criterion* for the given *model* and *sample*. Args: sample (dict): the mini-batch. The format is defined by the :class:`~fairseq.data.FairseqDataset`. model (~fairseq.models.BaseFairseqModel): the model criterion (~fairseq.criterions.FairseqCriterion): the criterion optimizer (~fairseq.optim.FairseqOptimizer): the optimizer update_num (int): the current update ignore_grad (bool): multiply loss by 0 if this is set to True Returns: tuple: - the loss - the sample size, which is used as the denominator for the gradient - logging outputs to display while training """ model.train() model.set_num_updates(update_num) with torch.autograd.profiler.record_function("forward"): loss, sample_size, logging_output = criterion(model, sample['gpt']) if ignore_grad: loss *= 0 with torch.autograd.profiler.record_function("backward"): 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 = criterion(model, sample['gpt']) return loss, sample_size, logging_output
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/tasks/gpt_base.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import argparse import importlib import os # register dataclass TASK_DATACLASS_REGISTRY = {} TASK_REGISTRY = {} TASK_CLASS_NAMES = set() # automatically import any Python files in the tasks/ directory tasks_dir = os.path.dirname(__file__) for file in os.listdir(tasks_dir): path = os.path.join(tasks_dir, file) if ( not file.startswith("_") and not file.startswith(".") and (file.endswith(".py") or os.path.isdir(path)) ): task_name = file[: file.find(".py")] if file.endswith(".py") else file module = importlib.import_module("tasks." + task_name) # expose `task_parser` for sphinx if task_name in TASK_REGISTRY: parser = argparse.ArgumentParser(add_help=False) group_task = parser.add_argument_group("Task name") # fmt: off group_task.add_argument('--task', metavar=task_name, help='Enable this task with: ``--task=' + task_name + '``') # fmt: on group_args = parser.add_argument_group("Additional command-line arguments") TASK_REGISTRY[task_name].add_args(group_args) globals()[task_name + "_parser"] = parser
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/tasks/__init__.py
import os import json from argparse import Namespace import torch from fairseq import utils from fairseq.data import Dictionary from fairseq.tasks import register_task from fairseq.tasks.language_modeling import LanguageModelingTask from fairseq.data.encoders.gpt2_bpe import GPT2BPE from dataclasses import dataclass, field import sentencepiece from .data.spm_lm_loader import SpmLmLoader as LMLoader from .data.laion_loader import LaionLoader from .data.wild_loader import WildLoader from .data.utils import EOL_SYMBOL, BOI_SYMBOL, EOI_SYMBOL, image_code_to_token from .data.basic_loader import ConcatLoader from .gpt_base import GPTLanguageModelingConfig DEFAULT_ENCODER_JSON = "https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/encoder.json" DEFAULT_VOCAB_BPE = "https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/vocab.bpe" IMAGE_COOEBOOK_SIZE = 8192 @dataclass class VLGPTLanguageModelingConfig(GPTLanguageModelingConfig): wild_data_dir: str = field(default="", metadata={"help": ""}) wild_batch_size: int = field(default=4, metadata={"help": ""}) laion_data_dir: str = field(default="", metadata={"help": ""}) laion_batch_size: int = field(default=32, metadata={"help": ""}) @register_task('vl_gpt_pretraining', dataclass=VLGPTLanguageModelingConfig) class VLGPTPretrainingTask(LanguageModelingTask): def __init__(self, args, dictionary, tokenizer, output_dictionary=None, targets=None): super().__init__(args, dictionary, output_dictionary=output_dictionary, targets=targets) self.cfg = args self.tokenizer = tokenizer @classmethod def setup_task(cls, cfg, **kwargs): """Setup the task (e.g., load dictionaries). Args: args (argparse.Namespace): parsed command-line arguments """ paths = utils.split_paths(cfg.data) assert len(paths) > 0 if len(cfg.dict_path) > 0: dictionary = Dictionary.load(cfg.dict_path) else: dictionary = Dictionary.load(os.path.join(paths[0], "dict.txt")) dictionary.add_symbol(EOL_SYMBOL) dictionary.add_symbol(BOI_SYMBOL) dictionary.add_symbol(EOI_SYMBOL) for i in range(IMAGE_COOEBOOK_SIZE): dictionary.add_symbol(image_code_to_token(i)) print('| dictionary: {} types'.format(len(dictionary))) output_dictionary = dictionary args = cfg # upgrade old checkpoints if getattr(args, "exclude_self_target", False): args.self_target = False targets = [] if getattr(args, "self_target", False): targets.append("self") if getattr(args, "future_target", False): targets.append("future") if getattr(args, "past_target", False): targets.append("past") if len(targets) == 0: # standard language modeling targets = ["future"] if len(cfg.spm_model) > 0: tokenizer = sentencepiece.SentencePieceProcessor(model_file=cfg.spm_model) else: tokenizer = GPT2BPE(Namespace( gpt2_vocab_bpe=cfg.gpt2_vocab_bpe, gpt2_encoder_json=cfg.gpt2_encoder_json)) return cls(cfg, dictionary, tokenizer, output_dictionary, targets=targets) def load_dataset(self, split, epoch=1, combine=False, **kwargs): if "tnlg" in self.cfg.data and split == "train": self.datasets[split] = { # 'data': json.load(open(f'{self.cfg.data}/json/{split}-nogithub.json')) if split == 'train' else json.load(open(f'{self.cfg.data}/json/{split}.json')), # 'data': json.load(open(f'{self.cfg.data}/json/{split}-nogithub-noarvix-nopubmed.json')) if split == 'train' else json.load(open(f'{self.cfg.data}/json/{split}.json')), 'data': json.load(open(f'{self.cfg.data}/json/{split}-nogithub-noarvix-nopubmed-mtnlg.json')) if split == 'train' else json.load(open(f'{self.cfg.data}/json/{split}.json')), 'data_dir': self.cfg.data, 'shuffle': True if split == 'train' else False, } else: self.datasets[split] = { 'data': json.load(open(f'{self.cfg.data}/json/{split}.json')), 'data_dir': self.cfg.data, 'shuffle': True if split == 'train' else False, } self.datasets[split] = Namespace(**self.datasets[split]) def dataset(self, split): if split not in self.datasets: raise KeyError("Dataset not loaded: " + split) return self.datasets[split] def get_batch_iterator( self, dataset, max_tokens=None, max_sentences=None, max_positions=None, ignore_invalid_inputs=False, required_batch_size_multiple=1, seed=1, num_shards=1, shard_id=0, num_workers=0, epoch=1, data_buffer_size=0, disable_iterator_cache=False, skip_remainder_batch=False, grouped_shuffling=False, update_epoch_batch_itr=False ): data_loader_list = [] disable_prefetching = False config_split = 'train' if not dataset.shuffle: # for valid and test shard_id = 0 disable_prefetching = True config_split = 'valid' if self.cfg.wild_data_dir: wild_dataset = Namespace(**{ 'data': json.load(open(f'{self.cfg.wild_data_dir}/json/{config_split}.json')), 'data_dir': self.cfg.wild_data_dir, 'shuffle': dataset.shuffle}) wild_vl_loader = WildLoader( self.cfg, wild_dataset, self.dictionary, self.tokenizer, max_tokens=max_tokens, max_sentences=max_sentences, max_positions=max_positions, ignore_invalid_inputs=ignore_invalid_inputs, required_batch_size_multiple=required_batch_size_multiple, seed=seed, epoch=epoch, num_shards=num_shards, shard_id=shard_id, disable_prefetching=disable_prefetching, data_name='wild' ) data_loader_list.append(wild_vl_loader) if self.cfg.laion_data_dir: laion_dataset = Namespace(**{ 'data': json.load(open(f'{self.cfg.laion_data_dir}/json/{config_split}.json')), 'data_dir': self.cfg.laion_data_dir, 'shuffle': dataset.shuffle}) lain_vl_loader = LaionLoader( self.cfg, laion_dataset, self.dictionary, self.tokenizer, max_tokens=max_tokens, max_sentences=self.cfg.laion_batch_size, max_positions=max_positions, ignore_invalid_inputs=ignore_invalid_inputs, required_batch_size_multiple=required_batch_size_multiple, seed=seed, epoch=epoch, num_shards=num_shards, shard_id=shard_id, disable_prefetching=disable_prefetching, data_name='laion' ) data_loader_list.append(lain_vl_loader) lm_loader = LMLoader( self.cfg, dataset, self.dictionary, self.tokenizer, max_tokens=max_tokens, max_sentences=max_sentences, max_positions=max_positions, ignore_invalid_inputs=ignore_invalid_inputs, required_batch_size_multiple=required_batch_size_multiple, seed=seed, epoch=epoch, num_shards=num_shards, shard_id=shard_id, disable_prefetching=disable_prefetching, ) data_loader_list.append(lm_loader) concat_loader = ConcatLoader(data_loader_list) return concat_loader @property def source_dictionary(self): return self.dictionary @property def target_dictionary(self): return self.dictionary def train_step( self, sample, model, criterion, optimizer, update_num, ignore_grad=False ): """ Do forward and backward, and return the loss as computed by *criterion* for the given *model* and *sample*. Args: sample (dict): the mini-batch. The format is defined by the :class:`~fairseq.data.FairseqDataset`. model (~fairseq.models.BaseFairseqModel): the model criterion (~fairseq.criterions.FairseqCriterion): the criterion optimizer (~fairseq.optim.FairseqOptimizer): the optimizer update_num (int): the current update ignore_grad (bool): multiply loss by 0 if this is set to True Returns: tuple: - the loss - the sample size, which is used as the denominator for the gradient - logging outputs to display while training """ model.train() model.set_num_updates(update_num) agg_loss, agg_sample_size, agg_logging_output = 0., 0., {} with torch.autograd.profiler.record_function("forward"): loss, sample_size, logging_output = criterion(model, sample['gpt'], loss_name='gpt') if ignore_grad: loss *= 0 with torch.autograd.profiler.record_function("backward"): optimizer.backward(loss) agg_loss += loss.detach().item() agg_sample_size += sample_size agg_logging_output.update(logging_output) if 'laion' in sample: with torch.autograd.profiler.record_function("forward"): loss, sample_size, logging_output = criterion(model, sample['laion'], loss_name='laion') if ignore_grad: loss *= 0 with torch.autograd.profiler.record_function("backward"): optimizer.backward(loss) agg_loss += loss.detach().item() agg_sample_size += sample_size for key, value in logging_output.items(): if key not in agg_logging_output: agg_logging_output[key] = value else: agg_logging_output[key] += value if 'wild' in sample: with torch.autograd.profiler.record_function("forward"): loss, sample_size, logging_output = criterion(model, sample['wild'], loss_name='wild') if ignore_grad: loss *= 0 with torch.autograd.profiler.record_function("backward"): optimizer.backward(loss) agg_loss += loss.detach().item() agg_sample_size += sample_size for key, value in logging_output.items(): if key not in agg_logging_output: agg_logging_output[key] = value else: agg_logging_output[key] += value return agg_loss, agg_sample_size, agg_logging_output def valid_step(self, sample, model, criterion): model.eval() with torch.no_grad(): loss, sample_size, logging_output = criterion(model, sample['gpt']) return loss, sample_size, logging_output
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/tasks/vl_gpt_base.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import torch from infinibatch.iterators import CheckpointableIterator from . import utils from .utils import ConcatIterator class BaseBatchGen(CheckpointableIterator): """ This is a base class for batch generators that use infinibatch """ def __init__(self): self._iter = None self.epoch = 1 self.next_epoch_idx = 1 self.sharded_checkpoint = False self.should_close_after_finished = True def _build_iter(self): """ Build infinibatch iterator and assign to self._iter """ raise NotImplementedError() def _move_to_tensor(self, batch): def to_tensor(x): return torch.tensor(x) return utils.apply_to_sample(to_tensor, batch) @property def iterator(self): if self._iter is None: raise NotImplementedError("_build_iter() must called first") return self._iter def __iter__(self): if self._iter is None: raise NotImplementedError("_build_iter() must called first") return self._iter def __next__(self): return next(self._iter) def setstate(self, value): self._iter.setstate(value) def getstate(self): return self._iter.getstate() def close(self): self._iter.close() def __len__(self) -> int: return 819200000 def next_epoch_itr( self, shuffle=True, fix_batches_to_gpus=False, set_dataset_epoch=True ): return self def end_of_epoch(self) -> bool: return False def state_dict(self): """Returns a dictionary containing a whole state of the iterator.""" return self.getstate() def load_state_dict(self, state_dict): """Copies the state of the iterator from the given *state_dict*.""" self.setstate(state_dict) @property def first_batch(self): return "DUMMY" class ConcatLoader(BaseBatchGen): def __init__(self, dataloaders): super().__init__() self.dataloaders = list(dataloaders) self._build_iter() def _build_iter(self): """ Build infinibatch iterator and assign to self._iter """ self._iter = ConcatIterator([dataloader.iterator for dataloader in self.dataloaders])
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/tasks/data/basic_loader.py
import sys,os sys.path.append(os.getcwd()) from typing import NamedTuple import os import argparse import json import sentencepiece as spm # from fairseq.data.dictionary import Dictionary # from laion_loader import LaionLoader def image_code_to_token(code): return "<image{}>".format(code) def to_word(item, dictionary): print(dictionary.string(item['net_input']['src_tokens'][0])) print(dictionary.string(item['target'][0])) def run(): parser = argparse.ArgumentParser() parser.add_argument('--seed', type=int, default=1) parser.add_argument('--data', type=str, default='/mnt/msranlp/shumma/data/16g') parser.add_argument('--spm_path', type=str, default='/mnt/msranlp/shumma/data/16g/sentencepiece.bpe.model') parser.add_argument('--tokens_per_sample', type=int, default=2048) parser.add_argument('--sample_break_mode', type=str, default='') parser.add_argument('--batch_read_ahead', type=int, default=1) parser.add_argument('--mask_prob', type=float, default=0.15) parser.add_argument('--span_length', type=int, default=3) parser.add_argument('--dynamic_mask', default=True) parser.add_argument('--max_sentences', type=int, default=1) # batch size parser.add_argument('--max_image_num', type=int, default=5) parser.add_argument('--image_token_length', type=int, default=64) args = parser.parse_args() Dataset = NamedTuple('Dataset', [('data', str), ('data_dir', str), ('shuffle', bool)]) dataset = Dataset(json.load(open(f'{args.data}/json/train.json')), args.data, True) dictionary = Dictionary.load(os.path.join(args.data, 'dict.txt')) dictionary.add_symbol('</line>') dictionary.add_symbol('<image>') dictionary.add_symbol('</image>') for i in range(8192): dictionary.add_symbol(image_code_to_token(i)) tokenizer = spm.SentencePieceProcessor(model_file=args.spm_path) mlm_loader = LaionLoader( args, dataset, dictionary, tokenizer, max_tokens=args.tokens_per_sample, max_sentences=args.max_sentences, max_positions=None, ignore_invalid_inputs=False, required_batch_size_multiple=1, seed=1, num_shards=1, shard_id=0, disable_prefetching=True, ) num = 0 i = 0 for item in mlm_loader: print(item) i += 1 if i > num: break # for item in tqdm.tqdm(mlm_loader): # i += 1 def cook_json(): data = [] item = { "source": [], "source_lang": "wild", "weight": 1.0, "name": "wild" } for i in range(7190): item['source'].append("../nemavq2_encoder_base_decoder_centercrop_wild/partition.{:03d}.ndjson".format(i)) data.append(item) json.dump(data, open('train.json', 'w', encoding='utf-8'), indent=2) # def cook_json(): # data = [] # item = { # "source": [], # "source_lang": "laion", # "weight": 1.0, # "name": "laion" # } # for i in range(128): # for j in range(94): # item['source'].append("../laion2b_filtered_tsvs_v1/{:05d}/{:05d}_{:05d}.tsv".format(i, i, j)) # data.append(item) # json.dump(data, open('train.json', 'w', encoding='utf-8'), indent=2) if __name__ == '__main__': # run() cook_json()
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/tasks/data/laion_loader_test.py
import sys,os sys.path.append(os.getcwd()) from typing import NamedTuple import os import argparse import json import sentencepiece as spm from fairseq.data.dictionary import Dictionary from wild_loader import WildLoader def image_code_to_token(code): return "<image{}>".format(code) def to_word(item, dictionary): print(dictionary.string(item['net_input']['src_tokens'][0])) print(dictionary.string(item['target'][0])) def run(): parser = argparse.ArgumentParser() parser.add_argument('--seed', type=int, default=1) parser.add_argument('--data', type=str, default='/mnt/msranlp/shumma/data/16g') parser.add_argument('--spm_path', type=str, default='/mnt/msranlp/shumma/data/16g/sentencepiece.bpe.model') parser.add_argument('--tokens_per_sample', type=int, default=2048) parser.add_argument('--sample_break_mode', type=str, default='') parser.add_argument('--batch_read_ahead', type=int, default=1) parser.add_argument('--mask_prob', type=float, default=0.15) parser.add_argument('--span_length', type=int, default=3) parser.add_argument('--dynamic_mask', default=True) parser.add_argument('--max_sentences', type=int, default=1) # batch size parser.add_argument('--max_image_num', type=int, default=5) parser.add_argument('--image_token_length', type=int, default=64) args = parser.parse_args() Dataset = NamedTuple('Dataset', [('data', str), ('data_dir', str), ('shuffle', bool)]) dataset = Dataset(json.load(open(f'{args.data}/json/train.json')), args.data, True) dictionary = Dictionary.load(os.path.join(args.data, 'dict.txt')) dictionary.add_symbol('</line>') dictionary.add_symbol('<image>') dictionary.add_symbol('</image>') for i in range(8192): dictionary.add_symbol(image_code_to_token(i)) tokenizer = spm.SentencePieceProcessor(model_file=args.spm_path) mlm_loader = WildLoader( args, dataset, dictionary, tokenizer, max_tokens=args.tokens_per_sample, max_sentences=args.max_sentences, max_positions=None, ignore_invalid_inputs=False, required_batch_size_multiple=1, seed=1, num_shards=1, shard_id=0, disable_prefetching=True, ) num = 0 i = 0 for item in mlm_loader: print(item) i += 1 if i > num: break # for item in tqdm.tqdm(mlm_loader): # i += 1 def cook_json(): data = [] item = { "source": [], "source_lang": "laion", "weight": 1.0, "name": "laion" } for i in range(128): for j in range(94): item['source'].append("../laion2b_filtered_tsvs_v1/{:05d}/{:05d}_{:05d}.tsv".format(i, i, j)) data.append(item) json.dump(data, open('train.json', 'w', encoding='utf-8'), indent=2) if __name__ == '__main__': run() # cook_json()
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/tasks/data/wild_loader_test_2.py
IMAGE_KEY="Images" TEXT_KEY="Extracted" import os, json, random, re max_image_num = 5 tokens_per_sample = 2048 from spacy.lang.en import English import sentencepiece as spm nlp_sentencizer = English() nlp_sentencizer.add_pipe("sentencizer") spm_tokenizer = spm.SentencePieceProcessor(model_file=r"C:\Users\shaohanh\Desktop\sentencepiece.bpe.model") def text_transform(line): tokens = spm_tokenizer.encode(line, out_type=str) return tokens def clean(text): # python re, remove html tags clean = re.compile('<.*?>') return re.sub(clean, '', text) def _read_from_files(source_file): """ <s> <image> image token </image> sentence <image> image token </image> sentence </s> 1, sample a random subsequnece: 3 sentences + the first image ... take up to 5 images + 3 sentences 2, filter html tags <p>, <br>, <br/> 3, single image, random sample rate as 0.5 """ file_path = os.path.join(source_file) if not os.path.exists(file_path): print('| file {} not exists'.format(file_path), flush=True) return iter([]) # skip bad file try: with open(file_path, 'r', encoding='utf8') as f: lines = f.read().strip().split('\n') except: return iter([]) # skip bad file for doc_jsonstr in lines: json_obj = json.loads(doc_jsonstr) doc = ['bos'] start_idx = 0 image_num = len(json_obj[IMAGE_KEY]) if image_num == 1: r = random.random() if r > 0.5: continue for image_idx, image_item in enumerate(json_obj[IMAGE_KEY]): if image_idx >= max_image_num: yield doc break text_snippet = json_obj[TEXT_KEY][start_idx:image_item['Span'][0]-1] text_snippet = clean(text_snippet) if len(text_snippet) != 0: if image_idx == 0: # crop 3 sentences before the first image sentences = list(nlp_sentencizer(text_snippet).sents) text_snippet = ' '.join([str(sent) for sent in sentences[-3:]]) text_token = text_transform(text_snippet) doc.extend(text_token) if len(doc) >= tokens_per_sample: # drop too long sentence # data.append(doc[:]) doc = doc[:tokens_per_sample - 2] doc.append('eos') yield doc break image_tokens = [i for i in image_item['input_ids']] doc.append('BOI_SYMBOL') doc.extend(image_tokens) doc.append('EOI_SYMBOL') start_idx = image_item['Span'][1] + 1 if image_idx == image_num - 1: # crop 3 sentences after the last image text_snippet = json_obj[TEXT_KEY][start_idx:] text_snippet = clean(text_snippet) sentences = list(nlp_sentencizer(text_snippet).sents) text_snippet = ' '.join([str(sent) for sent in sentences[:3]]) text_token = text_transform(text_snippet) doc.extend(text_token) doc.append('eos') if len(doc) < tokens_per_sample: yield doc break all_length = [] image_num = [] token_length = [] j = 0 for item in _read_from_files(r"C:\Users\shaohanh\Desktop\partition.000.ndjson"): # all_length.append(len(item)) # image_num.append(item.count('BOI_SYMBOL')) # token_length.append(len(item) - item.count('BOI_SYMBOL') * 197) print(item) j += 1 if j > 10: break # if j % 1000 == 0: # print(len(all_length), flush=True) # print(j) print('average length: ', sum(all_length) / len(all_length)) print('average image num: ', sum(image_num) / len(image_num)) print('average token length: ', sum(token_length) / len(token_length))
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/tasks/data/wild_loader_test.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details]
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/tasks/data/__init__.py
import json import os import random import re from infinibatch import iterators from tasks.data.lm_loader import LMLoader from tasks.data.utils import NativeCheckpointableIterator, WeightIterator, BOI_SYMBOL, EOI_SYMBOL, image_code_to_token from fairseq.data.encoders.gpt2_bpe import GPT2BPE from spacy.lang.en import English IMAGE_KEY="Images" TEXT_KEY="Extracted" class WildLoader(LMLoader): def _setup(self): self.nlp_sentencizer = English() self.nlp_sentencizer.add_pipe("sentencizer") self.max_image_num = 5 def _tokenize(self): multilingual_iters = [] weights = [] for data in self.data: multilingual_iters.append( self._tokenize_foreach_lang(data) ) if 'weight' in data: weights.append(float(data['weight'])) else: weights.append(int(data['count'])) if len(multilingual_iters) == 1: return multilingual_iters[0] sampling_iterator = WeightIterator(weights, self.seed) control_iterator = NativeCheckpointableIterator(sampling_iterator) tokenized_lines = iterators.MultiplexIterator(control_iterator, multilingual_iters) return tokenized_lines def _tokenize_foreach_lang(self, data): dataset = list(zip(data['source'])) if self.shuffle: chunk_files = iterators.InfinitePermutationSourceIterator( dataset, seed=self.seed, shuffle=self.shuffle, num_instances=self.num_shards, instance_rank=self.shard_id,) else: chunk_files = iterators.ChunkedSourceIterator( dataset, num_instances=self.num_shards, instance_rank=self.shard_id,) tokenized_lines = iterators.SelectManyIterator(chunk_files, lambda files: self._read_from_files(*files)) tokenized_lines = iterators.SamplingRandomMapIterator(tokenized_lines, self._prepare, self.seed) return tokenized_lines @staticmethod def fs_encode_line(fs_dict, words, append_eos=True): ids = [] for i, word in enumerate(words): idx = fs_dict.index(word) ids.append(idx) if append_eos: ids.append(fs_dict.eos_index) return ids def text_transform(self, line): spm_tokenizer=self.tokenizer if isinstance(spm_tokenizer, GPT2BPE): tokens = spm_tokenizer.encode(line).split(' ') else: tokens = spm_tokenizer.encode(line, out_type=str) tokenized_tokens = WildLoader.fs_encode_line(self.dictionary, tokens, append_eos=False) return tokenized_tokens def clean(self, text): # python re, remove html tags clean = re.compile('<.*?>') return re.sub(clean, '', text) def _read_from_files(self, source_file): """ <s> <image> image token </image> sentence <image> image token </image> sentence </s> 1, sample a random subsequnece: 3 sentences + the first image ... take up to 5 images + 3 sentences 2, filter html tags <p>, <br>, <br/> 3, single image, random sample rate as 0.5 """ file_path = os.path.join(self.data_dir, source_file) if not os.path.exists(file_path): print('| file {} not exists'.format(file_path), flush=True) return iter([]) # skip bad file try: with open(file_path, 'r', encoding='utf8') as f: lines = f.read().strip().split('\n') except: return iter([]) # skip bad file for doc_jsonstr in lines: try: json_obj = json.loads(doc_jsonstr) doc = [self.dictionary.bos()] start_idx = 0 image_num = len(json_obj[IMAGE_KEY]) if image_num == 1: r = random.random() if r > 0.5: continue for image_idx, image_item in enumerate(json_obj[IMAGE_KEY]): if image_idx >= self.max_image_num: if len(doc) < self.tokens_per_sample: yield doc break text_snippet = json_obj[TEXT_KEY][start_idx:image_item['Span'][0]-1] text_snippet = self.clean(text_snippet) if len(text_snippet) != 0: if image_idx == 0: # crop 3 sentences before the first image sentences = list(self.nlp_sentencizer(text_snippet).sents) text_snippet = ' '.join([str(sent) for sent in sentences[-3:]]) text_token = self.text_transform(text_snippet) doc.extend(text_token) if len(doc) >= self.tokens_per_sample: # drop too long sentence # data.append(doc[:]) doc = doc[:self.tokens_per_sample - 2] doc.append(self.dictionary.eos()) yield doc break image_tokens = [image_code_to_token(i) for i in image_item['input_ids']] image_tokens = WildLoader.fs_encode_line(self.dictionary, image_tokens, append_eos=False) doc.append(self.dictionary.index(BOI_SYMBOL)) doc.extend(image_tokens) doc.append(self.dictionary.index(EOI_SYMBOL)) start_idx = image_item['Span'][1] + 1 if image_idx == image_num - 1: # crop 3 sentences after the last image text_snippet = json_obj[TEXT_KEY][start_idx:] text_snippet = self.clean(text_snippet) sentences = list(self.nlp_sentencizer(text_snippet).sents) text_snippet = ' '.join([str(sent) for sent in sentences[:3]]) text_token = self.text_transform(text_snippet) doc.extend(text_token) doc.append(self.dictionary.eos()) if len(doc) < self.tokens_per_sample: yield doc break except: continue
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/tasks/data/wild_loader.py
import os import numpy as np import json from infinibatch import iterators from .basic_loader import BaseBatchGen from .utils import EOL_SYMBOL from .utils import safe_getattr class LMLoader(BaseBatchGen): def __init__( self, args, dataset, dictionary, tokenizer, max_tokens=None, max_sentences=None, max_positions=None, ignore_invalid_inputs=False, required_batch_size_multiple=1, seed=1, epoch=1, num_shards=1, shard_id=0, disable_prefetching=False, data_name='gpt', ): super().__init__() self.args = args self.data = dataset.data self.data_dir = dataset.data_dir self.shuffle = dataset.shuffle self.dictionary = dictionary self.tokenizer = tokenizer self.max_tokens = max_tokens self.max_sentences = max_sentences self.max_positions = max_positions self.tokens_per_sample = args.tokens_per_sample self.mlm_cut_length = safe_getattr(args, "mlm_cut_length", 0) self.mlm_tokens_proportion = safe_getattr(args, "mlm_tokens_proportion", 0) self.pad_to_max_len = safe_getattr(args, "pad_to_max_len", False) self.ignore_invalid_inputs = ignore_invalid_inputs self.required_batch_size_multiple = required_batch_size_multiple self.seed = str(seed) self.epoch = epoch self.num_shards = num_shards self.shard_id = shard_id self.batch_read_ahead = args.batch_read_ahead self.disable_prefetching = disable_prefetching self.data_name = data_name self._setup() self._build_iter() def _setup(self): pass def _build_iter(self): tokenized_lines = self._tokenize() self.padded_batches = self._batchify(tokenized_lines) if self.disable_prefetching: prefetch_batches = self.padded_batches else: prefetch_batches = iterators.PrefetchIterator( self.padded_batches, buffer_size=10000, buffer_in_main_process=True, log_empty_buffer_warning=True and self.shard_id == 0, ) prefetch_batches = iterators.MapIterator( prefetch_batches, self._move_to_tensor ) self._iter = prefetch_batches def _tokenize(self): ''' data: { 'source': list[Path], } ''' dataset = list(zip(self.data['source'])) if self.shuffle: chunk_files = \ iterators.InfinitePermutationSourceIterator( dataset, seed=self.seed, shuffle=self.shuffle, num_instances=self.num_shards, instance_rank=self.shard_id, ) else: chunk_files = \ iterators.ChunkedSourceIterator( dataset, num_instances=self.num_shards, instance_rank=self.shard_id, ) tokenized_lines = iterators.SelectManyIterator(chunk_files, lambda files: self._read_from_files(*files)) tokenized_lines = iterators.SamplingRandomMapIterator(tokenized_lines, self._prepare, self.seed) return tokenized_lines def getstate(self): state = super().getstate() state["epoch"] = self.epoch state["iterations_in_epoch"] = None return state def _batchify(self, lines): if self.max_sentences is not None: if self.batch_read_ahead > 0: lines = iterators.BlockwiseShuffleIterator(lines, self.batch_read_ahead, self.seed) batches = iterators.FixedBatchIterator(lines, self.max_sentences) else: # - def dynamic_batch_size(sample): lengths = [len(x) for x in sample] batch_size = self.max_tokens // max(lengths) // self.required_batch_size_multiple * self.required_batch_size_multiple return max(1, batch_size) batches = iterators.BucketedReadaheadBatchIterator( lines, read_ahead=self.batch_read_ahead, key=(lambda x: max(len(x[0]), len(x[1]))) if self.shuffle else None, batch_size=dynamic_batch_size, shuffle=self.shuffle, seed=self.seed, ) def collate(batch): batch_size = len(batch) mlm_batch_size = sum([len(x[2]) for x in batch]) gpt_max_length = max([len(x[0]) for x in batch]) if self.pad_to_max_len: gpt_max_length = self.tokens_per_sample mlm_max_length = 0 mlm_ntokens = 0 for x in batch: for y in x[2]: mlm_max_length = max(mlm_max_length, len(y)) mlm_ntokens += len(y) gpt_source_ids = np.full(shape=(batch_size, gpt_max_length-1), dtype=np.int32, fill_value=self.dictionary.pad()) gpt_target_ids = np.full(shape=(batch_size, gpt_max_length-1), dtype=np.int32, fill_value=self.dictionary.pad()) mlm_source_ids = np.full(shape=(mlm_batch_size, mlm_max_length), dtype=np.int32, fill_value=self.dictionary.pad()) gpt_input_mask_all = np.full(shape=(batch_size, gpt_max_length-1), dtype=np.int32, fill_value=0) gpt_loss_mask_all = np.full(shape=(batch_size, gpt_max_length-1), dtype=np.int32, fill_value=1) mlm_mask_all = np.full(shape=(mlm_batch_size, mlm_max_length), dtype=np.int32, fill_value=0) mlm_index = 0 for i, (gpt_ids, gpt_input_mask, mlm_ids_list, mlm_mask_list, gpt_loss_mask) in enumerate(batch): gpt_source_ids[i, :len(gpt_ids)-1] = gpt_ids[:-1] gpt_target_ids[i, :len(gpt_ids)-1] = gpt_ids[1:] gpt_input_mask_all[i, :len(gpt_ids)-1] = gpt_input_mask[:-1] gpt_loss_mask_all[i, :len(gpt_ids)-1] = gpt_loss_mask[1:] for j, (mlm_ids, mlm_mask) in enumerate(zip(mlm_ids_list, mlm_mask_list)): mlm_source_ids[mlm_index, :len(mlm_ids)] = mlm_ids mlm_mask_all[mlm_index, :len(mlm_mask)] = mlm_mask mlm_index += 1 ret_batch = { 'text':{ 'net_input': { 'src_tokens': gpt_source_ids.astype(np.int64), 'mlm_src_tokens': mlm_source_ids.astype(np.int64) if mlm_batch_size !=0 else None, 'gpt_input_mask': gpt_input_mask_all.astype(np.bool_), 'gpt_loss_mask': gpt_loss_mask_all.astype(np.bool_), 'mlm_mask': mlm_mask_all.astype(np.bool_) if mlm_batch_size !=0 else None }, 'target': gpt_target_ids.astype(np.int64), 'nsentences': batch_size, 'ntokens': sum([len(x[0]) for x in batch]), 'mlm_ntokens': mlm_ntokens } } return ret_batch def collate_for_gpt(batch): batch_size = len(batch) gpt_max_length = max([len(x[0]) for x in batch]) if self.pad_to_max_len: gpt_max_length = self.tokens_per_sample gpt_source_ids = np.full(shape=(batch_size, gpt_max_length-1), dtype=np.int32, fill_value=self.dictionary.pad()) gpt_target_ids = np.full(shape=(batch_size, gpt_max_length-1), dtype=np.int32, fill_value=self.dictionary.pad()) gpt_input_mask_all = np.full(shape=(batch_size, gpt_max_length-1), dtype=np.int32, fill_value=0) gpt_loss_mask_all = np.full(shape=(batch_size, gpt_max_length-1), dtype=np.int32, fill_value=1) for i, (gpt_ids, gpt_input_mask, mlm_ids_list, mlm_mask_list, gpt_loss_mask) in enumerate(batch): gpt_source_ids[i, :len(gpt_ids)-1] = gpt_ids[:-1] gpt_target_ids[i, :len(gpt_ids)-1] = gpt_ids[1:] gpt_input_mask_all[i, :len(gpt_ids)-1] = gpt_input_mask[:-1] gpt_loss_mask_all[i, :len(gpt_ids)-1] = gpt_loss_mask[1:] ret_batch = { self.data_name:{ 'net_input': { 'src_tokens': gpt_source_ids.astype(np.int64), }, 'target': gpt_target_ids.astype(np.int64), 'nsentences': batch_size, 'ntokens': sum([len(x[0]) for x in batch]), 'mlm_ntokens': 0 } } return ret_batch if self.mlm_tokens_proportion == 0: padded_batches = iterators.MapIterator( batches, collate_for_gpt ) else: padded_batches = iterators.MapIterator( batches, collate ) return padded_batches def _prepare(self, _random, doc): mlm_tokens, mlm_mask, gpt_input_mask, gpt_loss_mask = self._mlm_cut(_random, doc) full_tokens = self._gpt(doc) return full_tokens, gpt_input_mask, mlm_tokens, mlm_mask, gpt_loss_mask def _mlm_cut(self, _random, doc): eod_index = self.dictionary.indices[EOL_SYMBOL] if self.mlm_tokens_proportion == 0: mlm_tokens = [] mlm_mask = [] gpt_input_mask = [0] * len(doc) gpt_loss_mask = [1] * len(doc) return mlm_tokens, mlm_mask, gpt_input_mask, gpt_loss_mask cut_start = np.arange(1, len(doc)-3/2*self.mlm_cut_length, self.mlm_cut_length, dtype=int) _random.shuffle(cut_start) mlm_tokens = [] mlm_mask = [] start_list = [] gpt_input_mask = np.zeros(len(doc), dtype=int) gpt_loss_mask = np.ones(len(doc), dtype=int) mlm_tokens_total_num = (len(doc)-1) * self.mlm_tokens_proportion mlm_tokens_cur_num = 0 for start in cut_start: eod_num = doc[start:start+self.mlm_cut_length].count(eod_index) if eod_num >= 2: continue elif eod_num == 1: eod_pos = doc[start:start+self.mlm_cut_length].index(eod_index) if self.mlm_cut_length - eod_pos < 20: continue start_ind, end_ind = start+eod_pos+1, start + self.mlm_cut_length else: cut_pos = _random.randint(0, self.mlm_cut_length-1) if cut_pos >= self.mlm_cut_length/2: start_ind, end_ind = start, start + cut_pos + 1 else: start_ind, end_ind = start + cut_pos, start + self.mlm_cut_length assert eod_index not in doc[start_ind:end_ind] start_list.append(start) mlm_tokens.append([self.dictionary.bos()] + doc[start_ind:end_ind]) mlm_tokens_cur_num += end_ind - start_ind mlm_mask.append([0] + [1]*(end_ind - start_ind)) gpt_input_mask[start_ind:end_ind] = 1 gpt_loss_mask[start_ind:end_ind-1] = 0 if mlm_tokens_cur_num > mlm_tokens_total_num: break ind = np.array(start_list).argsort() start_list = np.array(start_list)[ind] mlm_tokens = np.array(mlm_tokens, dtype=object)[ind] mlm_mask = np.array(mlm_mask, dtype=object)[ind] return mlm_tokens, mlm_mask, gpt_input_mask, gpt_loss_mask def _gpt(self, doc): return doc def _read_from_files(self, source_file): data = [] file_path = os.path.join(self.data_dir, source_file) if not os.path.exists(file_path): print('| file {} not exists'.format(file_path), flush=True) return iter([]) # skip bad file with open(file_path, 'r', encoding='utf8') as f: lines = f.read().strip().split('\n') gpt_format_text = [] for line in lines: gpt_format_text.extend(list(filter(None, json.loads(line)["text"].split("\n")))) gpt_format_text.append('') tokenized_lines = [self.tokenizer.encode(line) for line in gpt_format_text] tokenized_ids = [self.dictionary.encode_line(line, add_if_not_exist=False) for line in tokenized_lines] doc = [self.dictionary.bos()] for ids in tokenized_ids: if len(ids) > self.tokens_per_sample: # drop too long sentence continue if len(doc) + len(ids) > self.tokens_per_sample: if len(doc) > 5/2*self.mlm_cut_length + 1: data.append(doc) doc = [self.dictionary.bos()] doc.extend(ids) if len(doc) > 1 and len(doc) <= self.tokens_per_sample: if len(doc) > 5/2*self.mlm_cut_length + 1: data.append(doc) return data
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/tasks/data/lm_loader.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import collections from random import Random from typing import Dict, Iterable, Optional import numpy as np from infinibatch import iterators EOL_SYMBOL = "</line>" BOI_SYMBOL = "<image>" EOI_SYMBOL = "</image>" def apply_to_sample(f, sample): if hasattr(sample, "__len__") and len(sample) == 0: return {} def _apply(x): if isinstance(x, np.ndarray): return f(x) elif isinstance(x, collections.OrderedDict): # OrderedDict has attributes that needs to be preserved od = collections.OrderedDict( (key, _apply(value)) for key, value in x.items() ) od.__dict__ = x.__dict__ return od elif isinstance(x, dict): return {key: _apply(value) for key, value in x.items()} elif isinstance(x, list): return [_apply(x) for x in x] elif isinstance(x, tuple): return tuple(_apply(x) for x in x) elif isinstance(x, set): return {_apply(x) for x in x} else: return x return _apply(sample) class NativeCheckpointableIterator(iterators.CheckpointableIterator): def __init__(self, iterable: Iterable): self._input_iterable = iterable self.setstate(None) def getstate(self) -> Dict: return {"num_items_yielded": self._num_items_yielded} def setstate(self, checkpoint: Optional[Dict]): self._iterator = iter(self._input_iterable) self._num_items_yielded = ( iterators._advance_iterator(self._iterator, checkpoint["num_items_yielded"]) if checkpoint is not None else 0 ) def __next__(self): item = next(self._iterator) self._num_items_yielded += 1 return item def close(self): pass class WeightIterator(object): def __init__(self, weights, seed): self.weights = weights self.seed = seed self.control_index = list(range(len(weights))) self.setstate(None) def __iter__(self): return self def getstate(self): return {"random_state": self._random_state} def setstate(self, checkpoint): self._random_state = checkpoint["random_state"] if checkpoint else None self._random = ( None # this will trigger the lazy initialization in self.__next__ ) def __next__(self): if self._random is None: self._random = Random(self.seed) if self._random_state is not None: self._random.setstate(self._random_state) idx = self._random.choices(self.control_index, self.weights)[0] self._random_state = self._random.getstate() return idx def close(self): pass def safe_getattr(obj, k, default=None): """Returns obj[k] if it exists and is not None, otherwise returns default.""" from omegaconf import OmegaConf if OmegaConf.is_config(obj): return obj[k] if k in obj and obj[k] is not None else default return getattr(obj, k, default) def safe_hasattr(obj, k): """Returns True if the given key exists and is not None.""" return getattr(obj, k, None) is not None def image_code_to_token(code): return "<image{}>".format(code) class ConcatIterator(iterators.CheckpointableIterator): """ Concat items from all given iterators. """ def __init__(self, source_iterators): """ Args: source_iterators: list of iterators to zip, item by item """ # TODO: Use all function? for source_iterator in source_iterators: if not isinstance(source_iterator, iterators.CheckpointableIterator): raise ValueError('all iterators in source_iterators have to be CheckpointableIterator') self._source_iterators = source_iterators # type: List[CheckpointableIterator] def getstate(self): return {'input_states': tuple(iterator.getstate() for iterator in self._source_iterators)} def setstate(self, checkpoint): if checkpoint is None: for iterator in self._source_iterators: iterator.setstate(None) else: # TODO: Add check that both lists have the same length? for iterator, state in zip(self._source_iterators, checkpoint['input_states']): iterator.setstate(state) def __next__(self): res = {} # (note: can't use a generator expression, as it gets confused when a next() call raises StopIteration) for iterator in self._source_iterators: res.update(next(iterator)) return res def close(self): for it in self._source_iterators: it.close()
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/tasks/data/utils.py
import json import os import random from infinibatch import iterators from tasks.data.lm_loader import LMLoader from tasks.data.utils import NativeCheckpointableIterator, WeightIterator, BOI_SYMBOL, EOI_SYMBOL, image_code_to_token from fairseq.data.encoders.gpt2_bpe import GPT2BPE class LaionLoader(LMLoader): def _tokenize(self): multilingual_iters = [] weights = [] for data in self.data: multilingual_iters.append( self._tokenize_foreach_lang(data) ) if 'weight' in data: weights.append(float(data['weight'])) else: weights.append(int(data['count'])) if len(multilingual_iters) == 1: return multilingual_iters[0] sampling_iterator = WeightIterator(weights, self.seed) control_iterator = NativeCheckpointableIterator(sampling_iterator) tokenized_lines = iterators.MultiplexIterator(control_iterator, multilingual_iters) return tokenized_lines def _tokenize_foreach_lang(self, data): dataset = list(zip(data['source'])) if self.shuffle: chunk_files = iterators.InfinitePermutationSourceIterator( dataset, seed=self.seed, shuffle=self.shuffle, num_instances=self.num_shards, instance_rank=self.shard_id,) else: chunk_files = iterators.ChunkedSourceIterator( dataset, num_instances=self.num_shards, instance_rank=self.shard_id,) tokenized_lines = iterators.SelectManyIterator(chunk_files, lambda files: self._read_from_files(*files)) tokenized_lines = iterators.SamplingRandomMapIterator(tokenized_lines, self._prepare, self.seed) return tokenized_lines @staticmethod def fs_encode_line(fs_dict, words, append_eos=True): ids = [] for i, word in enumerate(words): idx = fs_dict.index(word) ids.append(idx) if append_eos: ids.append(fs_dict.eos_index) return ids def _read_from_files(self, source_file): """ <s> <image> image token </image> sentence </s> <s> sentence <image> image token </image> </s> """ file_path = os.path.join(self.data_dir, source_file) if not os.path.exists(file_path): print('| file {} not exists'.format(file_path), flush=True) return iter([]) # skip bad file try: with open(file_path, 'r', encoding='utf8') as f: lines = f.read().strip().split('\n') except: return iter([]) # skip bad file for doc_jsonstr in lines: try: obj = json.loads(doc_jsonstr) if int(obj['width']) < 200 or int(obj['height']) < 200: continue line = obj['caption'] spm_tokenizer=self.tokenizer if isinstance(spm_tokenizer, GPT2BPE): tokens = spm_tokenizer.encode(line).split(' ') else: tokens = spm_tokenizer.encode(line, out_type=str) tokenized_tokens = LaionLoader.fs_encode_line(self.dictionary, tokens, append_eos=False) image_tokens = [image_code_to_token(i) for i in obj['input_ids']] image_tokens = LaionLoader.fs_encode_line(self.dictionary, image_tokens, append_eos=False) r = random.random() doc = [self.dictionary.bos()] if r < 0.5: doc.append(self.dictionary.index(BOI_SYMBOL)) doc.extend(image_tokens) doc.append(self.dictionary.index(EOI_SYMBOL)) doc.extend(tokenized_tokens) else: doc.extend(tokenized_tokens) doc.append(self.dictionary.index(BOI_SYMBOL)) doc.extend(image_tokens) doc.append(self.dictionary.index(EOI_SYMBOL)) doc.append(self.dictionary.eos()) yield doc except: continue
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/tasks/data/laion_loader.py
import json import os from infinibatch import iterators from .lm_loader import LMLoader from .utils import NativeCheckpointableIterator, WeightIterator, EOL_SYMBOL from fairseq.data.encoders.gpt2_bpe import GPT2BPE class SpmLmLoader(LMLoader): def _tokenize(self): multilingual_iters = [] weights = [] for data in self.data: multilingual_iters.append( self._tokenize_foreach_lang(data) ) if 'weight' in data: weights.append(float(data['weight'])) else: weights.append(int(data['count'])) if len(multilingual_iters) == 1: return multilingual_iters[0] sampling_iterator = WeightIterator(weights, self.seed) control_iterator = NativeCheckpointableIterator(sampling_iterator) tokenized_lines = iterators.MultiplexIterator(control_iterator, multilingual_iters) return tokenized_lines def _tokenize_foreach_lang(self, data): dataset = list(zip(data['source'])) if self.shuffle: chunk_files = iterators.InfinitePermutationSourceIterator( dataset, seed=self.seed, shuffle=self.shuffle, num_instances=self.num_shards, instance_rank=self.shard_id,) else: chunk_files = iterators.ChunkedSourceIterator( dataset, num_instances=self.num_shards, instance_rank=self.shard_id,) tokenized_lines = iterators.SelectManyIterator(chunk_files, lambda files: self._read_from_files(*files)) tokenized_lines = iterators.SamplingRandomMapIterator(tokenized_lines, self._prepare, self.seed) return tokenized_lines @staticmethod def fs_encode_line(fs_dict, words, append_eos=True): ids = [] for i, word in enumerate(words): idx = fs_dict.index(word) ids.append(idx) if append_eos: ids.append(fs_dict.eos_index) return ids @staticmethod def _doc_jsonstr_to_ids(doc_jsonstr, spm_tokenizer=None, fs_dict=None): assert EOL_SYMBOL in fs_dict.indices eol_index = fs_dict.indices[EOL_SYMBOL] tokenized_ids = [] for line in filter(None, json.loads(doc_jsonstr)["text"].split("\n")): if isinstance(spm_tokenizer, GPT2BPE): tokens = spm_tokenizer.encode(line).split(' ') else: tokens = spm_tokenizer.encode(line, out_type=str) tokenized_tokens = SpmLmLoader.fs_encode_line(fs_dict, tokens, append_eos=False) tokenized_tokens.append(eol_index) tokenized_ids.append(tokenized_tokens) if len(tokenized_ids) > 0: last_line_ids = tokenized_ids[-1] if last_line_ids[-1] == eol_index: last_line_ids[-1] = fs_dict.eos_index else: print("[W] At SpmLmLoader._doc_jsonstr_to_ids, last line does not end with eol!") last_line_ids.append(fs_dict.eos_index) else: print("[W] At SpmLmLoader._doc_jsonstr_to_ids, A null document with no lines!") tokenized_ids = [[fs_dict.eos_index]] return tokenized_ids def _read_from_files(self, source_file): data = [] file_path = os.path.join(self.data_dir, source_file) if not os.path.exists(file_path): print('| file {} not exists'.format(file_path), flush=True) return iter([]) # skip bad file try: with open(file_path, 'r', encoding='utf8') as f: lines = f.read().strip().split('\n') except: return iter([]) # skip bad file # NOTE #### simple spm implementation ############### tokenized_ids = [] for doc_jsonstr in lines: ret = SpmLmLoader._doc_jsonstr_to_ids(doc_jsonstr, spm_tokenizer=self.tokenizer, fs_dict=self.dictionary) tokenized_ids.extend(ret) # ################################################### doc = [self.dictionary.bos()] for ids in tokenized_ids: if getattr(self.args, "debug_p100", False): if len(ids) > 256: ids = ids[:256] if len(ids) >= self.tokens_per_sample: # drop too long sentence # data.append(doc[:]) ids = ids[:self.tokens_per_sample - 1] # continue if len(doc) + len(ids) > self.tokens_per_sample: if len(doc) > 5/2*self.mlm_cut_length + 1: data.append(doc) doc = [self.dictionary.bos()] doc.extend(ids) if len(doc) > 1 and len(doc) <= self.tokens_per_sample: if len(doc) > 5/2*self.mlm_cut_length + 1: data.append(doc) return data
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/tasks/data/spm_lm_loader.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import copy import itertools import os import numpy as np from infinibatch import iterators from .basic_loader import BaseBatchGen from .utils import NativeCheckpointableIterator, WeightIterator class MLMLoader(BaseBatchGen): def __init__( self, args, dataset, dictionary, tokenizer, max_tokens=None, max_sentences=None, max_positions=None, ignore_invalid_inputs=False, required_batch_size_multiple=1, seed=1, num_shards=1, shard_id=0, ): super().__init__() self.args = args self.data = dataset.data self.data_dir = dataset.data_dir self.shuffle = dataset.shuffle self.dictionary = dictionary self.tokenizer = tokenizer self.max_tokens = max_tokens self.max_sentences = max_sentences self.max_positions = max_positions self.tokens_per_sample = args.tokens_per_sample self.sample_break_mode = args.sample_break_mode self.ignore_invalid_inputs = ignore_invalid_inputs self.required_batch_size_multiple = required_batch_size_multiple self.seed = str(seed) self.num_shards = num_shards self.shard_id = shard_id self.batch_read_ahead = args.batch_read_ahead self._build_iter() def _build_iter(self): tokenized_lines = self._multilingual_tokenize() self.padded_batches = self._batchify(tokenized_lines) prefetch_batches = iterators.PrefetchIterator( self.padded_batches, buffer_size=10000, buffer_in_main_process=True, log_empty_buffer_warning=True and self.shard_id == 0, ) prefetch_batches = iterators.MapIterator(prefetch_batches, self._move_to_tensor) self._iter = prefetch_batches def _multilingual_tokenize(self): multilingual_iters = [] weights = [] for data in self.data: multilingual_iters.append(self._tokenize(data)) if "weight" in data: weights.append(float(data["weight"])) else: weights.append(int(data["count"])) if len(multilingual_iters) == 1: return multilingual_iters[0] sampling_iterator = WeightIterator(weights) control_iterator = NativeCheckpointableIterator(sampling_iterator) tokenized_lines = iterators.MultiplexIterator( control_iterator, multilingual_iters ) return tokenized_lines def _tokenize(self, data): """ data: { 'source': list[Path], 'source_lang': str, 'count': int, 'weight': float, 'name': str, } """ dataset = list( zip( data["source"], itertools.repeat(data["source_lang"]), ) ) if self.shuffle: chunk_files = iterators.InfinitePermutationSourceIterator( dataset, seed=self.seed, shuffle=self.shuffle, num_instances=self.num_shards, instance_rank=self.shard_id, ) else: chunk_files = iterators.ChunkedSourceIterator( dataset, num_instances=self.num_shards, instance_rank=self.shard_id, ) tokenized_lines = iterators.SelectManyIterator( chunk_files, lambda files: self._read_from_files(*files) ) tokenized_lines = iterators.SamplingRandomMapIterator( tokenized_lines, self._prepare, self.seed ) return tokenized_lines def _batchify(self, lines): if self.max_sentences is not None: if self.batch_read_ahead > 0: lines = iterators.BlockwiseShuffleIterator( lines, self.batch_read_ahead, self.seed ) batches = iterators.FixedBatchIterator(lines, self.max_sentences) else: def dynamic_batch_size(sample): lengths = [len(x) for x in sample] batch_size = self.max_tokens // max(lengths) batch_size = ( batch_size // self.required_batch_size_multiple * self.required_batch_size_multiple ) return max(1, batch_size) batches = iterators.BucketedReadaheadBatchIterator( lines, read_ahead=self.batch_read_ahead, key=(lambda x: max(len(x[0]), len(x[1]))) if self.shuffle else None, batch_size=dynamic_batch_size, shuffle=self.shuffle, seed=self.seed, ) def collate(batch): batch_size = len(batch) mlm_source_max_length = max([len(x[0]) for x in batch]) mlm_target_max_length = max([len(x[1]) for x in batch]) s2s_source_max_length = max([len(x[2]) for x in batch]) s2s_target_max_length = max([len(x[3]) for x in batch]) mlm_source_ids = np.full( shape=(batch_size, mlm_source_max_length), dtype=np.int32, fill_value=self.dictionary.pad(), ) mlm_target_ids = np.full( shape=(batch_size, mlm_target_max_length), dtype=np.int32, fill_value=self.dictionary.pad(), ) s2s_source_ids = np.full( shape=(batch_size, s2s_source_max_length), dtype=np.int32, fill_value=self.dictionary.pad(), ) s2s_target_ids = np.full( shape=(batch_size, s2s_target_max_length - 1), dtype=np.int32, fill_value=self.dictionary.pad(), ) s2s_prev_input_ids = np.full( shape=(batch_size, s2s_target_max_length - 1), dtype=np.int32, fill_value=self.dictionary.pad(), ) for i, ( mlm_input_ids, mlm_label_ids, s2s_input_ids, s2s_label_ids, ) in enumerate(batch): mlm_source_ids[i, : len(mlm_input_ids)] = mlm_input_ids mlm_target_ids[i, : len(mlm_label_ids)] = mlm_label_ids s2s_source_ids[i, : len(s2s_input_ids)] = s2s_input_ids s2s_target_ids[i, : len(s2s_label_ids) - 1] = s2s_label_ids[1:] s2s_prev_input_ids[i, : len(s2s_label_ids) - 1] = s2s_label_ids[:-1] ret_batch = { "net_input": { "src_tokens": mlm_source_ids.astype(np.int64), }, "target": mlm_target_ids.astype(np.int64), "nsentences": batch_size, "ntokens": sum([len(x[0]) for x in batch]), } return ret_batch padded_batches = iterators.MapIterator(batches, collate) return padded_batches def _prepare(self, _random, doc): nonmasked_tokens, masked_tokens = self._mask_lm(_random, doc) nonnoise_spans, noise_spans = self._span_corruption(_random, doc) return nonmasked_tokens, masked_tokens, nonnoise_spans, noise_spans def _mask_lm(self, _random, doc): def mask_tokens(): return "<mask>" length = len(doc) mask_tokens_num = int(length * self.args.mask_prob) mask_tokens_num = min(max(mask_tokens_num, 1), length - 1) possible_mask_positions = _random.sample(range(length), k=mask_tokens_num) possible_mask_positions = sorted(possible_mask_positions) nonmasked_tokens = copy.deepcopy(doc) masked_tokens = [self.dictionary.pad() for _ in range(len(doc))] for position in possible_mask_positions: # masked_tokens.append(nonmasked_tokens[position]) masked_tokens[position] = nonmasked_tokens[position] nonmasked_tokens[position] = self.dictionary.indices[mask_tokens()] return nonmasked_tokens, masked_tokens def _span_corruption(self, _random, doc): def mask_tokens(i): return f"<mask_{i}>" length = len(doc) noise_tokens_num = int(length * self.args.mask_prob) noise_tokens_num = min(max(noise_tokens_num, 1), length - 1) noise_spans_num = int(noise_tokens_num / self.args.span_length) noise_spans_num = max(noise_spans_num, 1) nonnoise_tokens_num = length - noise_tokens_num if noise_spans_num == 1: noise_split_positions = [0, noise_tokens_num] else: possible_split_positions = list(range(1, noise_tokens_num)) _random.shuffle(possible_split_positions) noise_split_positions = sorted( possible_split_positions[: noise_spans_num - 1] ) noise_split_positions = [0] + noise_split_positions + [noise_tokens_num] possible_insert_positions = list(range(nonnoise_tokens_num)) _random.shuffle(possible_insert_positions) noise_insert_positions = sorted(possible_insert_positions[:noise_spans_num]) nonnoise_spans, noise_spans = [], [] last_end = 0 for i in range(noise_spans_num): start_pos = noise_insert_positions[i] + noise_split_positions[i] end_pos = noise_insert_positions[i] + noise_split_positions[i + 1] mask_id = self.dictionary.indices[mask_tokens(i)] if getattr(self.args, "remove_target_sentinel", False): noise_spans.append(doc[start_pos:end_pos]) else: noise_spans.append([mask_id] + doc[start_pos:end_pos]) if getattr(self.args, "remove_source_sentinel", False): nonnoise_spans.extend(doc[last_end:start_pos]) else: nonnoise_spans.extend(doc[last_end:start_pos] + [mask_id]) last_end = end_pos nonnoise_spans.extend(doc[last_end:]) noise_spans = sum(noise_spans, []) return nonnoise_spans, noise_spans def _read_from_files(self, source_file, source_lang): # data = [] file_path = os.path.join(self.data_dir, source_file) if not os.path.exists(file_path): print("| file {} not exists".format(file_path), flush=True) return iter([]) # skip bad file with open(file_path, "r", encoding="utf8") as f: lines = f.read().strip().split("\n") doc = [self.dictionary.bos()] for line in lines: if line == "": if self.sample_break_mode == "complete_doc": # data.append(doc) yield doc doc = [self.dictionary.bos()] continue tokenized_line = self.tokenizer.EncodeAsPieces(line) tokenized_id = [ self.dictionary.index(token) for token in tokenized_line ] + [self.dictionary.eos_index] if len(tokenized_id) > self.tokens_per_sample: continue if len(doc) + len(tokenized_id) > self.tokens_per_sample: # data.append(doc) yield doc doc = [self.dictionary.bos()] doc.extend(tokenized_id) if len(doc) > 1 and len(doc) <= self.tokens_per_sample: # data.append(doc) yield doc # return data
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/tasks/data/mlm_loader.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import math import warnings import torch import torch.distributed as dist from fairseq.utils import multi_tensor_l2norm_available, multi_tensor_total_norm @torch.no_grad() def clip_grad_norm_( params, max_norm, moe_expert_count, aggregate_norm_fn=None ) -> torch.Tensor: def grad_exists(p): return p is not None and getattr(p, "grad", None) is not None if isinstance(params, torch.Tensor): params = [params] params = list(params) params = list(filter(grad_exists, params)) grads, expert_grads, base_expert_grads, sharded_grads = [], [], [], [] denom = math.sqrt(max(dist.get_global_world_size(), moe_expert_count)) for p in params: if hasattr(p, "expert"): expert_grads.append(p.grad.detach() / denom) elif hasattr(p, "base_expert"): base_expert_grads.append(p.grad.detach()) elif hasattr(p, "_is_sharded"): sharded_grads.append(p.grad.detach()) else: grads.append(p.grad.detach()) if len(grads) == 0: if len(params) > 0: total_norm = params[0].new_tensor(0.0) else: total_norm = torch.tensor(0.0) elif len(grads) == 1: total_norm = torch.norm(grads[0], p=2, dtype=torch.float32) else: if multi_tensor_l2norm_available: total_norm = multi_tensor_total_norm(grads) else: if torch.cuda.is_available(): warnings.warn( "amp_C fused kernels unavailable, disabling multi_tensor_l2norm; " "you may get better performance by installing NVIDIA's apex library" ) device = torch.cuda.current_device() elif grads[0].device.type == "xla": device = grads[0].device else: device = torch.device("cpu") total_norm = torch.norm( torch.stack( [torch.norm(g, p=2, dtype=torch.float32).to(device) for g in grads] ) ) # calculate split_norm and all_reduce with other workers norms = [total_norm] for split_grads in [expert_grads, sharded_grads]: if len(split_grads) == 0: continue split_norm = torch.norm( torch.stack([torch.norm(g, p=2, dtype=torch.float32) for g in split_grads]) ) if dist.is_initialized(): split_norm.pow_(2) dist.all_reduce(split_norm) split_norm.sqrt_() norms.append(split_norm) if len(norms) > 1: total_norm = torch.norm(torch.stack(norms)) if aggregate_norm_fn is not None: total_norm = aggregate_norm_fn(total_norm) if max_norm > 0: max_norm = float(max_norm) clip_coef = (max_norm / (total_norm + 1e-6)).clamp_(max=1) for g in grads + expert_grads + sharded_grads + base_expert_grads: g.mul_(clip_coef) return total_norm
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/utils/sparse_clip.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details]
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/utils/__init__.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] # 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 from dataclasses import dataclass, field from typing import Optional import torch from fairseq import distributed_utils, utils from fairseq.dataclass import ChoiceEnum, FairseqDataclass from fairseq.models import ( FairseqIncrementalDecoder, FairseqLanguageModel, register_model, register_model_architecture, ) from fairseq.models.transformer import DEFAULT_MIN_PARAMS_TO_WRAP, Embedding from fairseq.modules import PositionalEmbedding from omegaconf import II from torchscale.architecture.config import DecoderConfig from torchscale.architecture.decoder import Decoder DEFAULT_MAX_TARGET_POSITIONS = 1024 logger = logging.getLogger(__name__) @dataclass class LanguageConfig(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_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"} ) 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 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." ) }, ) moe_freq: int = field( default=0, metadata={"help": "Frequency at which we insert MoE Transformer layers"}, ) moe_expert_count: int = field( default=0, metadata={"help": "Number of experts in each MoE Layer"} ) moe_gating_use_fp32: bool = field( default=False, metadata={"help": "Use FP32 computations in MoE top2 gating function"}, ) moe_second_expert_policy: str = field( default="sampling", metadata={"help": "policy for second expert, options: all/sampling/random"}, ) moe_normalize_gate_prob_before_dropping: bool = field( default=False, metadata={ "help": "whether to normalize gate probs before or after dropping experts for capacity and randomization" }, ) moe_expert_ffn_dim: Optional[int] = field( default=None, metadata={"help": "MoE expert FFN dimension"} ) moe_top1_expert: Optional[bool] = field( default=False, metadata={"help": "Use top1 gate instead of top2"} ) moe_eval_capacity_token_fraction: Optional[float] = field( default=0.25, metadata={ "help": ( "Default: 0.25, Fraction of tokens as capacity during validation, " "if set to negative, use same as training. range: (0.0, 1.0]." ) }, ) moe_normalize_expert_grad: Optional[str] = field( default="world_size", metadata={ "help": "Divide expert gradients by (1) 'world_size' (2) 'sqrt_world_size'" }, ) record_a2a_perf_stats: Optional[bool] = field( default=False, metadata={"help": "records all to all perf stats during distributed training"}, ) dummy_a2a: Optional[bool] = field( default=False, metadata={ "help": "By passes all to all during distributed training by returning the input buffer as output" }, ) moe_batch_prioritized_routing: Optional[bool] = field( default=False, metadata={ "help": "if true orders token by the gate prob before capacity dropping." }, ) use_xmoe: Optional[bool] = field( default=False, ) flash_attention: Optional[bool] = field( default=False, ) sope_rel_pos: Optional[bool] = field( default=False, metadata={"help": "use SoPE as the relative position embhedding"}, ) scale_length: Optional[int] = field( default=2048, ) # 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") memory_efficient_fp16: bool = II("common.memory_efficient_fp16") fp16: bool = II("common.fp16") fp16_no_flatten_grads: bool = II("common.fp16_no_flatten_grads") ddp_backend: str = II("distributed_training.ddp_backend") world_size: int = II("distributed_training.distributed_world_size") distributed_rank: int = II("distributed_training.distributed_rank") ddp_rank: int = II("distributed_training.distributed_rank") deepnorm: Optional[bool] = field( default=False, ) subln: Optional[bool] = field( default=False, ) rel_pos_buckets: Optional[int] = field( default=0, ) max_rel_pos: Optional[int] = field( default=0, ) @register_model("lm", dataclass=LanguageConfig) class LanguageModel(FairseqLanguageModel): def __init__(self, args, decoder): self.args = args super().__init__(decoder) @classmethod def build_model(cls, args, task): if getattr(args, "max_target_positions", None) is None: args.max_target_positions = getattr( args, "tokens_per_sample", DEFAULT_MAX_TARGET_POSITIONS ) embed_tokens = cls.build_embedding( args, task.source_dictionary, args.decoder_embed_dim ) embed_positions = ( PositionalEmbedding( args.max_target_positions, args.decoder_embed_dim, task.dictionary.pad(), learned=args.decoder_learned_pos, ) if not args.no_token_positional_embeddings else None ) if args.share_decoder_input_output_embed: output_projection = torch.nn.Linear( embed_tokens.weight.shape[1], embed_tokens.weight.shape[0], bias=False, ) output_projection.weight = embed_tokens.weight else: output_projection = torch.nn.Linear( args.decoder_embed_dim, len(task.dictionary), bias=False ) torch.nn.init.normal_( output_projection.weight, mean=0, std=args.decoder_embed_dim**-0.5 ) if getattr(args, "moe_freq", 0) > 0 and ( getattr(args, "fp16", False) and not getattr(args, "memory_efficient_fp16", False) and getattr(args, "ddp_backend", None) != "fully_sharded" ): assert ( args.fp16_no_flatten_grads ), "If training moe models, set --fp16-no-flatten-grads to calculate correct gradnorm" args.ddp_rank = distributed_utils.get_data_parallel_rank() config = DecoderConfig() config.override(args) decoder = LMDecoder( config, embed_tokens, embed_positions, output_projection, is_encoder_decoder=False, dictionary=task.dictionary, ) return cls(args, decoder) @classmethod def build_embedding(cls, args, dictionary, embed_dim, path=None): return Embedding(len(dictionary), embed_dim, dictionary.pad()) class LMDecoder(Decoder, FairseqIncrementalDecoder): def forward(self, src_tokens, **kwargs): self_attn_padding_mask = src_tokens.eq(self.dictionary.pad()) return super().forward(src_tokens, self_attn_padding_mask, **kwargs) def max_positions(self): return self.embed_positions.max_positions def reorder_incremental_state_scripting( self, incremental_state, new_order, ): for module in incremental_state: for key in incremental_state[module]: result = incremental_state[module][key].index_select(0, new_order) incremental_state[module][key] = result @register_model_architecture("lm", "lm_base") 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.base_layers = getattr(args, "base_layers", 0) args.base_sublayers = getattr(args, "base_sublayers", 1) args.base_shuffle = getattr(args, "base_shuffle", False) 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
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/models/language_modeling.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import argparse import importlib import os MODEL_REGISTRY = {} MODEL_DATACLASS_REGISTRY = {} ARCH_MODEL_REGISTRY = {} ARCH_MODEL_NAME_REGISTRY = {} ARCH_MODEL_INV_REGISTRY = {} ARCH_CONFIG_REGISTRY = {} # 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("models." + model_name) # extra `model_parser` for sphinx if model_name in MODEL_REGISTRY: parser = argparse.ArgumentParser(add_help=False) group_archs = parser.add_argument_group("Named architectures") group_archs.add_argument( "--arch", choices=ARCH_MODEL_INV_REGISTRY[model_name] ) group_args = parser.add_argument_group("Additional command-line arguments") MODEL_REGISTRY[model_name].add_args(group_args) globals()[model_name + "_parser"] = parser
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/models/__init__.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] # 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 from typing import Dict, List, Optional, Tuple import torch from fairseq import distributed_utils, utils from fairseq.distributed import utils as fsdp_wrap from fairseq.models import ( FairseqEncoder, FairseqEncoderDecoderModel, register_model, register_model_architecture, ) from fairseq.models.transformer import Embedding from fairseq.modules import PositionalEmbedding from torch import Tensor from torchscale.architecture.config import DecoderConfig, EncoderConfig from torchscale.architecture.encoder import Encoder from .language_modeling import LMDecoder as MTDecoder logger = logging.getLogger(__name__) DEFAULT_MAX_SOURCE_POSITIONS = 1024 DEFAULT_MAX_TARGET_POSITIONS = 1024 DEFAULT_MIN_PARAMS_TO_WRAP = int(1e8) @register_model("mt") class TranslationModel(FairseqEncoderDecoderModel): def __init__(self, args, encoder, decoder): super().__init__(encoder, decoder) self.args = args @staticmethod def add_args(parser): """Add model-specific arguments to the parser.""" # fmt: off parser.add_argument('--activation-fn', choices=utils.get_available_activation_fns(), help='activation function to use') 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', '--relu-dropout', type=float, metavar='D', help='dropout probability after activation in FFN.') parser.add_argument('--encoder-embed-path', type=str, metavar='STR', help='path to pre-trained encoder embedding') parser.add_argument('--encoder-embed-dim', type=int, metavar='N', help='encoder embedding dimension') 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') parser.add_argument('--encoder-normalize-before', action='store_true', help='apply layernorm before each encoder block') parser.add_argument('--encoder-learned-pos', action='store_true', help='use learned positional embeddings in the encoder') parser.add_argument('--decoder-embed-path', type=str, metavar='STR', help='path to pre-trained decoder embedding') parser.add_argument('--decoder-embed-dim', type=int, metavar='N', help='decoder embedding dimension') parser.add_argument('--decoder-ffn-embed-dim', type=int, metavar='N', help='decoder embedding dimension for FFN') parser.add_argument('--decoder-layers', type=int, metavar='N', help='num decoder layers') parser.add_argument('--decoder-attention-heads', type=int, metavar='N', help='num decoder attention heads') parser.add_argument('--decoder-learned-pos', action='store_true', help='use learned positional embeddings in the decoder') parser.add_argument('--decoder-normalize-before', action='store_true', help='apply layernorm before each decoder block') parser.add_argument('--decoder-output-dim', type=int, metavar='N', help='decoder output dimension (extra linear layer ' 'if different from decoder embed dim') parser.add_argument('--share-decoder-input-output-embed', action='store_true', help='share decoder input and output embeddings') parser.add_argument('--share-all-embeddings', action='store_true', help='share encoder, decoder and output embeddings' ' (requires shared dictionary and embed dim)') parser.add_argument('--no-token-positional-embeddings', default=False, action='store_true', help='if set, disables positional embeddings (outside self attention)') parser.add_argument('--adaptive-softmax-cutoff', metavar='EXPR', help='comma separated list of adaptive softmax cutoff points. ' 'Must be used with adaptive_loss criterion'), parser.add_argument('--adaptive-softmax-dropout', type=float, metavar='D', help='sets adaptive softmax dropout for the tail projections') parser.add_argument('--layernorm-embedding', action='store_true', help='add layernorm to embedding') parser.add_argument('--no-scale-embedding', action='store_true', help='if True, dont scale embeddings') 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.') # args for "Cross+Self-Attention for Transformer Models" (Peitz et al., 2019) parser.add_argument('--no-cross-attention', default=False, action='store_true', help='do not perform cross-attention') parser.add_argument('--cross-self-attention', default=False, action='store_true', help='perform cross+self-attention') # 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('--decoder-layerdrop', type=float, metavar='D', default=0, help='LayerDrop probability for decoder') parser.add_argument('--encoder-layers-to-keep', default=None, help='which layers to *keep* when pruning as a comma-separated list') parser.add_argument('--decoder-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 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.' ) ) # args for mixture-of-expert layers parser.add_argument('--moe-freq', type=int, metavar='D', default=0, help='Frequency at which we insert MoE Transformer layers') parser.add_argument('--encoder-moe-freq', type=int, metavar='D', default=0, help='Frequency at which we insert MoE Transformer encoder layers') parser.add_argument('--decoder-moe-freq', type=int, metavar='D', default=0, help='Frequency at which we insert MoE Transformer decoder layers') parser.add_argument('--moe-expert-count', type=int, metavar='D', default=0, help='Number of experts in each MoE Layer') parser.add_argument('--moe-gating-use-fp32', default=False, action='store_true', help="Use FP32 computations in MoE top2 gating function") parser.add_argument('--moe-second-expert-policy', type=str, default='sampling', help="policy for second expert, options: all/sampling/random") parser.add_argument( '--moe-normalize-gate-prob-before-dropping', default=False, action='store_true', help=( "whether to normalize gate probs before or after dropping experts " "for capacity and randomization" ) ) parser.add_argument('--moe-expert-ffn-dim', type=int, default=0, help="MoE Expert FFN dimension") parser.add_argument('--moe-top1-expert', default=False, action='store_true', help="Use top1 gate instead of top2") parser.add_argument( '--moe-eval-capacity-token-fraction', type=float, default=0.25, help=( "Fraction of tokens as capacity during validation" "if set to negative, use same as training. range: (0.0, 1.0]." ) ) parser.add_argument('--moe-normalize-expert-grad', type=str, default='world_size', help="Divide expert gradients by (1) 'world_size' (2) 'sqrt_world_size'") parser.add_argument('--use-moe-pad-mask', default=False, action='store_true', help="Don't route padding tokens to any expert") parser.add_argument('--use-xmoe', default=False, action='store_true', help="Enable X-Moe") parser.add_argument('--freeze-moe', default=False, action='store_true', help="Freeze MoE Params") parser.add_argument('--deepnorm', default=False, action='store_true', help="Enable DeepNorm") parser.add_argument('--subln', default=False, action='store_true', help="Enable SubLN") parser.add_argument('--pretrained-dense-mt-model-path', type=str, default='') # args for pseudo-MoE layers parser.add_argument('--alternate-ffn-embed-dim', type=int, default=0, help="FFN embed dim of alternate pseudo-MoE blocks") parser.add_argument('--rel-pos-buckets', type=int, default=0, help='') parser.add_argument('--max-rel-pos', type=int, default=0, help='') # fmt: on @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 getattr(args, "max_source_positions", None) is None: args.max_source_positions = DEFAULT_MAX_SOURCE_POSITIONS if getattr(args, "max_target_positions", None) is None: args.max_target_positions = DEFAULT_MAX_TARGET_POSITIONS args.ddp_rank = distributed_utils.get_data_parallel_rank() src_dict, tgt_dict = task.source_dictionary, task.target_dictionary if args.share_all_embeddings: if src_dict != tgt_dict: raise ValueError("--share-all-embeddings requires a joined dictionary") if args.encoder_embed_dim != args.decoder_embed_dim: raise ValueError( "--share-all-embeddings requires --encoder-embed-dim to match --decoder-embed-dim" ) if args.decoder_embed_path and ( args.decoder_embed_path != args.encoder_embed_path ): raise ValueError( "--share-all-embeddings not compatible with --decoder-embed-path" ) encoder_embed_tokens = cls.build_embedding( args, src_dict, args.encoder_embed_dim, args.encoder_embed_path ) decoder_embed_tokens = encoder_embed_tokens args.share_decoder_input_output_embed = True else: encoder_embed_tokens = cls.build_embedding( args, src_dict, args.encoder_embed_dim, args.encoder_embed_path ) decoder_embed_tokens = cls.build_embedding( args, tgt_dict, args.decoder_embed_dim, args.decoder_embed_path ) if getattr(args, "offload_activations", False): args.checkpoint_activations = True # offloading implies checkpointing encoder_embed_positions = ( PositionalEmbedding( args.max_source_positions, args.encoder_embed_dim, src_dict.pad(), learned=args.encoder_learned_pos, ) if not args.no_token_positional_embeddings else None ) decoder_embed_positions = ( PositionalEmbedding( args.max_target_positions, args.decoder_embed_dim, tgt_dict.pad(), learned=args.decoder_learned_pos, ) if not args.no_token_positional_embeddings else None ) if args.share_decoder_input_output_embed: output_projection = torch.nn.Linear( decoder_embed_tokens.weight.shape[1], decoder_embed_tokens.weight.shape[0], bias=False, ) output_projection.weight = decoder_embed_tokens.weight else: output_projection = torch.nn.Linear( args.decoder_embed_dim, len(tgt_dict), bias=False ) torch.nn.init.normal_( output_projection.weight, mean=0, std=args.decoder_embed_dim**-0.5 ) encoder = cls.build_encoder( args, encoder_embed_tokens, encoder_embed_positions, src_dict, ) decoder = cls.build_decoder( args, decoder_embed_tokens, decoder_embed_positions, output_projection, tgt_dict, ) if not args.share_all_embeddings: min_params_to_wrap = getattr( args, "min_params_to_wrap", DEFAULT_MIN_PARAMS_TO_WRAP ) # fsdp_wrap is a no-op when --ddp-backend != fully_sharded encoder = fsdp_wrap(encoder, min_num_params=min_params_to_wrap) decoder = fsdp_wrap(decoder, min_num_params=min_params_to_wrap) return cls(args, encoder, decoder) @classmethod def build_embedding(cls, args, dictionary, embed_dim, path=None): num_embeddings = len(dictionary) padding_idx = dictionary.pad() emb = Embedding(num_embeddings, embed_dim, padding_idx) # if provided, load from preloaded dictionaries if path: embed_dict = utils.parse_embedding(path) utils.load_embedding(embed_dict, dictionary, emb) return emb @classmethod def build_encoder(cls, args, embed_tokens, embed_positions, dictionary): config = EncoderConfig() config.override(args) return MTEncoder( config, embed_tokens, embed_positions, is_encoder_decoder=True, dictionary=dictionary, ) @classmethod def build_decoder( cls, args, embed_tokens, embed_positions, output_projection, dictionary ): config = DecoderConfig() config.override(args) return MTDecoder( config, embed_tokens, embed_positions, output_projection, is_encoder_decoder=True, dictionary=dictionary, ) def forward( self, src_tokens, src_lengths, prev_output_tokens, return_all_hiddens: bool = False, features_only: bool = False, **kwargs ): encoder_out = self.encoder(src_tokens, return_all_hiddens=return_all_hiddens) decoder_out = self.decoder( prev_output_tokens, encoder_out=encoder_out, features_only=features_only, return_all_hiddens=return_all_hiddens, ) return decoder_out def get_normalized_probs( self, net_output: Tuple[Tensor, Optional[Dict[str, List[Optional[Tensor]]]]], log_probs: bool, sample: Optional[Dict[str, Tensor]] = None, ): """Get normalized probabilities (or log probs) from a net's output.""" return self.get_normalized_probs_scriptable(net_output, log_probs, sample) class MTEncoder(Encoder, FairseqEncoder): def forward(self, src_tokens, **kwargs): self_attn_padding_mask = src_tokens.eq(self.dictionary.pad()) return super().forward( src_tokens=src_tokens, encoder_padding_mask=self_attn_padding_mask, **kwargs ) def reorder_encoder_out(self, encoder_out, new_order): new_encoder_out = encoder_out["encoder_out"].index_select(1, new_order) new_encoder_embedding = encoder_out["encoder_embedding"].index_select( 0, new_order ) new_encoder_padding_mask = encoder_out["encoder_padding_mask"].index_select( 0, new_order ) encoder_states = encoder_out["encoder_states"] if len(encoder_states) > 0: for idx, state in enumerate(encoder_states): encoder_states[idx] = state.index_select(1, new_order) return { "encoder_out": new_encoder_out, # T x B x C "encoder_padding_mask": new_encoder_padding_mask, "encoder_embedding": new_encoder_embedding, # B x T x C "encoder_states": encoder_states, # List[T x B x C] } def max_positions(self): return self.embed_positions.max_positions @register_model_architecture("mt", "mt_base") def base_architecture(args): args.encoder_embed_path = getattr(args, "encoder_embed_path", None) args.encoder_embed_dim = getattr(args, "encoder_embed_dim", 512) args.encoder_ffn_embed_dim = getattr(args, "encoder_ffn_embed_dim", 2048) args.encoder_layers = getattr(args, "encoder_layers", 6) args.encoder_attention_heads = getattr(args, "encoder_attention_heads", 8) args.encoder_normalize_before = getattr(args, "encoder_normalize_before", False) args.encoder_learned_pos = getattr(args, "encoder_learned_pos", False) args.decoder_embed_path = getattr(args, "decoder_embed_path", None) args.decoder_embed_dim = getattr(args, "decoder_embed_dim", args.encoder_embed_dim) args.decoder_ffn_embed_dim = getattr( args, "decoder_ffn_embed_dim", args.encoder_ffn_embed_dim ) args.decoder_layers = getattr(args, "decoder_layers", 6) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 8) args.decoder_normalize_before = getattr(args, "decoder_normalize_before", False) args.decoder_learned_pos = getattr(args, "decoder_learned_pos", False) args.attention_dropout = getattr(args, "attention_dropout", 0.0) args.activation_dropout = getattr(args, "activation_dropout", 0.0) args.activation_fn = getattr(args, "activation_fn", "relu") args.dropout = getattr(args, "dropout", 0.1) args.adaptive_softmax_cutoff = getattr(args, "adaptive_softmax_cutoff", None) args.adaptive_softmax_dropout = getattr(args, "adaptive_softmax_dropout", 0) args.share_decoder_input_output_embed = getattr( args, "share_decoder_input_output_embed", False ) args.share_all_embeddings = getattr(args, "share_all_embeddings", False) args.no_token_positional_embeddings = getattr( args, "no_token_positional_embeddings", False ) args.adaptive_input = getattr(args, "adaptive_input", False) args.no_cross_attention = getattr(args, "no_cross_attention", False) args.cross_self_attention = getattr(args, "cross_self_attention", 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) args.no_scale_embedding = getattr(args, "no_scale_embedding", False) args.layernorm_embedding = getattr(args, "layernorm_embedding", False) args.tie_adaptive_weights = getattr(args, "tie_adaptive_weights", 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 args.encoder_layers_to_keep = getattr(args, "encoder_layers_to_keep", None) args.decoder_layers_to_keep = getattr(args, "decoder_layers_to_keep", None) args.encoder_layerdrop = getattr(args, "encoder_layerdrop", 0) args.decoder_layerdrop = getattr(args, "decoder_layerdrop", 0) 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.is_moe = getattr(args, "is_moe", False) args.selected_expert_count = getattr(args, "selected_expert_count", 2)
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/models/machine_translation.py
# Copyright (c) 2022 Microsoft # Licensed under The MIT License [see LICENSE for details] import logging from dataclasses import dataclass, field from typing import Optional import torch import torch.nn as nn import torch.nn.functional as F from apex.normalization import FusedLayerNorm as LayerNorm from fairseq import utils from fairseq.dataclass import ChoiceEnum, FairseqDataclass from fairseq.models import BaseFairseqModel, register_model, register_model_architecture from fairseq.models.squad import SQuADHead from fairseq.models.transformer import DEFAULT_MIN_PARAMS_TO_WRAP, Embedding from fairseq.modules import PositionalEmbedding from omegaconf import II from torchscale.architecture.config import EncoderConfig from .machine_translation import MTEncoder as Encoder DEFAULT_MAX_SOURCE_POSITIONS = 1024 logger = logging.getLogger(__name__) @dataclass class BertConfig(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."} ) encoder_embed_dim: int = field( default=512, metadata={"help": "encoder embedding dimension"} ) encoder_output_dim: int = field( default=512, metadata={"help": "encoder output dimension"} ) encoder_input_dim: int = field( default=512, metadata={"help": "encoder input dimension"} ) encoder_ffn_embed_dim: int = field( default=2048, metadata={"help": "encoder embedding dimension for FFN"} ) encoder_layers: int = field(default=6, metadata={"help": "num encoder layers"}) encoder_attention_heads: int = field( default=8, metadata={"help": "num encoder attention heads"} ) encoder_normalize_before: bool = field( default=False, metadata={"help": "apply layernorm before each encoder block"} ) no_encoder_final_norm: bool = field( default=False, metadata={"help": "don't add an extra layernorm after the last encoder block"}, ) no_token_positional_embeddings: bool = field( default=False, metadata={ "help": "if set, disables positional embeddings (outside self attention)" }, ) share_encoder_input_output_embed: bool = field( default=False, metadata={"help": "share encoder input and output embeddings"} ) encoder_learned_pos: bool = field( default=False, metadata={"help": "use learned positional embeddings in the encoder"}, ) 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) encoder_layerdrop: float = field( default=0.0, metadata={"help": "LayerDrop probability for encoder"} ) encoder_layers_to_keep: Optional[str] = field( default=None, metadata={ "help": "which layers to *keep* when pruning as a comma-separated list" }, ) # 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." ) }, ) max_source_positions: int = field( default=1024, metadata={"help": "max source positions"} ) pooler_activation_fn: ChoiceEnum(utils.get_available_activation_fns()) = field( default="relu", metadata={"help": "activation function to use for pooler layer"} ) pooler_dropout: float = field( default=0.0, metadata={"help": "dropout probability in the masked_lm pooler layers"}, ) # 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") tpu: bool = II("common.tpu") rel_pos_buckets: int = field(default=0, metadata={"help": ""}) max_rel_pos: int = field(default=0, metadata={"help": ""}) moe_freq: int = field( default=0, metadata={"help": "Frequency at which we insert MoE Transformer layers"}, ) moe_expert_count: int = field( default=0, metadata={"help": "Number of experts in each MoE Layer"} ) moe_gating_use_fp32: bool = field( default=False, metadata={"help": "Use FP32 computations in MoE top2 gating function"}, ) moe_second_expert_policy: str = field( default="sampling", metadata={"help": "policy for second expert, options: all/sampling/random"}, ) moe_normalize_gate_prob_before_dropping: bool = field( default=False, metadata={ "help": "whether to normalize gate probs before or after dropping experts for capacity and randomization" }, ) moe_expert_ffn_dim: Optional[int] = field( default=None, metadata={"help": "MoE expert FFN dimension"} ) moe_top1_expert: Optional[bool] = field( default=False, metadata={"help": "Use top1 gate instead of top2"} ) moe_eval_capacity_token_fraction: Optional[float] = field( default=0.25, metadata={ "help": ( "Default: 0.25, Fraction of tokens as capacity during validation, " "if set to negative, use same as training. range: (0.0, 1.0]." ) }, ) moe_normalize_expert_grad: Optional[str] = field( default="world_size", metadata={ "help": "Divide expert gradients by (1) 'world_size' (2) 'sqrt_world_size'" }, ) record_a2a_perf_stats: Optional[bool] = field( default=False, metadata={"help": "records all to all perf stats during distributed training"}, ) dummy_a2a: Optional[bool] = field( default=False, metadata={ "help": "By passes all to all during distributed training by returning the input buffer as output" }, ) moe_batch_prioritized_routing: Optional[bool] = field( default=False, metadata={ "help": "if true orders token by the gate prob before capacity dropping." }, ) ddp_rank: int = II("distributed_training.distributed_rank") deepnorm: Optional[bool] = field( default=False, ) subln: Optional[bool] = field( default=False, ) @register_model("mlm", dataclass=BertConfig) class BertModel(BaseFairseqModel): def __init__(self, args, encoder): super().__init__() self.args = args self.encoder = encoder self.padding_idx = self.encoder.embed_tokens.padding_idx self.classification_heads = nn.ModuleDict() @classmethod def build_model(cls, args, task): """Build a new model instance.""" args.max_source_positions = getattr( args, "max_source_positions", DEFAULT_MAX_SOURCE_POSITIONS ) embed_tokens = cls.build_embedding( args, task.dictionary, args.encoder_embed_dim ) embed_positions = ( PositionalEmbedding( args.max_source_positions, args.encoder_embed_dim, task.dictionary.pad(), learned=args.encoder_learned_pos, ) if not args.no_token_positional_embeddings else None ) lm_head = cls.build_lm_head( args, args.encoder_embed_dim, len(task.dictionary), args.activation_fn, weight=embed_tokens.weight, ) config = EncoderConfig() config.override(args) encoder = Encoder( config, embed_tokens=embed_tokens, embed_positions=embed_positions, output_projection=lm_head, is_encoder_decoder=False, dictionary=task.dictionary, ) return cls(args, encoder) @classmethod def build_embedding(cls, args, dictionary, embed_dim, path=None): embed_tokens = Embedding(len(dictionary), embed_dim, dictionary.pad()) return embed_tokens @classmethod def build_lm_head(cls, args, embed_dim, output_dim, activation_fn, weight): return LMHead(embed_dim, output_dim, activation_fn, weight) def output_layer(self, features, masked_tokens=None): return self.encoder.output_projection(features, masked_tokens=masked_tokens) 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] = 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, ) def register_question_answering_head(self, name, num_classes=None): self.classification_heads[name] = SQuADHead( self.args.encoder_embed_dim, ) def upgrade_state_dict_named(self, state_dict, name): prefix = name + "." if name != "" else "" # 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] # noqa: E203 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 def forward( self, src_tokens=None, features_only=False, return_all_hiddens=False, classification_head_name=None, masked_tokens=None, **kwargs ): encoder_out = self.encoder( src_tokens, features_only=True, return_all_hiddens=return_all_hiddens ) x, extra = encoder_out["encoder_out"], encoder_out x = x.transpose(0, 1) if classification_head_name is not None: x = self.classification_heads[classification_head_name](x) elif not features_only: x = self.output_layer(x, masked_tokens=masked_tokens) return x, extra class ClassificationHead(nn.Module): """Head for sentence-level classification tasks.""" def __init__( self, input_dim, inner_dim, num_classes, activation_fn, pooler_dropout, ): 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 = nn.Linear(inner_dim, num_classes) 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 LMHead(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 @register_model_architecture("mlm", "mlm_base") def base_unilm_architecture(args): if hasattr(args, "encoder_final_norm"): args.no_encoder_final_norm = not args.encoder_final_norm args.dropout = getattr(args, "dropout", 0.1) args.attention_dropout = getattr(args, "attention_dropout", 0.0) args.activation_dropout = getattr(args, "activation_dropout", 0.0) args.pooler_dropout = getattr(args, "pooler_dropout", 0.0) args.encoder_embed_dim = getattr(args, "encoder_embed_dim", 768) args.encoder_ffn_embed_dim = getattr(args, "encoder_ffn_embed_dim", 3072) args.encoder_layers = getattr(args, "encoder_layers", 12) args.encoder_attention_heads = getattr(args, "encoder_attention_heads", 12) args.encoder_learned_pos = getattr(args, "encoder_learned_pos", True) args.activation_fn = getattr(args, "activation_fn", "gelu") args.pooler_activation_fn = getattr(args, "pooler_activation_fn", "tanh") args.encoder_layerdrop = getattr(args, "encoder_layerdrop", 0) args.encoder_layers_to_keep = getattr(args, "encoder_layers_to_keep", None) # args.add_bos_token = getattr(args, "add_bos_token", False) args.no_token_positional_embeddings = getattr( args, "no_token_positional_embeddings", False ) args.share_encoder_input_output_embed = getattr( args, "share_encoder_input_output_embed", True ) args.encoder_output_dim = getattr( args, "encoder_output_dim", args.encoder_embed_dim ) args.encoder_input_dim = getattr(args, "encoder_input_dim", args.encoder_embed_dim) # Model training is not stable without this args.encoder_normalize_before = getattr(args, "encoder_normalize_before", False) args.no_encoder_final_norm = getattr(args, "no_encoder_final_norm", False) args.no_scale_embedding = getattr(args, "no_scale_embedding", True) args.layernorm_embedding = getattr(args, "layernorm_embedding", True) args.checkpoint_activations = getattr(args, "checkpoint_activations", False) args.offload_activations = getattr(args, "offload_activations", False) if args.offload_activations: args.checkpoint_activations = True
KosmosX-API-main
kosmosX/torchscale/examples/fairseq/models/bert.py
KosmosX-API-main
kosmosX/unilm/__init__.py
import logging import os from dataclasses import dataclass, field import numpy as np import torch from fairseq import utils from fairseq.data import ( FairseqDataset, AppendTokenDataset, Dictionary, IdDataset, NestedDictionaryDataset, NumelDataset, PadDataset, StripTokenDataset, TokenBlockDataset, ) from fairseq import utils from fairseq.tasks import register_task from fairseq.tasks.language_modeling import LanguageModelingConfig, LanguageModelingTask from fairseq.data import Dictionary from omegaconf import II from fairseq import utils from unilm.data.utils import add_location_symbols logger = logging.getLogger(__name__) @dataclass class GenerationObjConfig(LanguageModelingConfig): required_batch_size_multiple: int = II("dataset.required_batch_size_multiple") dict_path: str = field( default="", metadata={ "help": "dictionary path" }, ) image_feature_length: int = field( default=0, metadata={ "help": "image feature length" }, ) input_resolution: int = field(default=224, metadata={"help": ""}) # newsetting location_bin_size: int = field( default=16, metadata={ "help": "used to discrete the continuous coordinates" }, ) locate_special_token: int = field( default=0, metadata={"help": "used to discrete the continuous coordinates"} ) class RawImageDataset(FairseqDataset): def __init__(self, labels): super().__init__() self.labels = labels def __getitem__(self, index): return self.labels[index] def __len__(self): return len(self.labels) def collater(self, samples): return torch.stack(samples) @register_task("generation_obj", dataclass=GenerationObjConfig) class GenerationObjTask(LanguageModelingTask): """ Sentence (or sentence pair) prediction (classification or regression) task. Args: dictionary (Dictionary): the dictionary for the input of the task """ @classmethod def setup_dictionary(cls, args, **kwargs): dictionary = None output_dictionary = None paths = utils.split_paths(args.data) assert len(paths) > 0 if len(args.dict_path) > 0: dictionary = Dictionary.load(args.dict_path) else: dictionary = Dictionary.load(os.path.join(paths[0], "dict.txt")) dictionary.add_symbol("<mask>") # location task, add patch index token as special symbols for special_symbol in add_location_symbols(args.location_bin_size, args.locate_special_token): dictionary.add_symbol(special_symbol) dictionary.pad_to_multiple_(args.required_batch_size_multiple) # pdb.set_trace() output_dictionary = dictionary logger.info("dictionary from {}: {} types".format(args.dict_path, len(dictionary))) return (dictionary, output_dictionary) def build_dataset_for_caption_inference(self, src_tokens, src_lengths, img_src_tokens, img_gpt_input_mask, **kwargs): """ Generate batches for inference. We prepend an eos token to src_tokens (or bos if `--add-bos-token` is set) and we append a <pad> to target. This is convenient both for generation with a prefix and LM scoring. """ dataset = StripTokenDataset( TokenBlockDataset( src_tokens, src_lengths, block_size=None, # ignored for "eos" break mode pad=self.source_dictionary.pad(), eos=self.source_dictionary.eos(), break_mode="eos", ), # remove eos from (end of) target sequence self.source_dictionary.eos(), ) img_gpt_input_mask = StripTokenDataset( TokenBlockDataset( img_gpt_input_mask, src_lengths, block_size=None, # ignored for "eos" break mode pad=self.source_dictionary.pad(), eos=self.source_dictionary.eos(), break_mode="eos", ), # remove eos from (end of) target sequence self.source_dictionary.eos(), ) src_dataset = dataset tgt_dataset = AppendTokenDataset(dataset, token=self.source_dictionary.pad()) return NestedDictionaryDataset( { "id": IdDataset(), "net_input": { "src_tokens": PadDataset( src_dataset, pad_idx=self.source_dictionary.pad(), left_pad=False, ), 'img_src_tokens': RawImageDataset( img_src_tokens, ), 'img_gpt_input_mask': PadDataset( img_gpt_input_mask, pad_idx=0, left_pad=False, ), "src_lengths": NumelDataset(src_dataset, reduce=False), }, "target": PadDataset( tgt_dataset, pad_idx=self.source_dictionary.pad(), left_pad=False ), }, sizes=[np.array(src_lengths)], ) def build_dataset_for_speech_inference(self, src_tokens, src_lengths, aud_src_tokens, aud_gpt_input_mask, audio_masks, **kwargs): """ Generate batches for inference. We prepend an eos token to src_tokens (or bos if `--add-bos-token` is set) and we append a <pad> to target. This is convenient both for generation with a prefix and LM scoring. """ dataset = StripTokenDataset( TokenBlockDataset( src_tokens, src_lengths, block_size=None, # ignored for "eos" break mode pad=self.source_dictionary.pad(), eos=self.source_dictionary.eos(), break_mode="eos", ), # remove eos from (end of) target sequence self.source_dictionary.eos(), ) aud_gpt_input_mask = StripTokenDataset( TokenBlockDataset( aud_gpt_input_mask, src_lengths, block_size=None, # ignored for "eos" break mode pad=self.source_dictionary.pad(), eos=self.source_dictionary.eos(), break_mode="eos", ), # remove eos from (end of) target sequence self.source_dictionary.eos(), ) src_dataset = dataset tgt_dataset = AppendTokenDataset(dataset, token=self.source_dictionary.pad()) return NestedDictionaryDataset( { "id": IdDataset(), "net_input": { "src_tokens": PadDataset( src_dataset, pad_idx=self.source_dictionary.pad(), left_pad=False, ), 'aud_src_tokens': RawImageDataset( aud_src_tokens, ), 'aud_gpt_input_mask': PadDataset( aud_gpt_input_mask, pad_idx=0, left_pad=False, ), 'aud_masks': RawImageDataset( audio_masks, ), "src_lengths": NumelDataset(src_dataset, reduce=False), }, "target": PadDataset( tgt_dataset, pad_idx=self.source_dictionary.pad(), left_pad=False ), }, sizes=[np.array(src_lengths)], ) def inference_step( self, generator, models, sample, prefix_tokens=None, constraints=None ): with torch.no_grad(): # Generation will always be conditioned on bos_token if getattr(self.args, "add_bos_token", False): bos_token = self.source_dictionary.bos() else: bos_token = self.source_dictionary.eos() if constraints is not None: raise NotImplementedError( "Constrained decoding with the language_modeling task is not supported" ) # SequenceGenerator doesn't use src_tokens directly, we need to # pass the `prefix_tokens` argument instead if prefix_tokens is None and sample["net_input"]["src_tokens"].nelement(): prefix_tokens = sample["net_input"]["src_tokens"] # if prefix_tokens[:, 0].eq(bos_token).all(): # prefix_tokens = prefix_tokens[:, 1:] return generator.generate( models, sample, prefix_tokens=prefix_tokens, bos_token=bos_token )
KosmosX-API-main
kosmosX/unilm/tasks/generation_obj.py
import os from fairseq.tasks import import_tasks tasks_dir = os.path.dirname(__file__) import_tasks(tasks_dir, "unilm.tasks")
KosmosX-API-main
kosmosX/unilm/tasks/__init__.py
from dataclasses import dataclass, field import logging import copy import torch import torch.nn as nn import torch.nn.functional as F from fairseq import checkpoint_utils from fairseq import utils from fairseq.utils import safe_getattr from fairseq.models import ( BaseFairseqModel, register_model, register_model_architecture, ) from fairseq.models.roberta import ( roberta_large_architecture, roberta_base_architecture, RobertaModel, ) from fairseq.models.transformer_lm import ( base_gpt3_architecture, ) from unilm.models.connector import build_connector from unilm.models.gpt import GPTmodel, GPTModelConfig logger = logging.getLogger(__name__) def slice_tokens_for_mlm(A, indx, num_elem=2): all_indx = indx[:,None] + torch.arange(num_elem) return A[torch.arange(all_indx.shape[0])[:,None], all_indx] @dataclass class UniGPTModelConfig(GPTModelConfig): text_encoder: str = field( default="none", metadata={ "help": "enable text encoder, options: none, roberta, electra" }, ) image_encoder: str = field( default="none", metadata={ "help": "enable image encoder, options: none, clip, beit" }, ) audio_encoder: str = field( default="none", metadata={ "help": "enable audio encoder, options: none, " }, ) # parameters for MLM connector: str = field( default='complex', metadata={ "help": "connector: none, complex, simple, xconnector" }, ) latent_query_num: int = field( default=64, metadata={ "help": "number of latent query tokens" }, ) remain_tokens: int = field( default=300, metadata={ "help": "at least k tokens to produce gpt loss" }, ) mlm_model_path: str = field( default="", metadata={"help": "mlm checkpoint path"}, ) mlm_dict: str = field( default="", metadata={"help": "mlm dict path"}, ) mlm_tokens_per_sample: int = field( default=512, metadata={"help": "mlm max length"}, ) freeze_gpt: bool = field( default=False, metadata={ "help": "freeze gpt parameters" }, ) # parameters for visual visual_model_name: str = field( default="ViT-B-16", metadata={"help": "model_name for open_clip"},) visual_pretrained: str = field( default="laion400m_e32", metadata={"help": "model_name for visual_pretrained"},) visual_output_dim: int = field( default=768, metadata={"help": "output dimension for visual_pretrained"},) visual_output_dim_str: str = field( default='768', metadata={"help": "output dimension for visual_pretrained"},) no_freeze_layer: str = field( default='', metadata={ "help": "freeze last layer of visual_pretrained" },) # parameters for speech speech_model_path: str = field( default="", metadata={"help": "speech checkpoint path"}, ) audio_output_dim: int = field( default=768, metadata={"help": "output dimension for audio_pretrained"},) # parameters for fine-tuning ft_type: int = field( default=3, metadata={ "help": "fine-tuning type: \ 1: gpt only \ 2: roberta only \ 3: roberta + gpt \ 4: roberta + gpt(freeze) \ 5: roberta(freeze) + gpt " }, ) pooler_dropout: float = field( default=0.1, metadata={"help": "mlm max length"}, ) pretrained_ckpt_path: str = field( default="", metadata={"help": "model checkpoint path"}, ) @register_model("unigptmodel", dataclass=UniGPTModelConfig) class UniGPTmodel(BaseFairseqModel): def __init__(self, args, gpt_model, text_model=None, img_model=None, aud_model=None, text_connector=None, img_connector=None, aud_connector=None, bos=0, eos=2): """ text_model: bidirectional text model, such as roberta, bert, electra img_model: image model, such as ViT, CLIP, BEIT aud_model: audio model, such as HuBERT, wavLM """ super().__init__() self.args = args self.gpt_model = gpt_model self.text_model = text_model self.text_connector = text_connector self.img_model = img_model self.img_connector = img_connector self.aud_model = aud_model self.aud_connector = aud_connector self.bos = bos self.eos = eos self.classification_heads = nn.ModuleDict() self.ft_type = args.ft_type if args.freeze_gpt: for p in self.gpt_model.parameters(): p.requires_grad = False @classmethod def build_model(cls, args, task): if hasattr(task, "all_dict"): task.dictionary = task.all_dict gpt_model = GPTmodel.build_model(args, task) logger.info("gpt args is".format()) text_model, text_connector = cls.load_text_model(args, task) img_model, img_connector = cls.load_image_model(args, task) aud_model, aud_connector = cls.load_audio_model(args, task) model = cls(args, gpt_model, text_model=text_model, text_connector=text_connector, img_model=img_model, img_connector=img_connector, aud_model=aud_model, aud_connector=aud_connector, bos=task.dictionary.bos_index, eos=task.dictionary.eos_index) if args.pretrained_ckpt_path != "": state = checkpoint_utils.load_checkpoint_to_cpu(args.pretrained_ckpt_path) model.load_state_dict(state["model"], strict=True, args=args) # freeze text model if model.text_model is not None: for p in model.text_model.parameters(): p.requires_grad = False # freeze image model if model.img_model is not None: for p_name, p in model.img_model.named_parameters(): if args.no_freeze_layer: no_freeze_layers = args.no_freeze_layer.split(',') for no_freeze_layer in no_freeze_layers: if no_freeze_layer in p_name: print("no_freeze_layer: {}".format(p_name)) p.requires_grad = True break p.requires_grad = False # freeze audio model if model.aud_model is not None: for p in model.aud_model.parameters(): p.requires_grad = False return model def forward(self, src_tokens, mlm_src_tokens=None, gpt_input_mask=None, img_src_tokens=None, img_gpt_input_mask=None, aud_src_tokens=None, aud_gpt_input_mask=None, gpt_loss_mask=None, mlm_mask=None, classification_head_name=None, **kwargs): if classification_head_name is None: # pre-training if mlm_src_tokens is not None: # mlm mlm_output, _ = self.text_model(mlm_src_tokens, features_only=True) mlm_output = mlm_output[mlm_mask] if self.text_connector is not None: # linear projection layer mlm_output = self.text_connector(mlm_output) else: mlm_output = None if img_src_tokens is not None: img_output = self.get_image_representation(img_src_tokens) else: img_output = None if aud_src_tokens is not None: aud_output = self.get_audio_representation(aud_src_tokens, kwargs['aud_mask']) else: aud_output = None # pdb.set_trace() # gpt x, extra = self.gpt_model(src_tokens, mlm_features=mlm_output, gpt_input_mask=gpt_input_mask, img_features=img_output, img_gpt_input_mask=img_gpt_input_mask, aud_features=aud_output, aud_gpt_input_mask=aud_gpt_input_mask, **kwargs) # loss mask extra["loss_mask"] = gpt_loss_mask return x, extra # fine-tuning raise NotImplementedError def get_image_representation(self, img_src_tokens): # image img_output = self.img_model(img_src_tokens) src_len = img_output.size(0) img_output = img_output.transpose(0, 1) # T x B x C -> B x T x C img_output = img_output.reshape(-1, img_output.size(-1)) if self.img_connector is not None: img_output = self.img_connector(img_output, src_len=src_len) return img_output def get_audio_representation(self, aud_src_tokens, aud_mask): raise NotImplementedError 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] = 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.ft_type ) 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) @property def supported_targets(self): return {"future"} def upgrade_state_dict_named(self, state_dict, name): prefix = name + '.' if name != '' else '' # 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 @classmethod def load_text_model(cls, args, task): """Load a roberta model from the fairseq library.""" if args.text_encoder == "none": return None, None mlm_args = copy.deepcopy(args) mlm_task = task logger.info("Roberta dictionary: {} types".format(len(mlm_task.dictionary))) mlm_args.layernorm_embedding = True mlm_args.no_scale_embedding = True mlm_args.dropout = 0.1 mlm_args.attention_dropout = 0.1 mlm_args.tokens_per_sample = mlm_args.mlm_tokens_per_sample mlm_model = RobertaModel.build_model(mlm_args, mlm_task) logger.info("mlm args is {}".format(mlm_args)) if args.mlm_model_path != "": state = checkpoint_utils.load_checkpoint_to_cpu(args.mlm_model_path) mlm_model.load_state_dict(state["model"], strict=True, args=mlm_args) connector = build_connector(args, args.encoder_embed_dim, args.decoder_embed_dim) return mlm_model, connector @classmethod def load_image_model(cls, args, task): def build_backbone_clip(args, visual_model_name, visual_pretrained): from unilm.models.vl.clip import create_model force_quick_gelu = False if 'ViT-L' in visual_model_name: force_quick_gelu = True model = create_model(visual_model_name, pretrained=visual_pretrained, force_quick_gelu=force_quick_gelu) return model if args.image_encoder == "none": return None, None if args.image_encoder == "clip": model = build_backbone_clip(args, args.visual_model_name, args.visual_pretrained) connector = build_connector(args, args.visual_output_dim, args.decoder_embed_dim) return model, connector raise NotImplementedError("Unknown model name {}".format(args.image_encoder)) @classmethod def load_audio_model(cls, args, task): return None, None class ClassificationHead(nn.Module): """Head for sentence-level classification tasks.""" def __init__( self, input_dim, inner_dim, num_classes, activation_fn, pooler_dropout, ft_type ): 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 = nn.Linear(inner_dim, num_classes) self.ft_type = ft_type 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 @register_model_architecture("unigptmodel", "unigptmodel_small") def gptmodel_small(args): # 125M params args.decoder_layers = safe_getattr(args, "decoder_layers", 12) args.decoder_embed_dim = safe_getattr(args, "decoder_embed_dim", 768) args.decoder_attention_heads = safe_getattr(args, "decoder_attention_heads", 12) args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", False) base_gpt3_architecture(args) roberta_base_architecture(args) @register_model_architecture("unigptmodel", "unigptmodel_medium") def gptmodel_medium(args): # 355M params args.decoder_layers = safe_getattr(args, "decoder_layers", 24) args.decoder_embed_dim = safe_getattr(args, "decoder_embed_dim", 1024) args.decoder_attention_heads = safe_getattr(args, "decoder_attention_heads", 16) args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", False) args.pooler_dropout = safe_getattr(args, "pooler_dropout", 0.1) base_gpt3_architecture(args) roberta_base_architecture(args) @register_model_architecture("unigptmodel", "unigptmodel_large") def gptmodel_large(args): # 1.3B params args.decoder_layers = safe_getattr(args, "decoder_layers", 24) args.decoder_embed_dim = safe_getattr(args, "decoder_embed_dim", 1536) args.decoder_attention_heads = safe_getattr(args, "decoder_attention_heads", 16) args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", False) args.dropout = safe_getattr(args, "dropout", 0.1) args.attention_dropout = safe_getattr(args, "attention_dropout", 0.1) base_gpt3_architecture(args) roberta_large_architecture(args) @register_model_architecture("unigptmodel", "unigptmodel_xl") def gptmodel_xl(args): # 1.3B params args.decoder_layers = safe_getattr(args, "decoder_layers", 24) args.decoder_embed_dim = safe_getattr(args, "decoder_embed_dim", 2048) args.decoder_attention_heads = safe_getattr(args, "decoder_attention_heads", 32) args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", False) args.dropout = safe_getattr(args, "dropout", 0.1) args.attention_dropout = safe_getattr(args, "attention_dropout", 0.1) base_gpt3_architecture(args) roberta_large_architecture(args) @register_model_architecture("unigptmodel", "unigptmodel_2b") def gptmodel_2B(args): # 1.3B params args.decoder_layers = safe_getattr(args, "decoder_layers", 36) args.decoder_embed_dim = safe_getattr(args, "decoder_embed_dim", 2048) args.decoder_attention_heads = safe_getattr(args, "decoder_attention_heads", 32) args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", False) args.dropout = safe_getattr(args, "dropout", 0.1) args.attention_dropout = safe_getattr(args, "attention_dropout", 0.1) base_gpt3_architecture(args) roberta_large_architecture(args) @register_model_architecture("unigptmodel", "unigptmodel_6b") def gptmodel_6B(args): # 1.3B params args.decoder_layers = safe_getattr(args, "decoder_layers", 40) args.decoder_embed_dim = safe_getattr(args, "decoder_embed_dim", 3584) args.decoder_attention_heads = safe_getattr(args, "decoder_attention_heads", 28) args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", False) args.dropout = safe_getattr(args, "dropout", 0.1) args.attention_dropout = safe_getattr(args, "attention_dropout", 0.1) base_gpt3_architecture(args) roberta_large_architecture(args)
KosmosX-API-main
kosmosX/unilm/models/unigpt.py
import os from fairseq.models import import_models models_dir = os.path.dirname(__file__) import_models(models_dir, "unilm.models")
KosmosX-API-main
kosmosX/unilm/models/__init__.py
import torch import torch.nn as nn from fairseq.modules import MultiheadAttention from fairseq import utils def build_connector(args, input_dim, output_dim): if isinstance(args, str): connector_name = args else: connector_name = args.text_connector if hasattr(args, "text_connector") else args.connector if connector_name == "none": connector = None elif connector_name == "complex": connector = ComplexConnector(input_dim, output_dim, args.activation_fn) elif connector_name == "simple": connector = SimpleConnector(input_dim, output_dim) elif connector_name == "xconnector": connector = XConnector(input_dim, output_dim, args) else: raise ValueError("Invalid text connector type: {}".format(connector_name)) return connector class SimpleConnector(nn.Module): """Connector model of GPT and MLM.""" def __init__(self, input_dim, output_dim): super().__init__() self.dense = nn.Linear(input_dim, output_dim) def forward(self, features, **kwargs): x = self.dense(features) return x class ComplexConnector(nn.Module): """Connector model of GPT and MLM.""" def __init__(self, input_dim, output_dim, activation_fn): super().__init__() self.dense = nn.Linear(input_dim, input_dim) self.activation_fn = utils.get_activation_fn(activation_fn) self.predict = nn.Linear(input_dim, output_dim) def forward(self, features, **kwargs): x = self.dense(features) x = self.activation_fn(x) x = self.predict(x) return x class XConnector(nn.Module): """Connector model of GPT and MLM.""" def __init__(self, input_dim, output_dim, args,): super().__init__() self.dense = nn.Linear(input_dim, output_dim) self.latent_query = torch.nn.Parameter(torch.randn(args.latent_query_num, output_dim)) self.x_attn = MultiheadAttention( output_dim, args.decoder_attention_heads, kdim=output_dim, vdim=output_dim, dropout=args.attention_dropout, encoder_decoder_attention=True, ) def forward(self, features, **kwargs): x = self.dense(features) # x = attention_i(q=latent_query, kv=concat([x, latent_query])) # shape of x is [batch_size * seq_len, output_dim] -> [seq_len, batch_size, output_dim] x = x.view(-1, kwargs['src_len'], x.size(-1)).transpose(0, 1) bsz = x.size(1) latent_query = self.latent_query.unsqueeze(1).expand(-1, bsz, -1) x, _ = self.x_attn(latent_query, torch.cat([x, latent_query]), torch.cat([x, latent_query])) return x.transpose(0, 1).contiguous().view(-1, x.size(-1)) # [batch_size * seq_len, output_dim]
KosmosX-API-main
kosmosX/unilm/models/connector.py
from dataclasses import dataclass, field from typing import Optional from torch import Tensor import torch from fairseq import distributed_utils from fairseq.utils import safe_getattr from fairseq.models import ( register_model, register_model_architecture, ) from fairseq.models.transformer_lm import ( TransformerLanguageModelConfig, TransformerLanguageModel, base_gpt3_architecture, ) from fairseq.models import ( FairseqIncrementalDecoder, register_model, register_model_architecture, ) from fairseq.models.transformer import Embedding from fairseq.modules import PositionalEmbedding from omegaconf import II from torchscale.architecture.config import DecoderConfig from torchscale.architecture.decoder import Decoder from torchscale.architecture.config import DecoderConfig from torchscale.architecture.decoder import Decoder DEFAULT_MAX_TARGET_POSITIONS = 1024 @dataclass class GPTModelConfig(TransformerLanguageModelConfig): scale_final_logits: bool = field( default=False, metadata={ "help": "scale final logits by sqrt(d)" }, ) gpt_model_path: str = field( default="", metadata={"help": "gpt checkpoint path"}, ) rescale_init: bool = field( default=False, metadata={ "help": "whether to use rescale initialization" }, ) deepnet: bool = field( default=False, metadata={ "help": "enable deepnet in decoder" }, ) last_ln_scale: bool = field( default=False, metadata={ "help": "enable last_ln_scale in decoder" }, ) # 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") memory_efficient_fp16: bool = II("common.memory_efficient_fp16") fp16: bool = II("common.fp16") fp16_no_flatten_grads: bool = II("common.fp16_no_flatten_grads") ddp_backend: str = II("distributed_training.ddp_backend") world_size: int = II("distributed_training.distributed_world_size") distributed_rank: int = II("distributed_training.distributed_rank") ddp_rank: int = II("distributed_training.distributed_rank") deepnorm: Optional[bool] = field( default=False, ) subln: Optional[bool] = field( default=False, ) rel_pos_buckets: Optional[int] = field( default=0, ) max_rel_pos: Optional[int] = field( default=0, ) flash_attention: bool = field( default=False, ) sope_rel_pos: Optional[bool] = field( default=False, metadata={"help": "use SoPE as the relative position embhedding"}, ) scale_length: Optional[int] = field( default=2048, ) @register_model("gptmodel", dataclass=GPTModelConfig) class GPTmodel(TransformerLanguageModel): @classmethod def build_model(cls, args, task): model = TransformerLanguageModel.build_model(args, task) if getattr(args, "max_target_positions", None) is None: args.max_target_positions = getattr( args, "tokens_per_sample", DEFAULT_MAX_TARGET_POSITIONS ) embed_tokens = cls.build_embedding( args, task.source_dictionary, args.decoder_embed_dim ) embed_positions = ( PositionalEmbedding( args.max_target_positions, args.decoder_embed_dim, task.dictionary.pad(), learned=args.decoder_learned_pos, ) if not args.no_token_positional_embeddings else None ) if args.share_decoder_input_output_embed: output_projection = torch.nn.Linear( embed_tokens.weight.shape[1], embed_tokens.weight.shape[0], bias=False, ) output_projection.weight = embed_tokens.weight else: output_projection = torch.nn.Linear( args.decoder_embed_dim, len(task.dictionary), bias=False ) torch.nn.init.normal_( output_projection.weight, mean=0, std=args.decoder_embed_dim**-0.5 ) if getattr(args, "moe_freq", 0) > 0 and ( getattr(args, "fp16", False) and not getattr(args, "memory_efficient_fp16", False) and getattr(args, "ddp_backend", None) != "fully_sharded" ): assert ( args.fp16_no_flatten_grads ), "If training moe models, set --fp16-no-flatten-grads to calculate correct gradnorm" args.ddp_rank = distributed_utils.get_data_parallel_rank() config = DecoderConfig() config.override(args) decoder = LMDecoder( config, embed_tokens, embed_positions, output_projection, is_encoder_decoder=False, dictionary=task.dictionary, ) model.decoder = decoder if args.gpt_model_path != "": assert NotImplementedError # state = checkpoint_utils.load_checkpoint_to_cpu(args.gpt_model_path) # model.load_state_dict(state["model"], strict=True, args=args) return model @classmethod def build_embedding(cls, args, dictionary, embed_dim, path=None): return Embedding(len(dictionary), embed_dim, dictionary.pad()) class LMDecoder(Decoder, FairseqIncrementalDecoder): def forward(self, src_tokens, **kwargs): self_attn_padding_mask = src_tokens.eq(self.dictionary.pad()) return super().forward(src_tokens, self_attn_padding_mask, **kwargs) def max_positions(self): return self.embed_positions.max_positions def reorder_incremental_state_scripting( self, incremental_state, new_order, ): for module in incremental_state: for key in incremental_state[module]: result = incremental_state[module][key].index_select(0, new_order) incremental_state[module][key] = result def forward_embedding( self, tokens, token_embedding=None, incremental_state=None, first_step: bool = False, mlm_features: Optional[Tensor] = None, gpt_input_mask: Optional[Tensor] = None, img_features: Optional[Tensor] = None, img_gpt_input_mask: Optional[Tensor] = None, aud_features: Optional[Tensor] = None, aud_gpt_input_mask: Optional[Tensor] = None, ): positions = None if self.embed_positions is not None: positions = self.embed_positions( tokens, incremental_state=incremental_state ) if incremental_state is not None and not first_step: tokens = tokens[:, -1:] if positions is not None: positions = positions[:, -1:] if token_embedding is None: token_embedding = self.embed_tokens(tokens) gpt_embed_output = token_embedding if mlm_features is not None: gpt_embed_output[gpt_input_mask] = mlm_features if img_features is not None: gpt_embed_output[img_gpt_input_mask] = img_features if aud_features is not None: gpt_embed_output[aud_gpt_input_mask] = aud_features x = embed = self.embed_scale * gpt_embed_output if positions is not None: x += positions if self.layernorm_embedding is not None: x = self.layernorm_embedding(x) x = self.dropout_module(x) return x, embed def forward( self, prev_output_tokens, self_attn_padding_mask=None, encoder_out=None, incremental_state=None, features_only=False, return_all_hiddens=False, token_embeddings=None, first_step=False, **kwargs ): # embed tokens and positions x, _ = self.forward_embedding( prev_output_tokens, token_embeddings, incremental_state, first_step=first_step, **kwargs ) x = x.transpose(0, 1) # relative position self_attn_rel_pos_bias = None slen = prev_output_tokens.size(1) if self.self_attn_relative_position is not None: self_attn_rel_pos_bias = self.self_attn_relative_position( batch_size=x.size(1), qlen=slen, klen=slen ) if incremental_state is not None: self_attn_rel_pos_bias = self_attn_rel_pos_bias[:, -1:, :] cross_attn_rel_pos_bias = None if self.cross_attn_relative_position is not None: cross_attn_rel_pos_bias = self.cross_attn_relative_position( batch_size=x.size(1), qlen=slen, klen=encoder_out["encoder_out"].size(0), ) if incremental_state is not None: cross_attn_rel_pos_bias = cross_attn_rel_pos_bias[:, -1:, :] self_attn_sope_rel_pos = None # slen = prev_output_tokens.size(1) # if self.self_attn_sope is not None: # # offset = 0 if (incremental_state is None or first_step) else incremental_state[0]["prev_key"].shape[2] # # self_attn_sope_rel_pos = self.self_attn_sope(slen, offset) # rel_pos_len = slen if (incremental_state is None or first_step) else (incremental_state[0]["prev_key"].shape[2] + 1) # self_attn_sope_rel_pos = self.self_attn_sope(rel_pos_len) cross_attn_sope_rel_pos = None if self.cross_attn_sope is not None: cross_attn_sope_rel_pos = self.cross_attn_sope(slen + encoder_out["encoder_out"].size(0)) # decoder layers inner_states = [x] if encoder_out is None: l_aux = [] else: l_aux = encoder_out["l_aux"] if "l_aux" in encoder_out else [] for idx, layer in enumerate(self.layers): if incremental_state is None or first_step: self_attn_mask = torch.triu( torch.zeros([x.size(0), x.size(0)]) .float() .fill_(float("-inf")) .type_as(x), 1, ) if first_step and incremental_state is not None: if idx not in incremental_state: incremental_state[idx] = {} else: self_attn_mask = None if idx not in incremental_state: incremental_state[idx] = {} x, layer_attn, _, l_aux_i = layer( x, encoder_out["encoder_out"] if encoder_out is not None else None, encoder_out["encoder_padding_mask"] if encoder_out is not None else None, incremental_state[idx] if incremental_state is not None else None, self_attn_mask=self_attn_mask, self_attn_padding_mask=self_attn_padding_mask, self_attn_rel_pos=self_attn_rel_pos_bias, cross_attn_rel_pos=cross_attn_rel_pos_bias, self_attn_sope_rel_pos=self_attn_sope_rel_pos, cross_attn_sope_rel_pos=cross_attn_sope_rel_pos, ) l_aux.append(l_aux_i) inner_states.append(x) if self.layer_norm is not None: x = self.layer_norm(x) x = x.transpose(0, 1) if not features_only: x = self.output_layer(x) return x, { "inner_states": inner_states, "l_aux": l_aux, "attn": None, } @register_model_architecture("gptmodel", "gptmodel_small") def gptmodel_small(args): # 125M params args.decoder_layers = safe_getattr(args, "decoder_layers", 12) args.decoder_embed_dim = safe_getattr(args, "decoder_embed_dim", 768) args.decoder_attention_heads = safe_getattr(args, "decoder_attention_heads", 12) args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", False) base_gpt3_architecture(args) @register_model_architecture("gptmodel", "gptmodel_medium") def gptmodel_medium(args): # 350M params args.decoder_layers = safe_getattr(args, "decoder_layers", 24) args.decoder_embed_dim = safe_getattr(args, "decoder_embed_dim", 1024) args.decoder_attention_heads = safe_getattr(args, "decoder_attention_heads", 16) args.decoder_learned_pos = safe_getattr(args, "decoder_learned_pos", False) base_gpt3_architecture(args)
KosmosX-API-main
kosmosX/unilm/models/gpt.py
KosmosX-API-main
kosmosX/unilm/models/vl/__init__.py
# TODO load openai model
KosmosX-API-main
kosmosX/unilm/models/vl/openai.py
# 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 typing import Dict, List, Optional import sys import torch import torch.nn as nn from fairseq import search from fairseq.models import FairseqIncrementalDecoder from torch import Tensor from fairseq.ngram_repeat_block import NGramRepeatBlock class SequenceGenerator(nn.Module): def __init__( self, models, tgt_dict, beam_size=1, max_len_a=0, max_len_b=200, max_len=0, min_len=1, normalize_scores=True, len_penalty=1.0, unk_penalty=0.0, temperature=1.0, match_source_len=False, no_repeat_ngram_size=0, search_strategy=None, eos=None, symbols_to_strip_from_output=None, lm_model=None, lm_weight=1.0, ): """Generates translations of a given source sentence. Args: models (List[~fairseq.models.FairseqModel]): ensemble of models, currently support fairseq.models.TransformerModel for scripting beam_size (int, optional): beam width (default: 1) max_len_a/b (int, optional): generate sequences of maximum length ax + b, where x is the source length max_len (int, optional): the maximum length of the generated output (not including end-of-sentence) min_len (int, optional): the minimum length of the generated output (not including end-of-sentence) normalize_scores (bool, optional): normalize scores by the length of the output (default: True) len_penalty (float, optional): length penalty, where <1.0 favors shorter, >1.0 favors longer sentences (default: 1.0) unk_penalty (float, optional): unknown word penalty, where <0 produces more unks, >0 produces fewer (default: 0.0) temperature (float, optional): temperature, where values >1.0 produce more uniform samples and values <1.0 produce sharper samples (default: 1.0) match_source_len (bool, optional): outputs should match the source length (default: False) """ super().__init__() if isinstance(models, EnsembleModel): self.model = models else: self.model = EnsembleModel(models) self.tgt_dict = tgt_dict self.pad = tgt_dict.pad() self.unk = tgt_dict.unk() self.eos = tgt_dict.eos() if eos is None else eos self.symbols_to_strip_from_output = ( symbols_to_strip_from_output.union({self.eos}) if symbols_to_strip_from_output is not None else {self.eos} ) self.vocab_size = len(tgt_dict) self.beam_size = beam_size # the max beam size is the dictionary size - 1, since we never select pad self.beam_size = min(beam_size, self.vocab_size - 1) self.max_len_a = max_len_a self.max_len_b = max_len_b self.min_len = min_len self.max_len = max_len or self.model.max_decoder_positions() self.normalize_scores = normalize_scores self.len_penalty = len_penalty self.unk_penalty = unk_penalty self.temperature = temperature self.match_source_len = match_source_len if no_repeat_ngram_size > 0: self.repeat_ngram_blocker = NGramRepeatBlock(no_repeat_ngram_size) else: self.repeat_ngram_blocker = None assert temperature > 0, "--temperature must be greater than 0" self.search = ( search.BeamSearch(tgt_dict) if search_strategy is None else search_strategy ) # We only need to set src_lengths in LengthConstrainedBeamSearch. # As a module attribute, setting it would break in multithread # settings when the model is shared. self.should_set_src_lengths = ( hasattr(self.search, "needs_src_lengths") and self.search.needs_src_lengths ) self.model.eval() self.lm_model = lm_model self.lm_weight = lm_weight if self.lm_model is not None: self.lm_model.eval() def cuda(self): self.model.cuda() return self @torch.no_grad() def forward( self, sample: Dict[str, Dict[str, Tensor]], prefix_tokens: Optional[Tensor] = None, bos_token: Optional[int] = None, ): """Generate a batch of translations. Args: sample (dict): batch prefix_tokens (torch.LongTensor, optional): force decoder to begin with these tokens bos_token (int, optional): beginning of sentence token (default: self.eos) """ return self._generate(sample, prefix_tokens, bos_token=bos_token) # TODO(myleott): unused, deprecate after pytorch-translate migration # def generate_batched_itr(self, data_itr, beam_size=None, cuda=False, timer=None): # """Iterate over a batched dataset and yield individual translations. # Args: # cuda (bool, optional): use GPU for generation # timer (StopwatchMeter, optional): time generations # """ # for sample in data_itr: # s = utils.move_to_cuda(sample) if cuda else sample # if "net_input" not in s: # continue # input = s["net_input"] # # 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 input.items() if k != "prev_output_tokens" # } # if timer is not None: # timer.start() # with torch.no_grad(): # hypos = self.generate(encoder_input) # if timer is not None: # timer.stop(sum(len(h[0]["tokens"]) for h in hypos)) # for i, id in enumerate(s["id"].data): # # remove padding # src = utils.strip_pad(input["src_tokens"].data[i, :], self.pad) # ref = ( # utils.strip_pad(s["target"].data[i, :], self.pad) # if s["target"] is not None # else None # ) # yield id, src, ref, hypos[i] @torch.no_grad() def generate( self, models, sample: Dict[str, Dict[str, Tensor]], **kwargs ) -> List[List[Dict[str, Tensor]]]: """Generate translations. Match the api of other fairseq generators. Args: models (List[~fairseq.models.FairseqModel]): ensemble of models sample (dict): batch prefix_tokens (torch.LongTensor, optional): force decoder to begin with these tokens constraints (torch.LongTensor, optional): force decoder to include the list of constraints bos_token (int, optional): beginning of sentence token (default: self.eos) """ return self._generate(sample, **kwargs) def _generate( self, sample: Dict[str, Dict[str, Tensor]], prefix_tokens: Optional[Tensor] = None, constraints: Optional[Tensor] = None, bos_token: Optional[int] = None, ): incremental_states = torch.jit.annotate( List[Dict[str, Dict[str, Optional[Tensor]]]], [ torch.jit.annotate(Dict[str, Dict[str, Optional[Tensor]]], {}) for i in range(self.model.models_size) ], ) net_input = sample["net_input"] if "src_tokens" in net_input: raise NotImplementedError elif "image" in net_input: image = net_input["image"] src_lengths = [0] else: raise Exception( "expected src_tokens or source in net input. input keys: " + str(net_input.keys()) ) # bsz: total number of sentences in beam # Note that src_tokens may have more than 2 dimensions (i.e. audio features) bsz = image.size(0) beam_size = self.beam_size if constraints is not None and not self.search.supports_constraints: raise NotImplementedError( "Target-side constraints were provided, but search method doesn't support them" ) # Initialize constraints, when active self.search.init_constraints(constraints, beam_size) max_len = self.max_len assert ( self.min_len <= max_len ), "min_len cannot be larger than max_len, please adjust these!" # compute the encoder output for each beam with torch.autograd.profiler.record_function("EnsembleModel: forward_encoder"): encoder_outs = self.model.forward_encoder(net_input) # placeholder of indices for bsz * beam_size to hold tokens and accumulative scores new_order = torch.arange(bsz).view(-1, 1).repeat(1, beam_size).view(-1) new_order = new_order.to(image.device).long() encoder_outs = self.model.reorder_encoder_out(encoder_outs, new_order) # ensure encoder_outs is a List. assert encoder_outs is not None # initialize buffers scores = ( torch.zeros(bsz * beam_size, max_len + 1).to(image).float() ) # +1 for eos; pad is never chosen for scoring tokens = ( torch.zeros(bsz * beam_size, max_len + 2) .to(image) .long() .fill_(self.pad) ) # +2 for eos and pad tokens[:, 0] = self.eos if bos_token is None else bos_token attn: Optional[Tensor] = None # A list that indicates candidates that should be ignored. # For example, suppose we're sampling and have already finalized 2/5 # samples. Then cands_to_ignore would mark 2 positions as being ignored, # so that we only finalize the remaining 3 samples. cands_to_ignore = ( torch.zeros(bsz, beam_size).to(image).eq(-1) ) # forward and backward-compatible False mask # list of completed sentences finalized = torch.jit.annotate( List[List[Dict[str, Tensor]]], [torch.jit.annotate(List[Dict[str, Tensor]], []) for i in range(bsz)], ) # contains lists of dictionaries of infomation about the hypothesis being finalized at each step # a boolean array indicating if the sentence at the index is finished or not finished = [False for i in range(bsz)] num_remaining_sent = bsz # number of sentences remaining # number of candidate hypos per step cand_size = 2 * beam_size # 2 x beam size in case half are EOS # offset arrays for converting between different indexing schemes bbsz_offsets = ( (torch.arange(0, bsz) * beam_size) .unsqueeze(1) .type_as(tokens) .to(image.device) ) cand_offsets = torch.arange(0, cand_size).type_as(tokens).to(image.device) reorder_state: Optional[Tensor] = None batch_idxs: Optional[Tensor] = None original_batch_idxs: Optional[Tensor] = None if "id" in sample and isinstance(sample["id"], Tensor): original_batch_idxs = sample["id"] else: original_batch_idxs = torch.arange(0, bsz).type_as(tokens) for step in range(max_len + 1): # one extra step for EOS marker # reorder decoder internal states based on the prev choice of beams if reorder_state is not None: if batch_idxs is not None: # update beam indices to take into account removed sentences corr = batch_idxs - torch.arange(batch_idxs.numel()).type_as( batch_idxs ) reorder_state.view(-1, beam_size).add_( corr.unsqueeze(-1) * beam_size ) original_batch_idxs = original_batch_idxs[batch_idxs] self.model.reorder_incremental_state(incremental_states, reorder_state) encoder_outs = self.model.reorder_encoder_out( encoder_outs, reorder_state ) with torch.autograd.profiler.record_function( "EnsembleModel: forward_decoder" ): lprobs, avg_attn_scores = self.model.forward_decoder( tokens[:, : step + 1], encoder_outs, incremental_states, self.temperature, ) if self.lm_model is not None: lm_out = self.lm_model(tokens[:, : step + 1]) probs = self.lm_model.get_normalized_probs( lm_out, log_probs=True, sample=None ) probs = probs[:, -1, :] * self.lm_weight lprobs += probs lprobs[lprobs != lprobs] = torch.tensor(-math.inf).to(lprobs) lprobs[:, self.pad] = -math.inf # never select pad lprobs[:, self.unk] -= self.unk_penalty # apply unk penalty # handle max length constraint if step >= max_len: lprobs[:, : self.eos] = -math.inf lprobs[:, self.eos + 1 :] = -math.inf # handle prefix tokens (possibly with different lengths) if ( prefix_tokens is not None and step < prefix_tokens.size(1) and step < max_len ): lprobs, tokens, scores = self._prefix_tokens( step, lprobs, scores, tokens, prefix_tokens, beam_size ) elif step < self.min_len: # minimum length constraint (does not apply if using prefix_tokens) lprobs[:, self.eos] = -math.inf # Record attention scores, only support avg_attn_scores is a Tensor if avg_attn_scores is not None: if attn is None: attn = torch.empty( bsz * beam_size, avg_attn_scores.size(1), max_len + 2 ).to(scores) attn[:, :, step + 1].copy_(avg_attn_scores) scores = scores.type_as(lprobs) eos_bbsz_idx = torch.empty(0).to( tokens ) # indices of hypothesis ending with eos (finished sentences) eos_scores = torch.empty(0).to( scores ) # scores of hypothesis ending with eos (finished sentences) if self.should_set_src_lengths: pass # self.search.set_src_lengths(src_lengths) if self.repeat_ngram_blocker is not None: lprobs = self.repeat_ngram_blocker(tokens, lprobs, bsz, beam_size, step) # Shape: (batch, cand_size) cand_scores, cand_indices, cand_beams = self.search.step( step, lprobs.view(bsz, -1, self.vocab_size), scores.view(bsz, beam_size, -1)[:, :, :step], tokens[:, : step + 1], original_batch_idxs, ) # cand_bbsz_idx contains beam indices for the top candidate # hypotheses, with a range of values: [0, bsz*beam_size), # and dimensions: [bsz, cand_size] cand_bbsz_idx = cand_beams.add(bbsz_offsets) # finalize hypotheses that end in eos # Shape of eos_mask: (batch size, beam size) eos_mask = cand_indices.eq(self.eos) & cand_scores.ne(-math.inf) eos_mask[:, :beam_size][cands_to_ignore] = torch.tensor(0).to(eos_mask) # only consider eos when it's among the top beam_size indices # Now we know what beam item(s) to finish # Shape: 1d list of absolute-numbered eos_bbsz_idx = torch.masked_select( cand_bbsz_idx[:, :beam_size], mask=eos_mask[:, :beam_size] ) finalized_sents: List[int] = [] if eos_bbsz_idx.numel() > 0: eos_scores = torch.masked_select( cand_scores[:, :beam_size], mask=eos_mask[:, :beam_size] ) finalized_sents = self.finalize_hypos( step, eos_bbsz_idx, eos_scores, tokens, scores, finalized, finished, beam_size, attn, src_lengths, max_len, ) num_remaining_sent -= len(finalized_sents) assert num_remaining_sent >= 0 if num_remaining_sent == 0: break if self.search.stop_on_max_len and step >= max_len: break assert step < max_len, f"{step} < {max_len}" # Remove finalized sentences (ones for which {beam_size} # finished hypotheses have been generated) from the batch. if len(finalized_sents) > 0: new_bsz = bsz - len(finalized_sents) # construct batch_idxs which holds indices of batches to keep for the next pass batch_mask = torch.ones( bsz, dtype=torch.bool, device=cand_indices.device ) batch_mask[finalized_sents] = False # TODO replace `nonzero(as_tuple=False)` after TorchScript supports it batch_idxs = torch.arange( bsz, device=cand_indices.device ).masked_select(batch_mask) # Choose the subset of the hypothesized constraints that will continue self.search.prune_sentences(batch_idxs) eos_mask = eos_mask[batch_idxs] cand_beams = cand_beams[batch_idxs] bbsz_offsets.resize_(new_bsz, 1) cand_bbsz_idx = cand_beams.add(bbsz_offsets) cand_scores = cand_scores[batch_idxs] cand_indices = cand_indices[batch_idxs] if prefix_tokens is not None: prefix_tokens = prefix_tokens[batch_idxs] src_lengths = src_lengths[batch_idxs] cands_to_ignore = cands_to_ignore[batch_idxs] scores = scores.view(bsz, -1)[batch_idxs].view(new_bsz * beam_size, -1) tokens = tokens.view(bsz, -1)[batch_idxs].view(new_bsz * beam_size, -1) if attn is not None: attn = attn.view(bsz, -1)[batch_idxs].view( new_bsz * beam_size, attn.size(1), -1 ) bsz = new_bsz else: batch_idxs = None # Set active_mask so that values > cand_size indicate eos hypos # and values < cand_size indicate candidate active hypos. # After, the min values per row are the top candidate active hypos # Rewrite the operator since the element wise or is not supported in torchscript. eos_mask[:, :beam_size] = ~((~cands_to_ignore) & (~eos_mask[:, :beam_size])) active_mask = torch.add( eos_mask.type_as(cand_offsets) * cand_size, cand_offsets[: eos_mask.size(1)], ) # get the top beam_size active hypotheses, which are just # the hypos with the smallest values in active_mask. # {active_hypos} indicates which {beam_size} hypotheses # from the list of {2 * beam_size} candidates were # selected. Shapes: (batch size, beam size) new_cands_to_ignore, active_hypos = torch.topk( active_mask, k=beam_size, dim=1, largest=False ) # update cands_to_ignore to ignore any finalized hypos. cands_to_ignore = new_cands_to_ignore.ge(cand_size)[:, :beam_size] # Make sure there is at least one active item for each sentence in the batch. assert (~cands_to_ignore).any(dim=1).all() # update cands_to_ignore to ignore any finalized hypos # {active_bbsz_idx} denotes which beam number is continued for each new hypothesis (a beam # can be selected more than once). active_bbsz_idx = torch.gather(cand_bbsz_idx, dim=1, index=active_hypos) active_scores = torch.gather(cand_scores, dim=1, index=active_hypos) active_bbsz_idx = active_bbsz_idx.view(-1) active_scores = active_scores.view(-1) # copy tokens and scores for active hypotheses # Set the tokens for each beam (can select the same row more than once) tokens[:, : step + 1] = torch.index_select( tokens[:, : step + 1], dim=0, index=active_bbsz_idx ) # Select the next token for each of them tokens.view(bsz, beam_size, -1)[:, :, step + 1] = torch.gather( cand_indices, dim=1, index=active_hypos ) if step > 0: scores[:, :step] = torch.index_select( scores[:, :step], dim=0, index=active_bbsz_idx ) scores.view(bsz, beam_size, -1)[:, :, step] = torch.gather( cand_scores, dim=1, index=active_hypos ) # Update constraints based on which candidates were selected for the next beam self.search.update_constraints(active_hypos) # copy attention for active hypotheses if attn is not None: attn[:, :, : step + 2] = torch.index_select( attn[:, :, : step + 2], dim=0, index=active_bbsz_idx ) # reorder incremental state in decoder reorder_state = active_bbsz_idx # sort by score descending for sent in range(len(finalized)): scores = torch.tensor( [float(elem["score"].item()) for elem in finalized[sent]] ) _, sorted_scores_indices = torch.sort(scores, descending=True) finalized[sent] = [finalized[sent][ssi] for ssi in sorted_scores_indices] finalized[sent] = torch.jit.annotate( List[Dict[str, Tensor]], finalized[sent] ) return finalized def _prefix_tokens( self, step: int, lprobs, scores, tokens, prefix_tokens, beam_size: int ): """Handle prefix tokens""" prefix_toks = prefix_tokens[:, step].unsqueeze(-1).repeat(1, beam_size).view(-1) prefix_lprobs = lprobs.gather(-1, prefix_toks.unsqueeze(-1)) prefix_mask = prefix_toks.ne(self.pad) lprobs[prefix_mask] = torch.tensor(-math.inf).to(lprobs) lprobs[prefix_mask] = lprobs[prefix_mask].scatter( -1, prefix_toks[prefix_mask].unsqueeze(-1), prefix_lprobs[prefix_mask] ) # if prefix includes eos, then we should make sure tokens and # scores are the same across all beams eos_mask = prefix_toks.eq(self.eos) if eos_mask.any(): # validate that the first beam matches the prefix first_beam = tokens[eos_mask].view(-1, beam_size, tokens.size(-1))[ :, 0, 1 : step + 1 ] eos_mask_batch_dim = eos_mask.view(-1, beam_size)[:, 0] target_prefix = prefix_tokens[eos_mask_batch_dim][:, :step] assert (first_beam == target_prefix).all() # copy tokens, scores and lprobs from the first beam to all beams tokens = self.replicate_first_beam(tokens, eos_mask_batch_dim, beam_size) scores = self.replicate_first_beam(scores, eos_mask_batch_dim, beam_size) lprobs = self.replicate_first_beam(lprobs, eos_mask_batch_dim, beam_size) return lprobs, tokens, scores def replicate_first_beam(self, tensor, mask, beam_size: int): tensor = tensor.view(-1, beam_size, tensor.size(-1)) tensor[mask] = tensor[mask][:, :1, :] return tensor.view(-1, tensor.size(-1)) def finalize_hypos( self, step: int, bbsz_idx, eos_scores, tokens, scores, finalized: List[List[Dict[str, Tensor]]], finished: List[bool], beam_size: int, attn: Optional[Tensor], src_lengths, max_len: int, ): """Finalize hypothesis, store finalized information in `finalized`, and change `finished` accordingly. A sentence is finalized when {beam_size} finished items have been collected for it. Returns number of sentences (not beam items) being finalized. These will be removed from the batch and not processed further. Args: bbsz_idx (Tensor): """ assert bbsz_idx.numel() == eos_scores.numel() # clone relevant token and attention tensors. # tokens is (batch * beam, max_len). So the index_select # gets the newly EOS rows, then selects cols 1..{step + 2} tokens_clone = tokens.index_select(0, bbsz_idx)[ :, 1 : step + 2 ] # skip the first index, which is EOS tokens_clone[:, step] = self.eos attn_clone = ( attn.index_select(0, bbsz_idx)[:, :, 1 : step + 2] if attn is not None else None ) # compute scores per token position pos_scores = scores.index_select(0, bbsz_idx)[:, : step + 1] pos_scores[:, step] = eos_scores # convert from cumulative to per-position scores pos_scores[:, 1:] = pos_scores[:, 1:] - pos_scores[:, :-1] # normalize sentence-level scores if self.normalize_scores: eos_scores /= (step + 1) ** self.len_penalty # cum_unfin records which sentences in the batch are finished. # It helps match indexing between (a) the original sentences # in the batch and (b) the current, possibly-reduced set of # sentences. cum_unfin: List[int] = [] prev = 0 for f in finished: if f: prev += 1 else: cum_unfin.append(prev) cum_fin_tensor = torch.tensor(cum_unfin, dtype=torch.int).to(bbsz_idx) unfin_idx = bbsz_idx // beam_size sent = unfin_idx + torch.index_select(cum_fin_tensor, 0, unfin_idx) # Create a set of "{sent}{unfin_idx}", where # "unfin_idx" is the index in the current (possibly reduced) # list of sentences, and "sent" is the index in the original, # unreduced batch # For every finished beam item # sentence index in the current (possibly reduced) batch seen = (sent << 32) + unfin_idx unique_seen: List[int] = torch.unique(seen).tolist() if self.match_source_len: condition = step > torch.index_select(src_lengths, 0, unfin_idx) eos_scores = torch.where(condition, torch.tensor(-math.inf), eos_scores) sent_list: List[int] = sent.tolist() for i in range(bbsz_idx.size()[0]): # An input sentence (among those in a batch) is finished when # beam_size hypotheses have been collected for it if len(finalized[sent_list[i]]) < beam_size: if attn_clone is not None: # remove padding tokens from attn scores hypo_attn = attn_clone[i] else: hypo_attn = torch.empty(0) finalized[sent_list[i]].append( { "tokens": tokens_clone[i], "score": eos_scores[i], "attention": hypo_attn, # src_len x tgt_len "alignment": torch.empty(0), "positional_scores": pos_scores[i], } ) newly_finished: List[int] = [] for unique_s in unique_seen: # check termination conditions for this sentence unique_sent: int = unique_s >> 32 unique_unfin_idx: int = unique_s - (unique_sent << 32) if not finished[unique_sent] and self.is_finished( step, unique_unfin_idx, max_len, len(finalized[unique_sent]), beam_size ): finished[unique_sent] = True newly_finished.append(unique_unfin_idx) return newly_finished def is_finished( self, step: int, unfin_idx: int, max_len: int, finalized_sent_len: int, beam_size: int, ): """ Check whether decoding for a sentence is finished, which occurs when the list of finalized sentences has reached the beam size, or when we reach the maximum length. """ assert finalized_sent_len <= beam_size if finalized_sent_len == beam_size or step == max_len: return True return False class EnsembleModel(nn.Module): """A wrapper around an ensemble of models.""" def __init__(self, models): super().__init__() self.models_size = len(models) # method '__len__' is not supported in ModuleList for torch script self.single_model = models[0] self.models = nn.ModuleList(models) self.has_incremental: bool = False if all( hasattr(m, "decoder") and isinstance(m.decoder, FairseqIncrementalDecoder) for m in models ): self.has_incremental = True def forward(self): pass def has_encoder(self): return hasattr(self.single_model, "encoder") def has_incremental_states(self): return self.has_incremental def max_decoder_positions(self): return min( [ m.max_decoder_positions() for m in self.models if hasattr(m, "max_decoder_positions") ] + [sys.maxsize] ) @torch.jit.export def forward_encoder(self, net_input: Dict[str, Tensor]): if not self.has_encoder(): return None return [model.encoder.forward_torchscript(net_input) for model in self.models] @torch.jit.export def forward_decoder( self, tokens, encoder_outs: List[Dict[str, List[Tensor]]], incremental_states: List[Dict[str, Dict[str, Optional[Tensor]]]], temperature: float = 1.0, ): log_probs = [] avg_attn: Optional[Tensor] = None encoder_out: Optional[Dict[str, List[Tensor]]] = None for i, model in enumerate(self.models): if self.has_encoder(): encoder_out = encoder_outs[i] # decode each model if self.has_incremental_states(): decoder_out = model.decoder.forward( tokens, encoder_out=encoder_out, incremental_state=incremental_states[i], ) else: if hasattr(model, "decoder"): decoder_out = model.decoder.forward(tokens, encoder_out=encoder_out) else: decoder_out = model.forward(tokens) attn: Optional[Tensor] = None decoder_len = len(decoder_out) if decoder_len > 1 and decoder_out[1] is not None: if isinstance(decoder_out[1], Tensor): attn = decoder_out[1] else: attn_holder = decoder_out[1]["attn"] if isinstance(attn_holder, Tensor): attn = attn_holder elif attn_holder is not None: attn = attn_holder[0] if attn is not None: attn = attn[:, -1, :] decoder_out_tuple = ( decoder_out[0][:, -1:, :].div_(temperature), None if decoder_len <= 1 else decoder_out[1], ) probs = model.get_normalized_probs( decoder_out_tuple, log_probs=True, sample=None ) probs = probs[:, -1, :] if self.models_size == 1: return probs, attn log_probs.append(probs) if attn is not None: if avg_attn is None: avg_attn = attn else: avg_attn.add_(attn) avg_probs = torch.logsumexp(torch.stack(log_probs, dim=0), dim=0) - math.log( self.models_size ) if avg_attn is not None: avg_attn.div_(self.models_size) return avg_probs, avg_attn @torch.jit.export def reorder_encoder_out( self, encoder_outs: Optional[List[Dict[str, List[Tensor]]]], new_order ): """ Reorder encoder output according to *new_order*. Args: encoder_out: output from the ``forward()`` method new_order (LongTensor): desired order Returns: *encoder_out* rearranged according to *new_order* """ new_outs: List[Dict[str, List[Tensor]]] = [] if not self.has_encoder(): return new_outs for i, model in enumerate(self.models): assert encoder_outs is not None new_outs.append( model.encoder.reorder_encoder_out(encoder_outs[i], new_order) ) return new_outs @torch.jit.export def reorder_incremental_state( self, incremental_states: List[Dict[str, Dict[str, Optional[Tensor]]]], new_order, ): if not self.has_incremental_states(): return for i, model in enumerate(self.models): model.decoder.reorder_incremental_state_scripting( incremental_states[i], new_order )
KosmosX-API-main
kosmosX/unilm/models/vl/vlm_generator.py
import logging import os import torch from copy import deepcopy from typing import Callable from torch import nn from torch.nn import functional as F from open_clip.model import CLIPVisionCfg, QuickGELU, TimmModel, ModifiedResNet, to_2tuple, LayerNorm, Transformer from open_clip.factory import _MODEL_CONFIGS, list_models, get_pretrained_url, download_pretrained, load_state_dict logger = logging.getLogger(__name__) class VisualTransformer4Seq2Seq(nn.Module): def __init__( self, image_size: int, patch_size: int, width: int, layers: int, heads: int, mlp_ratio: float, output_dim: int, act_layer: Callable = nn.GELU): super().__init__() self.image_size = to_2tuple(image_size) self.patch_size = to_2tuple(patch_size) self.grid_size = (self.image_size[0] // self.patch_size[0], self.image_size[1] // self.patch_size[1]) self.output_dim = output_dim self.conv1 = nn.Conv2d(in_channels=3, out_channels=width, kernel_size=patch_size, stride=patch_size, bias=False) scale = width ** -0.5 self.class_embedding = nn.Parameter(scale * torch.randn(width)) self.positional_embedding = nn.Parameter(scale * torch.randn(self.grid_size[0] * self.grid_size[1] + 1, width)) self.ln_pre = LayerNorm(width) self.transformer = Transformer(width, layers, heads, mlp_ratio, act_layer=act_layer) self.ln_post = LayerNorm(width) # self.proj = nn.Parameter(scale * torch.randn(width, output_dim)) def lock(self, unlocked_groups=0, freeze_bn_stats=False): assert unlocked_groups == 0, 'partial locking not currently supported for this model' for param in self.parameters(): param.requires_grad = False @torch.jit.ignore def set_grad_checkpointing(self, enable=True): self.transformer.grad_checkpointing = enable def forward(self, x: torch.Tensor): x = self.conv1(x) # shape = [*, width, grid, grid] x = x.reshape(x.shape[0], x.shape[1], -1) # shape = [*, width, grid ** 2] x = x.permute(0, 2, 1) # shape = [*, grid ** 2, width] x = torch.cat( [self.class_embedding.to(x.dtype) + torch.zeros(x.shape[0], 1, x.shape[-1], dtype=x.dtype, device=x.device), x], dim=1) # shape = [*, grid ** 2 + 1, width] x = x + self.positional_embedding.to(x.dtype) x = self.ln_pre(x) x = x.permute(1, 0, 2) # NLD -> LND x = self.transformer(x) # NOTE encoder output is T, B, C for seq2seq # x = x.permute(1, 0, 2) # LND -> NLD # x = self.ln_post(x[:, 0, :]) x = self.ln_post(x) # [*, grid ** 2 + 1, width] # if self.proj is not None: # x = x @ self.proj return x class ClipVisualOnly(nn.Module): # text_cfg for compatibility with original CLIP def __init__(self, embed_dim, vision_cfg, text_cfg, quick_gelu=False): super().__init__() if isinstance(vision_cfg, dict): vision_cfg = CLIPVisionCfg(**vision_cfg) # OpenAI models are pretrained w/ QuickGELU but native nn.GELU is both faster and more # memory efficient in recent PyTorch releases (>= 1.10). # NOTE: timm models always use native GELU regardless of quick_gelu flag. act_layer = QuickGELU if quick_gelu else nn.GELU if vision_cfg.timm_model_name: raise NotImplementedError self.visual = TimmModel( vision_cfg.timm_model_name, pretrained=vision_cfg.timm_model_pretrained, pool=vision_cfg.timm_pool, proj=vision_cfg.timm_proj, embed_dim=embed_dim, image_size=vision_cfg.image_size) act_layer = nn.GELU # so that text transformer doesn't use QuickGELU w/ timm models elif isinstance(vision_cfg.layers, (tuple, list)): raise NotImplementedError vision_heads = vision_cfg.width * 32 // vision_cfg.head_width self.visual = ModifiedResNet( layers=vision_cfg.layers, output_dim=embed_dim, heads=vision_heads, image_size=vision_cfg.image_size, width=vision_cfg.width) else: vision_heads = vision_cfg.width // vision_cfg.head_width self.visual = VisualTransformer4Seq2Seq( image_size=vision_cfg.image_size, patch_size=vision_cfg.patch_size, width=vision_cfg.width, layers=vision_cfg.layers, heads=vision_heads, mlp_ratio=vision_cfg.mlp_ratio, output_dim=embed_dim, act_layer=act_layer,) self.init_parameters() def init_parameters(self): if hasattr(self.visual, 'init_parameters'): self.visual.init_parameters() @torch.jit.ignore def set_grad_checkpointing(self, enable=True): self.visual.set_grad_checkpointing(enable) def encode_image(self, image): return self.visual(image) def forward(self, image): image_features = self.encode_image(image) image_features = F.normalize(image_features, dim=-1) return image_features def load_checkpoint4vision_only(model, checkpoint_path, strict=True): state_dict = load_state_dict(checkpoint_path) incompatible_keys = model.load_state_dict(state_dict, strict=strict) return incompatible_keys def create_model( model_name: str, pretrained: str = '', jit: bool = False, force_quick_gelu: bool = False, pretrained_image: bool = False,): model_name = model_name.replace('/', '-') # for callers using old naming with / in ViT names if pretrained and pretrained.lower() == 'openai': raise NotImplementedError else: if model_name in _MODEL_CONFIGS: logger.info(f'Loading {model_name} model config.') model_cfg = deepcopy(_MODEL_CONFIGS[model_name]) else: logging.error(f'Model config for {model_name} not found; available models {list_models()}.') raise RuntimeError(f'Model config for {model_name} not found.') if force_quick_gelu: # override for use of QuickGELU on non-OpenAI transformer models model_cfg["quick_gelu"] = True if pretrained_image: if 'timm_model_name' in model_cfg.get('vision_cfg', {}): # pretrained weight loading for timm models set via vision_cfg model_cfg['vision_cfg']['timm_model_pretrained'] = True else: assert False, 'pretrained image towers currently only supported for timm models' model = ClipVisualOnly(**model_cfg) if not pretrained: # reload attn weights into ts attn dim = model.visual.transformer.resblocks[0].attn.in_proj_weight.shape[0] // 3 for resblock in model.visual.transformer.resblocks: resblock.ts_attn.q_proj.weight = nn.Parameter(resblock.attn.in_proj_weight[:dim].clone()) resblock.ts_attn.q_proj.bias = nn.Parameter(resblock.attn.in_proj_bias[:dim].clone()) resblock.ts_attn.k_proj.weight = nn.Parameter(resblock.attn.in_proj_weight[dim:2*dim].clone()) resblock.ts_attn.k_proj.bias = nn.Parameter(resblock.attn.in_proj_bias[dim:2*dim].clone()) resblock.ts_attn.v_proj.weight = nn.Parameter(resblock.attn.in_proj_weight[2*dim:].clone()) resblock.ts_attn.v_proj.bias = nn.Parameter(resblock.attn.in_proj_bias[2*dim:].clone()) resblock.ts_attn.out_proj.weight = nn.Parameter(resblock.attn.out_proj.weight.clone()) resblock.ts_attn.out_proj.bias = nn.Parameter(resblock.attn.out_proj.bias.clone()) resblock.attn = None if pretrained: logger.info(f'Loading {model_name} checkpoint from: {pretrained}') checkpoint_path = '' url = get_pretrained_url(model_name, pretrained) if url: checkpoint_path = download_pretrained(url) elif os.path.exists(pretrained): checkpoint_path = pretrained if checkpoint_path: logging.info(f'Loading pretrained {model_name} weights ({pretrained}).') # NOTE TODO remove, strict=True is only for debug load_checkpoint4vision_only(model, checkpoint_path, strict=False) # reload attn weights into ts attn dim = model.visual.transformer.resblocks[0].attn.in_proj_weight.shape[0] // 3 for resblock in model.visual.transformer.resblocks: resblock.ts_attn.q_proj.weight = nn.Parameter(resblock.attn.in_proj_weight[:dim].clone()) resblock.ts_attn.q_proj.bias = nn.Parameter(resblock.attn.in_proj_bias[:dim].clone()) resblock.ts_attn.k_proj.weight = nn.Parameter(resblock.attn.in_proj_weight[dim:2*dim].clone()) resblock.ts_attn.k_proj.bias = nn.Parameter(resblock.attn.in_proj_bias[dim:2*dim].clone()) resblock.ts_attn.v_proj.weight = nn.Parameter(resblock.attn.in_proj_weight[2*dim:].clone()) resblock.ts_attn.v_proj.bias = nn.Parameter(resblock.attn.in_proj_bias[2*dim:].clone()) resblock.ts_attn.out_proj.weight = nn.Parameter(resblock.attn.out_proj.weight.clone()) resblock.ts_attn.out_proj.bias = nn.Parameter(resblock.attn.out_proj.bias.clone()) resblock.attn = None else: logging.warning(f'Pretrained weights ({pretrained}) not found for model {model_name}.') raise RuntimeError(f'Pretrained weights ({pretrained}) not found for model {model_name}.') if jit: model = torch.jit.script(model) return model
KosmosX-API-main
kosmosX/unilm/models/vl/clip.py
KosmosX-API-main
kosmosX/unilm/data/__init__.py
import numpy as np from random import Random from typing import Dict, Iterable, Optional import collections from infinibatch import iterators EOD_SYMBOL = "</doc>" BOI_SYMBOL = "<image>" EOI_SYMBOL = "</image>" EOC_SYMBOL = "</chunk>" EOL_SYMBOL = "</line>" GRD_SYMBOL="<grounding>" BOP_SYMBOL="<phrase>" EOP_SYMBOL="</phrase>" BOO_SYMBOL="<object>" EOO_SYMBOL="</object>" DOM_SYMBOL="</delimiter_of_multi_objects/>" SPECIAL_SYMBOLS = [EOD_SYMBOL, BOI_SYMBOL, EOI_SYMBOL, EOC_SYMBOL, EOL_SYMBOL] def add_location_symbols(quantized_size, locate_special_token=0): custom_sp_symbols = [] for symbol in SPECIAL_SYMBOLS: custom_sp_symbols.append(symbol) for symbol in [BOP_SYMBOL, EOP_SYMBOL, BOO_SYMBOL, EOO_SYMBOL, DOM_SYMBOL]: custom_sp_symbols.append(symbol) if locate_special_token > 0: custom_sp_symbols.append(GRD_SYMBOL) for i in range(quantized_size ** 2): token_name = f"<patch_index_{str(i).zfill(4)}>" custom_sp_symbols.append(token_name) return custom_sp_symbols def apply_to_sample(f, sample): if hasattr(sample, "__len__") and len(sample) == 0: return {} def _apply(x): if isinstance(x, np.ndarray): return f(x) elif isinstance(x, collections.OrderedDict): # OrderedDict has attributes that needs to be preserved od = collections.OrderedDict((key, _apply(value)) for key, value in x.items()) od.__dict__ = x.__dict__ return od elif isinstance(x, dict): return {key: _apply(value) for key, value in x.items()} elif isinstance(x, list): return [_apply(x) for x in x] elif isinstance(x, tuple): return tuple(_apply(x) for x in x) elif isinstance(x, set): return {_apply(x) for x in x} else: return x return _apply(sample) class NativeCheckpointableIterator(iterators.CheckpointableIterator): def __init__(self, iterable: Iterable): self._input_iterable = iterable self.setstate(None) def getstate(self) -> Dict: return {'num_items_yielded': self._num_items_yielded} def setstate(self, checkpoint: Optional[Dict]): self._iterator = iter(self._input_iterable) self._num_items_yielded = iterators._advance_iterator(self._iterator, checkpoint['num_items_yielded']) if checkpoint is not None else 0 def __next__(self): item = next(self._iterator) self._num_items_yielded += 1 return item def close(self): pass class WeightIterator(object): def __init__(self, weights, seed): self.weights = weights self.seed = seed self.control_index = list(range(len(weights))) self.setstate(None) def __iter__(self): return self def getstate(self): return {"random_state": self._random_state} def setstate(self, checkpoint): self._random_state = checkpoint["random_state"] if checkpoint else None self._random = None # this will trigger the lazy initialization in self.__next__ def __next__(self): if self._random is None: self._random = Random(self.seed) if self._random_state is not None: self._random.setstate(self._random_state) idx = self._random.choices(self.control_index, self.weights)[0] self._random_state = self._random.getstate() return idx def close(self): pass class ConcatIterator(iterators.CheckpointableIterator): """ Concat items from all given iterators. """ def __init__(self, source_iterators): """ Args: source_iterators: list of iterators to zip, item by item """ # TODO: Use all function? for source_iterator in source_iterators: if not isinstance(source_iterator, iterators.CheckpointableIterator): raise ValueError('all iterators in source_iterators have to be CheckpointableIterator') self._source_iterators = source_iterators # type: List[CheckpointableIterator] def getstate(self): return {'input_states': tuple(iterator.getstate() for iterator in self._source_iterators)} def setstate(self, checkpoint): if checkpoint is None: for iterator in self._source_iterators: iterator.setstate(None) else: # TODO: Add check that both lists have the same length? for iterator, state in zip(self._source_iterators, checkpoint['input_states']): iterator.setstate(state) def __next__(self): res = {} # (note: can't use a generator expression, as it gets confused when a next() call raises StopIteration) for iterator in self._source_iterators: res.update(next(iterator)) return res def close(self): for it in self._source_iterators: it.close() class MixIterator(iterators.CheckpointableIterator): """ Concat items from all given iterators. """ def __init__(self, source_iterators, weights): """ Args: source_iterators: list of iterators to zip, item by item """ # TODO: Use all function? for source_iterator in source_iterators: if not isinstance(source_iterator, iterators.CheckpointableIterator): raise ValueError('all iterators in source_iterators have to be CheckpointableIterator') self._source_iterators = source_iterators # type: List[CheckpointableIterator] assert len(weights) == len(source_iterators) self.weights = weights self.population = list(range(len(source_iterators))) def getstate(self): return {'input_states': tuple(iterator.getstate() for iterator in self._source_iterators)} def setstate(self, checkpoint): if checkpoint is None: for iterator in self._source_iterators: iterator.setstate(None) else: # TODO: Add check that both lists have the same length? for iterator, state in zip(self._source_iterators, checkpoint['input_states']): iterator.setstate(state) def __next__(self): _random = Random() res = {} # (note: can't use a generator expression, as it gets confused when a next() call raises StopIteration) idx = _random.choices(self.population, self.weights)[0] res.update(next(self._source_iterators[idx])) return res def close(self): for it in self._source_iterators: it.close()
KosmosX-API-main
kosmosX/unilm/data/utils.py
from dataclasses import dataclass, field import math from omegaconf import II import torch.nn.functional as F from fairseq import metrics, utils from fairseq.criterions import FairseqCriterion, register_criterion from fairseq.dataclass import FairseqDataclass LOSS_NAMES = ["gpt", "image_wild", "image_laion"] @dataclass class UniGPTLossConfig(FairseqDataclass): ignore_eos: bool = field( default=False, metadata={"help": "ignore mlm output at eos token."}, ) sentence_avg: bool = II("optimization.sentence_avg") @register_criterion( "unigpt", dataclass=UniGPTLossConfig ) class UniGPTLoss(FairseqCriterion): def __init__(self, cfg, task): super().__init__(task) self.cfg = cfg def forward(self, model, sample, reduce=True, loss_name="gpt"): """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"][sample["net_input"]['gpt_loss_mask']] != self.padding_idx).sum().int() logging_output = { "loss": loss.data, "ntokens": sample["ntokens"], "nsentences": sample["target"].size(0), "sample_size": sample_size, } # for loss_name_item in LOSS_NAMES: # logging_output[loss_name_item] = 0.01 # logging_output[loss_name_item + "sample_size"] = 1 logging_output[loss_name] = loss.data logging_output[loss_name + "sample_size"] = sample["ntokens"] return loss, sample_size, logging_output def compute_loss(self, model, net_output, sample, reduce=True): if hasattr(model, "gpt_model"): lprobs = model.gpt_model.get_normalized_probs(net_output, log_probs=True) else: lprobs = model.get_normalized_probs(net_output, log_probs=True) loss_mask = sample["net_input"]['gpt_loss_mask'] lprobs = lprobs[loss_mask] target = model.get_targets(sample, net_output)[loss_mask].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) loss_items = [] # log individual losses for loss_name in LOSS_NAMES: loss_sum = sum(log.get(loss_name, 0) for log in logging_outputs) single_sample_size = sum(log.get(loss_name + "sample_size", 0) for log in logging_outputs) if loss_sum != 0: metrics.log_scalar( loss_name, loss_sum / single_sample_size / math.log(2), single_sample_size, round=3 ) metrics.log_scalar( loss_name + "_sample_size", single_sample_size, round=3 ) loss_items.append(loss_sum / single_sample_size / math.log(2)) else: metrics.log_scalar( loss_name + "_sample_size", 0, round=3 ) metrics.log_scalar( "loss", sum(loss_items) / len(loss_items), 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
KosmosX-API-main
kosmosX/unilm/criterions/unigpt.py
import importlib import os # automatically import any Python files in the criterions/ directory for file in sorted(os.listdir(os.path.dirname(__file__))): if file.endswith(".py") and not file.startswith("_"): file_name = file[: file.find(".py")] importlib.import_module("unilm.criterions." + file_name)
KosmosX-API-main
kosmosX/unilm/criterions/__init__.py
from setuptools import setup, find_packages import site site.ENABLE_USER_SITE = True setup( name='infinibatch', version='0.1.0', url='https://github.com/microsoft/infinibatch', author='Frank Seide', author_email='[email protected]', description='Infinibatch is a library of checkpointable iterators for randomized data loading of massive data sets in deep neural network training.', packages=find_packages() )
KosmosX-API-main
kosmosX/infinibatch/setup.py
import copy import itertools import multiprocessing from random import Random import unittest import torch from infinibatch.iterators import * if __name__ == "__main__": unittest.main() class TestBase(unittest.TestCase): def setUp(self): self.lengths = [1, 2, 3, 42, 57] self.world_sizes = [1, 2, 3, 4, 5, 11, 16, 64, 73] self.seed = 42 def assertMultisetEqual(self, a, b): def list_to_dict(l): d = {} for item in l: d[item] = d.get(item, 0) + 1 return d self.assertEqual(list_to_dict(a), list_to_dict(b)) class TestFiniteIteratorMixin: """ Mixin to be used in combination with TestBase to test basic function of finite CheckpointableIterators """ def test_basic(self): for case_name, expected_result, it in self.test_cases: with self.subTest(case_name): result = list(it) self.assertEqual(result, expected_result) class TestFiniteIteratorCheckpointingMixin: """ Mixin to be used in combination with TestBase to test checkpointing functionality of finite CheckpointableIterators """ def test_checkpointing_reset(self): for case_name, _, it in self.test_cases: with self.subTest(case_name): expected_result = list(it) # extract data it.setstate(None) # reset to start result = list(it) self.assertEqual(result, expected_result) # TODO: Can this be rewritten in terms of _test_checkpointing_from_pos? def test_checkpointing_from_start(self): for case_name, _, it in self.test_cases: with self.subTest(case_name): checkpoint = it.getstate() expected_result = list(it) # extract data it.setstate(checkpoint) # reset to start result = list(it) self.assertEqual(result, expected_result) def _test_checkpointing_from_pos(self, it, pos): for _ in range(pos): # go to pos next(it) checkpoint = it.getstate() # take checkpoint expected_result = list(it) # extract data it.setstate(checkpoint) # reset to checkpoint result = list(it) self.assertEqual(result, expected_result) def test_checkpointing_from_one(self): for case_name, _, it in self.test_cases: with self.subTest(case_name): pos = 1 self._test_checkpointing_from_pos(it, pos) def test_checkpointing_from_quarter(self): for case_name, _, it in self.test_cases: with self.subTest(case_name): expected_result = list(it) it.setstate(None) pos = len(expected_result) // 4 self._test_checkpointing_from_pos(it, pos) def test_checkpointing_from_third(self): for case_name, _, it in self.test_cases: with self.subTest(case_name): expected_result = list(it) it.setstate(None) pos = len(expected_result) // 3 self._test_checkpointing_from_pos(it, pos) def test_checkpointing_from_half(self): for case_name, _, it in self.test_cases: with self.subTest(case_name): expected_result = list(it) it.setstate(None) pos = len(expected_result) // 2 self._test_checkpointing_from_pos(it, pos) def test_checkpointing_before_end(self): for case_name, _, it in self.test_cases: with self.subTest(case_name): expected_result = list(it) it.setstate(None) pos = len(expected_result) - 1 self._test_checkpointing_from_pos(it, pos) def test_checkpointing_at_end(self): for case_name, _, it in self.test_cases: with self.subTest(case_name): list(it) # exhaust iterator self.assertRaises(StopIteration, it.__next__) checkpoint = it.getstate() # take checkpoint it.setstate(None) # reset to beginning it.setstate(checkpoint) # reset to checkpoint self.assertRaises(StopIteration, it.__next__) def test_checkpointing_complex(self): for case_name, _, it in self.test_cases: with self.subTest(case_name): expected_result = list(it) # get a bunch of checkpoints at different positions it.setstate(None) positions = [ 0, len(expected_result) // 7, len(expected_result) // 6, len(expected_result) // 5, len(expected_result) // 4, len(expected_result) // 3, len(expected_result) // 2, ] checkpoints = [] for i in range(len(positions)): offset = positions[i] - positions[i - 1] if i > 0 else positions[0] for _ in range(offset): next(it) checkpoints.append(it.getstate()) # check that iterator returns correct result at all checkpoints for pos, checkpoint in zip(positions, checkpoints): it.setstate(checkpoint) self.assertEqual(list(it), expected_result[pos:]) # check that iterator returns correct result at all checkpoints in reverse order tuples = list(zip(positions, checkpoints)) tuples.reverse() for pos, checkpoint in tuples: it.setstate(checkpoint) self.assertEqual(list(it), expected_result[pos:]) # check that iterator returns correct result at all checkpoints # while resetting between any two checkpoints for pos, checkpoint in zip(positions, checkpoints): it.setstate(None) it.setstate(checkpoint) self.assertEqual(list(it), expected_result[pos:]) # and as the grand finale: reset and check again it.setstate(None) result = list(it) self.assertEqual(result, expected_result) class TestInfinitePermutationSourceIterator(TestBase): def setUp(self): super().setUp() self.repeats = [1, 2, 3] def test_no_shuffle(self): for n, k, num_instances in itertools.product(self.lengths, self.repeats, self.world_sizes): data = list(range(n)) for instance_rank in range(num_instances): with self.subTest(f"n={n}, k={k}, num_instances={num_instances}, instance_rank={instance_rank}"): it = InfinitePermutationSourceIterator( copy.deepcopy(data), shuffle=False, num_instances=num_instances, instance_rank=instance_rank ) repeated_data = [] while len(repeated_data) < k * n * num_instances: repeated_data.extend(data) expected_result = [] pos = instance_rank while len(expected_result) < k * n: expected_result.append(repeated_data[pos]) pos += num_instances result = [next(it) for _ in range(k * n)] self.assertEqual(result, expected_result) def test_shuffle(self): for n, k, num_instances in itertools.product(self.lengths, self.repeats, self.world_sizes): data = list(range(n)) for instance_rank in range(num_instances): with self.subTest(f"n={n}, k={k}, num_instances={num_instances}, instance_rank={instance_rank}"): it = InfinitePermutationSourceIterator( copy.deepcopy(data), seed=self.seed, shuffle=True, num_instances=num_instances, instance_rank=instance_rank, ) random = Random(self.seed) repeated_data = [] while len(repeated_data) < k * n * num_instances: shuffled_data = copy.deepcopy(data) random.shuffle(shuffled_data) repeated_data.extend(shuffled_data) expected_result = [] pos = instance_rank while len(expected_result) < k * n: expected_result.append(repeated_data[pos]) pos += num_instances result = [next(it) for _ in range(k * n)] self.assertEqual(result, expected_result) def test_single_instance_no_shuffle(self): # this test is technically included in test_no_shuffle # but the calculation of the expected result is less error prone for n, k in itertools.product(self.lengths, self.repeats): with self.subTest(f"n={n}, k={k}"): data = list(range(n)) expected_result = data * k it = InfinitePermutationSourceIterator(copy.deepcopy(data), shuffle=False) result = [next(it) for _ in range(k * n)] self.assertEqual(result, expected_result) def test_single_instance_shuffle(self): # this test is technically included in test_shuffle # but the calculation of the expected result is less error prone for n, k in itertools.product(self.lengths, self.repeats): with self.subTest(f"n={n}, k={k}"): data = list(range(n)) expected_result = data * k it = InfinitePermutationSourceIterator(copy.deepcopy(data), seed=self.seed, shuffle=True) result = [next(it) for _ in range(k * n)] self.assertMultisetEqual(result, expected_result) def test_checkpointing_reset_no_shuffle(self): for n, k, num_instances in itertools.product(self.lengths, self.repeats, self.world_sizes): data = list(range(n)) for instance_rank in range(num_instances): with self.subTest(f"n={n}, k={k}, num_instances={num_instances}, instance_rank={instance_rank}"): it = InfinitePermutationSourceIterator( copy.deepcopy(data), shuffle=False, num_instances=num_instances, instance_rank=instance_rank ) expected_result = [next(it) for _ in range(k * n)] # extract data it.setstate(None) # reset to start result = [next(it) for _ in range(k * n)] self.assertEqual(result, expected_result) def test_checkpointing_reset_shuffle(self): for n, k, num_instances in itertools.product(self.lengths, self.repeats, self.world_sizes): data = list(range(n)) for instance_rank in range(num_instances): with self.subTest(f"n={n}, k={k}, num_instances={num_instances}, instance_rank={instance_rank}"): it = InfinitePermutationSourceIterator( copy.deepcopy(data), seed=self.seed, shuffle=True, num_instances=num_instances, instance_rank=instance_rank, ) expected_result = [next(it) for _ in range(k * n)] # extract data it.setstate(None) # reset to start result = [next(it) for _ in range(k * n)] self.assertEqual(result, expected_result) def test_checkpointing_from_start_no_shuffle(self): for n, k, num_instances in itertools.product(self.lengths, self.repeats, self.world_sizes): data = list(range(n)) for instance_rank in range(num_instances): with self.subTest(f"n={n}, k={k}, num_instances={num_instances}, instance_rank={instance_rank}"): it = InfinitePermutationSourceIterator( copy.deepcopy(data), shuffle=False, num_instances=num_instances, instance_rank=instance_rank ) checkpoint = it.getstate() expected_result = [next(it) for _ in range(k * n)] # extract data it.setstate(checkpoint) # reset to start result = [next(it) for _ in range(k * n)] self.assertEqual(result, expected_result) def test_checkpointing_from_start_shuffle(self): for n, k, num_instances in itertools.product(self.lengths, self.repeats, self.world_sizes): data = list(range(n)) for instance_rank in range(num_instances): with self.subTest(f"n={n}, k={k}, num_instances={num_instances}, instance_rank={instance_rank}"): it = InfinitePermutationSourceIterator( copy.deepcopy(data), seed=self.seed, shuffle=True, num_instances=num_instances, instance_rank=instance_rank, ) checkpoint = it.getstate() expected_result = [next(it) for _ in range(k * n)] # extract data it.setstate(checkpoint) # reset to start result = [next(it) for _ in range(k * n)] self.assertEqual(result, expected_result) def test_checkpointing_from_middle_no_shuffle(self): for n, k, num_instances in itertools.product(self.lengths, self.repeats, self.world_sizes): data = list(range(n)) for instance_rank in range(num_instances): with self.subTest(f"n={n}, k={k}, num_instances={num_instances}, instance_rank={instance_rank}"): it = InfinitePermutationSourceIterator( copy.deepcopy(data), shuffle=False, num_instances=num_instances, instance_rank=instance_rank ) checkpoint_pos = k * n // 3 for _ in range(checkpoint_pos): # go to checkpoint_pos next(it) checkpoint = it.getstate() # take checkpoint expected_result = [next(it) for _ in range(k * n)] # extract data for _ in range(checkpoint_pos): # move forward some more next(it) it.setstate(checkpoint) # reset to checkpoint result = [next(it) for _ in range(k * n)] # get data again self.assertEqual(result, expected_result) def test_checkpointing_from_middle_shuffle(self): for n, k, num_instances in itertools.product(self.lengths, self.repeats, self.world_sizes): data = list(range(n)) for instance_rank in range(num_instances): with self.subTest(f"n={n}, k={k}, num_instances={num_instances}, instance_rank={instance_rank}"): it = InfinitePermutationSourceIterator( copy.deepcopy(data), seed=self.seed, shuffle=True, num_instances=num_instances, instance_rank=instance_rank, ) checkpoint_pos = k * n // 3 for _ in range(checkpoint_pos): # go to checkpoint_pos next(it) checkpoint = it.getstate() # take checkpoint expected_result = [next(it) for _ in range(k * n)] # extract data for _ in range(checkpoint_pos): # move forward some more next(it) it.setstate(checkpoint) # reset to checkpoint result = [next(it) for _ in range(k * n)] # get data again self.assertEqual(result, expected_result) def test_checkpointing_at_boundary_no_shuffle(self): for n, k, num_instances in itertools.product(self.lengths, self.repeats, self.world_sizes): data = list(range(n)) for instance_rank in range(num_instances): with self.subTest(f"n={n}, k={k}, num_instances={num_instances}, instance_rank={instance_rank}"): it = InfinitePermutationSourceIterator( copy.deepcopy(data), shuffle=False, num_instances=num_instances, instance_rank=instance_rank ) checkpoint_pos = k * n for _ in range(checkpoint_pos): # go to checkpoint_pos next(it) checkpoint = it.getstate() # take checkpoint expected_result = [next(it) for _ in range(k * n)] # extract data for _ in range(checkpoint_pos): # move forward some more next(it) it.setstate(checkpoint) # reset to checkpoint result = [next(it) for _ in range(k * n)] # get data again self.assertEqual(result, expected_result) def test_checkpointing_at_boundary_shuffle(self): for n, k, num_instances in itertools.product(self.lengths, self.repeats, self.world_sizes): data = list(range(n)) for instance_rank in range(num_instances): with self.subTest(f"n={n}, k={k}, num_instances={num_instances}, instance_rank={instance_rank}"): it = InfinitePermutationSourceIterator( copy.deepcopy(data), seed=self.seed, shuffle=True, num_instances=num_instances, instance_rank=instance_rank, ) checkpoint_pos = k * n for _ in range(checkpoint_pos): # go to checkpoint_pos next(it) checkpoint = it.getstate() # take checkpoint expected_result = [next(it) for _ in range(k * n)] # extract data for _ in range(checkpoint_pos): # move forward some more next(it) it.setstate(checkpoint) # reset to checkpoint result = [next(it) for _ in range(k * n)] # get data again self.assertEqual(result, expected_result) def test_empty_source(self): def f(): return InfinitePermutationSourceIterator([]) self.assertRaises(ValueError, f) def test_rank_too_large(self): def f(): return InfinitePermutationSourceIterator([1], num_instances=2, instance_rank=2) self.assertRaises(ValueError, f) class TestChunkedSourceIterator(TestBase, TestFiniteIteratorMixin, TestFiniteIteratorCheckpointingMixin): def setUp(self): super().setUp() self.test_cases = [] for n in self.lengths: data = list(range(n)) it = ChunkedSourceIterator(copy.deepcopy(data)) self.test_cases.append(("n={}".format(n), data, it)) def test_multiple_instances(self): for n, num_instances in itertools.product(self.lengths, self.world_sizes): with self.subTest("n={}, num_instances={}".format(n, num_instances)): data = list(range(n)) result = [] sizes = [] for instance_rank in range(num_instances): it = ChunkedSourceIterator( copy.deepcopy(data), num_instances=num_instances, instance_rank=instance_rank ) output = list(it) result.extend(output) sizes.append(len(output)) self.assertEqual(data, result) self.assertTrue(max(sizes) - min(sizes) <= 1) # make sure data is split as evenly as possible def test_rank_too_large(self): def create_iterator(): ChunkedSourceIterator([1], num_instances=2, instance_rank=2) self.assertRaises(ValueError, create_iterator) class TestSamplingRandomMapIterator(TestBase, TestFiniteIteratorMixin, TestFiniteIteratorCheckpointingMixin): @staticmethod def transform(random, item): return item + random.random() def setUp(self): super().setUp() self.test_cases = [] for n in self.lengths: data = list(range(n)) random = Random() random.seed(self.seed) expected_result = [n + random.random() for n in data] it = SamplingRandomMapIterator(NativeCheckpointableIterator(data), transform=self.transform, seed=self.seed) self.test_cases.append(("n={}".format(n), expected_result, it)) class TestMapIterator(TestBase, TestFiniteIteratorMixin, TestFiniteIteratorCheckpointingMixin): @staticmethod def transform(item): return 2 * item def setUp(self): super().setUp() self.test_cases = [] for n in self.lengths: data = list(range(n)) expected_result = [self.transform(item) for item in data] it = MapIterator(NativeCheckpointableIterator(data), self.transform) self.test_cases.append(("n={}".format(n), expected_result, it)) class TestZipIterator(TestBase, TestFiniteIteratorMixin, TestFiniteIteratorCheckpointingMixin): def setUp(self): super().setUp() self.test_cases = [] # pairs for n in self.lengths: data1 = list(range(n)) data2 = [item * item for item in data1] expected_result = list(zip(data1, data2)) it = ZipIterator(NativeCheckpointableIterator(data1), NativeCheckpointableIterator(data2)) self.test_cases.append(("n={}, pairs".format(n), expected_result, it)) # triples for n in self.lengths: data1 = list(range(n)) data2 = [item * item for item in data1] data3 = [item * item for item in data2] expected_result = list(zip(data1, data2, data3)) it = ZipIterator( NativeCheckpointableIterator(data1), NativeCheckpointableIterator(data2), NativeCheckpointableIterator(data3), ) self.test_cases.append(("n={}, triples".format(n), expected_result, it)) # different lengths for n in self.lengths: if n > 3: # smaller n give us an empty iterator, which causes issues data1 = list(range(n)) data2 = [item * item for item in data1] data2 = data2[:-3] expected_result = list(zip(data1, data2)) it = ZipIterator(NativeCheckpointableIterator(data1), NativeCheckpointableIterator(data2)) self.test_cases.append(("n={}, different lengths".format(n), expected_result, it)) class TestPrefetchIterator(TestBase, TestFiniteIteratorMixin, TestFiniteIteratorCheckpointingMixin): def setUp(self): super().setUp() self.test_cases = [] for n in self.lengths: for buffer_size in self.lengths: data = list(range(n)) it = PrefetchIterator(NativeCheckpointableIterator(data), buffer_size) self.test_cases.append(("n={}, buffer_size={}".format(n, buffer_size), data, it)) def test_zero_buffer_size(self): def f(): return PrefetchIterator(NativeCheckpointableIterator([0]), buffer_size=0) self.assertRaises(ValueError, f) def test_torch_tensors(self): for n in self.lengths: for buffer_size in self.lengths: with self.subTest("n={}, buffer_size={}".format(n, buffer_size)): data = [torch.Tensor([float(i)]) for i in range(n)] it = PrefetchIterator(NativeCheckpointableIterator(copy.deepcopy(data)), buffer_size) result = list(it) self.assertEqual(result, data) class TestPrefetchIteratorExperimental(TestBase, TestFiniteIteratorMixin, TestFiniteIteratorCheckpointingMixin): def setUp(self): super().setUp() self.test_cases = [] for n in self.lengths: for buffer_size in self.lengths: data = list(range(n)) it = PrefetchIterator(NativeCheckpointableIterator(data), buffer_size, buffer_in_main_process=True) self.test_cases.append(("n={}, buffer_size={}".format(n, buffer_size), data, it)) def test_zero_buffer_size(self): def f(): return PrefetchIterator(NativeCheckpointableIterator([0]), buffer_size=0, buffer_in_main_process=True) self.assertRaises(ValueError, f) def test_closing(self): if multiprocessing.get_start_method() != "fork": return # dummy iterator used, skip test it = PrefetchIterator(NativeCheckpointableIterator([0]), buffer_size=42, buffer_in_main_process=True) it.close() def f(): return it.__next__() self.assertRaises(RuntimeError, f) def f(): return it.setstate(None) self.assertRaises(RuntimeError, f) def test_nested(self): for n in self.lengths: for buffer_size in self.lengths: for depth in [2, 3, 4, 5]: with self.subTest("n={}, buffer_size={}, depth={}".format(n, buffer_size, depth)): data = [torch.Tensor([float(i)]) for i in range(n)] it = NativeCheckpointableIterator(copy.deepcopy(data)) for _ in range(depth): it = PrefetchIterator(it, buffer_size, buffer_in_main_process=True) result = list(it) self.assertEqual(result, data) it.close() def test_torch_tensors(self): for n in self.lengths: for buffer_size in self.lengths: with self.subTest("n={}, buffer_size={}".format(n, buffer_size)): data = [torch.Tensor([float(i)]) for i in range(n)] it = PrefetchIterator( NativeCheckpointableIterator(copy.deepcopy(data)), buffer_size, buffer_in_main_process=True ) result = list(it) self.assertEqual(result, data) it.close() def tearDown(self): if hasattr(self, "test_cases"): for _, _, it in self.test_cases: it.close() class TestMultiplexIterator(TestBase, TestFiniteIteratorMixin, TestFiniteIteratorCheckpointingMixin): # TODO: Add test cases for behavior when source iterators end but item is retrieved def setUp(self): super().setUp() random = Random() random.seed(42) self.test_cases = [] # two source iterators for n in self.lengths: indices = [random.randrange(0, 2) for _ in range(n)] data = [[2 * i + 0 for i in range(n)], [2 * i + 1 for i in range(n)]] data_copy = copy.deepcopy(data) expected_result = [data_copy[i].pop(0) for i in indices] it = MultiplexIterator( NativeCheckpointableIterator(indices), [NativeCheckpointableIterator(d) for d in data] ) self.test_cases.append(("n={}, two source iterators".format(n), expected_result, it)) # three source iterators for n in self.lengths: indices = [random.randrange(0, 3) for _ in range(n)] data = [[3 * i + 0 for i in range(n)], [3 * i + 1 for i in range(n)], [3 * i + 2 for i in range(n)]] data_copy = copy.deepcopy(data) expected_result = [data_copy[i].pop(0) for i in indices] it = MultiplexIterator( NativeCheckpointableIterator(indices), [NativeCheckpointableIterator(d) for d in data] ) self.test_cases.append(("n={}, three source iterators".format(n), expected_result, it)) class TestNativeCheckpointableIterator(TestBase, TestFiniteIteratorMixin, TestFiniteIteratorCheckpointingMixin): def setUp(self): super().setUp() self.test_cases = [] for n in self.lengths: data = list(range(n)) expected_result = copy.deepcopy(data) it = NativeCheckpointableIterator(data) self.test_cases.append(("n={}".format(n), expected_result, it)) def test_empty(self): it = NativeCheckpointableIterator([]) self.assertRaises(StopIteration, it.__next__) def test_iterator_exception(self): self.assertRaises(ValueError, NativeCheckpointableIterator, iter(range(10))) class TestFixedBatchIterator(TestBase, TestFiniteIteratorMixin, TestFiniteIteratorCheckpointingMixin): def setUp(self): super().setUp() self.test_cases = [] for n in self.lengths: for batch_size in self.lengths: data = list(range(n)) data_copy = copy.deepcopy(data) expected_result = [] while data_copy: expected_result.append(data_copy[:batch_size]) data_copy = data_copy[batch_size:] it = FixedBatchIterator(NativeCheckpointableIterator(data), batch_size=batch_size) self.test_cases.append(("n={}, batch_size={}".format(n, batch_size), expected_result, it)) def test_invalid_batch_size(self): def f(): return FixedBatchIterator(NativeCheckpointableIterator([0]), batch_size=0) self.assertRaises(ValueError, f) class TestRecurrentIterator(TestBase, TestFiniteIteratorMixin, TestFiniteIteratorCheckpointingMixin): @staticmethod def step_function(prev_state, item): output = prev_state + item return output, output def setUp(self): super().setUp() self.test_cases = [] for n in self.lengths: data = list(range(n)) expected_result = [data[0]] for i in data[1:]: expected_result.append(self.step_function(expected_result[-1], i)[1]) it = RecurrentIterator(NativeCheckpointableIterator(data), self.step_function, initial_state=0) self.test_cases.append(("n={}".format(n), expected_result, it)) class TestSelectManyIterator(TestBase, TestFiniteIteratorMixin, TestFiniteIteratorCheckpointingMixin): @staticmethod def custom_selector(l): return [l[0]] def setUp(self): super().setUp() self.test_cases = [] # default selector for n in self.lengths: for list_length in [1, 4, 9]: data = list(range(n)) expected_result = copy.deepcopy(data) lists = [] while data: lists.append(data[:list_length]) data = data[list_length:] it = SelectManyIterator(NativeCheckpointableIterator(lists)) self.test_cases.append( ("n={}, list_length={}, default selector".format(n, list_length), expected_result, it) ) # custom selector for n in self.lengths: for list_length in [4, 9]: data = list(range(n)) expected_result = [item for i, item in enumerate(data) if (i % list_length) == 0] lists = [] while data: lists.append(data[:list_length]) data = data[list_length:] it = SelectManyIterator(NativeCheckpointableIterator(lists), collection_selector=self.custom_selector) self.test_cases.append( ("n={}, list_length={}, custom selector".format(n, list_length), expected_result, it) ) class TestBlockwiseShuffleIterator(TestBase, TestFiniteIteratorCheckpointingMixin): def setUp(self): super().setUp() self.test_cases = [] for n in self.lengths: for block_size in self.lengths: data = list(range(n)) it = BlockwiseShuffleIterator(NativeCheckpointableIterator(copy.deepcopy(data)), block_size, self.seed) self.test_cases.append(("n={}, block_size={}".format(n, block_size), data, it)) def test_basic(self): for case_name, expected_result, it in self.test_cases: with self.subTest(case_name): result = list(it) self.assertMultisetEqual(result, expected_result) class TestWindowedIterator(TestBase, TestFiniteIteratorMixin, TestFiniteIteratorCheckpointingMixin): def setUp(self): super().setUp() self.test_cases = [] for n in self.lengths: for window_size in self.lengths: if n < window_size: continue data = list(range(n)) it = WindowedIterator(NativeCheckpointableIterator(copy.deepcopy(data)), window_size) expected_result = [] for i in range(len(data)): if i + window_size > len(data): break expected_result.append(tuple(data[i : i + window_size])) self.test_cases.append(("n={}, window_size={}".format(n, window_size), expected_result, it)) class TestSourceIterator(TestBase): # TODO: Do we need more tests for this? def test_exception(self): self.assertRaises(ValueError, create_source_iterator, [1], train=False, shuffle=True) class TestBucketedReadaheadBatchIterator(TestBase, TestFiniteIteratorCheckpointingMixin): dynamic_batch_size = 15 @staticmethod def key_fn(item): return len(item) @staticmethod def batch_size_fn(item): return TestBucketedReadaheadBatchIterator.dynamic_batch_size // len(item) @staticmethod def boundary_key_fn(item): return len(item) < 5 @staticmethod def setup_data(n): data = [] for i in range(n): data.append(tuple(range(i % 10 + 1))) return data def setUp(self): super().setUp() self.batch_sizes = [1, 2, 3, 9] self.test_cases = [] # fixed batch size, not shuffled, no boundary key for n, read_ahead in itertools.product(self.lengths, self.lengths): for batch_size in self.batch_sizes: data = self.setup_data(n) it = BucketedReadaheadBatchIterator( NativeCheckpointableIterator(copy.deepcopy(data)), read_ahead=read_ahead, key=self.key_fn, batch_size=batch_size, shuffle=False, ) self.test_cases.append( ( "n={}, read_ahead={}, batch_size={}, boundary_key=None, shuffled=False".format( n, read_ahead, batch_size ), data, it, ) ) # fixed batch size, shuffled, no boundary key for n, read_ahead in itertools.product(self.lengths, self.lengths): for batch_size in self.batch_sizes: data = self.setup_data(n) it = BucketedReadaheadBatchIterator( NativeCheckpointableIterator(copy.deepcopy(data)), read_ahead=read_ahead, key=self.key_fn, batch_size=batch_size, shuffle=True, seed=self.seed, ) self.test_cases.append( ( "n={}, read_ahead={}, batch_size={}, boundary_key=None, shuffled=True".format( n, read_ahead, batch_size ), data, it, ) ) # dynamic batch size, not shuffled, no boundary key for n, read_ahead in itertools.product(self.lengths, self.lengths): data = self.setup_data(n) it = BucketedReadaheadBatchIterator( NativeCheckpointableIterator(copy.deepcopy(data)), read_ahead=read_ahead, key=self.key_fn, batch_size=self.batch_size_fn, shuffle=False, ) self.test_cases.append( ( "n={}, read_ahead={}, batch_size=dynamic, boundary_key=None, shuffled=False".format(n, read_ahead), data, it, ) ) # dynamic batch size, shuffled, no boundary key for n, read_ahead in itertools.product(self.lengths, self.lengths): data = self.setup_data(n) it = BucketedReadaheadBatchIterator( NativeCheckpointableIterator(copy.deepcopy(data)), read_ahead=read_ahead, key=self.key_fn, batch_size=self.batch_size_fn, shuffle=True, seed=self.seed, ) self.test_cases.append( ( "n={}, read_ahead={}, batch_size=dynamic, boundary_key=None, shuffled=True".format(n, read_ahead), data, it, ) ) # fixed batch size, not shuffled, boundary key for n, read_ahead in itertools.product(self.lengths, self.lengths): for batch_size in self.batch_sizes: data = self.setup_data(n) it = BucketedReadaheadBatchIterator( NativeCheckpointableIterator(copy.deepcopy(data)), read_ahead=read_ahead, key=self.key_fn, batch_size=batch_size, boundary_key=self.boundary_key_fn, shuffle=False, ) self.test_cases.append( ( "n={}, read_ahead={}, batch_size={}, boundary_key=len(item)<5, shuffled=False".format( n, read_ahead, batch_size ), data, it, ) ) # fixed batch size, shuffled, boundary key for n, read_ahead in itertools.product(self.lengths, self.lengths): for batch_size in self.batch_sizes: data = self.setup_data(n) it = BucketedReadaheadBatchIterator( NativeCheckpointableIterator(copy.deepcopy(data)), read_ahead=read_ahead, key=self.key_fn, batch_size=batch_size, boundary_key=self.boundary_key_fn, shuffle=True, seed=self.seed, ) self.test_cases.append( ( "n={}, read_ahead={}, batch_size={}, boundary_key=len(item)<5, shuffled=True".format( n, read_ahead, batch_size ), data, it, ) ) # dynamic batch size, not shuffled, boundary key for n, read_ahead in itertools.product(self.lengths, self.lengths): data = self.setup_data(n) it = BucketedReadaheadBatchIterator( NativeCheckpointableIterator(copy.deepcopy(data)), read_ahead=read_ahead, key=self.key_fn, batch_size=self.batch_size_fn, boundary_key=self.boundary_key_fn, shuffle=False, seed=self.seed, ) self.test_cases.append( ( "n={}, read_ahead={}, batch_size=dynamic, boundary_key=len(item)<5, shuffled=False".format( n, read_ahead ), data, it, ) ) # dynamic batch size, shuffled, boundary key for n, read_ahead in itertools.product(self.lengths, self.lengths): data = self.setup_data(n) it = BucketedReadaheadBatchIterator( NativeCheckpointableIterator(copy.deepcopy(data)), read_ahead=read_ahead, key=self.key_fn, batch_size=self.batch_size_fn, boundary_key=self.boundary_key_fn, shuffle=True, seed=self.seed, ) self.test_cases.append( ( "n={}, read_ahead={}, batch_size=dynamic, boundary_key=len(item)<5, shuffled=True".format( n, read_ahead ), data, it, ) ) def test_basic(self): for case_name, expected_result, it in self.test_cases: with self.subTest(case_name): result = list(it) flattened_result = [item for batch in result for item in batch] self.assertMultisetEqual(flattened_result, expected_result) def test_max_len(self): for case_name, expected_result, it in self.test_cases: if "batch_size=dynamic" in case_name: with self.subTest(case_name): result = list(it) for batch in result: length = sum((len(item) for item in batch)) self.assertTrue(length <= TestBucketedReadaheadBatchIterator.dynamic_batch_size) def test_boundary_key(self): for case_name, expected_result, it in self.test_cases: if "boundary_key=len(item)<5" in case_name: with self.subTest(case_name): result = list(it) for batch in result: boundary_keys = [self.boundary_key_fn(item) for item in batch] self.assertTrue(all(boundary_keys) or not any(boundary_keys))
KosmosX-API-main
kosmosX/infinibatch/test/test_iterators.py
import gzip import itertools from random import Random import os import shutil import tempfile from typing import Iterator import unittest import gc from infinibatch.datasets import chunked_dataset_iterator class TestBase(unittest.TestCase): def setUp(self): self.test_data = [ ["item number one", "item number two", "item number three", "item number four"], ["item number five"], [ "item number six", "item number seven", "item number eight", "item number nine", "item number ten", "item number eleven", ], ["item number twelve", "item number thirteen", "item number fourteen",], ] self.flattened_test_data = [] for chunk in self.test_data: for item in chunk: self.flattened_test_data.append(item) self.data_dir = tempfile.mkdtemp() self.chunk_file_paths = [] for chunk_id, chunk in enumerate(self.test_data): file_name = os.path.join(self.data_dir, "chunk_" + str(chunk_id).zfill(10) + ".gz") self.chunk_file_paths.append(file_name) file_content = "\n".join(chunk) with gzip.open(file_name, "wt", encoding="utf-8") as f: f.write(file_content) @staticmethod def read_chunk(textfile_path: str) -> Iterator[str]: # read_chunk_fn for chunked_dataset_iterator with gzip.open(textfile_path, "rt", encoding="utf-8") as f: return iter(f.read().splitlines()) def tearDown(self): gc.collect() # this will get the pre-fetch terminated in some tests, which otherwise may still want to read these files shutil.rmtree(self.data_dir) def assertMultisetEqual(self, a, b): self.assertEqual(len(a), len(b)) self.assertSetEqual(set(a), set(b)) class Test_chunked_dataset_iterator(TestBase): def test_no_shuffle(self): items = list( itertools.islice( chunked_dataset_iterator(self.chunk_file_paths, self.read_chunk, shuffle=False, buffer_size=1000), len(self.flattened_test_data), ) ) self.assertListEqual(items, self.flattened_test_data) def test_other_files_present(self): with open(os.path.join(self.data_dir, "i_do_not_belong_here.txt"), "w") as f: f.write("really ...") items = list( itertools.islice( chunked_dataset_iterator(self.chunk_file_paths, self.read_chunk, shuffle=False, buffer_size=1000), len(self.flattened_test_data), ) ) self.assertListEqual(items, self.flattened_test_data) def test_transform(self): def transform(s): return s + "!" modified_test_data = [transform(s) for s in self.flattened_test_data] items = list( itertools.islice( chunked_dataset_iterator( self.chunk_file_paths, self.read_chunk, shuffle=False, buffer_size=1000, transform=transform ), len(self.flattened_test_data), ) ) self.assertListEqual(items, modified_test_data) def test_two_instances(self): dataset0 = chunked_dataset_iterator( self.chunk_file_paths, self.read_chunk, shuffle=False, buffer_size=1000, num_instances=2, instance_rank=0 ) dataset1 = chunked_dataset_iterator( self.chunk_file_paths, self.read_chunk, shuffle=False, buffer_size=1000, num_instances=2, instance_rank=1 ) items0 = list(itertools.islice(dataset0, len(self.test_data[0]) + len(self.test_data[2]))) items1 = list(itertools.islice(dataset1, len(self.test_data[1]) + len(self.test_data[3]))) self.assertMultisetEqual(set(items0 + items1), self.flattened_test_data) def test_checkpointing(self): random = Random(1) for use_windowed in (True, False): for i in range(2): first_length = random.randrange(11, 21) extra_length = random.randrange(11, 21) dataset = chunked_dataset_iterator( self.chunk_file_paths, self.read_chunk, shuffle=(i % 2 == 0), buffer_size=1000, seed=i, num_instances=2, instance_rank=0, use_windowed=use_windowed, ) for _ in range(first_length): next(dataset) checkpoint = dataset.getstate() items1 = list(itertools.islice(dataset, extra_length)) dataset.setstate(checkpoint) items2 = list(itertools.islice(dataset, extra_length)) self.assertListEqual(items1, items2) if __name__ == "__main__": unittest.main()
KosmosX-API-main
kosmosX/infinibatch/test/test_datasets.py
""" This file causes the doctests to be included as part of unit tests. To make sure the doctests of a specific module are included, please replicate the `addTests` call for the iterators module below. """ import doctest import infinibatch.iterators def load_tests(loader, tests, ignore): tests.addTests(doctest.DocTestSuite(infinibatch.iterators)) return tests
KosmosX-API-main
kosmosX/infinibatch/test/test_doctests.py
from .iterators import create_source_iterator, CheckpointableIterator, SelectManyIterator, PrefetchIterator, BufferedShuffleIterator, BlockwiseShuffleIterator, MapIterator from typing import List, Iterator, Callable, Any, Optional """ This module contains common datasets, which are implemented as convenience functions that compose underlying Infinibatch iterators. """ def bump_seed(seed: Optional[int], step = 1): """ Helper to bump a random seed if not None. """ return None if seed is None else seed + 1 def chunked_dataset_iterator(chunk_refs: List, read_chunk_fn: Callable[[Any], Iterator], buffer_size: int, train: bool=True, seed: Optional[int]=None, shuffle: bool=True, use_windowed: bool=False, transform: Callable[[Any],Any]=None, prefetch: bool=False, num_instances: int=1, instance_rank: int=0) -> CheckpointableIterator: """ Dataset reading data from gzipped chunks. If train=True, this chunks are strided assigned to instances in strides and the data is infinitely repeated in permutations. Otherwise, the chunks are split among the instances in consecutive blocks and the data is not repeated. This way, when using this dataset for inference on multiple GPUs, to order the outputs in a way that corresponds to the original order of the data items in the dataset, one simply has to collect the lists of outputs from each GPU and then concatenate these lists in order of increasing rank. When using MPI, this can be achieved by a gather-operation to get a list of lists of outputs, one list per GPU, followed by flattening the lists back into a single list. Args: chunk_refs: references (such as path names) to chunk files read_chunk_fn: function(chunk_ref) -> Iterator to read a chunk's content into an iterator over its items, e.g. read a file and split into text lines train: see above shuffle: if true, the data is shuffled. If train is False then shuffle must be False as well. buffer_size: size of the buffer in number of samples / data items used for shuffling (default: 2**20) transform: transform to be applied to each data item (transform(Any) -> Any) prefetch: if True, insert a prefetch iterator with buffer_size seed: random seed (or None) num_instances: number of instances of this dataset. Meant for use with multi-process data loading, e.g., in distributed training. instance_rank: rank of this instance of the dataset. Meant for use with multi-process data loading, e.g., in distributed training. use_windowed: temporary option to switch back to the WindowedShuffleIterator (default False). Will go away once shown that we don't need it anymore. """ if not train and shuffle: raise ValueError('shuffling is not supported when train=False') # set up the chunk reader chunks = create_source_iterator(chunk_refs, train=train, seed=seed, shuffle=shuffle, num_instances=num_instances, instance_rank=instance_rank) # set up the item reader samples = SelectManyIterator(source_iterator=chunks, collection_selector=read_chunk_fn) # type: CheckpointableIterator # wrap the I/O operation in a prefetch iterator if prefetch: samples = PrefetchIterator(samples, buffer_size) # set up the item randomizer if shuffle: if use_windowed: samples = BufferedShuffleIterator(samples, buffer_size, bump_seed(seed, 1)) else: samples = BlockwiseShuffleIterator(samples, buffer_size, bump_seed(seed, 1)) # apply transform, if given if transform is not None: samples = MapIterator(samples, transform) # this is what we are serving out return samples
KosmosX-API-main
kosmosX/infinibatch/infinibatch/datasets.py
""" Infinibatch is a library of checkpointable iterators for randomized data loading of massive data sets in deep neural network training. ## Features * support for corpora much larger than fit into RAM * hierarchical block+sentence-level randomization over the whole corpus, different randomization in each epoch * only load the data that is needed * very fast start-up time (does not need to read full corpus) * only requires the most basic of data preparation (e.g. no indexing) * for multi-GPU, only load what the respective GPU needs * 100% accurate check-pointing, restore from checkpoint should not read all data up to the checkpoint * support automatic bucketed batching with dynamic batch sizes * pre-fetching thread * composable, as to support for complex batching, e.g. negative samples from multiple documents ## Getting Started Infinibatch requires Python 3.5 and has no dependencies. There is presently no pip package. To install it, see README.md ## Tutorial This little tutorial walks you through the steps of preparing your data and consuming them from Python code as batches. ### Infinibatch Basics: Iterators and Checkpointing Infinibatch provides [Python iterators](https://docs.python.org/3.5/glossary.html#term-iterator) to read your data. An iterator represents a stream of data that can be retrieved item by item, e.g. via a `for` loop or repeatedly calling `next()` on it. Infinibatch is agnostic to the data type of the items, which is determined by a user-supplied file-read function. In NLP applications, items would typically be tuples of text. In other applications, they can be images or an audio file with a textual annotation. Infinibatch makes it easy to read your data in randomized order, and supports checkpointing, which allows you to restart training exactly where you left off. Randomization is done _on the fly_, which means that it is not necessary to read the entire data set into memory to be shuffled. Infinibatch implements a hierarchical shuffling algorithm that only holds a subset of the data in RAM at any point in time. Infinibatch iterators are _checkpointable_. Checkpointing lets you retrieve the current position (the "checkpoint") in the data stream at any time, so that later, you can "rewind" to that same position. The sad reality is that long-running trainings occasionally crash. To be able to continue a crashed training as if it had not crashed, save your Infinibatch iterator's checkpoint to disk whenever you save an intermediate model during training. To restart a crashed training, reset the iterator to the saved checkpoint. The data reader will now yield the exact same data-item sequence it would have yielded without the crash. ### Data Preparation Infinibatch has one requirement on your data organization: To use your data with Infinibatch, it must be split into a large number of small chunks. A chunk is the smallest unit of data that is loaded from disk into RAM. Infinibatch holds a random subset of chunks in memory that it randomly draws samples from. Below we want to show how such a split can be created. An easy way to split your data into chunks is with the Linux `split` command. In this tutorial, our "corpus" consists of 6 lines of text, where each line is one data item. To create that corpus, please run this command in a bash shell. It creates a 6-line text file named `corpus.txt`: ```bash echo \\ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. The quick brown fox jumps over the lazy dog.' \\ > corpus.txt ``` Now let us split it into 3 chunks of 2 lines each. Each chunk is stored as a zipped text file. We will create them inside a new subdirectory called `corpus_chunks`: ```bash mkdir corpus_chunks split --lines 2 --numeric-suffixes \\ --filter 'gzip > corpus_chunks/$FILE.txt.gz' \\ corpus.txt corpus. ``` This will have created three files: `corpus_chunks/corpus.00.txt.gz`, `corpus_chunks/corpus.01.txt.gz`, and `corpus_chunks/corpus.02.txt.gz`. To verify whether the data has been split as expected, you can use this command: ```bash zcat corpus_chunks/corpus.*.txt.gz ``` Hint: For large corpora, we recommend replacing `gzip` by `pigz` (`apt-get install pigz`), which runs notably faster via multi-threading. ### Reading Items in Random Order With Infinibatch We will first show the easiest way to read data with Infinibatch, using the helper function `chunked_dataset_iterator``()`. This function will create an Infinibatch iterator that yields the content of your data in random order. Please the following program: ```python import gzip, glob from infinibatch import datasets as ds ds = ds.chunked_dataset_iterator( chunk_refs = glob.glob('corpus_chunks/corpus.*.txt.gz'), read_chunk_fn = lambda path: iter(gzip.decompress(open(path, "rb") \\ .read()).decode(encoding='utf-8') \\ .splitlines()), buffer_size = 6, seed = 1) for i in range(10): print(next(ds)) ``` You should get output that contains the 6 example lines in randomized order: ```text Lorem ipsum dolor sit amet, consectetur adipiscing elit, Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. The quick brown fox jumps over the lazy dog. sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. consectetur adipiscing elit, Lorem ipsum dolor sit amet, The quick brown fox jumps over the lazy dog. sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ``` Note: The `buffer_size` parameter determines how many sentences are read into memory at any given time, to draw randomized items from. In real settings with corpora of hundreds of millions of text lines, the `buffer_size` parameter should be set in the millions. RAM usage and startup time will be proportional to the buffer size (but much lower than having to load the entire corpus into RAM). ### Reading Items of Different Lengths in Batches For deep learning, we want to group multiple items into batches. For NLP tasks, items are often lines of text of varying length. Infinibatch implements an algorithm that randomizes the input sequence and groups it into batches of approximately the same length (aka _bucketing_). Infinibatch's `BucketedReadaheadBatchIterator` performs this task. It implements an algorithm modeled after the [Marian toolkit](https://github.com/marian-nmt/marian) that preloads a large number of randomized items (typically millions; in this example: 6), sorts them and groups them into batches of similar length, and then yields them, in turn, in randomized order. Here is an example. Note that the `BucketedReadaheadBatchIterator` accepts the previous randomized sentence sequence iterator (`ds`) as the source of items to randomize over. This is an example how one forms pipelines of iterators with Infinibatch (a concept familiar from Python's own `itertools`). Once an iterator is passed to another as its source, consider it owned by that other iterator, it must no longer be accessed by the calling code. ```python import gzip, glob from infinibatch import datasets as ds from infinibatch import iterators as it ds = ds.chunked_dataset_iterator( chunk_refs = glob.glob('corpus_chunks/corpus.*.txt.gz'), read_chunk_fn = lambda path: iter(gzip.decompress(open(path, "rb") \\ .read()).decode(encoding='utf-8') \\ .splitlines()), buffer_size = 6, seed = 1) bs = it.BucketedReadaheadBatchIterator( source_iterator = ds, # note: this is the iterator from above read_ahead = 6, key = lambda line: len(line), batch_size = 2, seed = 1) for i in range(25): print(next(bs)) ``` This code should output something like this: ```python ['sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.', 'The quick brown fox jumps over the lazy dog.'] ['consectetur adipiscing elit,', 'Lorem ipsum dolor sit amet,'] ['Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.', 'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.'] ``` followed by different permutations of the same tuples. As you can see, the sentences are in random order and grouped in batches of 2 of approximately the same length. You may notice that there is no variation in how the items get grouped into batches--that is an artifact of this example, and generally not the case in real use when the data size is much larger than the batch size. In NLP, sentence length often varies considerably. As a result, using batches of a fixed number of lines, as in the example above, will waste GPU RAM and cores. This is because the number of lines is limited by the longest possible sequence; batches of shorter lines would leave GPU cycles on the table. Ideally, one would use batches that have as many lines as fit into GPU RAM, given the number of tokens of the longest line in the batch. To support variable batch sizes, Infinibatch allows to pass a function as the `batch_size` parameter. That function will be given the longest item of a batch and should estimate how many items of at most this length can fit. In our example, we assume that batches can hold at most 150 tokens. Please change the above code as follows: ```python batch_size = lambda longest_line: 150 // len(longest_line), ``` The output looks like this: ``` ['consectetur adipiscing elit,', 'Lorem ipsum dolor sit amet,'] ['Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'] ['sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.', 'The quick brown fox jumps over the lazy dog.'] ['Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.'] ``` That shorter sentences got grouped, while longer did not because they would exceed the total of 150 characters. ### Reading Batches Into Numpy Arrays Lastly, we will need to feed batches into our favorite deep-learning tool. We will show how to convert the batches of text lines into padded `numpy` arrays. In a typical NLP application, text items would be tokenized, and then each token would be represented by an index into a unit vocabulary. For simplicity, in this example each character is its own token, and each token's numeric unit index is just its ASCII code. These sequences are then padded to equal length with -1, and converted into a `numpy` array. Please rerun the previous example, but first insert the following code before the final `for` loop. This example uses an Infinibatch `MapIterator`, which applies a user-supplied function or lambda to each item: ```python import numpy as np def collate(lines_batch): # tokenize all lines in the batch and map to unit ids ids_batch = [[ord(c) for c in line] for line in lines_batch] # create a padded numpy array as wide as the longest line, # where shorter sequences are padded with -1 width = max(len(ids) for ids in ids_batch) return np.array([ids + [-1] * (width-len(ids)) for ids in ids_batch]) bs = it.MapIterator( source_iterator = bs, transform = collate) ``` This will output batches like this. Note that in batches with multiple sentences, some entries are padded with `-1`. ```python [[ 99 111 110 115 101 99 116 101 116 117 114 32 97 100 105 112 105 115 99 105 110 103 32 101 108 105 116 44] [ 76 111 114 101 109 32 105 112 115 117 109 32 100 111 108 111 114 32 115 105 116 32 97 109 101 116 44 -1]] [[ 85 116 32 101 110 105 109 32 97 100 32 109 105 110 105 109 32 118 101 110 105 97 109 44 32 113 117 105 115 32 110 111 115 116 114 117 100 32 101 120 101 114 99 105 116 97 116 105 111 110 32 117 108 108 97 109 99 111 32 108 97 98 111 114 105 115 32 110 105 115 105 32 117 116 32 97 108 105 113 117 105 112 32 101 120 32 101 97 32 99 111 109 109 111 100 111 32 99 111 110 115 101 113 117 97 116 46]] [[115 101 100 32 100 111 32 101 105 117 115 109 111 100 32 116 101 109 112 111 114 32 105 110 99 105 100 105 100 117 110 116 32 117 116 32 108 97 98 111 114 101 32 101 116 32 100 111 108 111 114 101 32 109 97 103 110 97 32 97 108 105 113 117 97 46] [ 84 104 101 32 113 117 105 99 107 32 98 114 111 119 110 32 102 111 120 32 106 117 109 112 115 32 111 118 101 114 32 116 104 101 32 108 97 122 121 32 100 111 103 46 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1]] [[ 68 117 105 115 32 97 117 116 101 32 105 114 117 114 101 32 100 111 108 111 114 32 105 110 32 114 101 112 114 101 104 101 110 100 101 114 105 116 32 105 110 32 118 111 108 117 112 116 97 116 101 32 118 101 108 105 116 32 101 115 115 101 32 99 105 108 108 117 109 32 100 111 108 111 114 101 32 101 117 32 102 117 103 105 97 116 32 110 117 108 108 97 32 112 97 114 105 97 116 117 114 46]] ``` ## Where To Go From Here The above tutorial showed you the use of the most common iterator type, as created by the convenience function `chunked_dataset_iterator()`. Not all real-life scenarios are covered by this function. For example, multi-task learning scenarios require more complex combinations of data. To create those, you will need to compose the necessary data reader from the underlying building blocks. This is described at the documentation of the module `iterators`. """ from .iterators import *
KosmosX-API-main
kosmosX/infinibatch/infinibatch/__init__.py
""" ## Overview This part of the documentation covers the __advanced usage__ of Infinibatch by assembling __custom data loading pipelines__. Before you continue, please go through the tutorial on the top-level of the documentation of the `infinibatch` module. Two of the main features of Infinibatch are __lazy evaluation__ through the use of __iterators__ and built-in support for __checkpointing__. In this section, we give an introduction to these features and the basic usage of the Infinibatch iterator library. ### Iterators As a Python programmer, you are probably familiar with the concept of iterators. According to the [Python documentation](https://docs.python.org/3.5/glossary.html#term-iterator), an iterator is an object representing a stream of data, and repeated calls to the iterator's `__next__()` method (or passing it to the built-in function `next()`) return successive items in the stream. It is important not to confuse an [iterator](https://docs.python.org/3.5/glossary.html#term-iterator) with an [iterable](https://docs.python.org/3.5/glossary.html#term-iterable). For more information on this subject, please follow the links above. The Python standard library contains a module of iterators called `itertools` that bears some resembles to Infinibatch. Infinibatch differs from `itertools` in two ways: 1. Infinibatch provides iterators specifically for the purpose of creating __randomized batches of data for machine learning__. 2. All iterators in Infinibatch support __checkpointing__ (see the following section). Infinibatch iterators are not directly compatible with itertools due to the checkpointing requirement. Infinibatch enables you to build complex data loaders by combining iterators from this module into a pipeline. To give you a high-level idea of how this is works, we provide a very simple example. Note that this example is completely artificial and does not solve any useful task. Its only purpose is to demonstrate the behavior of a pipeline of iterators. We provide a more realistic example in a later section. First, we create a small test data set. >>> dataset = list(range(6)) # 0, 1, 2, 3, 4, 5 We can turn this data set into an Infinibatch iterator by wrapping it in a `NativeCheckpointableIterator`. >>> it = NativeCheckpointableIterator(dataset) # 0, 1, 2, 3, 4, 5 We can then transform the data items using a `MapIterator`, which applies a given function to each individual data item. For example, we can multiply each data item by 2. >>> it = MapIterator(it, lambda n: 2 * n) # 0, 2, 4, 6, 8, 10 We can restructure the data set by batching together pairs of data items into lists using a `FixedBatchIterator`. >>> it = FixedBatchIterator(it, batch_size=2) # [0, 2], [4, 6], [8, 10] Using another `MapIterator`, we can reduce each of these lists to its second element. >>> it = MapIterator(it, lambda l: l[1]) # 2, 6, 10 Finally, we can use the resulting iterator `it` just like any standard Python iterator. ```py >>> for item in it: ... print(item) 2 6 10 ``` By using iterators, Infinibatch operates in a __lazy__ fashion: It generally doesn't apply operations to an entire data set at once, but rather operates on individual data items on-the-fly as they are consumed. When used correctly, this allows Infinibatch to have a low start-up time and low memory overhead. For more detail on this, please consult the section on performance considerations below. ### Checkpointing The main features that sets Infinibatch iterators apart from standard Python iterators is that they support __checkpointing__. A checkpoint encapsulates the internal state of an entire pipeline of iterators at a specific point while iterating through a data set. Once you retrieve a checkpoint, you can later use it to reset the pipeline of iterators to the exact state it was in when the checkpoint was created. Checkpoints can easily be serialized and stored to disk using [Pythons `pickle` module](https://docs.python.org/3.5/library/pickle.html). Infinibatch's checkpointing feature is particularly useful when you're training large deep neural network models over days or weeks, and you want to make sure that, in case your training is interrupted for any reason, __you can pick up your training exactly where you left off__. The checkpointing interface consists of two functions `getstate` and `setstate` that are defined in `CheckpointableIterator`, the common base class of all iterators in this module. As the names suggest `getstate` returns a checkpoint object that represents the state of a pipeline at the time the function is called, and 'setstate' receives a checkpoint object to reset the state of a pipeline. `setstate` also accepts `None`, which resets a pipeline to the __beginning__ of the iteration, i.e. the state of the pipeline immediately after its construction. It is important to realize that __a checkpoint represents the state of a complete pipeline of iterators__. If you have a pipeline consisting of a sequence of iterators, you only have to call `getstate` on the __last__ iterator in the sequence to capture the state of the entire pipeline. Internally, this is achieved by recursive calls that traverse the entire data loading pipeline to collect the state of every iterator in it. Similarly, when you want to reset a pipeline to a previous state, you only have to call `setstate` on the __last__ iterator in the pipeline. To demonstrate this, we recreate the pipeline from the previous section. >>> dataset = list(range(6)) # 0, 1, 2, 3, 4, 5 >>> it = NativeCheckpointableIterator(dataset) # 0, 1, 2, 3, 4, 5 >>> it = MapIterator(it, lambda n: 2 * n) # 0, 2, 4, 6, 8, 10 >>> it = FixedBatchIterator(it, batch_size=2) # [0, 2], [4, 6], [8, 10] >>> it = MapIterator(it, lambda l: l[1]) # 2, 6, 10 Since `it` behaves just like a standard Python iterator, we can call `next` to retrieve its first element. >>> next(it) 2 We can now call `getstate` on `it` (which is the last `MapIterator` in the pipeline) to get a checkpoint of the internal state of the entire data loading pipeline. >>> checkpoint = it.getstate() Note that the checkpoint represents the internal state of the pipeline after the data item `2` has been retrieved. Using the checkpoint, we can always return to this __exact__ point in the data set. To show this, let's exhaust the iterator by casting it to a list. >>> list(it) [6, 10] Since the iterator is now exhausted, calling `next` raises a `StopIteration` exception. ``` >>> next(it) Traceback (most recent call last): ... StopIteration ``` We can now reset the pipeline to the checkpoint using `setstate`. >>> it.setstate(checkpoint) This recovers the state of the pipeline after the data item `2` has been retrieved. Thereby, we expect the next element to be `6`. >>> next(it) 6 ## Types of Iterators This section provides a brief overview of the different types of iterators in Infinibatch. ### Classes and Factory Functions Most iterators in this module are implemented as classes that inherit from the abstract base class `CheckpointableIterator`. However, some iterators (such as the `BlockwiseShuffleIterator`) are simple combinations of other iterators. These iterators are implemented as __factory functions__ that construct a pipeline of iterators and return the last iterator in the pipeline. For consistency with class-based iterators, we name these factory function using CamelCase instead of the more pythonic use_of_underscores. .. todo:: We currently also have one factory function that actually looks like one: `create_source_iterator`. Provide a comment on this describing why that is. ### Source Iterators There are three iterators that are intended to go at the __beginning__ of a data loading pipeline: - `InfinitePermutationSourceIterator`: This iterator accepts a list, shuffles it, and yields its elements. It repeats this infinitely, shuffling the list after each pass. Thereby, __this iterator is infinte and cannot be exhausted__. This iterator is meant to be used as the first iterator in a training scenario and supports splitting the data for multi-GPU training. - `ChunkedSourceIterator`: This iterator accepts a list and yields its elements. It is meant to be used as the first iterator in an inference or validation scenario and supports splitting the data for mult-GPU inference. - `NativeCheckpointableIterator`: This iterator wraps a Python iterable and makes it checkpointable. It is mainly intended for demonstration and debugging purposes. ### Shuffling .. todo:: Describe `BufferedShuffleIterator` and `BlockwiseShuffleIterator`. ### Batching, SelectMany, and Windowing .. todo:: Describe `FixedBatchIterator`, `SelectManyIterator`, and `WindowedIterator`. ### Mapping .. todo:: Describe `MapIterator`, `ParallelMapIterator`, `RecurrentIterator`, and `SamplingRandomMapIterator`. ### Other Iterators .. todo:: Describe `ZipIterator`, `PrefetchIterator`, and `BucketedReadaheadBatchIterator`. ## Complete Example .. todo:: Give a more realistic example following, in broad strokes, the ChunkedDataset including: - use gzip chunks - training pipeline example - inference pipeline example - pipeline that can do both - etc. ## Performance Considerations .. todo:: Describe what parameters influence performance measures such as memory usage and start-up time. """ from abc import abstractmethod import collections import copy from itertools import islice import logging import multiprocessing import os import queue from random import Random import threading from typing import Any, Callable, Dict, Iterable, Iterator, List, Optional, Tuple, Union, cast logger = logging.getLogger(__name__) # TODO for next release: # - benchmark the accuracy when using BlockwiseShuffleIterator vs. the BufferedShuffleIterator # - change all convenience functions back to true classes, using a wrapper class # TODO later: # - make iterator pipeline work for streaming data def _advance_iterator(iterator: Iterator, n: int): """ Little helper to advance an iterator by n items """ for i in range(n): try: next(iterator) except StopIteration: raise RuntimeError('Trying to advance iterator by {} but iterator raised StopIteration exception on call to next with index {}.'.format(n, i)) return n class CheckpointableIterator(collections.abc.Iterator): """ Abstract base class that defines the interface for checkpointing. The interface (getstate, setstate) is inspired by Python's random package. """ def __iter__(self) -> 'CheckpointableIterator': return self @abstractmethod def getstate(self) -> Dict: """ Get checkpoint of current state of iterator In a pipeline of iterators, this function __recursively__ calls itself on the preceeding iterator and includes the gathered information in the returned checkpoint. Thereby, to obtain a checkpoint of the state of an entire pipeline of iterators you only have to call this function on the __last__ iterator in the pipeline. A checkpoint is represented as a `dict`, but the caller should treat a checkpoint as an opaque object and not make any assumptions about the existence or meaning of the `dict` entries. """ pass @abstractmethod def setstate(self, checkpoint: Optional[Dict]): """ Set state of iterator to given checkpoint In a pipeline of iterators, this function __recursively__ calls itself on the preceeding iterator. Thereby, to set the state of an entire pipeline of iterators to a given checkpoint you only have to call this function on the __last__ iterator in the pipeline. Args: checkpoint: Checkpoint that should be used to reset the state of the iterator (or pipeline). If this is __None__, the state of the iterator (or pipeline) is reset to the initial state immediately after construction. """ pass def __getstate__(self) -> Dict: # implementation of pickle Protocol return self.getstate() def __setstate__(self, checkpoint: Optional[Dict]): self.setstate(checkpoint) @abstractmethod def __next__(self): pass @abstractmethod def close(self): """ Close all PrefetchIterators in this pipeline PrefetchIterators have internal resources that need to be properly managed by calling close() manually. Failure to do so can lead to dangling processes and threads, or the PrefetchIterator hanging on finalization. Note that it is not correct to rely on the garbage collector to destroy PrefetchIterators as CPython does not assure that the finalizer (__del__) of a PrefetchIterator will be called. This function, which is implemented for every CheckpointableIterator, recursively traverses all preceeding iterators and closes all PrefetchIterators in the pipeline. For pipelines that do not contain PrefetchIterators this function has no effect. """ pass class NativeCheckpointableIterator(CheckpointableIterator): """ Simple wrapper class that turns a Python Iterable into a CheckpointableIterator When calling setstate on this class, it simply replays the iterator all the way to the checkpoint one element at a time, which makes it generally inefficient. Warning: This class cannot be used with Iterators (as opposed to Iterables), which have an `__iter__` function that simply returns self, but does not reset. """ def __init__(self, iterable: Iterable): # check whether iterable is iterable or iterator: # if the variable iterable contains an iterator, the function __iter__ returns self # if the variable iterable is an actual iterator, it should not return self if iter(iterable) is iterable: raise ValueError('It looks like you are passing an iterator instead of an iterable. This is not supported and can cause undefined behavior when used with checkpointing.') self._input_iterable = iterable self.setstate(None) def getstate(self) -> Dict: return {'num_items_yielded': self._num_items_yielded} def setstate(self, checkpoint: Optional[Dict]): self._iterator = iter(self._input_iterable) self._num_items_yielded = _advance_iterator(self._iterator, checkpoint['num_items_yielded']) if checkpoint is not None else 0 def __next__(self): item = next(self._iterator) # call this before increasing _num_items_yielded to correctly handle the case when a StopIteration exception is thrown self._num_items_yielded += 1 return item def close(self): pass def create_source_iterator(source_items: List, train: bool=True, seed: Optional[int]=None, shuffle: bool=True, num_instances: int=1, instance_rank: int=0) -> CheckpointableIterator: if not train and shuffle: raise ValueError('shuffling is not supported when train=False') if train: return InfinitePermutationSourceIterator(source_items, seed=seed, shuffle=shuffle, num_instances=num_instances, instance_rank=instance_rank) else: return ChunkedSourceIterator(source_items, num_instances=num_instances, instance_rank=instance_rank) def ChunkedSourceIterator(source_items: List, num_instances: int=1, instance_rank: int=0) -> CheckpointableIterator: """ Cuts source list into chunks, one per instance, and serves out items in chunk corresponding to instance_rank This is a source iterator: It is meant to be used at the beginning of a data loading pipeline. As such, it takes a list as its source and not a CheckpointableIterator. Args: source_items: input list, must not be empty and must be small enough to fit into RAM entirely, ownership of the list and the data goes to the iterator, do not modify it! num_instances: number of instances of this iterator. Meant for use with multi-process data loading, e.g., in distributed training. instance_rank: rank of this instance of the iterator. Meant for use with multi-process data loading, e.g., in distributed training. """ if instance_rank >= num_instances: raise ValueError("invalid instance_rank") # we split the data into num_instances consecutive parts # that differ by at most 1 in size num_items_per_rank = len(source_items) // num_instances ranks_with_additional_item = len(source_items) - num_instances * num_items_per_rank def boundary(rank): return rank * num_items_per_rank + min(rank, ranks_with_additional_item) items = source_items[boundary(instance_rank):boundary(instance_rank + 1)] return NativeCheckpointableIterator(items) class InfinitePermutationSourceIterator(CheckpointableIterator): """ Infinitely generates permutations of the items in the given list. This is a source iterator: It is meant to be used at the beginning of a data loading pipeline. As such, it takes a list as its source and not a CheckpointableIterator. The given list is loaded completely into RAM. For example, this is used for randomizing the pathnames of data blocks read by ChunkedReadlinesIterator. """ def __init__( self, source_items: List, seed: int = 0, shuffle: bool = True, num_instances: int = 1, instance_rank: int = 0, ): """ Args: source_items: input list, must not be empty, must be small enough to fit into RAM entirely, and must support deepcopies seed: random seed used for shuffling shuffle: set False to bypass the shuffling. Then this is just a checkpointed version of itertools.cycle(). (Default: True) num_instances: number of instances of this iterator. Meant for use with multi-process data loading, e.g., in distributed training. instance_rank: rank of this instance of the iterator. Meant for use with multi-process data loading, e.g., in distributed training. """ if not source_items: raise ValueError("source must not be empty") if instance_rank >= num_instances: raise ValueError("invalid instance_rank") self._source_items = copy.deepcopy(source_items) self._shuffle = shuffle self._seed = seed self._num_instances = num_instances self._instance_rank = instance_rank self.setstate(None) def getstate(self) -> Dict: return {"random_state": self._random_state, "index": self._index} def setstate(self, checkpoint: Optional[Dict]): self._random_state = checkpoint["random_state"] if checkpoint else None self._index = checkpoint["index"] if checkpoint else self._instance_rank self._random = None # this will trigger the lazy initialization in self.__next__ def __next__(self): if self._random is None: # lazy initialization self._random = Random(self._seed) if self._random_state is not None: self._random.setstate(self._random_state) if self._shuffle: self._reshuffle() # create initial permutation self._reshuffle_as_necessary() # reshuffle as often as necesary to bring self._index into range else: self._index = self._index % len(self._source_items) assert 0 <= self._index and self._index < len(self._source_items) if self._shuffle: result = self._shuffled_items[self._index] self._index += self._num_instances self._reshuffle_as_necessary() # reshuffle as often as necesary to bring self._index into range else: result = self._source_items[self._index] self._index = (self._index + self._num_instances) % len(self._source_items) assert 0 <= self._index and self._index < len(self._source_items) return result def close(self): pass def _reshuffle_as_necessary(self): while self._index >= len(self._source_items): # The new index is out of range, so we need to reshuffle. # Since len(self._source_items) can be smaller than self._num_instances, # we might have to reshuffle multiple times to "skip through" permutations of self._source_items. # Even though there might be intermediate permutations that are not actually used, # we have to generate all of them to make sure we get the right RNG state # to guarantee correctness when using multiple instances. self._reshuffle() self._index -= len(self._source_items) def _reshuffle(self): self._random_state = self._random.getstate() self._shuffled_items = copy.deepcopy(self._source_items) self._random.shuffle(self._shuffled_items) class MultiplexIterator(CheckpointableIterator): """ Multiplexes multiple input iterators. A control iterator is expected to yield a sequence of indices into an array of input iterators. The next item is selected from the input iterator whose index was read from the control iterator """ def __init__(self, control_iterator: CheckpointableIterator, source_iterators: List[CheckpointableIterator]): if any(not isinstance(it, CheckpointableIterator) for it in [control_iterator] + source_iterators): raise ValueError('control_iterator and source_iterators have to be CheckpointableIterators') self._control_iterator = control_iterator # type: CheckpointableIterator self._source_iterators = list(source_iterators) # type: List[CheckpointableIterator] self.setstate(None) def getstate(self) -> Dict: return {'control_iterator_state': self._control_iterator.getstate(), 'source_iterator_states': [source_iterator.getstate() for source_iterator in self._source_iterators]} def setstate(self, checkpoint: Optional[Dict]): self._control_iterator.setstate(checkpoint['control_iterator_state'] if checkpoint else None) for i, source_iterator in enumerate(self._source_iterators): source_iterator.setstate(checkpoint['source_iterator_states'][i] if checkpoint else None) def _generate(): for index in self._control_iterator: item = next(self._source_iterators[index]) yield item self._iterator = _generate() def __next__(self): return next(self._iterator) def close(self): self._control_iterator.close() for it in self._source_iterators: it.close() class SelectManyIterator(CheckpointableIterator): """ Projects each element of a source sequence to a sequence and flattens the resulting sequences into one sequence. """ def __init__(self, source_iterator: CheckpointableIterator, collection_selector: Optional[Callable[[Any], Iterator]]=None): """ Args: source_iterator: iterator over the items to pass to collection_selector() collection_selector: user callback that maps an item into an Iterable, whose items will be yielded. The returned Iterator is used only once. Hence, it is also allowed to return self-iterables, such as iterators and generator expressions. If None is given, no callback is applied. """ if not isinstance(source_iterator, CheckpointableIterator): raise ValueError('source_iterator has to be a CheckpointableIterator') self._source_iterator = source_iterator # type: CheckpointableIterator self._collection_selector = collection_selector # type: Optional[Callable[[Any], Iterator]] self.setstate(None) def getstate(self) -> Dict: return {'source_state': self._source_state, 'flattened_items_yielded': self._flattened_items_yielded} def setstate(self, checkpoint: Optional[Dict]): self._source_state = checkpoint['source_state'] if checkpoint else None self._flattened_items_yielded = checkpoint['flattened_items_yielded'] if checkpoint else 0 self._source_iterator.setstate(self._source_state) def _generate(): skip_to_checkpoint = self._flattened_items_yielded # main loop over source source_items for source_item in self._source_iterator: if self._collection_selector is not None: data = iter(self._collection_selector(source_item)) else: data = iter(source_item) self._flattened_items_yielded = 0 if skip_to_checkpoint: #print("Skipping to index", skip_to_checkpoint, file=sys.stderr) self._flattened_items_yielded += _advance_iterator(data, skip_to_checkpoint) skip_to_checkpoint = 0 # main loop over lines for item in data: self._flattened_items_yielded += 1 yield item self._source_state = self._source_iterator.getstate() self._iterator = _generate() def __next__(self): return next(self._iterator) def close(self): self._source_iterator.close() class BufferedShuffleIterator(CheckpointableIterator): """ Shuffles given iterable using a limited buffer. """ def __init__(self, source_iterator: CheckpointableIterator, buffer_size: int, seed: int=0): """ Args: source_iterator: checkpointable iterator or restartable iterable over input items to shuffle buffer_size: size of the buffer in number of items used for shuffling seed: random seed used for shuffling (or None) """ if not isinstance(source_iterator, CheckpointableIterator): raise ValueError('source_iterator has to be a CheckpointableIterator') self._source_iterator = source_iterator self._buffer_size = buffer_size self._seed = seed self.setstate(None) def getstate(self) -> Dict: return {'source_state': self._source_iterator.getstate(), 'buffer': copy.deepcopy(self._buffer), # create deepcopy so that iterator cannot modify checkpoint after it was taken 'random_state': self._random.getstate()} def setstate(self, checkpoint: Optional[Dict]): if checkpoint: self._source_iterator.setstate(checkpoint['source_state']) self._buffer = copy.deepcopy(checkpoint['buffer']) # create deepcopy so that iterator cannot modify checkpoint self._random.setstate(checkpoint['random_state']) # @TODO: Can we add a comment how the flush part is handled? else: self._source_iterator.setstate(None) self._buffer = [None for _ in range(self._buffer_size)] self._random = Random(self._seed) # type: Random self._iterator = self._generate() def _generate(self) -> Iterator: # shuffle data with a buffer: # this is similar to what the Fisher-Yates shuffle does, # but modified to run with a constant-size buffer # see https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle # this was inspired by an algorithm implemented in Kaldi # see https://kaldi-asr.org/doc/nnet-shuffle-egs_8cc.html for item in self._source_iterator: index = self._random.randrange(0, len(self._buffer)) result = None if self._buffer[index] is not None: result = self._buffer[index] self._buffer[index] = item # only yield value once buffer is updated to allow for correct checkpointing! if result is not None: yield result # flush buffer while self._buffer: item = self._buffer.pop() if item is not None: yield item def __next__(self): return next(self._iterator) def close(self): self._source_iterator.close() class MapIterator(CheckpointableIterator): """ Applies given tranform to each data item """ def __init__(self, source_iterator: CheckpointableIterator, transform: Callable[[str],Any]): """ Args: source_iterator: checkpointable iterator transform: function to be applied to each data item """ if not isinstance(source_iterator, CheckpointableIterator): raise ValueError('source_iterator has to be a CheckpointableIterator') self._source_iterator = source_iterator self._transform = transform def getstate(self) -> Dict: return self._source_iterator.getstate() def setstate(self, checkpoint: Optional[Dict]): self._source_iterator.setstate(checkpoint) def __next__(self): return self._transform(next(self._source_iterator)) def close(self): self._source_iterator.close() def ParallelMapIterator(source_iterator: CheckpointableIterator, transform: Callable[[str],Any], num_processes: int, num_items_per_process: int) -> CheckpointableIterator: """ Applies given transform to each data item Behaves the same as MapIterator, but applies transform in parallel using multiple processes in a parallel map operation. Warning: The transform function has to be pickleable because it is sent across process boundaries. To achieve this, transform should be a top-level function. Args: source_iterator: checkpointable iterator transform: function to be applied to each data item, has to be pickleable, see above num_processes: number of processes to use for parallel map num_items_per_process: number of data items each process operates on """ # divide stream of data items into batches batched_samples = FixedBatchIterator(source_iterator, num_processes * num_items_per_process) # create process pool and capture it in closure that performs parallel map p = multiprocessing.Pool(num_processes) def parallel_map_transform(buffer): return p.map(transform, buffer) # apply transform in parallel to data items in a batch batched_transformed_samples = MapIterator(batched_samples, parallel_map_transform) # unpack batches to go back to stream of (now transformed) data items transformed_samples = SelectManyIterator(batched_transformed_samples) return transformed_samples class ZipIterator(CheckpointableIterator): """ Zips items from all given iterators, like the Python standard function zip(). Like Python's build-in zip(), the iteration stops when the shortest input iterable is exhausted. """ def __init__(self, *source_iterators: CheckpointableIterator): """ Args: source_iterators: list of iterators to zip, item by item """ # TODO: Use all function? for source_iterator in source_iterators: if not isinstance(source_iterator, CheckpointableIterator): raise ValueError('all iterators in source_iterators have to be CheckpointableIterator') self._source_iterators = list(source_iterators) # type: List[CheckpointableIterator] def getstate(self) -> Dict: return {'input_states': tuple(iterator.getstate() for iterator in self._source_iterators)} def setstate(self, checkpoint: Optional[Dict]): if checkpoint is None: for iterator in self._source_iterators: iterator.setstate(None) else: # TODO: Add check that both lists have the same length? for iterator, state in zip(self._source_iterators, checkpoint['input_states']): iterator.setstate(state) def __next__(self): res = [] # (note: can't use a generator expression, as it gets confused when a next() call raises StopIteration) for iterator in self._source_iterators: res.append(next(iterator)) return tuple(res) def close(self): for it in self._source_iterators: it.close() # @TODO: The yield makes a (shallow) copy of the window, which has complexity O(width * length). In some cases, # we don't actually need to consume all items in the window. Hence, to make this faster, we should use # double-buffering and return a slice view (which we'd have to write). class WindowedIterator(CheckpointableIterator): """ Yields 'width' consecutive items in a sliding window. E.g. [1, 2, 3, 4, 5, 6] with width = 3 will yield [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)] """ def __init__(self, source_iterator: CheckpointableIterator, width: int): """ Args: source_iterator: checkpointable input iterators """ if not isinstance(source_iterator, CheckpointableIterator): raise ValueError('source_iterator has to be a CheckpointableIterator') self._source_iterator = source_iterator # type: CheckpointableIterator self._width = width # type: int self.setstate(None) def getstate(self) -> Dict: return {'source_state': self._source_state, # state for first item in FIFO 'item_index': self._item_index} # index of next item to serve def setstate(self, checkpoint: Optional[Dict]): self._source_state = checkpoint['source_state'] if checkpoint else None self._item_index = checkpoint['item_index'] if checkpoint else 0 self._source_iterator.setstate(self._source_state) self._iterator = self._generate() def _fifo_slice(self, i): # returns a window into the FIFO beginning at i # @TODO: for efficiency, make this a slice view return tuple(self._fifo[i:i + self._width]) def _generate(self) -> Iterator: self._source_state = self._source_iterator.getstate() self._fifo = list(islice(self._source_iterator, self._width)) # we do this in overlapping blocks of length 2*width, for easier checkpointing and potential efficiency while len(self._fifo) == self._width: # we got 'width' items; append another 'width' (or less if at end) next_input_state = self._source_iterator.getstate() self._fifo.extend(islice(self._source_iterator, self._width)) # now serve all positions in first half (last = width - 1). If at end, then limit accordingly. last = min(self._width - 1, len(self._fifo) - self._width) while self._item_index <= last: window = self._fifo_slice(self._item_index) self._item_index += 1 yield window # drop all we just served; if < width left, we have hit the end self._fifo = self._fifo[last + 1:] # Note: This must be a new list, since the old might still be in a slice view. self._source_state = next_input_state # this reflects now the first element in the FIFO self._item_index = 0 def __next__(self): return next(self._iterator) def close(self): self._source_iterator.close() # @TODO: research on whether this operation has a well-known name class FixedBatchIterator(CheckpointableIterator): """ Batches N consecutive items into a single item that is a list of these items. E.g. [1, 2, 3 4, 5, 6, 7, 8] with batch_size = 3 will yield [[1, 2, 3], [4, 5, 6], [7, 8]] """ def __init__(self, source_iterator: CheckpointableIterator, batch_size: int): """ Args: source_iterator: checkpointable input iterators batch_size: number of items per batch """ if not isinstance(source_iterator, CheckpointableIterator): raise ValueError('source_iterator has to be a CheckpointableIterator') if batch_size <= 0: raise ValueError('batch_size has to be positive') self._source_iterator = source_iterator # type: CheckpointableIterator self._batch_size = batch_size # type: int self.setstate(None) def getstate(self) -> Dict: return {'source_state': self._source_iterator.getstate()} # state for first item in next batch def setstate(self, checkpoint: Optional[Dict]): self._source_state = checkpoint['source_state'] if checkpoint else None self._source_iterator.setstate(self._source_state) self._iterator = self._generate() def _generate(self) -> Iterator: while True: batch = list(islice(self._source_iterator, self._batch_size)) if not batch: break yield batch def __next__(self): return next(self._iterator) def close(self): self._source_iterator.close() class RandomIterator(CheckpointableIterator): """ Iterator to generate uniformly distributed random numbers in the interval [0,1). Very similar to Random.random(), except that random numbers are obtained via next(). """ def __init__(self, seed: int=0): """ Args: seed: Random seed. """ self._seed = seed self._random = Random(self._seed) # type: Random def getstate(self) -> Dict: return {'random_state': self._random.getstate()} def setstate(self, checkpoint: Optional[Dict]): if checkpoint is None: self._random.seed(self._seed) else: self._random.setstate(checkpoint['random_state']) def __next__(self): return self._random.random() def close(self): pass class RecurrentIterator(CheckpointableIterator): """ Iterates statefully over a step function. The step function accepts a state and a new item, and returns a new state and an output item, which is yielded. """ def __init__(self, source_iterator: CheckpointableIterator, step_function: Callable[[Any,Any], Tuple[Any,Any]], initial_state: Any = None): """ Args: source_iterator: checkpointable iterator to recur over step_function: user-supplied function with signature step_function(state, item) -> (new_state, output) initial_state: initial state to be passed to the step_function upon first invocation """ if not isinstance(source_iterator, CheckpointableIterator): raise ValueError('source_iterator has to be a CheckpointableIterator') self._source_iterator = source_iterator # type: CheckpointableIterator self._step_function = step_function # type: Callable[[Any,Any], Tuple[Any,Any]] # take deepcopy of initial state so that user cannot change initial state after iterator is created self._initial_state = copy.deepcopy(initial_state) # type: Any self.setstate(None) def getstate(self): # return deepcopy of recurrent state so that user cannot change recurrent state within a checkpoint after it was taken # by modifying the recurrent_state in place during the step_function return {'recurrent_state': copy.deepcopy(self._recurrent_state), 'source_state': self._source_iterator.getstate()} def setstate(self, checkpoint): # take deepcopy of recurrent_state from checkpoint and initial state so that user cannot modify the checkpoint / the initial state # by modifying the recurrent_state in place during the step_function self._recurrent_state = copy.deepcopy(checkpoint['recurrent_state']) if checkpoint else copy.deepcopy(self._initial_state) self._source_iterator.setstate(checkpoint['source_state'] if checkpoint else None) def _generate(): for item in self._source_iterator: # with all the deepcopies above, in-place modification of recurrent_state within the step_function is now ok self._recurrent_state, output = self._step_function(self._recurrent_state, item) yield output self._iterator = _generate() def __next__(self): return next(self._iterator) def close(self): self._source_iterator.close() def SamplingRandomMapIterator(source_iterator: CheckpointableIterator, transform: Callable[[Random,Any],Any], seed: int=0): """ An iterator that calls a transform function on each item, while also passing a checkpointed random generator. Args: source_iterator: checkpointable iterator to recur over step_function: user-supplied function with signature step_function(random, item) -> result_item seed: random seed """ _random = Random(seed) def _step_function(state, item): _random.setstate(state) output = transform(_random, item) return _random.getstate(), output return RecurrentIterator(source_iterator, _step_function, initial_state=_random.getstate()) def BlockwiseShuffleIterator(source_iterator: CheckpointableIterator, block_size: int, seed: int=0): """ Shuffles a sequence of items by grouping consecutive items in blocks of fixed size, shuffling each block, and yielding the shuffled items of all blocks as a flat sequence. E.g. [1, 2, 3, 4, 5, 6, 7, 8] with block_size = 3 may yield [3, 1, 2, 4, 6, 5, 8, 7]. Args: source_iterator: checkpointable iterator or restartable iterable over input items to shuffle block_size: size of the buffer in number of items used for shuffling seed: random seed used for shuffling (or None) """ # This is implemented as a pipeline: # - group N consecutive items together # - shuffle them # - flatten the result blocks = FixedBatchIterator(source_iterator, batch_size=block_size) def shuffle_block_fn(random: Random, block: List): random.shuffle(block) return block shuffled_blocks = SamplingRandomMapIterator(blocks, transform=shuffle_block_fn, seed=seed) samples = SelectManyIterator(shuffled_blocks, collection_selector=lambda shuffled_block: iter(shuffled_block)) return samples def PrefetchIterator(source_iterator: CheckpointableIterator, buffer_size: int, buffer_in_main_process:bool=False, log_empty_buffer_warning: bool=False): """ An iterator prefetching data into a buffer on a seperate process. Args: source_iterator: checkpointable iterator to recur over buffer_size: number of items to prefetch; this is the maximum number of items held in the prefetch queue buffer_in_main_process: use experimental version of PrefetchBuffer that has buffer in main process instead of prefetch process log_empty_buffer_warning: log warning message if prefetch buffer is empty, only supported if buffer_in_main_process=True """ if not isinstance(source_iterator, CheckpointableIterator): raise ValueError('source_iterator has to be a CheckpointableIterator') if buffer_size <= 0: raise ValueError('buffer_size must be positive') if multiprocessing.get_start_method() != 'fork': print('WARNING: \ PrefetchIterator is only supported on operating system that use fork to create new processes.\ This excludes Windows.\ A dummy iterator is inserted instead of a PrefetchIterator.\ This also means that checkpoints of this iterator pipeline cannot be ported to a system that uses fork.') return source_iterator else: if buffer_in_main_process: return _ForkPrefetchIteratorExperimental(source_iterator, buffer_size, log_empty_buffer_warning) else: return _ForkPrefetchIterator(source_iterator, buffer_size) class _ForkPrefetchIterator(CheckpointableIterator): """ Actual internal implementation of the prefetch iterator for systems that support creating processes through fork. Args: source_iterator: checkpointable iterator to recur over buffer_size: number of items to prefetch; this is the maximum number of items held in the prefetch queue """ def __init__(self, source_iterator: CheckpointableIterator, buffer_size: int): self._source_iterator = source_iterator # type:CheckpointableIterator self._buffer_size = buffer_size # type: int self._prefetch_process = None # type: Optional[multiprocessing.Process] self.setstate(None) def getstate(self) -> Dict: return {'source_state': self._source_state, 'item_offset' : self._item_offset } def setstate(self, checkpoint: Optional[Dict]): self._terminate_and_join_prefetch_process() # kill current process if any self._source_state = checkpoint['source_state'] if checkpoint is not None else None self._item_offset = checkpoint['item_offset' ] if checkpoint is not None else 0 self._source_iterator.setstate(self._source_state) self._queue = multiprocessing.Queue(maxsize=self._buffer_size) _prefetch_process = multiprocessing.Process(target=self._prefetch_process_fn, args=(self._source_iterator, self._item_offset, # @TODO: why pass all these parameters? They are forked anyways. Seems a left-over from thread days. self._buffer_size, self._queue)) _prefetch_process.start() # this invokes fork() self._prefetch_process = _prefetch_process # make sure that in case of an unexpected shutdown, we still get rid of any active child process import atexit atexit.register(_ForkPrefetchIterator._join_process, self._prefetch_process) @staticmethod def _prefetch_process_fn(source, item_offset, buffer_size, queue): # behavior of the prefetching process, only to be called from that process! _advance_iterator(source, item_offset) # skip to checkpoint while True: try: item = next(source) except StopIteration: queue.put(StopIteration()) # It seems Python Queue has a bug: if we return here, then the StopIteration message is never sent to the receiver. # So we just dead-loop, assuming that the process will be killed anyways when the consuming side destructs the prefetcher. import time while True: time.sleep(1000) return # we never actually get here if item_offset == buffer_size - 1: # for efficiency, we send a new source state only at the END of each window of length _buffer_size source_state = source.getstate() # this is the state for retrieving the NEXT element, i.e. the first element of the next buffer item_offset = 0 else: source_state = None item_offset += 1 msg = (item, source_state) queue.put(msg) def __next__(self): if self._queue is None: # iterator has already been exhausted raise StopIteration() msg = self._queue.get() if isinstance(msg, StopIteration): self._queue = None raise StopIteration() item, prefetch_source_state = msg # for efficiency, the prefetch_source_state is only transmitted at the end of each window of length _buffer_size if prefetch_source_state is not None: assert self._item_offset == self._buffer_size - 1 # we expect a new source state at then END of each window of length _buffer_size self._source_state = prefetch_source_state self._item_offset = 0 else: self._item_offset = self._item_offset + 1 assert self._item_offset < self._buffer_size return item # for debugging, its useful to return msg instead of item def __del__(self): # note: this is often not called. If you really need it, gc.collect() will do the trick. self._terminate_and_join_prefetch_process() def _terminate_and_join_prefetch_process(self): # terminate the pre-fetch process if one is running if hasattr(self, "_prefetch_process") and self._prefetch_process: _ForkPrefetchIterator._join_process(self._prefetch_process) self._prefetch_process = None @staticmethod def _join_process(p): # called from setstate(), __del__(), and atexit handler # We create prefetching processes with UNIX fork. # That means that we might end up with inactive copies # of prefetchers in the memory of prefetching processes. # These inactive copies can never create their # own prefetching processes, even if setstate is called. # All prefetching processes are exclusively created by # the main process, even if there are nested PrefetchIterators. # Hence, the main process should be the only one to terminate # and join prefetching processes. # The if-statement below guarantees that, even if __del__ is called # on a copy of a PrefetchIterator in another process if p._parent_pid != os.getpid(): return if p.exitcode is not None: # already joined: p.pid is invalid return # Note that we must terminate here instead of cleanly shutting down # the prefetching process, e.g. using synchronization primitives. # This is deliberate (and unfortunate). # The prefetching process might spend an arbitrary amount of time # in the preceeding iterators before it checks for potential termination messages. # This would hold up the entire pipeline due to the join below. # Hence, we terminate the process immediately. # In some cases, the process function already ran its course. In that case, # the terminate() call will have no effect. p.terminate() p.join() def close(self): # this functionality is currently not implemented for this iterator self._source_iterator.close() class _ForkPrefetchIteratorExperimental(CheckpointableIterator): """ Actual internal implementation of the prefetch iterator for systems that support creating processes through fork. WARNING: PrefetchIterators have internal resources that need to be properly managed by calling close() manually. Failure to do so can lead to dangling processes and threads, or the PrefetchIterator hanging on finalization. Note that it is not correct to rely on the garbage collector to destroy the PrefetchIterator as CPython does not assure that the finalizer (__del__) of the PrefetchIterator will be called. The close() function is implemented for every CheckpointableIterator. It recursively traverses all preceeding iterators in the pipeline and closes all PrefetchIterators. Args: source_iterator: checkpointable iterator to recur over buffer_size: number of items to prefetch; this is the maximum number of items held in the prefetch queue log_empty_buffer_warning: log warning message if prefetch buffer is empty """ # HOW THIS ITERATOR WORKS, AND WHY: # # This iterator offloads the work of evaluating all preceeding iterators # into a separate prefetch process and tries to maintain a buffer # of buffer_size many items in order to hide any latency spikes in the evaluation # of the preceeding iterators. # # The prefetch process (self._prefetch_process) generates items and puts them # into an inter-process queue (self._inter_process_queue of type multiprocessing.Queue). # The sole purpose of this queue is to act as a means for inter-process communication. # Its purpose is NOT to act as the above-mentioned buffer. # Accordingly, the size of this queue is restricted to 1. # # The actual buffer is realized as a local, thread-safe queue (queue.Queue) that lives # in the main process (self._local_queue). We create a thread (self._queue_fetcher_thread) # within the main process that is responsible for fetching items from the # inter-process queue and storing them in the local queue. We then obtain an item from the # local queue whenever __next__ is called within the main thread of the main process. # # You might wonder why we jump through all of these hoops instead of just using # a multiprocessing.Queue with the desired buffer_size to act as both a means for # inter-process communication and as a buffer to smooth out latency at the same time. # In fact, this iterator used to be implemented that way. # However, we observed severe issues with that implementation. # # Specifically, with multiprocessing.Queue the buffer lives in the prefetch process # and a queue feeding thread is responsible for moving items from the buffer onto a pipe # (see https://docs.python.org/3.6/library/multiprocessing.html#multiprocessing.Queue) # The main thread in the prefetch process, which is responsible for generating items, # competes with the queue feeder thread for CPU cycles, and because of CPython's # global interpreter lock only one of these two processes can run at any given time. # # We observed situations in which the main thread is so busy generating new items # that the queue feeder thread does not get enough CPU time to keep the pipe filled. # This starvation of the queue feeder thread led to severe hangs (up to a minute) # in calls to multiprocessing.Queue.get in the main process, even though the buffer had # hundreds of items stored in it. # # This problem gets even worse when PyTorch tensors are sent over the queue. PyTorch registers # custom reducers to the ForkingPickler used to pickle tensors before sending them over a pipe # (see https://github.com/pytorch/pytorch/blob/master/torch/multiprocessing/reductions.py). # As a consequence, the actual tensor data is not sent via the queue, but shared via shared # memory. This process involves spawning yet another thread in the prefetch process and opening # sockets to transmit file descriptors # (see https://pytorch.org/docs/stable/multiprocessing.html#file-descriptor-file-descriptor). # So in this case, there is yet another thread competing for the global interpreter lock. # # By restricting the size of the inter-process queue to 1 we avoid or at least lessen # the starvation issues in the prefetch process caused by multiple threads fighting # for the global interpreter lock. Any remaining hangs or delays in the prefetch process # are hidden by having the buffer in the main process instead of the prefetch process. # # We suspect the hanging issues described above to be manifestations of the "Convoy effect": # https://bugs.python.org/issue7946 # http://www.dabeaz.com/python/GIL.pdf # https://in.pycon.org/2011/static/files/talks/41/Python-threads_v1.0.pdf def __init__( self, source_iterator: CheckpointableIterator, buffer_size: int, log_empty_buffer_warning: bool = False ): self._source_iterator = source_iterator # type: CheckpointableIterator self._buffer_size = buffer_size # type: int self._log_empty_buffer_warning = log_empty_buffer_warning self._is_closed = False self.setstate(None) def getstate(self) -> Dict: return {"source_state": self._source_state, "item_offset": self._item_offset} def setstate(self, checkpoint: Optional[Dict]): if self._is_closed: raise RuntimeError("PrefetchIterator has already been closed.") # terminate the prefetch process and queue fetcher thread if they are running self._shutdown() # set state according to checkpoint self._source_state = checkpoint["source_state"] if checkpoint is not None else None self._item_offset = checkpoint["item_offset"] if checkpoint is not None else 0 self._source_iterator.setstate(self._source_state) # In the given checkpoint, the source iterator might already be exhausted. # We will figure that out once we try to get the first item. # For now, we have to reset this variable. self._is_exhausted = False def __next__(self): if self._is_closed: raise RuntimeError("PrefetchIterator has already been closed.") if not hasattr(self, "_prefetch_process") or self._prefetch_process is None: # prefetcher process has not yet been started self._startup() if self._is_exhausted: raise StopIteration() if self._log_empty_buffer_warning: if self._local_queue.empty(): logger.warning("trying to fetch item, but prefetch buffer is empty") # This get-operation cannot deadlock: # Under the assumption that the prefetch process and the queue fetcher thread work correctly, # this operation can only deadlock if at the time of this call the source iterator is # exhausted, the local queue is empty, and there are no items in transit to the local queue. # In that case, a StopIteration was the last item ever put on the local queue. # That stop iteration would have caused self._is_exhausted to be set to True, # which means the following line would never have been reached, a contradiction. msg = self._local_queue.get() if isinstance(msg, StopIteration): self._is_exhausted = True # The source iterator is exhausted. # At this point, the queue fetcher thread should already have terminated. # The prefetch process will only terminate once we signal it via _prefetch_process_should_terminate. # This is because we have to make sure no more items are taken from the inter_process_queue # before we shut down the prefetch process as explained in _startup(). # We would like to terminate the prefetch process, but we cannot use _shutdown() here # because that would set self._prefetch_process = None, # which would mean we would call _startup() on the next call of __next__. # Instead, manually make sure the queue fetcher thread has actually terminated so that # no more elements are taken from the inter_process_queue # and then signal the prefetch process to terminate. self._queue_fetcher_thread.join() self._prefetch_process_should_terminate.set() raise StopIteration() # for efficiency, the prefetch_source_state is only transmitted at the end of each window of length _buffer_size item, prefetch_source_state = msg if prefetch_source_state is not None: # we expect a new source state at then END of each window of length _buffer_size assert self._item_offset == self._buffer_size - 1 self._source_state = prefetch_source_state self._item_offset = 0 else: self._item_offset = self._item_offset + 1 assert self._item_offset < self._buffer_size return item # for debugging, its useful to return msg instead of item def close(self): """ Close all PrefetchIterators in this pipeline PrefetchIterators have internal resources that need to be properly managed by calling close() manually. Failure to do so can lead to dangling processes and threads, or the PrefetchIterator hanging on finalization. Note that it is not correct to rely on the garbage collector to destroy PrefetchIterators as CPython does not assure that the finalizer (__del__) of a PrefetchIterator will be called. This function, which is implemented for every CheckpointableIterator, recursively traverses all preceeding iterators and closes all PrefetchIterators in the pipeline. For pipelines that do not contain PrefetchIterators this function has no effect. """ if not self._is_closed: self._is_closed = True self._shutdown() self._source_iterator.close() def _startup(self): # set up prefetch process and associated queue self._inter_process_queue = multiprocessing.Queue(maxsize=1) # Because of the way PyTorch transfers tensors through shared memory (see comment at top of this class) # we have to keep the prefetch process alive until we are sure that # no more items are taken from the inter-process queue. # This event is used to communicate to the prefetch process that it is safe to terminate. self._prefetch_process_should_terminate = multiprocessing.Event() _prefetch_process = multiprocessing.Process( target=self._prefetch_process_fn, args=( self._source_iterator, self._item_offset, self._buffer_size, self._inter_process_queue, self._prefetch_process_should_terminate, ), ) _prefetch_process.start() # this invokes fork() # set self._prefetch_process after fork so that variable never exists for within prefetch process self._prefetch_process = _prefetch_process # set up queue fetcher thread self._local_queue = queue.Queue(maxsize=self._buffer_size) self._queue_fetcher_thread_should_terminate = threading.Event() self._queue_fetcher_thread = threading.Thread(target=self._queue_fetcher_thread_fn, daemon=True) self._queue_fetcher_thread.start() def _shutdown(self): # Only shut down if this is the parent process and the prefetcher is running. # The variable self._prefetch_process can only exist in the parent process. # The variable exists and is not None only if the prefetcher is running. if hasattr(self, "_prefetch_process") and self._prefetch_process is not None: # if self._prefetch_process is not None, neither should self._queue_fetcher_thread assert self._queue_fetcher_thread is not None # sanity check that this is actually the parent of the prefetch process assert self._prefetch_process._parent_pid == os.getpid() # shut down queue fetcher thread self._queue_fetcher_thread_should_terminate.set() self._queue_fetcher_thread.join() self._queue_fetcher_thread = None # shut down prefetch process self._prefetch_process_should_terminate.set() self._prefetch_process.join() self._prefetch_process = None @staticmethod def _prefetch_process_fn( source_iterator, item_offset, buffer_size, inter_process_queue, should_terminate_event ): # behavior of the prefetching process, only to be called from that process! _advance_iterator(source_iterator, item_offset) # skip to checkpoint while True: try: item = next(source_iterator) except StopIteration: _ForkPrefetchIteratorExperimental._try_put(inter_process_queue, StopIteration(), should_terminate_event) # Because of the way PyTorch transfers tensors through shared memory (see comment at top of this class) # we have to keep the prefetch process alive until we are sure that # no more items are taken from the inter-process queue. # This event is used to communicate to the prefetch process that it is safe to terminate. should_terminate_event.wait() break if item_offset == buffer_size - 1: # for efficiency, we send a new source state only at the END of each window of length _buffer_size # this is the state for retrieving the NEXT element, i.e. the first element of the next buffer source_state = source_iterator.getstate() item_offset = 0 else: source_state = None item_offset += 1 msg = (item, source_state) should_terminate = _ForkPrefetchIteratorExperimental._try_put( inter_process_queue, msg, should_terminate_event ) if should_terminate: break source_iterator.close() def _queue_fetcher_thread_fn(self): while True: # This get-operation cannot deadlock: # For his operation to deadlock, the queue must be empty and the prefetch process must never put # another item on the queue. Under the assumption that the prefetch process works correctly, # this can only happen in two ways. First, the prefetch process could exhaust its source iterator # and put a StopIteration on the queue. In this case, this thread will receive the StopIteration # and terminate, which means the operation does not deadlock. Second, the prefetch process could # terminate because the corresponding signal is set as part of a _shutdown(). However, we terminate # and join this thread before we set the terminate signal for the prefetch process, # so this case cannot happen. msg = self._inter_process_queue.get() should_terminate = _ForkPrefetchIteratorExperimental._try_put( self._local_queue, msg, self._queue_fetcher_thread_should_terminate ) if should_terminate: return if isinstance(msg, StopIteration): return @staticmethod def _try_put(q, msg, should_terminate_event, timeout=0.001): """ Repeatedly try to put message on queue until success or should_terminate_event is set. If success, return False. If should_terminate_event is set, return True. """ while not should_terminate_event.is_set(): try: q.put(msg, timeout=timeout) return False except queue.Full: pass return True def __del__(self): if hasattr(self, "_prefetch_process") and not self._is_closed: logger.warning( f"unclosed PrefetchIterator {self!r}: not closing a PrefetchIterator may lead to dangling processes and hangs on finalization" ) self._shutdown() class BucketedReadaheadBatchIterator(CheckpointableIterator): """ Iterates over items from a checkpointable iterator and groups items of similar length into batches. The algorithm reads a head a certain number of lines (e.g. 10 million), sorts them by length, and them groups them into batches from start to end. The sort is stable, such that prior randomization is not undone (except for the length grouping). The batch size is dynamic, and determined by a user-provided callback. This is based on Marian NMT's BatchGenerator. """ def __init__(self, source_iterator: CheckpointableIterator, read_ahead: int, key: Callable[[Any], Any], batch_size: Union[int,Callable[[Any], int]], boundary_key: Callable[[Any], Any]=None, shuffle: bool=True, seed: int=0): """ Args: source_iterator: The data set that is read from. Typically this is an infinite source. read_ahead: Number of items to fetch ahead for grouping purposes. key: User-provided callback to define how data is sorted for purpose of batching. batch_size: Batch size in number of items. Either an integer or a callback to determine batch size for a given first batch item. boundary_key: This optional callback, which maps an item to a key, allows to impose an additional restriction on the way batches are formed. Specifically, the iterator starts a new batch whenever the key changes. Thereby, it guarantees that all items in a batch have the same key. Keys are not allowed to be None. shuffle: Pass False to not randomize the batches. (default: True) seed: Random seed for batch shuffling. """ if not isinstance(source_iterator, CheckpointableIterator): raise ValueError('source_iterator has to be a CheckpointableIterator') # keep arguments self._key = key # type: Callable[[Any], Any] self._batch_size = batch_size # type: Union[int,Callable[[Any], int]] self._boundary_key = boundary_key # type: Callable[[Any], Any] self._read_ahead = read_ahead # type: int # initialize state self._seed = seed self._random = None # type: Optional[Random] if shuffle: self._random = Random(self._seed) self._source_iterator = cast(CheckpointableIterator, iter(source_iterator)) # type: CheckpointableIterator self.setstate(None) def getstate(self): return {'source_state': self._source_state, 'random_state': self._random_state, 'num_served': self._num_batches_yielded} def setstate(self, checkpoint: Optional[Dict]): self._source_state = checkpoint['source_state'] if checkpoint else None # type: Dict # state of input before reading the current set of batches self._random_state = checkpoint['random_state'] if checkpoint else None # type: Any # state of random generator at _source_state self._num_batches_yielded = checkpoint['num_served'] if checkpoint else 0 # type: int # number of batches served from the current set of batches # checkpointing: restore to start of current set of batches self._source_iterator.setstate(self._source_state) if self._random: if self._random_state: self._random.setstate(self._random_state) else: self._random.seed(self._seed) self._source_exhausted = False # type: bool # set to True once we hit StopIteration on source def _generate(): skip_to_checkpoint = self._num_batches_yielded source_exhausted = False while not source_exhausted: # prefetch the readahead buffer self._source_state = self._source_iterator.getstate() self._random_state = self._random.getstate() if self._random else None items = list(islice(self._source_iterator, self._read_ahead)) source_exhausted = (len(items) < self._read_ahead) # create batches batches = self._create_batches(items) # shuffle the batches if self._random: self._random.shuffle(batches) # on first loop iteration, restore iterator inside batches from checkpoint batches = iter(batches) self._num_batches_yielded = _advance_iterator(batches, skip_to_checkpoint) skip_to_checkpoint = 0 # main loop over batches in current read-ahead section for batch in batches: self._num_batches_yielded += 1 yield batch self._iterator = _generate() # type: Iterator # iterator into current set of batches def _create_batches(self, items: List[Any]) -> List[List[Any]]: # helper to form batches from a list of items # sort by length, longest first if self._key: items.sort(key=self._key, reverse=True) # note: sort() is stable, so we won't undo any randomization besides the bucketing # group into batches cur_batch = None # type: Optional[List[Any]] prev_val = None batches = [] # type: List[Any] for item in items: if self._boundary_key and self._boundary_key(item) != prev_val: if cur_batch: batches.append(cur_batch) cur_batch = None prev_val = None if not cur_batch: batch_size = self._batch_size if isinstance(self._batch_size, int) else \ self._batch_size(item) cur_batch = [] cur_batch.append(item) if self._boundary_key: prev_val = self._boundary_key(item) assert prev_val is not None if len(cur_batch) >= batch_size: # this batch is full batches.append(cur_batch) cur_batch = None prev_val = None if cur_batch: batches.append(cur_batch) return batches def __next__(self): return next(self._iterator) def close(self): self._source_iterator.close()
KosmosX-API-main
kosmosX/infinibatch/infinibatch/iterators.py
#!/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 os import subprocess import sys from setuptools import Extension, find_packages, setup if sys.version_info < (3, 6): sys.exit("Sorry, Python >= 3.6 is required for fairseq.") def write_version_py(): with open(os.path.join("fairseq", "version.txt")) as f: version = f.read().strip() # append latest commit hash to version string try: sha = ( subprocess.check_output(["git", "rev-parse", "HEAD"]) .decode("ascii") .strip() ) version += "+" + sha[:7] except Exception: pass # write version info to fairseq/version.py with open(os.path.join("fairseq", "version.py"), "w") as f: f.write('__version__ = "{}"\n'.format(version)) return version version = write_version_py() with open("README.md") as f: readme = f.read() if sys.platform == "darwin": extra_compile_args = ["-stdlib=libc++", "-O3"] else: extra_compile_args = ["-std=c++11", "-O3"] class NumpyExtension(Extension): """Source: https://stackoverflow.com/a/54128391""" def __init__(self, *args, **kwargs): self.__include_dirs = [] super().__init__(*args, **kwargs) @property def include_dirs(self): import numpy return self.__include_dirs + [numpy.get_include()] @include_dirs.setter def include_dirs(self, dirs): self.__include_dirs = dirs extensions = [ Extension( "fairseq.libbleu", sources=[ "fairseq/clib/libbleu/libbleu.cpp", "fairseq/clib/libbleu/module.cpp", ], extra_compile_args=extra_compile_args, ), NumpyExtension( "fairseq.data.data_utils_fast", sources=["fairseq/data/data_utils_fast.pyx"], language="c++", extra_compile_args=extra_compile_args, ), NumpyExtension( "fairseq.data.token_block_utils_fast", sources=["fairseq/data/token_block_utils_fast.pyx"], language="c++", extra_compile_args=extra_compile_args, ), ] cmdclass = {} try: # torch is not available when generating docs from torch.utils import cpp_extension extensions.extend( [ cpp_extension.CppExtension( "fairseq.libbase", sources=[ "fairseq/clib/libbase/balanced_assignment.cpp", ], ) ] ) extensions.extend( [ cpp_extension.CppExtension( "fairseq.libnat", sources=[ "fairseq/clib/libnat/edit_dist.cpp", ], ), cpp_extension.CppExtension( "alignment_train_cpu_binding", sources=[ "examples/operators/alignment_train_cpu.cpp", ], ), ] ) if "CUDA_HOME" in os.environ: extensions.extend( [ cpp_extension.CppExtension( "fairseq.libnat_cuda", sources=[ "fairseq/clib/libnat_cuda/edit_dist.cu", "fairseq/clib/libnat_cuda/binding.cpp", ], ), cpp_extension.CppExtension( "fairseq.ngram_repeat_block_cuda", sources=[ "fairseq/clib/cuda/ngram_repeat_block_cuda.cpp", "fairseq/clib/cuda/ngram_repeat_block_cuda_kernel.cu", ], ), cpp_extension.CppExtension( "alignment_train_cuda_binding", sources=[ "examples/operators/alignment_train_kernel.cu", "examples/operators/alignment_train_cuda.cpp", ], ), ] ) cmdclass["build_ext"] = cpp_extension.BuildExtension except ImportError: pass if "READTHEDOCS" in os.environ: # don't build extensions when generating docs extensions = [] if "build_ext" in cmdclass: del cmdclass["build_ext"] # use CPU build of PyTorch dependency_links = [ "https://download.pytorch.org/whl/cpu/torch-1.7.0%2Bcpu-cp36-cp36m-linux_x86_64.whl" ] else: dependency_links = [] if "clean" in sys.argv[1:]: # Source: https://bit.ly/2NLVsgE print("deleting Cython files...") import subprocess subprocess.run( ["rm -f fairseq/*.so fairseq/**/*.so fairseq/*.pyd fairseq/**/*.pyd"], shell=True, ) extra_packages = [] if os.path.exists(os.path.join("fairseq", "model_parallel", "megatron", "mpu")): extra_packages.append("fairseq.model_parallel.megatron.mpu") def do_setup(package_data): setup( name="fairseq", version=version, description="Facebook AI Research Sequence-to-Sequence Toolkit", url="https://github.com/pytorch/fairseq", classifiers=[ "Intended Audience :: Science/Research", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Topic :: Scientific/Engineering :: Artificial Intelligence", ], long_description=readme, long_description_content_type="text/markdown", setup_requires=[ "cython", 'numpy<1.20.0; python_version<"3.7"', 'numpy; python_version>="3.7"', "setuptools>=18.0", ], install_requires=[ "cffi", "cython", 'dataclasses; python_version<"3.7"', "hydra-core>=1.0.7,<1.1", "omegaconf<2.1", 'numpy<1.20.0; python_version<"3.7"', 'numpy; python_version>="3.7"', "regex", "sacrebleu>=1.4.12", "torch", "tqdm", "bitarray", # "torchaudio>=0.8.0", ], dependency_links=dependency_links, packages=find_packages( exclude=[ "examples", "examples.*", "scripts", "scripts.*", "tests", "tests.*", ] ) + extra_packages, package_data=package_data, ext_modules=extensions, test_suite="tests", entry_points={ "console_scripts": [ "fairseq-eval-lm = fairseq_cli.eval_lm:cli_main", "fairseq-generate = fairseq_cli.generate:cli_main", "fairseq-hydra-train = fairseq_cli.hydra_train:cli_main", "fairseq-interactive = fairseq_cli.interactive:cli_main", "fairseq-preprocess = fairseq_cli.preprocess:cli_main", "fairseq-score = fairseq_cli.score:cli_main", "fairseq-train = fairseq_cli.train:cli_main", "fairseq-validate = fairseq_cli.validate:cli_main", ], }, cmdclass=cmdclass, zip_safe=False, ) def get_files(path, relative_to="fairseq"): all_files = [] for root, _dirs, files in os.walk(path, followlinks=True): root = os.path.relpath(root, relative_to) for file in files: if file.endswith(".pyc"): continue all_files.append(os.path.join(root, file)) return all_files if __name__ == "__main__": try: # symlink examples into fairseq package so package_data accepts them fairseq_examples = os.path.join("fairseq", "examples") if "build_ext" not in sys.argv[1:] and not os.path.exists(fairseq_examples): os.symlink(os.path.join("..", "examples"), fairseq_examples) package_data = { "fairseq": ( get_files(fairseq_examples) + get_files(os.path.join("fairseq", "config")) ) } do_setup(package_data) finally: if "build_ext" not in sys.argv[1:] and os.path.islink(fairseq_examples): os.unlink(fairseq_examples)
KosmosX-API-main
kosmosX/fairseq/setup.py
#!/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. """ Legacy entry point. Use fairseq_cli/train.py or fairseq-train instead. """ from fairseq_cli.train import cli_main if __name__ == "__main__": cli_main()
KosmosX-API-main
kosmosX/fairseq/train.py
# 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. """isort:skip_file""" import functools import importlib dependencies = [ "dataclasses", "hydra", "numpy", "omegaconf", "regex", "requests", "torch", ] # Check for required dependencies and raise a RuntimeError if any are missing. missing_deps = [] for dep in dependencies: try: importlib.import_module(dep) except ImportError: # Hack: the hydra package is provided under the "hydra-core" name in # pypi. We don't want the user mistakenly calling `pip install hydra` # since that will install an unrelated package. if dep == "hydra": dep = "hydra-core" missing_deps.append(dep) if len(missing_deps) > 0: raise RuntimeError("Missing dependencies: {}".format(", ".join(missing_deps))) # only do fairseq imports after checking for dependencies from fairseq.hub_utils import ( # noqa; noqa BPEHubInterface as bpe, TokenizerHubInterface as tokenizer, ) from fairseq.models import MODEL_REGISTRY # noqa # torch.hub doesn't build Cython components, so if they are not found then try # to build them here try: import fairseq.data.token_block_utils_fast # noqa except ImportError: try: import cython # noqa import os from setuptools import sandbox sandbox.run_setup( os.path.join(os.path.dirname(__file__), "setup.py"), ["build_ext", "--inplace"], ) except ImportError: print( "Unable to build Cython components. Please make sure Cython is " "installed if the torch.hub model you are loading depends on it." ) # automatically expose models defined in FairseqModel::hub_models for _model_type, _cls in MODEL_REGISTRY.items(): for model_name in _cls.hub_models().keys(): globals()[model_name] = functools.partial( _cls.from_pretrained, model_name, )
KosmosX-API-main
kosmosX/fairseq/hubconf.py
# 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 from pathlib import Path from typing import Callable, List, Optional, Union import torch from fairseq import utils from fairseq.data.indexed_dataset import get_available_dataset_impl from fairseq.dataclass.configs import ( CheckpointConfig, CommonConfig, CommonEvalConfig, DatasetConfig, DistributedTrainingConfig, EvalLMConfig, GenerationConfig, InteractiveConfig, OptimizationConfig, EMAConfig, ) from fairseq.dataclass.utils import gen_parser_from_dataclass # this import is for backward compatibility from fairseq.utils import csv_str_list, eval_bool, eval_str_dict, eval_str_list # noqa def get_preprocessing_parser(default_task="translation"): parser = get_parser("Preprocessing", default_task) add_preprocess_args(parser) return parser def get_training_parser(default_task="translation"): parser = get_parser("Trainer", default_task) add_dataset_args(parser, train=True) add_distributed_training_args(parser) add_model_args(parser) add_optimization_args(parser) add_checkpoint_args(parser) add_ema_args(parser) add_deepspeed_args(parser) return parser def get_generation_parser(interactive=False, default_task="translation"): parser = get_parser("Generation", default_task) add_dataset_args(parser, gen=True) add_distributed_training_args(parser, default_world_size=1) add_generation_args(parser) add_checkpoint_args(parser) if interactive: add_interactive_args(parser) return parser def get_speech_generation_parser(default_task="text_to_speech"): parser = get_parser("Speech Generation", default_task) add_dataset_args(parser, gen=True) add_distributed_training_args(parser, default_world_size=1) add_speech_generation_args(parser) return parser def get_interactive_generation_parser(default_task="translation"): return get_generation_parser(interactive=True, default_task=default_task) def get_eval_lm_parser(default_task="language_modeling"): parser = get_parser("Evaluate Language Model", default_task) add_dataset_args(parser, gen=True) add_distributed_training_args(parser, default_world_size=1) add_eval_lm_args(parser) return parser def get_validation_parser(default_task=None): parser = get_parser("Validation", default_task) add_dataset_args(parser, train=True) add_distributed_training_args(parser, default_world_size=1) group = parser.add_argument_group("Evaluation") gen_parser_from_dataclass(group, CommonEvalConfig()) return parser def parse_args_and_arch( parser: argparse.ArgumentParser, input_args: List[str] = None, parse_known: bool = False, suppress_defaults: bool = False, modify_parser: Optional[Callable[[argparse.ArgumentParser], None]] = None, ): """ Args: parser (ArgumentParser): the parser input_args (List[str]): strings to parse, defaults to sys.argv parse_known (bool): only parse known arguments, similar to `ArgumentParser.parse_known_args` suppress_defaults (bool): parse while ignoring all default values modify_parser (Optional[Callable[[ArgumentParser], None]]): function to modify the parser, e.g., to set default values """ if suppress_defaults: # Parse args without any default values. This requires us to parse # twice, once to identify all the necessary task/model args, and a second # time with all defaults set to None. args = parse_args_and_arch( parser, input_args=input_args, parse_known=parse_known, suppress_defaults=False, ) suppressed_parser = argparse.ArgumentParser(add_help=False, parents=[parser]) suppressed_parser.set_defaults(**{k: None for k, v in vars(args).items()}) args = suppressed_parser.parse_args(input_args) return argparse.Namespace( **{k: v for k, v in vars(args).items() if v is not None} ) from fairseq.models import ARCH_MODEL_REGISTRY, ARCH_CONFIG_REGISTRY, MODEL_REGISTRY # Before creating the true parser, we need to import optional user module # in order to eagerly import custom tasks, optimizers, architectures, etc. usr_parser = argparse.ArgumentParser(add_help=False, allow_abbrev=False) usr_parser.add_argument("--user-dir", default=None) usr_args, _ = usr_parser.parse_known_args(input_args) utils.import_user_module(usr_args) if modify_parser is not None: modify_parser(parser) # The parser doesn't know about model/criterion/optimizer-specific args, so # we parse twice. First we parse the model/criterion/optimizer, then we # parse a second time after adding the *-specific arguments. # If input_args is given, we will parse those args instead of sys.argv. args, _ = parser.parse_known_args(input_args) # Add model-specific args to parser. if hasattr(args, "arch"): model_specific_group = parser.add_argument_group( "Model-specific configuration", # Only include attributes which are explicitly given as command-line # arguments or which have default values. argument_default=argparse.SUPPRESS, ) if args.arch in ARCH_MODEL_REGISTRY: ARCH_MODEL_REGISTRY[args.arch].add_args(model_specific_group) elif args.arch in MODEL_REGISTRY: MODEL_REGISTRY[args.arch].add_args(model_specific_group) else: raise RuntimeError() if hasattr(args, "task"): from fairseq.tasks import TASK_REGISTRY TASK_REGISTRY[args.task].add_args(parser) if getattr(args, "use_bmuf", False): # hack to support extra args for block distributed data parallelism from fairseq.optim.bmuf import FairseqBMUF FairseqBMUF.add_args(parser) # Add *-specific args to parser. from fairseq.registry import REGISTRIES for registry_name, REGISTRY in REGISTRIES.items(): choice = getattr(args, registry_name, None) if choice is not None: cls = REGISTRY["registry"][choice] if hasattr(cls, "add_args"): cls.add_args(parser) elif hasattr(cls, "__dataclass"): gen_parser_from_dataclass(parser, cls.__dataclass()) # Modify the parser a second time, since defaults may have been reset if modify_parser is not None: modify_parser(parser) # Parse a second time. if parse_known: args, extra = parser.parse_known_args(input_args) else: args = parser.parse_args(input_args) extra = None # Post-process args. if ( hasattr(args, "batch_size_valid") and args.batch_size_valid is None ) or not hasattr(args, "batch_size_valid"): args.batch_size_valid = args.batch_size if hasattr(args, "max_tokens_valid") and args.max_tokens_valid is None: args.max_tokens_valid = args.max_tokens if getattr(args, "memory_efficient_fp16", False): args.fp16 = True if getattr(args, "memory_efficient_bf16", False): args.bf16 = True args.tpu = getattr(args, "tpu", False) args.bf16 = getattr(args, "bf16", False) if args.bf16: args.tpu = True if args.tpu and args.fp16: raise ValueError("Cannot combine --fp16 and --tpu, use --bf16 on TPUs") if getattr(args, "seed", None) is None: args.seed = 1 # default seed for training args.no_seed_provided = True else: args.no_seed_provided = False if getattr(args, "update_epoch_batch_itr", None) is None: if hasattr(args, "grouped_shuffling"): args.update_epoch_batch_itr = args.grouped_shuffling else: args.grouped_shuffling = False args.update_epoch_batch_itr = False # Apply architecture configuration. if hasattr(args, "arch") and args.arch in ARCH_CONFIG_REGISTRY: ARCH_CONFIG_REGISTRY[args.arch](args) if parse_known: return args, extra else: return args def get_parser(desc, default_task="translation"): # Before creating the true parser, we need to import optional user module # in order to eagerly import custom tasks, optimizers, architectures, etc. usr_parser = argparse.ArgumentParser(add_help=False, allow_abbrev=False) usr_parser.add_argument("--user-dir", default=None) usr_args, _ = usr_parser.parse_known_args() utils.import_user_module(usr_args) parser = argparse.ArgumentParser(allow_abbrev=False) gen_parser_from_dataclass(parser, CommonConfig()) from fairseq.registry import REGISTRIES for registry_name, REGISTRY in REGISTRIES.items(): parser.add_argument( "--" + registry_name.replace("_", "-"), default=REGISTRY["default"], choices=REGISTRY["registry"].keys(), ) # Task definitions can be found under fairseq/tasks/ from fairseq.tasks import TASK_REGISTRY parser.add_argument( "--task", metavar="TASK", default=default_task, choices=TASK_REGISTRY.keys(), help="task", ) # fmt: on return parser def add_preprocess_args(parser): group = parser.add_argument_group("Preprocessing") # fmt: off group.add_argument("-s", "--source-lang", default=None, metavar="SRC", help="source language") group.add_argument("-t", "--target-lang", default=None, metavar="TARGET", help="target language") group.add_argument("--trainpref", metavar="FP", default=None, help="train file prefix (also used to build dictionaries)") group.add_argument("--validpref", metavar="FP", default=None, help="comma separated, valid file prefixes " "(words missing from train set are replaced with <unk>)") group.add_argument("--testpref", metavar="FP", default=None, help="comma separated, test file prefixes " "(words missing from train set are replaced with <unk>)") group.add_argument("--align-suffix", metavar="FP", default=None, help="alignment file suffix") group.add_argument("--destdir", metavar="DIR", default="data-bin", help="destination dir") group.add_argument("--thresholdtgt", metavar="N", default=0, type=int, help="map words appearing less than threshold times to unknown") group.add_argument("--thresholdsrc", metavar="N", default=0, type=int, help="map words appearing less than threshold times to unknown") group.add_argument("--tgtdict", metavar="FP", help="reuse given target dictionary") group.add_argument("--srcdict", metavar="FP", help="reuse given source dictionary") group.add_argument("--nwordstgt", metavar="N", default=-1, type=int, help="number of target words to retain") group.add_argument("--nwordssrc", metavar="N", default=-1, type=int, help="number of source words to retain") group.add_argument("--alignfile", metavar="ALIGN", default=None, help="an alignment file (optional)") parser.add_argument('--dataset-impl', metavar='FORMAT', default='mmap', choices=get_available_dataset_impl(), help='output dataset implementation') group.add_argument("--joined-dictionary", action="store_true", help="Generate joined dictionary") group.add_argument("--only-source", action="store_true", help="Only process the source language") group.add_argument("--padding-factor", metavar="N", default=8, type=int, help="Pad dictionary size to be multiple of N") group.add_argument("--workers", metavar="N", default=1, type=int, help="number of parallel workers") group.add_argument("--dict-only", action='store_true', help="if true, only builds a dictionary and then exits") # fmt: on return parser def add_dataset_args(parser, train=False, gen=False): group = parser.add_argument_group("dataset_data_loading") gen_parser_from_dataclass(group, DatasetConfig()) # fmt: on return group def add_distributed_training_args(parser, default_world_size=None): group = parser.add_argument_group("distributed_training") if default_world_size is None: default_world_size = max(1, torch.cuda.device_count()) gen_parser_from_dataclass( group, DistributedTrainingConfig(distributed_world_size=default_world_size) ) return group def add_optimization_args(parser): group = parser.add_argument_group("optimization") # fmt: off gen_parser_from_dataclass(group, OptimizationConfig()) # fmt: on return group def add_checkpoint_args(parser): group = parser.add_argument_group("checkpoint") # fmt: off gen_parser_from_dataclass(group, CheckpointConfig()) # fmt: on return group def add_common_eval_args(group): gen_parser_from_dataclass(group, CommonEvalConfig()) def add_eval_lm_args(parser): group = parser.add_argument_group("LM Evaluation") add_common_eval_args(group) gen_parser_from_dataclass(group, EvalLMConfig()) def add_generation_args(parser): group = parser.add_argument_group("Generation") add_common_eval_args(group) gen_parser_from_dataclass(group, GenerationConfig()) return group def add_speech_generation_args(parser): group = parser.add_argument_group("Speech Generation") add_common_eval_args(group) # NOTE: remove_bpe is not needed # fmt: off group.add_argument('--eos_prob_threshold', default=0.5, type=float, help='terminate when eos probability exceeds this') # fmt: on return group def add_interactive_args(parser): group = parser.add_argument_group("Interactive") gen_parser_from_dataclass(group, InteractiveConfig()) def add_model_args(parser): group = parser.add_argument_group("Model configuration") # fmt: off # Model definitions can be found under fairseq/models/ # # The model architecture can be specified in several ways. # In increasing order of priority: # 1) model defaults (lowest priority) # 2) --arch argument # 3) --encoder/decoder-* arguments (highest priority) from fairseq.models import ARCH_MODEL_REGISTRY group.add_argument('--arch', '-a', metavar='ARCH', choices=ARCH_MODEL_REGISTRY.keys(), help='model architecture') # fmt: on return group def get_args( data: Union[str, Path], task: str = "translation", arch: str = "transformer", **overrides ): parser = get_training_parser(task) args = parse_args_and_arch(parser, [str(data), "--task", task, "--arch", arch]) for k, v in overrides.items(): setattr(args, k, v) return args def add_ema_args(parser): group = parser.add_argument_group("EMA configuration") gen_parser_from_dataclass(group, EMAConfig()) def add_deepspeed_args(parser): pass # group = parser.add_argument_group("DeepSpeed") # group.add_argument('--deepspeed', nargs='?', const=True, default=False, # help="Enable DeepSpeed with auto-generated config with flag and " \ # "no argument, or pass an argument to a ds_config json to use.") # group.add_argument("--zero", default=0, type=int, help="enable a specific ZeRO stage") # group.add_argument('--exit-interval', type=int, default=None, # help='Exit the program after the iteration is divisible ' # 'by this value.')
KosmosX-API-main
kosmosX/fairseq/fairseq/options.py
# 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 import namedtuple import numpy as np import torch from fairseq import utils DecoderOut = namedtuple( "IterativeRefinementDecoderOut", ["output_tokens", "output_scores", "attn", "step", "max_step", "history"], ) class IterativeRefinementGenerator(object): def __init__( self, tgt_dict, models=None, eos_penalty=0.0, max_iter=10, max_ratio=2, beam_size=1, decoding_format=None, retain_dropout=False, adaptive=True, retain_history=False, reranking=False, ): """ Generates translations based on iterative refinement. Args: tgt_dict: target dictionary eos_penalty: if > 0.0, it penalized early-stopping in decoding max_iter: maximum number of refinement iterations max_ratio: generate sequences of maximum length ax, where x is the source length decoding_format: decoding mode in {'unigram', 'ensemble', 'vote', 'dp', 'bs'} retain_dropout: retaining dropout in the inference adaptive: decoding with early stop """ self.bos = tgt_dict.bos() self.pad = tgt_dict.pad() self.unk = tgt_dict.unk() self.eos = tgt_dict.eos() self.vocab_size = len(tgt_dict) self.eos_penalty = eos_penalty self.max_iter = max_iter self.max_ratio = max_ratio self.beam_size = beam_size self.reranking = reranking self.decoding_format = decoding_format self.retain_dropout = retain_dropout self.retain_history = retain_history self.adaptive = adaptive self.models = models def generate_batched_itr( self, data_itr, maxlen_a=None, maxlen_b=None, cuda=False, timer=None, prefix_size=0, ): """Iterate over a batched dataset and yield individual translations. Args: maxlen_a/b: generate sequences of maximum length ax + b, where x is the source sentence length. cuda: use GPU for generation timer: StopwatchMeter for timing generations. """ for sample in data_itr: if "net_input" not in sample: continue if timer is not None: timer.start() with torch.no_grad(): hypos = self.generate( self.models, sample, prefix_tokens=sample["target"][:, :prefix_size] if prefix_size > 0 else None, ) if timer is not None: timer.stop(sample["ntokens"]) for i, id in enumerate(sample["id"]): # remove padding src = utils.strip_pad(sample["net_input"]["src_tokens"][i, :], self.pad) ref = utils.strip_pad(sample["target"][i, :], self.pad) yield id, src, ref, hypos[i] @torch.no_grad() def generate(self, models, sample, prefix_tokens=None, constraints=None): if constraints is not None: raise NotImplementedError( "Constrained decoding with the IterativeRefinementGenerator is not supported" ) # TODO: iterative refinement generator does not support ensemble for now. if not self.retain_dropout: for model in models: model.eval() model, reranker = models[0], None if self.reranking: assert len(models) > 1, "Assuming the last checkpoint is the reranker" assert ( self.beam_size > 1 ), "Reranking requires multiple translation for each example" reranker = models[-1] models = models[:-1] if len(models) > 1 and hasattr(model, "enable_ensemble"): assert model.allow_ensemble, "{} does not support ensembling".format( model.__class__.__name__ ) model.enable_ensemble(models) # TODO: better encoder inputs? src_tokens = sample["net_input"]["src_tokens"] src_lengths = sample["net_input"]["src_lengths"] bsz, src_len = src_tokens.size() # initialize encoder_out = model.forward_encoder([src_tokens, src_lengths]) prev_decoder_out = model.initialize_output_tokens(encoder_out, src_tokens) if self.beam_size > 1: assert ( model.allow_length_beam ), "{} does not support decoding with length beam.".format( model.__class__.__name__ ) # regenerate data based on length-beam length_beam_order = ( utils.new_arange(src_tokens, self.beam_size, bsz).t().reshape(-1) ) encoder_out = model.encoder.reorder_encoder_out( encoder_out, length_beam_order ) prev_decoder_out = model.regenerate_length_beam( prev_decoder_out, self.beam_size ) bsz = bsz * self.beam_size sent_idxs = torch.arange(bsz) prev_output_tokens = prev_decoder_out.output_tokens.clone() if self.retain_history: prev_decoder_out = prev_decoder_out._replace(history=[prev_output_tokens]) finalized = [[] for _ in range(bsz)] def is_a_loop(x, y, s, a): b, l_x, l_y = x.size(0), x.size(1), y.size(1) if l_x > l_y: y = torch.cat([y, x.new_zeros(b, l_x - l_y).fill_(self.pad)], 1) s = torch.cat([s, s.new_zeros(b, l_x - l_y)], 1) if a is not None: a = torch.cat([a, a.new_zeros(b, l_x - l_y, a.size(2))], 1) elif l_x < l_y: x = torch.cat([x, y.new_zeros(b, l_y - l_x).fill_(self.pad)], 1) return (x == y).all(1), y, s, a def finalized_hypos(step, prev_out_token, prev_out_score, prev_out_attn): cutoff = prev_out_token.ne(self.pad) tokens = prev_out_token[cutoff] if prev_out_score is None: scores, score = None, None else: scores = prev_out_score[cutoff] score = scores.mean() if prev_out_attn is None: hypo_attn, alignment = None, None else: hypo_attn = prev_out_attn[cutoff] alignment = hypo_attn.max(dim=1)[1] return { "steps": step, "tokens": tokens, "positional_scores": scores, "score": score, "hypo_attn": hypo_attn, "alignment": alignment, } for step in range(self.max_iter + 1): decoder_options = { "eos_penalty": self.eos_penalty, "max_ratio": self.max_ratio, "decoding_format": self.decoding_format, } prev_decoder_out = prev_decoder_out._replace( step=step, max_step=self.max_iter + 1, ) decoder_out = model.forward_decoder( prev_decoder_out, encoder_out, **decoder_options ) if self.adaptive: # terminate if there is a loop terminated, out_tokens, out_scores, out_attn = is_a_loop( prev_output_tokens, decoder_out.output_tokens, decoder_out.output_scores, decoder_out.attn, ) decoder_out = decoder_out._replace( output_tokens=out_tokens, output_scores=out_scores, attn=out_attn, ) else: terminated = decoder_out.output_tokens.new_zeros( decoder_out.output_tokens.size(0) ).bool() if step == self.max_iter: # reach last iteration, terminate terminated.fill_(1) # collect finalized sentences finalized_idxs = sent_idxs[terminated] finalized_tokens = decoder_out.output_tokens[terminated] finalized_scores = decoder_out.output_scores[terminated] finalized_attn = ( None if (decoder_out.attn is None or decoder_out.attn.size(0) == 0) else decoder_out.attn[terminated] ) if self.retain_history: finalized_history_tokens = [h[terminated] for h in decoder_out.history] for i in range(finalized_idxs.size(0)): finalized[finalized_idxs[i]] = [ finalized_hypos( step, finalized_tokens[i], finalized_scores[i], None if finalized_attn is None else finalized_attn[i], ) ] if self.retain_history: finalized[finalized_idxs[i]][0]["history"] = [] for j in range(len(finalized_history_tokens)): finalized[finalized_idxs[i]][0]["history"].append( finalized_hypos( step, finalized_history_tokens[j][i], None, None ) ) # check if all terminated if terminated.sum() == terminated.size(0): break # for next step not_terminated = ~terminated prev_decoder_out = decoder_out._replace( output_tokens=decoder_out.output_tokens[not_terminated], output_scores=decoder_out.output_scores[not_terminated], attn=decoder_out.attn[not_terminated] if (decoder_out.attn is not None and decoder_out.attn.size(0) > 0) else None, history=[h[not_terminated] for h in decoder_out.history] if decoder_out.history is not None else None, ) encoder_out = model.encoder.reorder_encoder_out( encoder_out, not_terminated.nonzero(as_tuple=False).squeeze() ) sent_idxs = sent_idxs[not_terminated] prev_output_tokens = prev_decoder_out.output_tokens.clone() if self.beam_size > 1: if reranker is not None: finalized = self.rerank( reranker, finalized, [src_tokens, src_lengths], self.beam_size ) # aggregate information from length beam finalized = [ finalized[ np.argmax( [ finalized[self.beam_size * i + j][0]["score"] for j in range(self.beam_size) ] ) + self.beam_size * i ] for i in range(len(finalized) // self.beam_size) ] return finalized def rerank(self, reranker, finalized, encoder_input, beam_size): def rebuild_batch(finalized): finalized_tokens = [f[0]["tokens"] for f in finalized] finalized_maxlen = max(f.size(0) for f in finalized_tokens) final_output_tokens = ( finalized_tokens[0] .new_zeros(len(finalized_tokens), finalized_maxlen) .fill_(self.pad) ) for i, f in enumerate(finalized_tokens): final_output_tokens[i, : f.size(0)] = f return final_output_tokens final_output_tokens = rebuild_batch(finalized) final_output_tokens[ :, 0 ] = self.eos # autoregressive model assumes starting with EOS reranker_encoder_out = reranker.encoder(*encoder_input) length_beam_order = ( utils.new_arange( final_output_tokens, beam_size, reranker_encoder_out.encoder_out.size(1) ) .t() .reshape(-1) ) reranker_encoder_out = reranker.encoder.reorder_encoder_out( reranker_encoder_out, length_beam_order ) reranking_scores = reranker.get_normalized_probs( reranker.decoder(final_output_tokens[:, :-1], reranker_encoder_out), True, None, ) reranking_scores = reranking_scores.gather(2, final_output_tokens[:, 1:, None]) reranking_masks = final_output_tokens[:, 1:].ne(self.pad) reranking_scores = ( reranking_scores[:, :, 0].masked_fill_(~reranking_masks, 0).sum(1) ) reranking_scores = reranking_scores / reranking_masks.sum(1).type_as( reranking_scores ) for i in range(len(finalized)): finalized[i][0]["score"] = reranking_scores[i] return finalized
KosmosX-API-main
kosmosX/fairseq/fairseq/iterative_refinement_generator.py
# 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 logger = logging.getLogger(__name__) class NanDetector: """ Detects the first NaN or Inf in forward and/or backward pass and logs, together with the module name """ def __init__(self, model, forward=True, backward=True): self.bhooks = [] self.fhooks = [] self.forward = forward self.backward = backward self.named_parameters = list(model.named_parameters()) self.reset() for name, mod in model.named_modules(): mod.__module_name = name self.add_hooks(mod) def __enter__(self): return self def __exit__(self, exc_type, exc_value, exc_traceback): # Dump out all model gnorms to enable better debugging norm = {} gradients = {} for name, param in self.named_parameters: if param.grad is not None: grad_norm = torch.norm(param.grad.data.float(), p=2) norm[name] = grad_norm.item() if torch.isnan(grad_norm).any() or torch.isinf(grad_norm).any(): gradients[name] = param.grad.data if len(gradients) > 0: logger.info("Detected nan/inf grad norm, dumping norms...") logger.info(f"norms: {norm}") logger.info(f"gradients: {gradients}") self.close() def add_hooks(self, module): if self.forward: self.fhooks.append(module.register_forward_hook(self.fhook_fn)) if self.backward: self.bhooks.append(module.register_backward_hook(self.bhook_fn)) def reset(self): self.has_printed_f = False self.has_printed_b = False def _detect(self, tensor, name, backward): err = None if ( torch.is_floating_point(tensor) # single value tensors (like the loss) will not provide much info and tensor.numel() >= 2 ): with torch.no_grad(): if torch.isnan(tensor).any(): err = "NaN" elif torch.isinf(tensor).any(): err = "Inf" if err is not None: err = f"{err} detected in output of {name}, shape: {tensor.shape}, {'backward' if backward else 'forward'}" return err def _apply(self, module, inp, x, backward): if torch.is_tensor(x): if isinstance(inp, tuple) and len(inp) > 0: inp = inp[0] err = self._detect(x, module.__module_name, backward) if err is not None: if torch.is_tensor(inp) and not backward: err += ( f" input max: {inp.max().item()}, input min: {inp.min().item()}" ) has_printed_attr = "has_printed_b" if backward else "has_printed_f" logger.warning(err) setattr(self, has_printed_attr, True) elif isinstance(x, dict): for v in x.values(): self._apply(module, inp, v, backward) elif isinstance(x, list) or isinstance(x, tuple): for v in x: self._apply(module, inp, v, backward) def fhook_fn(self, module, inp, output): if not self.has_printed_f: self._apply(module, inp, output, backward=False) def bhook_fn(self, module, inp, output): if not self.has_printed_b: self._apply(module, inp, output, backward=True) def close(self): for hook in self.fhooks + self.bhooks: hook.remove()
KosmosX-API-main
kosmosX/fairseq/fairseq/nan_detector.py
# Originally from Microsoft Corporation. # Licensed under the MIT License. """ Wrapper for ngram_repeat_block cuda extension """ import math import warnings from typing import Dict, List import torch from torch import nn try: from fairseq import ngram_repeat_block_cuda EXTENSION_BUILT = True except ImportError: EXTENSION_BUILT = False def is_cuda_extension_usable() -> bool: """Check whether ngram_repeat_block_cuda is built properly""" if not EXTENSION_BUILT or not torch.cuda.is_available(): return False bsz = 2 tokens = torch.tensor([[4, 4, 3, 2], [1, 2, 3, 4]], dtype=torch.long, device="cuda") lprobs = torch.rand((8, 12), device="cuda") try: outputs = ngram_repeat_block_cuda.forward(tokens, lprobs, bsz, 3, 4, 3) outputs = outputs + 4 # This line breaks if the extension is built incorrectly. return True except RuntimeError: warnings.warn( "NGramRepeatBlock extension must be rebuilt." 'Run TORCH_CUDA_ARCH_LIST="6.0;6.1;7.0" python setup.py build_ext --inplace' ) return False class NGramRepeatBlock(nn.Module): """Wrapper class for calling ngram_repeat_block cuda extension""" def __init__(self, no_repeat_ngram_size: int, use_extension: bool = True): super().__init__() self.use_extension = is_cuda_extension_usable() if use_extension else False self.no_repeat_ngram_size = no_repeat_ngram_size def reset_parameters(self): pass @torch.jit.unused def call_cuda_extension( self, tokens, lprobs, bsz: int, beam_size: int, step: int, ): return ngram_repeat_block_cuda.forward( tokens, lprobs, bsz, step, beam_size, self.no_repeat_ngram_size ) def forward( self, tokens, lprobs, bsz: int, beam_size: int, step: int, ): """ Args: tokens(Tensor): Input tokens(Bsz*beam, seq_len) lprobs(Tensor): likelihood probability, Expected to be updated in place.(Bsz*beam, vocab_size) bsz(int): batch size step(int): current step beam_size(int): beam size no_repeat_ngram_size(int): Ngram size """ msg = f"expected {bsz *beam_size} got" assert tokens.size(0) == bsz * beam_size, f"{msg} {tokens.size(0)}" assert lprobs.size(0) == bsz * beam_size, f"{msg} {lprobs.size(0)}" if self.use_extension: return self.call_cuda_extension(tokens, lprobs, bsz, beam_size, step) else: return self._no_repeat_ngram( tokens, lprobs, bsz, beam_size, step, ) def _no_repeat_ngram(self, tokens, lprobs, bsz: int, beam_size: int, step: int): """For each hypothesis generate a list of previous ngrams and set associated lprobs to -inf""" gen_ngrams: List[Dict[str, List[int]]] = [ torch.jit.annotate(Dict[str, List[int]], {}) for bbsz_idx in range(bsz * beam_size) ] cpu_tokens = tokens.cpu() for bbsz_idx in range(bsz * beam_size): gen_tokens: List[int] = cpu_tokens[bbsz_idx].tolist() for ngram in self.transpose_list( [gen_tokens[i:] for i in range(self.no_repeat_ngram_size)] ): key = ",".join([str(x) for x in ngram[:-1]]) gen_ngrams[bbsz_idx][key] = gen_ngrams[bbsz_idx].get( key, torch.jit.annotate(List[int], []) ) + [ngram[-1]] if step + 2 - self.no_repeat_ngram_size >= 0: # no banned tokens if we haven't generated no_repeat_ngram_size tokens yet banned_tokens = [ self.calculate_banned_tokens( tokens, step, gen_ngrams, self.no_repeat_ngram_size, bbsz_idx ) for bbsz_idx in range(bsz * beam_size) ] else: banned_tokens = [ torch.jit.annotate(List[int], []) for bbsz_idx in range(bsz * beam_size) ] for bbsz_idx in range(bsz * beam_size): lprobs[bbsz_idx][ torch.tensor(banned_tokens[bbsz_idx], dtype=torch.int64) ] = torch.tensor(-math.inf).to(lprobs) return lprobs @staticmethod def calculate_banned_tokens( tokens, step: int, gen_ngrams: List[Dict[str, List[int]]], no_repeat_ngram_size: int, bbsz_idx: int, ): tokens_list: List[int] = tokens[ bbsz_idx, step + 2 - no_repeat_ngram_size : step + 1 ].tolist() # before decoding the next token, prevent decoding of ngrams that have already appeared ngram_index = ",".join([str(x) for x in tokens_list]) return gen_ngrams[bbsz_idx].get(ngram_index, torch.jit.annotate(List[int], [])) @staticmethod def transpose_list(l: List[List[int]]): # GeneratorExp aren't supported in TS so ignoring the lint min_len = min([len(x) for x in l]) # noqa l2 = [[row[i] for row in l] for i in range(min_len)] return l2
KosmosX-API-main
kosmosX/fairseq/fairseq/ngram_repeat_block.py
# 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 argparse import Namespace from typing import Union from fairseq.dataclass import FairseqDataclass from fairseq.dataclass.utils import merge_with_parent from hydra.core.config_store import ConfigStore from omegaconf import DictConfig REGISTRIES = {} def setup_registry(registry_name: str, base_class=None, default=None, required=False): assert registry_name.startswith("--") registry_name = registry_name[2:].replace("-", "_") REGISTRY = {} REGISTRY_CLASS_NAMES = set() DATACLASS_REGISTRY = {} # maintain a registry of all registries if registry_name in REGISTRIES: return # registry already exists REGISTRIES[registry_name] = { "registry": REGISTRY, "default": default, "dataclass_registry": DATACLASS_REGISTRY, } def build_x(cfg: Union[DictConfig, str, Namespace], *extra_args, **extra_kwargs): if isinstance(cfg, DictConfig): choice = cfg._name if choice and choice in DATACLASS_REGISTRY: dc = DATACLASS_REGISTRY[choice] cfg = merge_with_parent(dc(), cfg) elif isinstance(cfg, str): choice = cfg if choice in DATACLASS_REGISTRY: cfg = DATACLASS_REGISTRY[choice]() else: choice = getattr(cfg, registry_name, None) if choice in DATACLASS_REGISTRY: cfg = DATACLASS_REGISTRY[choice].from_namespace(cfg) if choice is None: if required: raise ValueError("{} is required!".format(registry_name)) return None cls = REGISTRY[choice] if hasattr(cls, "build_" + registry_name): builder = getattr(cls, "build_" + registry_name) else: builder = cls return builder(cfg, *extra_args, **extra_kwargs) def register_x(name, dataclass=None): def register_x_cls(cls): if name in REGISTRY: raise ValueError( "Cannot register duplicate {} ({})".format(registry_name, name) ) if cls.__name__ in REGISTRY_CLASS_NAMES: raise ValueError( "Cannot register {} with duplicate class name ({})".format( registry_name, cls.__name__ ) ) if base_class is not None and not issubclass(cls, base_class): raise ValueError( "{} must extend {}".format(cls.__name__, base_class.__name__) ) if dataclass is not None and not issubclass(dataclass, FairseqDataclass): raise ValueError( "Dataclass {} must extend FairseqDataclass".format(dataclass) ) cls.__dataclass = dataclass if cls.__dataclass is not None: DATACLASS_REGISTRY[name] = cls.__dataclass cs = ConfigStore.instance() node = dataclass() node._name = name cs.store(name=name, group=registry_name, node=node, provider="fairseq") REGISTRY[name] = cls return cls return register_x_cls return build_x, register_x, REGISTRY, DATACLASS_REGISTRY
KosmosX-API-main
kosmosX/fairseq/fairseq/registry.py
# 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 numpy as np from fairseq.data.audio.speech_to_text_dataset import S2TDataConfig class SpeechGenerator(object): def __init__(self, model, vocoder, data_cfg: S2TDataConfig): self.model = model self.vocoder = vocoder stats_npz_path = data_cfg.global_cmvn_stats_npz self.gcmvn_stats = None if stats_npz_path is not None: self.gcmvn_stats = np.load(stats_npz_path) def gcmvn_denormalize(self, x): # x: B x T x C if self.gcmvn_stats is None: return x mean = torch.from_numpy(self.gcmvn_stats["mean"]).to(x) std = torch.from_numpy(self.gcmvn_stats["std"]).to(x) assert len(x.shape) == 3 and mean.shape[0] == std.shape[0] == x.shape[2] x = x * std.view(1, 1, -1).expand_as(x) return x + mean.view(1, 1, -1).expand_as(x) def get_waveform(self, feat): # T x C -> T return None if self.vocoder is None else self.vocoder(feat).squeeze(0) class AutoRegressiveSpeechGenerator(SpeechGenerator): def __init__( self, model, vocoder, data_cfg, max_iter: int = 6000, eos_prob_threshold: float = 0.5, ): super().__init__(model, vocoder, data_cfg) self.max_iter = max_iter self.eos_prob_threshold = eos_prob_threshold @torch.no_grad() def generate(self, model, sample, has_targ=False, **kwargs): model.eval() src_tokens = sample["net_input"]["src_tokens"] src_lengths = sample["net_input"]["src_lengths"] bsz, src_len = src_tokens.size()[:2] n_frames_per_step = model.decoder.n_frames_per_step out_dim = model.decoder.out_dim raw_dim = out_dim // n_frames_per_step # initialize encoder_out = model.forward_encoder( src_tokens, src_lengths, speaker=sample["speaker"] ) incremental_state = {} feat, attn, eos_prob = [], [], [] finished = src_tokens.new_zeros((bsz,)).bool() out_lens = src_lengths.new_zeros((bsz,)).long().fill_(self.max_iter) prev_feat_out = encoder_out["encoder_out"][0].new_zeros(bsz, 1, out_dim) for step in range(self.max_iter): cur_out_lens = out_lens.clone() cur_out_lens.masked_fill_(cur_out_lens.eq(self.max_iter), step + 1) _, cur_eos_out, cur_extra = model.forward_decoder( prev_feat_out, encoder_out=encoder_out, incremental_state=incremental_state, target_lengths=cur_out_lens, speaker=sample["speaker"], **kwargs ) cur_eos_prob = torch.sigmoid(cur_eos_out).squeeze(2) feat.append(cur_extra["feature_out"]) attn.append(cur_extra["attn"]) eos_prob.append(cur_eos_prob) cur_finished = cur_eos_prob.squeeze(1) > self.eos_prob_threshold out_lens.masked_fill_((~finished) & cur_finished, step + 1) finished = finished | cur_finished if finished.sum().item() == bsz: break prev_feat_out = cur_extra["feature_out"] feat = torch.cat(feat, dim=1) feat = model.decoder.postnet(feat) + feat eos_prob = torch.cat(eos_prob, dim=1) attn = torch.cat(attn, dim=2) alignment = attn.max(dim=1)[1] feat = feat.reshape(bsz, -1, raw_dim) feat = self.gcmvn_denormalize(feat) eos_prob = eos_prob.repeat_interleave(n_frames_per_step, dim=1) attn = attn.repeat_interleave(n_frames_per_step, dim=2) alignment = alignment.repeat_interleave(n_frames_per_step, dim=1) out_lens = out_lens * n_frames_per_step finalized = [ { "feature": feat[b, :out_len], "eos_prob": eos_prob[b, :out_len], "attn": attn[b, :, :out_len], "alignment": alignment[b, :out_len], "waveform": self.get_waveform(feat[b, :out_len]), } for b, out_len in zip(range(bsz), out_lens) ] if has_targ: assert sample["target"].size(-1) == out_dim tgt_feats = sample["target"].view(bsz, -1, raw_dim) tgt_feats = self.gcmvn_denormalize(tgt_feats) tgt_lens = sample["target_lengths"] * n_frames_per_step for b, (f, l) in enumerate(zip(tgt_feats, tgt_lens)): finalized[b]["targ_feature"] = f[:l] finalized[b]["targ_waveform"] = self.get_waveform(f[:l]) return finalized class NonAutoregressiveSpeechGenerator(SpeechGenerator): @torch.no_grad() def generate(self, model, sample, has_targ=False, **kwargs): model.eval() bsz, max_src_len = sample["net_input"]["src_tokens"].size() n_frames_per_step = model.encoder.n_frames_per_step out_dim = model.encoder.out_dim raw_dim = out_dim // n_frames_per_step feat, feat_post, out_lens, log_dur_out, _, _ = model( src_tokens=sample["net_input"]["src_tokens"], src_lengths=sample["net_input"]["src_lengths"], prev_output_tokens=sample["net_input"]["prev_output_tokens"], incremental_state=None, target_lengths=sample["target_lengths"], speaker=sample["speaker"], ) if feat_post is not None: feat = feat_post feat = feat.view(bsz, -1, raw_dim) feat = self.gcmvn_denormalize(feat) dur_out = torch.clamp(torch.round(torch.exp(log_dur_out) - 1).long(), min=0) def get_dur_plot_data(d): r = [] for i, dd in enumerate(d): r += [i + 1] * dd.item() return r out_lens = out_lens * n_frames_per_step finalized = [ { "feature": feat[b, :l] if l > 0 else feat.new_zeros([1, raw_dim]), "waveform": self.get_waveform( feat[b, :l] if l > 0 else feat.new_zeros([1, raw_dim]) ), "attn": feat.new_tensor(get_dur_plot_data(dur_out[b])), } for b, l in zip(range(bsz), out_lens) ] if has_targ: tgt_feats = sample["target"].view(bsz, -1, raw_dim) tgt_feats = self.gcmvn_denormalize(tgt_feats) tgt_lens = sample["target_lengths"] * n_frames_per_step for b, (f, l) in enumerate(zip(tgt_feats, tgt_lens)): finalized[b]["targ_feature"] = f[:l] finalized[b]["targ_waveform"] = self.get_waveform(f[:l]) return finalized class TeacherForcingAutoRegressiveSpeechGenerator(AutoRegressiveSpeechGenerator): @torch.no_grad() def generate(self, model, sample, has_targ=False, **kwargs): model.eval() src_tokens = sample["net_input"]["src_tokens"] src_lens = sample["net_input"]["src_lengths"] prev_out_tokens = sample["net_input"]["prev_output_tokens"] tgt_lens = sample["target_lengths"] n_frames_per_step = model.decoder.n_frames_per_step raw_dim = model.decoder.out_dim // n_frames_per_step bsz = src_tokens.shape[0] feat, eos_prob, extra = model( src_tokens, src_lens, prev_out_tokens, incremental_state=None, target_lengths=tgt_lens, speaker=sample["speaker"], ) attn = extra["attn"] # B x T_s x T_t alignment = attn.max(dim=1)[1] feat = feat.reshape(bsz, -1, raw_dim) feat = self.gcmvn_denormalize(feat) eos_prob = eos_prob.repeat_interleave(n_frames_per_step, dim=1) attn = attn.repeat_interleave(n_frames_per_step, dim=2) alignment = alignment.repeat_interleave(n_frames_per_step, dim=1) tgt_lens = sample["target_lengths"] * n_frames_per_step finalized = [ { "feature": feat[b, :tgt_len], "eos_prob": eos_prob[b, :tgt_len], "attn": attn[b, :, :tgt_len], "alignment": alignment[b, :tgt_len], "waveform": self.get_waveform(feat[b, :tgt_len]), } for b, tgt_len in zip(range(bsz), tgt_lens) ] if has_targ: tgt_feats = sample["target"].view(bsz, -1, raw_dim) tgt_feats = self.gcmvn_denormalize(tgt_feats) for b, (f, l) in enumerate(zip(tgt_feats, tgt_lens)): finalized[b]["targ_feature"] = f[:l] finalized[b]["targ_waveform"] = self.get_waveform(f[:l]) return finalized
KosmosX-API-main
kosmosX/fairseq/fairseq/speech_generator.py
# 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 import typing as tp def _safe_readline(fd) -> str: pos = fd.tell() while True: try: return fd.readline() except UnicodeDecodeError: pos -= 1 fd.seek(pos) # search where this character begins def find_offsets(filename: str, num_chunks: int) -> tp.List[int]: """ given a file and a number of chuncks, find the offsets in the file to be able to chunk around full lines. """ with open(filename, "r", encoding="utf-8") as f: size = os.fstat(f.fileno()).st_size chunk_size = size // num_chunks offsets = [0 for _ in range(num_chunks + 1)] for i in range(1, num_chunks): f.seek(chunk_size * i) _safe_readline(f) offsets[i] = f.tell() offsets[-1] = size return offsets class ChunkLineIterator: """ Iterator to properly iterate over lines of a file chunck. """ def __init__(self, fd, start_offset: int, end_offset: int): self._fd = fd self._start_offset = start_offset self._end_offset = end_offset def __iter__(self) -> tp.Iterable[str]: self._fd.seek(self._start_offset) # next(f) breaks f.tell(), hence readline() must be used line = _safe_readline(self._fd) while line: pos = self._fd.tell() # f.tell() does not always give the byte position in the file # sometimes it skips to a very large number # it is unlikely that through a normal read we go from # end bytes to end + 2**32 bytes (4 GB) and this makes it unlikely # that the procedure breaks by the undeterministic behavior of # f.tell() if ( self._end_offset > 0 and pos > self._end_offset and pos < self._end_offset + 2 ** 32 ): break yield line line = self._fd.readline() class Chunker: """ contextmanager to read a chunck of a file line by line. """ def __init__(self, path: str, start_offset: int, end_offset: int): self.path = path self.start_offset = start_offset self.end_offset = end_offset def __enter__(self) -> ChunkLineIterator: self.fd = open(self.path, "r", encoding="utf-8") return ChunkLineIterator(self.fd, self.start_offset, self.end_offset) def __exit__(self, exc_type, exc_val, exc_tb) -> None: self.fd.close()
KosmosX-API-main
kosmosX/fairseq/fairseq/file_chunker_utils.py
# 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. """isort:skip_file""" import os import sys try: from .version import __version__ # noqa except ImportError: version_txt = os.path.join(os.path.dirname(__file__), "version.txt") with open(version_txt) as f: __version__ = f.read().strip() __all__ = ["pdb"] # backwards compatibility to support `from fairseq.X import Y` from fairseq.distributed import utils as distributed_utils from fairseq.logging import meters, metrics, progress_bar # noqa sys.modules["fairseq.distributed_utils"] = distributed_utils sys.modules["fairseq.meters"] = meters sys.modules["fairseq.metrics"] = metrics sys.modules["fairseq.progress_bar"] = progress_bar # initialize hydra from fairseq.dataclass.initialize import hydra_init hydra_init() import fairseq.criterions # noqa import fairseq.distributed # noqa import fairseq.models # noqa import fairseq.modules # noqa import fairseq.optim # noqa import fairseq.optim.lr_scheduler # noqa import fairseq.pdb # noqa import fairseq.scoring # noqa import fairseq.tasks # noqa import fairseq.token_generation_constraints # noqa import fairseq.benchmark # noqa import fairseq.model_parallel # noqa
KosmosX-API-main
kosmosX/fairseq/fairseq/__init__.py
# 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 typing import Dict, List, Optional import sys import torch import torch.nn as nn from fairseq import search, utils from fairseq.data import data_utils from fairseq.models import FairseqIncrementalDecoder from torch import Tensor from fairseq.ngram_repeat_block import NGramRepeatBlock class SequenceGenerator(nn.Module): def __init__( self, models, tgt_dict, beam_size=1, max_len_a=0, max_len_b=200, max_len=0, min_len=1, normalize_scores=True, len_penalty=1.0, unk_penalty=0.0, temperature=1.0, match_source_len=False, no_repeat_ngram_size=0, search_strategy=None, eos=None, symbols_to_strip_from_output=None, lm_model=None, lm_weight=1.0, ): """Generates translations of a given source sentence. Args: models (List[~fairseq.models.FairseqModel]): ensemble of models, currently support fairseq.models.TransformerModel for scripting beam_size (int, optional): beam width (default: 1) max_len_a/b (int, optional): generate sequences of maximum length ax + b, where x is the source length max_len (int, optional): the maximum length of the generated output (not including end-of-sentence) min_len (int, optional): the minimum length of the generated output (not including end-of-sentence) normalize_scores (bool, optional): normalize scores by the length of the output (default: True) len_penalty (float, optional): length penalty, where <1.0 favors shorter, >1.0 favors longer sentences (default: 1.0) unk_penalty (float, optional): unknown word penalty, where <0 produces more unks, >0 produces fewer (default: 0.0) temperature (float, optional): temperature, where values >1.0 produce more uniform samples and values <1.0 produce sharper samples (default: 1.0) match_source_len (bool, optional): outputs should match the source length (default: False) """ super().__init__() if isinstance(models, EnsembleModel): self.model = models else: self.model = EnsembleModel(models) self.tgt_dict = tgt_dict self.pad = tgt_dict.pad() self.unk = tgt_dict.unk() self.eos = tgt_dict.eos() if eos is None else eos self.symbols_to_strip_from_output = ( symbols_to_strip_from_output.union({self.eos}) if symbols_to_strip_from_output is not None else {self.eos} ) self.vocab_size = len(tgt_dict) self.beam_size = beam_size # the max beam size is the dictionary size - 1, since we never select pad self.beam_size = min(beam_size, self.vocab_size - 1) self.max_len_a = max_len_a self.max_len_b = max_len_b self.min_len = min_len self.max_len = max_len or self.model.max_decoder_positions() self.normalize_scores = normalize_scores self.len_penalty = len_penalty self.unk_penalty = unk_penalty self.temperature = temperature self.match_source_len = match_source_len if no_repeat_ngram_size > 0: self.repeat_ngram_blocker = NGramRepeatBlock(no_repeat_ngram_size) else: self.repeat_ngram_blocker = None assert temperature > 0, "--temperature must be greater than 0" self.search = ( search.BeamSearch(tgt_dict) if search_strategy is None else search_strategy ) # We only need to set src_lengths in LengthConstrainedBeamSearch. # As a module attribute, setting it would break in multithread # settings when the model is shared. self.should_set_src_lengths = ( hasattr(self.search, "needs_src_lengths") and self.search.needs_src_lengths ) self.model.eval() self.lm_model = lm_model self.lm_weight = lm_weight if self.lm_model is not None: self.lm_model.eval() def cuda(self): self.model.cuda() return self @torch.no_grad() def forward( self, sample: Dict[str, Dict[str, Tensor]], prefix_tokens: Optional[Tensor] = None, bos_token: Optional[int] = None, ): """Generate a batch of translations. Args: sample (dict): batch prefix_tokens (torch.LongTensor, optional): force decoder to begin with these tokens bos_token (int, optional): beginning of sentence token (default: self.eos) """ return self._generate(sample, prefix_tokens, bos_token=bos_token) # TODO(myleott): unused, deprecate after pytorch-translate migration def generate_batched_itr(self, data_itr, beam_size=None, cuda=False, timer=None): """Iterate over a batched dataset and yield individual translations. Args: cuda (bool, optional): use GPU for generation timer (StopwatchMeter, optional): time generations """ for sample in data_itr: s = utils.move_to_cuda(sample) if cuda else sample if "net_input" not in s: continue input = s["net_input"] # 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 input.items() if k != "prev_output_tokens" } if timer is not None: timer.start() with torch.no_grad(): hypos = self.generate(encoder_input) if timer is not None: timer.stop(sum(len(h[0]["tokens"]) for h in hypos)) for i, id in enumerate(s["id"].data): # remove padding src = utils.strip_pad(input["src_tokens"].data[i, :], self.pad) ref = ( utils.strip_pad(s["target"].data[i, :], self.pad) if s["target"] is not None else None ) yield id, src, ref, hypos[i] @torch.no_grad() def generate( self, models, sample: Dict[str, Dict[str, Tensor]], **kwargs ) -> List[List[Dict[str, Tensor]]]: """Generate translations. Match the api of other fairseq generators. Args: models (List[~fairseq.models.FairseqModel]): ensemble of models sample (dict): batch prefix_tokens (torch.LongTensor, optional): force decoder to begin with these tokens constraints (torch.LongTensor, optional): force decoder to include the list of constraints bos_token (int, optional): beginning of sentence token (default: self.eos) """ return self._generate(sample, **kwargs) def _generate( self, sample: Dict[str, Dict[str, Tensor]], prefix_tokens: Optional[Tensor] = None, constraints: Optional[Tensor] = None, bos_token: Optional[int] = None, ): incremental_states = torch.jit.annotate( List[Dict[str, Dict[str, Optional[Tensor]]]], [ torch.jit.annotate(Dict[str, Dict[str, Optional[Tensor]]], {}) for i in range(self.model.models_size) ], ) net_input = sample["net_input"] prefix_tokens.clone() prefix_tokens = prefix_tokens[:, 1:] if "src_tokens" in net_input: src_tokens = net_input["src_tokens"] # length of the source text being the character length except EndOfSentence and pad src_lengths = ( (src_tokens.ne(self.eos) & src_tokens.ne(self.pad)).long().sum(dim=1) ) elif "source" in net_input: src_tokens = net_input["source"] src_lengths = ( net_input["padding_mask"].size(-1) - net_input["padding_mask"].sum(-1) if net_input["padding_mask"] is not None else torch.tensor(src_tokens.size(-1)).to(src_tokens) ) elif "features" in net_input: src_tokens = net_input["features"] src_lengths = ( net_input["padding_mask"].size(-1) - net_input["padding_mask"].sum(-1) if net_input["padding_mask"] is not None else torch.tensor(src_tokens.size(-1)).to(src_tokens) ) else: raise Exception( "expected src_tokens or source in net input. input keys: " + str(net_input.keys()) ) # bsz: total number of sentences in beam # Note that src_tokens may have more than 2 dimensions (i.e. audio features) bsz, src_len = src_tokens.size()[:2] beam_size = self.beam_size if constraints is not None and not self.search.supports_constraints: raise NotImplementedError( "Target-side constraints were provided, but search method doesn't support them" ) # Initialize constraints, when active self.search.init_constraints(constraints, beam_size) max_len: int = -1 if self.match_source_len: max_len = src_lengths.max().item() else: max_len = min( int(self.max_len_a * src_len + self.max_len_b), self.max_len - 1, ) assert ( self.min_len <= max_len ), "min_len cannot be larger than max_len, please adjust these!" # compute the encoder output for each beam with torch.autograd.profiler.record_function("EnsembleModel: forward_encoder"): encoder_outs = self.model.forward_encoder(net_input) # placeholder of indices for bsz * beam_size to hold tokens and accumulative scores new_order = torch.arange(bsz).view(-1, 1).repeat(1, beam_size).view(-1) new_order = new_order.to(src_tokens.device).long() encoder_outs = self.model.reorder_encoder_out(encoder_outs, new_order) # ensure encoder_outs is a List. assert encoder_outs is not None # initialize buffers scores = ( torch.zeros(bsz * beam_size, max_len + 1).to(src_tokens).float() ) # +1 for eos; pad is never chosen for scoring tokens = ( torch.zeros(bsz * beam_size, max_len + 2) .to(src_tokens) .long() .fill_(self.pad) ) # +2 for eos and pad tokens[:, 0] = self.eos if bos_token is None else bos_token attn: Optional[Tensor] = None # A list that indicates candidates that should be ignored. # For example, suppose we're sampling and have already finalized 2/5 # samples. Then cands_to_ignore would mark 2 positions as being ignored, # so that we only finalize the remaining 3 samples. cands_to_ignore = ( torch.zeros(bsz, beam_size).to(src_tokens).eq(-1) ) # forward and backward-compatible False mask # list of completed sentences finalized = torch.jit.annotate( List[List[Dict[str, Tensor]]], [torch.jit.annotate(List[Dict[str, Tensor]], []) for i in range(bsz)], ) # contains lists of dictionaries of infomation about the hypothesis being finalized at each step # a boolean array indicating if the sentence at the index is finished or not finished = [False for i in range(bsz)] num_remaining_sent = bsz # number of sentences remaining # number of candidate hypos per step cand_size = 2 * beam_size # 2 x beam size in case half are EOS # offset arrays for converting between different indexing schemes bbsz_offsets = ( (torch.arange(0, bsz) * beam_size) .unsqueeze(1) .type_as(tokens) .to(src_tokens.device) ) cand_offsets = torch.arange(0, cand_size).type_as(tokens).to(src_tokens.device) reorder_state: Optional[Tensor] = None batch_idxs: Optional[Tensor] = None original_batch_idxs: Optional[Tensor] = None if "id" in sample and isinstance(sample["id"], Tensor): original_batch_idxs = sample["id"] else: original_batch_idxs = torch.arange(0, bsz).type_as(tokens) prefix_lprobs = None multimodal_infer = False for step in range(max_len + 1): # one extra step for EOS marker # reorder decoder internal states based on the prev choice of beams if reorder_state is not None: if batch_idxs is not None: # update beam indices to take into account removed sentences corr = batch_idxs - torch.arange(batch_idxs.numel()).type_as( batch_idxs ) reorder_state.view(-1, beam_size).add_( corr.unsqueeze(-1) * beam_size ) original_batch_idxs = original_batch_idxs[batch_idxs] self.model.reorder_incremental_state(incremental_states, reorder_state) encoder_outs = self.model.reorder_encoder_out( encoder_outs, reorder_state ) with torch.autograd.profiler.record_function( "EnsembleModel: forward_decoder" ): if "img_src_tokens" in net_input and step == 0: # import pudb; pu.db # input: [B, D] -> [B, Beam Size, D] img_token_size = sample['net_input']['img_src_tokens'].size() if len(img_token_size) == 5: # few-shot setting, [B, K-shot, D(3, 244, 244)] img_token_size[0] img_token_size[1] img_tokens = sample['net_input']['img_src_tokens'].cuda().view(-1, *img_token_size[2:]) else: img_token_size[0] img_tokens = sample['net_input']['img_src_tokens'].cuda() multimodal_infer = True img_features = self.model.models[0].get_image_representation(img_tokens) first_src_tokens = sample['net_input']['src_tokens'].unsqueeze(1).repeat(1, beam_size, 1).view(bsz*beam_size, -1) img_feature_dim = img_features.size(-1) first_img_features = img_features.view(bsz, -1, img_feature_dim).unsqueeze(1).repeat(1, beam_size, 1, 1).view(-1, img_feature_dim) img_gpt_input_mask = sample['net_input']['img_gpt_input_mask'].cuda().bool() first_gpt_input_mask = img_gpt_input_mask.unsqueeze(1).repeat(1, beam_size, 1).view(bsz*beam_size, -1) decoder_out = self.model.models[0].gpt_model.decoder.forward( first_src_tokens, img_features=first_img_features, img_gpt_input_mask=first_gpt_input_mask, incremental_state=incremental_states[0], first_step=True) attn: Optional[Tensor] = None decoder_out_tuple = decoder_out[0].div_(self.temperature) decoder_out_tuple = (decoder_out_tuple, None) probs = self.model.models[0].gpt_model.get_normalized_probs( decoder_out_tuple, log_probs=True, sample=None ) if len(probs.size()) == 2: probs = probs.unsqueeze(0) prefix_lprobs = probs.clone().reshape(bsz*beam_size, probs.size(1), -1) lprobs, avg_attn_scores = prefix_lprobs[:,step,:].clone(), None elif "aud_src_tokens" in net_input and step == 0: # import pudb; pu.db multimodal_infer = True aud_features = self.model.models[0].get_audio_representation(sample['net_input']['aud_src_tokens'].cuda(), sample['net_input']['aud_masks'].cuda()) decoder_out = self.model.models[0].gpt_model.decoder.forward( sample['net_input']['src_tokens'], aud_features=aud_features, aud_gpt_input_mask=sample['net_input']['aud_gpt_input_mask'].cuda().bool(), incremental_state=incremental_states[0], first_step=True) attn: Optional[Tensor] = None decoder_out_tuple = decoder_out[0].div_(self.temperature) decoder_out_tuple = (decoder_out_tuple, None) probs = self.model.models[0].gpt_model.get_normalized_probs( decoder_out_tuple, log_probs=True, sample=None ) if len(probs.size()) == 2: probs = probs.unsqueeze(0) prefix_lprobs = probs.clone().unsqueeze(0).expand(beam_size, -1, -1, -1).reshape(bsz*beam_size, probs.size(1), -1) lprobs, avg_attn_scores = prefix_lprobs[:,step,:].clone(), None elif ("img_src_tokens" in net_input or "aud_src_tokens" in net_input) and step < len(sample['net_input']['src_tokens'][0]): lprobs, avg_attn_scores = prefix_lprobs[:,step,:].clone(), None multimodal_infer = True else: lprobs, avg_attn_scores = self.model.forward_decoder( tokens[:, : step + 1], encoder_outs, incremental_states, self.temperature, multimodal=multimodal_infer, ) if self.lm_model is not None: lm_out = self.lm_model(tokens[:, : step + 1]) probs = self.lm_model.get_normalized_probs( lm_out, log_probs=True, sample=None ) probs = probs[:, -1, :] * self.lm_weight lprobs += probs lprobs[lprobs != lprobs] = torch.tensor(-math.inf).to(lprobs) lprobs[:, self.pad] = -math.inf # never select pad lprobs[:, self.unk] -= self.unk_penalty # apply unk penalty # handle max length constraint if step >= max_len: lprobs[:, : self.eos] = -math.inf lprobs[:, self.eos + 1 :] = -math.inf # handle prefix tokens (possibly with different lengths) if ( prefix_tokens is not None and step < prefix_tokens.size(1) and step < max_len ): lprobs, tokens, scores = self._prefix_tokens( step, lprobs, scores, tokens, prefix_tokens, beam_size ) elif step < self.min_len: # minimum length constraint (does not apply if using prefix_tokens) lprobs[:, self.eos] = -math.inf # Record attention scores, only support avg_attn_scores is a Tensor if avg_attn_scores is not None: if attn is None: attn = torch.empty( bsz * beam_size, avg_attn_scores.size(1), max_len + 2 ).to(scores) attn[:, :, step + 1].copy_(avg_attn_scores) scores = scores.type_as(lprobs) eos_bbsz_idx = torch.empty(0).to( tokens ) # indices of hypothesis ending with eos (finished sentences) eos_scores = torch.empty(0).to( scores ) # scores of hypothesis ending with eos (finished sentences) if self.should_set_src_lengths: self.search.set_src_lengths(src_lengths) skip_ngram_blocker = False if ("img_src_tokens" in net_input or "aud_src_tokens" in net_input) and step < len(sample['net_input']['src_tokens'][0]): skip_ngram_blocker = True if self.repeat_ngram_blocker is not None and not skip_ngram_blocker: lprobs = self.repeat_ngram_blocker(tokens, lprobs, bsz, beam_size, step) # Shape: (batch, cand_size) cand_scores, cand_indices, cand_beams = self.search.step( step, lprobs.view(bsz, -1, self.vocab_size), scores.view(bsz, beam_size, -1)[:, :, :step], tokens[:, : step + 1], original_batch_idxs, ) # cand_bbsz_idx contains beam indices for the top candidate # hypotheses, with a range of values: [0, bsz*beam_size), # and dimensions: [bsz, cand_size] cand_bbsz_idx = cand_beams.add(bbsz_offsets) # finalize hypotheses that end in eos # Shape of eos_mask: (batch size, beam size) eos_mask = cand_indices.eq(self.eos) & cand_scores.ne(-math.inf) eos_mask[:, :beam_size][cands_to_ignore] = torch.tensor(0).to(eos_mask) if skip_ngram_blocker: eos_mask[:] = False # only consider eos when it's among the top beam_size indices # Now we know what beam item(s) to finish # Shape: 1d list of absolute-numbered eos_bbsz_idx = torch.masked_select( cand_bbsz_idx[:, :beam_size], mask=eos_mask[:, :beam_size] ) finalized_sents: List[int] = [] if eos_bbsz_idx.numel() > 0: eos_scores = torch.masked_select( cand_scores[:, :beam_size], mask=eos_mask[:, :beam_size] ) finalized_sents = self.finalize_hypos( step, eos_bbsz_idx, eos_scores, tokens, scores, finalized, finished, beam_size, attn, src_lengths, max_len, ) num_remaining_sent -= len(finalized_sents) assert num_remaining_sent >= 0 if num_remaining_sent == 0: break if self.search.stop_on_max_len and step >= max_len: break if step >= max_len: break assert step < max_len, f"{step} < {max_len}" # Remove finalized sentences (ones for which {beam_size} # finished hypotheses have been generated) from the batch. if len(finalized_sents) > 0: new_bsz = bsz - len(finalized_sents) # construct batch_idxs which holds indices of batches to keep for the next pass batch_mask = torch.ones( bsz, dtype=torch.bool, device=cand_indices.device ) batch_mask[finalized_sents] = False # TODO replace `nonzero(as_tuple=False)` after TorchScript supports it batch_idxs = torch.arange( bsz, device=cand_indices.device ).masked_select(batch_mask) # Choose the subset of the hypothesized constraints that will continue self.search.prune_sentences(batch_idxs) eos_mask = eos_mask[batch_idxs] cand_beams = cand_beams[batch_idxs] bbsz_offsets.resize_(new_bsz, 1) cand_bbsz_idx = cand_beams.add(bbsz_offsets) cand_scores = cand_scores[batch_idxs] cand_indices = cand_indices[batch_idxs] if prefix_tokens is not None: prefix_tokens = prefix_tokens[batch_idxs] src_lengths = src_lengths[batch_idxs] cands_to_ignore = cands_to_ignore[batch_idxs] scores = scores.view(bsz, -1)[batch_idxs].view(new_bsz * beam_size, -1) tokens = tokens.view(bsz, -1)[batch_idxs].view(new_bsz * beam_size, -1) if attn is not None: attn = attn.view(bsz, -1)[batch_idxs].view( new_bsz * beam_size, attn.size(1), -1 ) bsz = new_bsz else: batch_idxs = None # Set active_mask so that values > cand_size indicate eos hypos # and values < cand_size indicate candidate active hypos. # After, the min values per row are the top candidate active hypos # Rewrite the operator since the element wise or is not supported in torchscript. eos_mask[:, :beam_size] = ~((~cands_to_ignore) & (~eos_mask[:, :beam_size])) active_mask = torch.add( eos_mask.type_as(cand_offsets) * cand_size, cand_offsets[: eos_mask.size(1)], ) # get the top beam_size active hypotheses, which are just # the hypos with the smallest values in active_mask. # {active_hypos} indicates which {beam_size} hypotheses # from the list of {2 * beam_size} candidates were # selected. Shapes: (batch size, beam size) new_cands_to_ignore, active_hypos = torch.topk( active_mask, k=beam_size, dim=1, largest=False ) # update cands_to_ignore to ignore any finalized hypos. cands_to_ignore = new_cands_to_ignore.ge(cand_size)[:, :beam_size] # Make sure there is at least one active item for each sentence in the batch. assert (~cands_to_ignore).any(dim=1).all() # update cands_to_ignore to ignore any finalized hypos # {active_bbsz_idx} denotes which beam number is continued for each new hypothesis (a beam # can be selected more than once). active_bbsz_idx = torch.gather(cand_bbsz_idx, dim=1, index=active_hypos) active_scores = torch.gather(cand_scores, dim=1, index=active_hypos) active_bbsz_idx = active_bbsz_idx.view(-1) active_scores = active_scores.view(-1) # copy tokens and scores for active hypotheses # Set the tokens for each beam (can select the same row more than once) tokens[:, : step + 1] = torch.index_select( tokens[:, : step + 1], dim=0, index=active_bbsz_idx ) # Select the next token for each of them tokens.view(bsz, beam_size, -1)[:, :, step + 1] = torch.gather( cand_indices, dim=1, index=active_hypos ) if step > 0: scores[:, :step] = torch.index_select( scores[:, :step], dim=0, index=active_bbsz_idx ) scores.view(bsz, beam_size, -1)[:, :, step] = torch.gather( cand_scores, dim=1, index=active_hypos ) # Update constraints based on which candidates were selected for the next beam self.search.update_constraints(active_hypos) # copy attention for active hypotheses if attn is not None: attn[:, :, : step + 2] = torch.index_select( attn[:, :, : step + 2], dim=0, index=active_bbsz_idx ) # reorder incremental state in decoder reorder_state = active_bbsz_idx # sort by score descending for sent in range(len(finalized)): scores = torch.tensor( [float(elem["score"].item()) for elem in finalized[sent]] ) _, sorted_scores_indices = torch.sort(scores, descending=True) finalized[sent] = [finalized[sent][ssi] for ssi in sorted_scores_indices] finalized[sent] = torch.jit.annotate( List[Dict[str, Tensor]], finalized[sent] ) return finalized def _prefix_tokens( self, step: int, lprobs, scores, tokens, prefix_tokens, beam_size: int ): """Handle prefix tokens""" prefix_toks = prefix_tokens[:, step].unsqueeze(-1).repeat(1, beam_size).view(-1) prefix_lprobs = lprobs.gather(-1, prefix_toks.unsqueeze(-1)) prefix_mask = prefix_toks.ne(self.pad) lprobs[prefix_mask] = torch.tensor(-math.inf).to(lprobs) lprobs[prefix_mask] = lprobs[prefix_mask].scatter( -1, prefix_toks[prefix_mask].unsqueeze(-1), prefix_lprobs[prefix_mask] ) # if prefix includes eos, then we should make sure tokens and # scores are the same across all beams eos_mask = prefix_toks.eq(self.eos) if eos_mask.any(): # validate that the first beam matches the prefix first_beam = tokens[eos_mask].view(-1, beam_size, tokens.size(-1))[ :, 0, 1 : step + 1 ] eos_mask_batch_dim = eos_mask.view(-1, beam_size)[:, 0] target_prefix = prefix_tokens[eos_mask_batch_dim][:, :step] assert (first_beam == target_prefix).all() # copy tokens, scores and lprobs from the first beam to all beams tokens = self.replicate_first_beam(tokens, eos_mask_batch_dim, beam_size) scores = self.replicate_first_beam(scores, eos_mask_batch_dim, beam_size) lprobs = self.replicate_first_beam(lprobs, eos_mask_batch_dim, beam_size) return lprobs, tokens, scores def replicate_first_beam(self, tensor, mask, beam_size: int): tensor = tensor.view(-1, beam_size, tensor.size(-1)) tensor[mask] = tensor[mask][:, :1, :] return tensor.view(-1, tensor.size(-1)) def finalize_hypos( self, step: int, bbsz_idx, eos_scores, tokens, scores, finalized: List[List[Dict[str, Tensor]]], finished: List[bool], beam_size: int, attn: Optional[Tensor], src_lengths, max_len: int, ): """Finalize hypothesis, store finalized information in `finalized`, and change `finished` accordingly. A sentence is finalized when {beam_size} finished items have been collected for it. Returns number of sentences (not beam items) being finalized. These will be removed from the batch and not processed further. Args: bbsz_idx (Tensor): """ assert bbsz_idx.numel() == eos_scores.numel() # clone relevant token and attention tensors. # tokens is (batch * beam, max_len). So the index_select # gets the newly EOS rows, then selects cols 1..{step + 2} tokens_clone = tokens.index_select(0, bbsz_idx)[ :, 1 : step + 2 ] # skip the first index, which is EOS tokens_clone[:, step] = self.eos attn_clone = ( attn.index_select(0, bbsz_idx)[:, :, 1 : step + 2] if attn is not None else None ) # compute scores per token position pos_scores = scores.index_select(0, bbsz_idx)[:, : step + 1] pos_scores[:, step] = eos_scores # convert from cumulative to per-position scores pos_scores[:, 1:] = pos_scores[:, 1:] - pos_scores[:, :-1] # normalize sentence-level scores if self.normalize_scores: eos_scores /= (step + 1) ** self.len_penalty # cum_unfin records which sentences in the batch are finished. # It helps match indexing between (a) the original sentences # in the batch and (b) the current, possibly-reduced set of # sentences. cum_unfin: List[int] = [] prev = 0 for f in finished: if f: prev += 1 else: cum_unfin.append(prev) cum_fin_tensor = torch.tensor(cum_unfin, dtype=torch.int).to(bbsz_idx) unfin_idx = bbsz_idx // beam_size sent = unfin_idx + torch.index_select(cum_fin_tensor, 0, unfin_idx) # Create a set of "{sent}{unfin_idx}", where # "unfin_idx" is the index in the current (possibly reduced) # list of sentences, and "sent" is the index in the original, # unreduced batch # For every finished beam item # sentence index in the current (possibly reduced) batch seen = (sent << 32) + unfin_idx unique_seen: List[int] = torch.unique(seen).tolist() if self.match_source_len: condition = step > torch.index_select(src_lengths, 0, unfin_idx) eos_scores = torch.where(condition, torch.tensor(-math.inf), eos_scores) sent_list: List[int] = sent.tolist() for i in range(bbsz_idx.size()[0]): # An input sentence (among those in a batch) is finished when # beam_size hypotheses have been collected for it if len(finalized[sent_list[i]]) < beam_size: if attn_clone is not None: # remove padding tokens from attn scores hypo_attn = attn_clone[i] else: hypo_attn = torch.empty(0) finalized[sent_list[i]].append( { "tokens": tokens_clone[i], "score": eos_scores[i], "attention": hypo_attn, # src_len x tgt_len "alignment": torch.empty(0), "positional_scores": pos_scores[i], } ) newly_finished: List[int] = [] for unique_s in unique_seen: # check termination conditions for this sentence unique_sent: int = unique_s >> 32 unique_unfin_idx: int = unique_s - (unique_sent << 32) if not finished[unique_sent] and self.is_finished( step, unique_unfin_idx, max_len, len(finalized[unique_sent]), beam_size ): finished[unique_sent] = True newly_finished.append(unique_unfin_idx) return newly_finished def is_finished( self, step: int, unfin_idx: int, max_len: int, finalized_sent_len: int, beam_size: int, ): """ Check whether decoding for a sentence is finished, which occurs when the list of finalized sentences has reached the beam size, or when we reach the maximum length. """ assert finalized_sent_len <= beam_size if finalized_sent_len == beam_size or step == max_len: return True return False class EnsembleModel(nn.Module): """A wrapper around an ensemble of models.""" def __init__(self, models): super().__init__() self.models_size = len(models) # method '__len__' is not supported in ModuleList for torch script self.single_model = models[0] self.models = nn.ModuleList(models) self.has_incremental: bool = False if all( hasattr(m, "decoder") and isinstance(m.decoder, FairseqIncrementalDecoder) for m in models ): self.has_incremental = True if all( hasattr(m, "gpt_model") and isinstance(m.gpt_model.decoder, FairseqIncrementalDecoder) for m in models ): self.has_incremental = True def forward(self): pass def has_encoder(self): return hasattr(self.single_model, "encoder") def has_incremental_states(self): return self.has_incremental def max_decoder_positions(self): return min( [ m.max_decoder_positions() for m in self.models if hasattr(m, "max_decoder_positions") ] + [sys.maxsize] ) @torch.jit.export def forward_encoder(self, net_input: Dict[str, Tensor]): if not self.has_encoder(): return None return [model.encoder.forward_torchscript(net_input) for model in self.models] @torch.jit.export def forward_decoder( self, tokens, encoder_outs: List[Dict[str, List[Tensor]]], incremental_states: List[Dict[str, Dict[str, Optional[Tensor]]]], temperature: float = 1.0, multimodal: bool = False, ): log_probs = [] avg_attn: Optional[Tensor] = None encoder_out: Optional[Dict[str, List[Tensor]]] = None for i, model in enumerate(self.models): if self.has_encoder(): encoder_out = encoder_outs[i] # decode each model if self.has_incremental_states(): if hasattr(model, "gpt_model"): decoder_out = model.gpt_model.decoder.forward( tokens, encoder_out=encoder_out, incremental_state=incremental_states[i], ) else: decoder_out = model.decoder.forward( tokens, encoder_out=encoder_out, incremental_state=incremental_states[i], ) elif incremental_states is not None and hasattr(model, "gpt_model") and multimodal: # elif False: #TODO: skip this for text zero-shot decoder_out = model.gpt_model.decoder.forward( tokens, encoder_out=encoder_out, incremental_state=incremental_states[i], ) else: if hasattr(model, "decoder"): decoder_out = model.decoder.forward(tokens, encoder_out=encoder_out) else: decoder_out = model.forward(tokens) attn: Optional[Tensor] = None decoder_len = len(decoder_out) if decoder_len > 1 and decoder_out[1] is not None: if isinstance(decoder_out[1], Tensor): attn = decoder_out[1] else: attn_holder = decoder_out[1]["attn"] if isinstance(attn_holder, Tensor): attn = attn_holder elif attn_holder is not None: attn = attn_holder[0] if attn is not None: attn = attn[:, -1, :] decoder_out_tuple = ( decoder_out[0][:, -1:, :].div_(temperature), None if decoder_len <= 1 else decoder_out[1], ) probs = model.get_normalized_probs( decoder_out_tuple, log_probs=True, sample=None ) probs = probs[:, -1, :] if self.models_size == 1: return probs, attn log_probs.append(probs) if attn is not None: if avg_attn is None: avg_attn = attn else: avg_attn.add_(attn) avg_probs = torch.logsumexp(torch.stack(log_probs, dim=0), dim=0) - math.log( self.models_size ) if avg_attn is not None: avg_attn.div_(self.models_size) return avg_probs, avg_attn @torch.jit.export def reorder_encoder_out( self, encoder_outs: Optional[List[Dict[str, List[Tensor]]]], new_order ): """ Reorder encoder output according to *new_order*. Args: encoder_out: output from the ``forward()`` method new_order (LongTensor): desired order Returns: *encoder_out* rearranged according to *new_order* """ new_outs: List[Dict[str, List[Tensor]]] = [] if not self.has_encoder(): return new_outs for i, model in enumerate(self.models): assert encoder_outs is not None new_outs.append( model.encoder.reorder_encoder_out(encoder_outs[i], new_order) ) return new_outs @torch.jit.export def reorder_incremental_state( self, incremental_states: List[Dict[str, Dict[str, Optional[Tensor]]]], new_order, ): if not self.has_incremental_states(): return for i, model in enumerate(self.models): if hasattr(model, "gpt_model"): model.gpt_model.decoder.reorder_incremental_state_scripting( incremental_states[i], new_order ) else: model.decoder.reorder_incremental_state_scripting( incremental_states[i], new_order ) class SequenceGeneratorWithAlignment(SequenceGenerator): def __init__( self, models, tgt_dict, left_pad_target=False, print_alignment="hard", **kwargs ): """Generates translations of a given source sentence. Produces alignments following "Jointly Learning to Align and Translate with Transformer Models" (Garg et al., EMNLP 2019). Args: left_pad_target (bool, optional): Whether or not the hypothesis should be left padded or not when they are teacher forced for generating alignments. """ super().__init__(EnsembleModelWithAlignment(models), tgt_dict, **kwargs) self.left_pad_target = left_pad_target if print_alignment == "hard": self.extract_alignment = utils.extract_hard_alignment elif print_alignment == "soft": self.extract_alignment = utils.extract_soft_alignment @torch.no_grad() def generate(self, models, sample, **kwargs): finalized = super()._generate(sample, **kwargs) src_tokens = sample["net_input"]["src_tokens"] bsz = src_tokens.shape[0] beam_size = self.beam_size ( src_tokens, src_lengths, prev_output_tokens, tgt_tokens, ) = self._prepare_batch_for_alignment(sample, finalized) if any(getattr(m, "full_context_alignment", False) for m in self.model.models): attn = self.model.forward_align(src_tokens, src_lengths, prev_output_tokens) else: attn = [ finalized[i // beam_size][i % beam_size]["attention"].transpose(1, 0) for i in range(bsz * beam_size) ] if src_tokens.device != "cpu": src_tokens = src_tokens.to("cpu") tgt_tokens = tgt_tokens.to("cpu") attn = [i.to("cpu") for i in attn] # Process the attn matrix to extract hard alignments. for i in range(bsz * beam_size): alignment = self.extract_alignment( attn[i], src_tokens[i], tgt_tokens[i], self.pad, self.eos ) finalized[i // beam_size][i % beam_size]["alignment"] = alignment return finalized def _prepare_batch_for_alignment(self, sample, hypothesis): src_tokens = sample["net_input"]["src_tokens"] bsz = src_tokens.shape[0] src_tokens = ( src_tokens[:, None, :] .expand(-1, self.beam_size, -1) .contiguous() .view(bsz * self.beam_size, -1) ) src_lengths = sample["net_input"]["src_lengths"] src_lengths = ( src_lengths[:, None] .expand(-1, self.beam_size) .contiguous() .view(bsz * self.beam_size) ) prev_output_tokens = data_utils.collate_tokens( [beam["tokens"] for example in hypothesis for beam in example], self.pad, self.eos, self.left_pad_target, move_eos_to_beginning=True, ) tgt_tokens = data_utils.collate_tokens( [beam["tokens"] for example in hypothesis for beam in example], self.pad, self.eos, self.left_pad_target, move_eos_to_beginning=False, ) return src_tokens, src_lengths, prev_output_tokens, tgt_tokens class EnsembleModelWithAlignment(EnsembleModel): """A wrapper around an ensemble of models.""" def __init__(self, models): super().__init__(models) def forward_align(self, src_tokens, src_lengths, prev_output_tokens): avg_attn = None for model in self.models: decoder_out = model(src_tokens, src_lengths, prev_output_tokens) attn = decoder_out[1]["attn"][0] if avg_attn is None: avg_attn = attn else: avg_attn.add_(attn) if len(self.models) > 1: avg_attn.div_(len(self.models)) return avg_attn
KosmosX-API-main
kosmosX/fairseq/fairseq/sequence_generator.py
""" DeepSpeed trainer """ import os import sys import torch import time import logging import deepspeed import json from typing import Any, Dict, List from itertools import chain from argparse import Namespace import torch.distributed as dist from fairseq import optim, utils from fairseq.distributed import utils as distributed_utils from fairseq.dataclass.configs import FairseqConfig from fairseq.logging import meters, metrics from fairseq.optim import lr_scheduler from fairseq.optim.dynamic_loss_scaler import DynamicLossScaler from omegaconf import OmegaConf logger = logging.getLogger(__name__) def get_config(config, full_name, fairseq_value): _config = config for name in full_name.split(":"): if name in _config: _config = _config[name] else: _config = fairseq_value break assert _config == fairseq_value, f"deepspeed config: {full_name} does not align with fairseq value: {fairseq_value}" return _config class DeepSpeedTrainer(object): """Main class for data parallel training w. DeepSpeed. Similar to fairseq.Trainer this class supports synchronous distributed data parallel training. However, in this case we expose DeepSpeed features like ZeRO stages 1, 2, and 3. """ def __init__(self, cfg: FairseqConfig, task, model, criterion): if isinstance(cfg, Namespace): logger.warning( "argparse.Namespace configuration is deprecated! Automatically converting to OmegaConf" ) cfg = convert_namespace_to_omegaconf(cfg) self.cfg = cfg self.task = task # try: # subprocess.check_output('which rsync', shell=True) # except subprocess.CalledProcessError: # raise RuntimeError('Please install rsync, this is required for model checkpointing') ds_config = {} if isinstance(cfg.common.deepspeed, str): assert os.path.isfile(cfg.common.deepspeed), f"deepspeed config path is not a file: {cfg.common.deepspeed}" with open(cfg.common.deepspeed, 'r') as fd: ds_config = json.load(fd) # ds_config['zero_allow_untested_optimizer'] = True # gradient accumulation steps assert len(self.cfg.optimization.update_freq) == 1, "no support for gradient accumulation schedules" gas = ds_config.get("gradient_accumulation_steps", self.cfg.optimization.update_freq[0]) ds_config["gradient_accumulation_steps"] = gas # train_micro_batch_size_per_gpu micro_batch_size = get_config(ds_config, "train_micro_batch_size_per_gpu", self.cfg.dataset.batch_size) # micro_batch_size = get_config(ds_config, "train_micro_batch_size_per_gpu", self.cfg.dataset.max_tokens // self.cfg.task.tokens_per_sample) ds_config["train_micro_batch_size_per_gpu"] = int(micro_batch_size) # enable fp16 fp16 = get_config(config=ds_config, full_name="fp16:enabled", fairseq_value=self.cfg.common.fp16) if "fp16" not in ds_config: ds_config["fp16"] = {} ds_config["fp16"]["enabled"] = fp16 # gradient_clipping self.cfg.optimization.clip_norm grad_clip = get_config(ds_config, "gradient_clipping", self.cfg.optimization.clip_norm) ds_config["gradient_clipping"] = grad_clip # force zero elastic checkpoint disabled elastic_ckpt = get_config(ds_config, "zero_optimization:elastic_checkpoint", False) if "zero_optimization" not in ds_config: ds_config["zero_optimization"] = {} ds_config["zero_optimization"]["elastic_checkpoint"] = elastic_ckpt zero_stage = get_config(ds_config, "zero_optimization:stage", cfg.common.zero) ds_config["zero_optimization"]["stage"] = zero_stage self.zero_stage = int(zero_stage) ds_config["zero_optimization"]["contiguous_gradients"] = False assert cfg.common.zero != 3, "zero stage 3 is currently untested with this codebase" self.ds_config = ds_config print(f"****** fairseq generated ds-config: {self.ds_config}") # catalog shared parameters shared_params = _catalog_shared_params(model) self.tpu = cfg.common.tpu assert not self.tpu, "deepspeed does not support tpu" self.cuda = torch.cuda.is_available() assert self.cuda, "deepspeed assumes cuda devices are available" self.device = torch.device("cuda") self._criterion = criterion self._model = model # assert self.cfg.common.fp16, "only fp16 is supported" assert self.cfg.distributed_training.ddp_backend in ["c10d", "legacy_ddp", "no_c10d"] assert cfg.distributed_training.ddp_backend != "fully_sharded" assert not cfg.common.bf16, "bf16 not yet supported" assert not cfg.distributed_training.pipeline_model_parallel, "pipeline not yet supported" assert not self.cfg.optimization.use_bmuf, "bmuf not yet supported" assert self.cfg.distributed_training.zero_sharding != "os" assert not self.cfg.common.memory_efficient_fp16, "mem efficient fp16 not yet supported" assert self.cfg.distributed_training.ddp_backend != "slow_mo" self.pipeline_model_parallel = cfg.distributed_training.pipeline_model_parallel self.zero_enabled = False if cfg.common.fp16: self._criterion = self._criterion.half() self._model = self._model.half() assert not utils.has_parameters(self._criterion), "criterion has params, not supported yet" # check that shared parameters are preserved after device transfer for shared_param in shared_params: ref = _get_module_by_path(self._model, shared_param[0]) for path in shared_param[1:]: logger.info( "detected shared parameter: {} <- {}".format(shared_param[0], path) ) _set_module_by_path(self._model, path, ref) self._dummy_batch = None # indicates we don't have a dummy batch at first self._lr_scheduler = None self._num_updates = 0 self._num_xla_compiles = 0 # for TPUs self._optim_history = None self._optimizer = None self._warn_once = set() self._wrapped_criterion = None self._wrapped_model = None self.train_step_count = 0 if self.cuda and self.data_parallel_world_size > 1: self._grad_norm_buf = torch.cuda.DoubleTensor(self.data_parallel_world_size) else: self._grad_norm_buf = None self.quantizer = None # get detailed cuda environment if self.cuda: self.cuda_env = utils.CudaEnvironment() if self.data_parallel_world_size > 1: self.cuda_env_arr = distributed_utils.all_gather_list( self.cuda_env, group=distributed_utils.get_global_group() ) else: self.cuda_env_arr = [self.cuda_env] if self.data_parallel_rank == 0: utils.CudaEnvironment.pretty_print_cuda_env_list(self.cuda_env_arr) else: self.cuda_env = None self.cuda_env_arr = None metrics.log_start_time("wall", priority=790, round=2) self._start_time = time.time() self._previous_training_time = 0 self._cumulative_training_time = None self._build_optimizer() def _build_optimizer(self): params = list( filter( lambda p: p.requires_grad, chain(self.model.parameters(), self.criterion.parameters()), ) ) # create simple optimizer, DS will handle fp16 wrappers optimizer = optim.build_optimizer(self.cfg.optimizer, params) print(f"************ built fairseq optimizer: {optimizer}") os.environ['LOCAL_RANK'] = str(self.cfg.distributed_training.device_id) os.environ['OMPI_COMM_WORLD_LOCAL_RANK'] = str(self.cfg.distributed_training.device_id) self.device = torch.device("cuda", self.cfg.distributed_training.device_id) self.model.to(device=self.device) torch.distributed.barrier() print(f'done pre-engine rank={dist.get_rank()}') engine, optimizer, _, _ = deepspeed.initialize( model=self.model, optimizer=optimizer, config_params=self.ds_config ) self.zero_enabled = engine.zero_optimization_stage() > 0 # We should initialize the learning rate scheduler immediately after # building the optimizer, so that the initial learning rate is set. self._lr_scheduler = lr_scheduler.build_lr_scheduler( self.cfg.lr_scheduler, engine.optimizer, ) self._lr_scheduler.step_update(0) self._optimizer = optimizer self._wrapped_model = engine self.device = engine.device self._criterion.to(device=self.device) print(f"local_rank={torch.distributed.get_rank()}, engine.device={engine.device}") #, engine.module.device={engine.module.device}") torch.distributed.barrier() if getattr(self.cfg.common, "fp16_scale_window", None) is None: if len(self.cfg.optimization.update_freq) > 1: raise ValueError( "--fp16-scale-window must be given explicitly when using a " "custom --update-freq schedule" ) data_parallel_size = int( self.cfg.distributed_training.distributed_world_size / self.cfg.common.model_parallel_size ) scale_window = int( 2 ** 14 / data_parallel_size / self.cfg.optimization.update_freq[0] ) else: scale_window = self.cfg.common.fp16_scale_window self.scaler = DynamicLossScaler( init_scale=self.cfg.common.fp16_init_scale, scale_window=scale_window, tolerance=self.cfg.common.fp16_scale_tolerance, threshold=self.cfg.common.threshold_loss_scale, min_loss_scale=self.cfg.common.min_loss_scale, ) @metrics.aggregate("train") def train_step(self, samples, raise_oom=False): """Do forward, backward and parameter update.""" self._set_seed() self.model.train() self.criterion.train() self.zero_grad() metrics.log_start_time("train_wall", priority=800, round=2) grad_norm = torch.tensor(0.0).cuda() # let fairseq handle grad accumlation scaling self.model.scale_wrt_gas = False # forward and backward pass logging_outputs, sample_size, ooms = [], 0, 0 # print(f'** samples size: {len(samples)}') sample_count = len(samples) for i, sample in enumerate(samples): # delayed update loop sample, is_dummy_batch = self._prepare_sample(sample) if self.cfg.common.fp16: self.model.optimizer.override_loss_scale(self.scaler.loss_scale) self.model.set_gradient_accumulation_boundary(is_boundary=False) try: # forward and backward # print(f'i={i}, rank={dist.get_rank()}, pre task.train_step') dist.barrier() loss, sample_size_i, logging_output = self.task.train_step( sample=sample, model=self.model, criterion=self.criterion, optimizer=self.optimizer, update_num=self.get_num_updates(), ignore_grad=is_dummy_batch, ) self.train_step_count += 1 # increment deepspeed micro step on non-final train step since optimizer.step will increment it for us if (i + 1) != sample_count: self.model.micro_steps += 1 self.model.set_gradient_accumulation_boundary(is_boundary=True) if self.zero_stage <= 2: self.model.allreduce_gradients() # print(f'grads[0]={list([p.grad for p in self.model.optimizer.fp16_groups[0]])}') # print(f'train_step={self.train_step_count}, loss_scale={self.model.optimizer.cur_scale}') # print(f'i={i}, rank={dist.get_rank()}, loss={loss}') if torch.distributed.get_rank() == 0: _loss_scale = self.model.optimizer.external_loss_scale if self.cfg.common.fp16 else 0 print(f"[{torch.distributed.get_rank()}], " \ f"micro_step={self.model.micro_steps}, " \ f"gas_boundary={self.model.is_gradient_accumulation_boundary()}, " \ f"train_step={self.train_step_count}, " \ f"lr={self.get_lr()}, " \ f"loss_scale={_loss_scale}, " \ f"loss={loss}") if self.cfg.common.exit_interval and self.train_step_count % self.cfg.common.exit_interval == 0: if torch.distributed.get_rank() == 0: print("exiting early...") sys.exit() logging_outputs.append(logging_output) sample_size += sample_size_i # emptying the CUDA cache after the first step can # reduce the chance of OOM if self.cuda and self.get_num_updates() == 0: torch.cuda.empty_cache() except RuntimeError as e: if "out of memory" in str(e): self._log_oom(e) if raise_oom: raise e logger.warning( "attempting to recover from OOM in forward/backward pass" ) ooms += 1 self.zero_grad() if self.cuda: torch.cuda.empty_cache() if self.cfg.distributed_training.distributed_world_size == 1: return None else: raise e if is_dummy_batch: if torch.is_tensor(sample_size): sample_size.zero_() else: sample_size *= 0.0 if torch.is_tensor(sample_size): sample_size = sample_size.float() else: sample_size = float(sample_size) # gather logging outputs from all replicas if self.data_parallel_world_size > 1: train_time = self._local_cumulative_training_time() logging_outputs, ( sample_size, ooms, total_train_time, ) = self._aggregate_logging_outputs( logging_outputs, sample_size, ooms, train_time, ignore=is_dummy_batch ) self._cumulative_training_time = ( total_train_time / self.data_parallel_world_size ) # grad_norms = [] # for param in self.optimizer.fp16_params: # if param.grad is not None: # grad_norms.append(param.grad.norm()) # print(grad_norms) # self.model.optimizer.dump_grad_norms() ###################### ### multiply grads ### ###################### # numer = ( # self.data_parallel_world_size # if not self.cfg.optimization.use_bmuf or self._sync_stats() # else 1 # ) # self.optimizer.multiply_grads(numer / (sample_size or 1.0)) # grad_norm = self.optimizer.clip_grad_norm(self.cfg.optimization.clip_norm) overflow = False try: with torch.autograd.profiler.record_function("optimizer"): # take an optimization step self.task.optimizer_step( self.optimizer, model=self.model, update_num=self.get_num_updates() ) # pass overflow flag from ds to fairseq if self.cfg.common.fp16: overflow = self.model.optimizer.overflow self.scaler.check_overflow(overflow=overflow) self.scaler.update() _grad_norm = self.model.get_global_grad_norm() if _grad_norm is not None: grad_norm = torch.tensor(_grad_norm).to(self.device) except FloatingPointError: raise except OverflowError as e: logger.info(f"NOTE: gradient overflow detected, ignoring gradient, {str(e)}") overflow = True # import sys; sys.exit() # except RuntimeError as e: # raise e # except FloatingPointError: # # re-run the forward and backward pass with hooks attached to print # # out where it fails # self.zero_grad() # with NanDetector(self.get_model()): # for _, sample in enumerate(samples): # sample, _ = self._prepare_sample(sample) # self.task.train_step( # sample, # self.model, # self.criterion, # self.optimizer, # self.get_num_updates(), # ignore_grad=False, # ) # raise # except OverflowError as e: # overflow = True # logger.info(f"NOTE: gradient overflow detected, ignoring gradient, {str(e)}") # grad_norm = torch.tensor(0.0).cuda() # self.zero_grad() # except RuntimeError as e: # if "out of memory" in str(e): # self._log_oom(e) # logger.error("OOM during optimization, irrecoverable") # raise e # Some distributed wrappers (e.g., SlowMo) need access to the optimizer # after the step # if hasattr(self.model, "perform_additional_optimizer_actions"): # if hasattr(self.optimizer, "fp32_params"): # self.model.perform_additional_optimizer_actions( # self.optimizer.optimizer, self.optimizer.fp32_params # ) # else: # self.model.perform_additional_optimizer_actions( # self.optimizer.optimizer # ) logging_output = None if not overflow or self.cfg.distributed_training.ddp_backend == "slow_mo": self.set_num_updates(self.get_num_updates() + 1) if self.cuda and self.cuda_env is not None: # log minimum free memory over the iteration gb_used = torch.cuda.max_memory_allocated() / 1024 / 1024 / 1024 torch.cuda.reset_peak_memory_stats() gb_free = self.cuda_env.total_memory_in_GB - gb_used metrics.log_scalar( "gb_free", gb_free, priority=1500, round=1, weight=0 ) # log stats logging_output = self._reduce_and_log_stats( logging_outputs, sample_size, grad_norm ) # clear CUDA cache to reduce memory fragmentation # if ( # self.cuda # and self.cfg.common.empty_cache_freq > 0 # and ( # (self.get_num_updates() + self.cfg.common.empty_cache_freq - 1) # % self.cfg.common.empty_cache_freq # ) # == 0 # ): # torch.cuda.empty_cache() if self.cfg.common.fp16: metrics.log_scalar( "loss_scale", # self.optimizer.loss_scaler.loss_scaler, self.optimizer.cur_scale, priority=700, round=4, weight=0, ) metrics.log_stop_time("train_wall") return logging_output def _sync_stats(): if self.data_parallel_world_size == 1: return False else: return True def set_num_updates(self, num_updates): """Set the number of parameters updates.""" self._num_updates = num_updates self.lr_step_update() metrics.log_scalar("num_updates", self._num_updates, weight=0, priority=200) def _aggregate_logging_outputs( self, logging_outputs: List[Dict[str, Any]], *extra_stats_to_sum, ignore=False, ): if self.task.__class__.logging_outputs_can_be_summed(self.get_criterion()): return self._fast_stat_sync_sum( logging_outputs, *extra_stats_to_sum, ignore=ignore ) else: return self._all_gather_list_sync( logging_outputs, *extra_stats_to_sum, ignore=ignore ) def _fast_stat_sync_sum( self, logging_outputs: List[Dict[str, Any]], *extra_stats_to_sum, ignore=False, ): """ Sync logging outputs across workers. fast_stat_sync_sum is faster than all_gather_list_sync, but is only suitable when logging outputs are scalars and can be summed. Note that *logging_outputs* cannot contain any nested dicts/lists. """ data = {} for i, stat in enumerate(extra_stats_to_sum): data["extra_stats_" + str(i)] = stat if len(logging_outputs) > 0: log_keys = list(logging_outputs[0].keys()) for k in log_keys: if not ignore: v = sum(log[k] for log in logging_outputs if k in log) else: v = logging_outputs[0][k] v = torch.zeros_like(v) if torch.is_tensor(v) else 0 data["logging_outputs_" + k] = v else: log_keys = None data = distributed_utils.all_reduce_dict( data, device=self.device, group=self.data_parallel_process_group ) extra_stats_to_sum = [ data["extra_stats_" + str(i)] for i in range(len(extra_stats_to_sum)) ] if log_keys is not None: logging_outputs = [{k: data["logging_outputs_" + k] for k in log_keys}] else: logging_outputs = [] return logging_outputs, extra_stats_to_sum def _all_gather_list_sync( self, logging_outputs: List[Dict[str, Any]], *extra_stats_to_sum, ignore=False, ): """ Sync logging outputs across workers. all_gather_list_sync is suitable when logging outputs are complex types. """ if self.tpu: raise NotImplementedError if ignore: logging_outputs = [] results = list( zip( *distributed_utils.all_gather_list( [logging_outputs] + list(extra_stats_to_sum), max_size=getattr(self.cfg.common, "all_gather_list_size", 16384), group=self.data_parallel_process_group, ) ) ) logging_outputs, extra_stats_to_sum = results[0], results[1:] logging_outputs = list(chain.from_iterable(logging_outputs)) extra_stats_to_sum = [sum(s) for s in extra_stats_to_sum] return logging_outputs, extra_stats_to_sum def _reduce_and_log_stats(self, logging_outputs, sample_size, grad_norm=None): if grad_norm is not None and ( not torch.is_tensor(grad_norm) or torch.isfinite(grad_norm) ): metrics.log_speed("ups", 1.0, priority=100, round=2) metrics.log_scalar("gnorm", grad_norm, priority=400, round=3) if self.cfg.optimization.clip_norm > 0: metrics.log_scalar( "clip", torch.where( grad_norm > self.cfg.optimization.clip_norm, grad_norm.new_tensor(100), grad_norm.new_tensor(0), ), priority=500, round=1, ) with metrics.aggregate() as agg: if logging_outputs is not None: self.task.reduce_metrics(logging_outputs, self.get_criterion()) del logging_outputs # extra warning for criterions that don't properly log a loss value if "loss" not in agg: if "loss" not in self._warn_once: self._warn_once.add("loss") logger.warning( "Criterion.reduce_metrics did not log a 'loss' value, " "which may break some functionality" ) metrics.log_scalar("loss", -1) # support legacy interface if self.tpu: logging_output = {} else: logging_output = agg.get_smoothed_values() logging_output["sample_size"] = sample_size for key_to_delete in ["ppl", "wps", "wpb", "bsz"]: if key_to_delete in logging_output: del logging_output[key_to_delete] return logging_output def cumulative_training_time(self): if self._cumulative_training_time is None: # single GPU return self._local_cumulative_training_time() else: return self._cumulative_training_time def _local_cumulative_training_time(self): """Aggregate training time in seconds.""" return time.time() - self._start_time + self._previous_training_time def _prepare_sample(self, sample, is_dummy=False): #DS: untouched if sample == "DUMMY": raise Exception( "Trying to use an uninitialized 'dummy' batch. This usually indicates " "that the total number of batches is smaller than the number of " "participating GPUs. Try reducing the batch size or using fewer GPUs." ) if sample is None or len(sample) == 0: assert ( self._dummy_batch is not None and len(self._dummy_batch) > 0 ), "Invalid dummy batch: {}".format(self._dummy_batch) sample, _ = self._prepare_sample(self._dummy_batch, is_dummy=True) return sample, True if self.cuda: if self.pipeline_model_parallel: if "target" in sample: sample["target"] = utils.move_to_cuda( sample["target"], device=self.last_device ) else: sample = utils.move_to_cuda(sample) elif self.tpu and is_dummy: # the dummy batch may not be on the appropriate device sample = utils.move_to_cuda(sample, device=self.device) def apply_half(t): if t.dtype is torch.float32: return t.half() return t def apply_bfloat16(t): if t.dtype is torch.float32: return t.to(dtype=torch.bfloat16) return t if self.cfg.common.fp16: sample = utils.apply_to_sample(apply_half, sample) if self.cfg.common.bf16: sample = utils.apply_to_sample(apply_bfloat16, sample) if self._dummy_batch == "DUMMY": self._dummy_batch = sample return sample, False def lr_step(self, epoch, val_loss=None): """Adjust the learning rate at the end of the epoch.""" self.lr_scheduler.step(epoch, val_loss) # prefer updating the LR based on the number of steps return self.lr_step_update() def get_lr(self): """Get the current learning rate.""" return self.model.optimizer.get_lr() def get_num_updates(self): """Get the number of parameters updates.""" return self._num_updates def lr_step_update(self): """Update the learning rate after each update.""" new_lr = self.lr_scheduler.step_update(self.get_num_updates()) if isinstance(new_lr, dict): for k, v in new_lr.items(): metrics.log_scalar(f"lr_{k}", v, weight=0, priority=300) new_lr = new_lr.get("default", next(iter(new_lr.values()))) else: metrics.log_scalar("lr", new_lr, weight=0, priority=300) return new_lr def begin_epoch(self, epoch): """Called at the beginning of each epoch.""" logger.info("begin training epoch {}".format(epoch)) self.lr_step_begin_epoch(epoch) # task specific setup per epoch self.task.begin_epoch(epoch, self.get_model()) def lr_step_begin_epoch(self, epoch): """Adjust the learning rate at the beginning of the epoch.""" self.lr_scheduler.step_begin_epoch(epoch) # prefer updating the LR based on the number of steps return self.lr_step_update() def get_model(self): """Get the (non-wrapped) model instance.""" return self._model def get_criterion(self): """Get the (non-wrapped) criterion instance.""" return self._criterion def _set_seed(self): # Set seed based on args.seed and the update number so that we get # reproducible results when resuming from checkpoints seed = self.cfg.common.seed + self.get_num_updates() utils.set_torch_seed(seed) def consolidate_optimizer(self): """ DeepSpeed doesn't require any optimizer consolidation. """ return False @metrics.aggregate("valid") def valid_step(self, sample, raise_oom=False): """Do forward pass in evaluation mode.""" with torch.no_grad(): self.model.eval() self.criterion.eval() sample, is_dummy_batch = self._prepare_sample(sample) try: _loss, sample_size, logging_output = self.task.valid_step( sample, self.model, self.criterion ) except RuntimeError as e: if "out of memory" in str(e): self._log_oom(e) if not raise_oom: logger.warning( "ran out of memory in validation step, retrying batch" ) for p in self.model.parameters(): if p.grad is not None: p.grad = None # free some memory if self.cuda: torch.cuda.empty_cache() return self.valid_step(sample, raise_oom=True) raise e logging_outputs = [logging_output] if is_dummy_batch: if torch.is_tensor(sample_size): sample_size.zero_() else: sample_size *= 0.0 # gather logging outputs from all replicas if self.data_parallel_world_size > 1: logging_outputs, (sample_size,) = self._aggregate_logging_outputs( logging_outputs, sample_size, ignore=is_dummy_batch, ) # log validation stats if self.tpu: logging_outputs = self._xla_markstep_and_send_to_cpu(logging_outputs) # don't reduce here, otherwise the metric is wrong # logging_output = self._reduce_and_log_stats(logging_outputs, sample_size) return logging_outputs def begin_valid_epoch(self, epoch): """Called at the beginning of each validation epoch.""" # task specific setup per validation epoch self.task.begin_valid_epoch(epoch, self.get_model()) def get_valid_iterator( self, subset, disable_iterator_cache=False, ): """Return an EpochBatchIterator over given validation subset for a given epoch.""" batch_iterator = self.task.get_batch_iterator( dataset=self.task.dataset(subset), max_tokens=self.cfg.dataset.max_tokens_valid, max_sentences=self.cfg.dataset.batch_size_valid, max_positions=utils.resolve_max_positions( self.task.max_positions(), self.model.max_positions(), ), ignore_invalid_inputs=self.cfg.dataset.skip_invalid_size_inputs_valid_test, required_batch_size_multiple=self.cfg.dataset.required_batch_size_multiple, seed=self.cfg.common.seed, num_shards=self.data_parallel_world_size, shard_id=self.data_parallel_rank, num_workers=self.cfg.dataset.num_workers, # always pass a fixed "epoch" to keep validation data consistent # across training epochs epoch=1, data_buffer_size=self.cfg.dataset.data_buffer_size, disable_iterator_cache=disable_iterator_cache, ) self.reset_dummy_batch(batch_iterator.first_batch) return batch_iterator def load_checkpoint( self, filename, reset_optimizer=False, reset_lr_scheduler=False, optimizer_overrides=None, reset_meters=False, ): """ Load all training state from a checkpoint file. rank = 0 will load the checkpoint, and then broadcast it to all other ranks. """ extra_state, self._optim_history, last_optim_state = None, [], None logger.info(f"Preparing to load checkpoint {filename}") bexists = os.path.isdir(filename) #PathManager.isfile(filename) if not bexists: logger.info("No existing checkpoint found {}".format(filename)) return None def load_model(src, dst): if torch.distributed.get_rank() == 0: print(self.cfg.model) dst.load_state_dict(src, strict=False, model_cfg=self.cfg.model) load_path, client_states = self.model.load_checkpoint(load_dir=filename, load_optimizer_states=not reset_optimizer, model_f=load_model) print(f'[{torch.distributed.get_rank()}] ckpt client states={client_states}') assert not utils.has_parameters(self.get_criterion()), "criterion w. params not supported yet" extra_state = client_states["extra_state"] if not reset_optimizer and not reset_lr_scheduler: self.lr_scheduler.load_state_dict(client_states["lr_scheduler_state"]) self.set_num_updates(client_states["num_updates"]) self.scaler.loss_scale = client_states["loss_scale"] if extra_state is not None: itr_state = extra_state["train_iterator"] epoch = itr_state.get("epoch", 1) if "previous_training_time" in extra_state: self._previous_training_time = extra_state["previous_training_time"] self._start_time = time.time() self.lr_step(epoch) if itr_state.get("version", 1) >= 2 and itr_state["iterations_in_epoch"] == 0: # reset meters at start of epoch reset_meters = True if "metrics" in extra_state and not reset_meters: metrics.load_state_dict(extra_state["metrics"]) # reset TimeMeters, since their start times don't make sense anymore for meter in metrics.get_meters("default"): if isinstance(meter, meters.TimeMeter): meter.reset() logger.info( "Loaded checkpoint {} (epoch {} @ {} updates)".format( filename, epoch, self.get_num_updates() ) ) return extra_state def get_train_iterator( self, epoch, combine=True, load_dataset=True, data_selector=None, shard_batch_itr=True, disable_iterator_cache=False, ): """Return an EpochBatchIterator over the training set for a given epoch.""" if load_dataset: logger.info("loading train data for epoch {}".format(epoch)) self.task.load_dataset( self.cfg.dataset.train_subset, epoch=epoch, combine=combine, data_selector=data_selector, tpu=self.tpu, ) seed = self.cfg.common.seed if hasattr(self.cfg, 'infinibatch_dataloader'): print("| If using infinibatch, do reset_dataloader!", flush=True) assert self.cfg.reset_dataloader seed += self.get_num_updates() print("| Set seed {}={}+{}".format(seed, self.cfg.common.seed, self.get_num_updates()), flush=True) batch_iterator = self.task.get_batch_iterator( dataset=self.task.dataset(self.cfg.dataset.train_subset), max_tokens=self.cfg.dataset.max_tokens, max_sentences=self.cfg.dataset.batch_size, max_positions=utils.resolve_max_positions( self.task.max_positions(), self.model.max_positions(), self.cfg.dataset.max_tokens, ), ignore_invalid_inputs=True, required_batch_size_multiple=self.cfg.dataset.required_batch_size_multiple, seed=seed, num_shards=self.data_parallel_world_size if shard_batch_itr else 1, shard_id=self.data_parallel_rank if shard_batch_itr else 0, num_workers=self.cfg.dataset.num_workers, epoch=epoch, data_buffer_size=self.cfg.dataset.data_buffer_size, disable_iterator_cache=disable_iterator_cache, ) self.reset_dummy_batch(batch_iterator.first_batch) return batch_iterator def reset_dummy_batch(self, batch): self._dummy_batch = batch def zero_grad(self): self.optimizer.zero_grad() @property def model(self): if self._wrapped_model is None: self._wrapped_model = self._model return self._wrapped_model @property def criterion(self): if self._wrapped_criterion is None: self._wrapped_criterion = self._criterion return self._wrapped_criterion @property def data_parallel_world_size(self): if self.cfg.distributed_training.distributed_world_size == 1: return 1 return distributed_utils.get_data_parallel_world_size() @property def lr_scheduler(self): if self._lr_scheduler is None: self._build_optimizer() # this will initialize self._lr_scheduler return self._lr_scheduler @property def optimizer(self): if self._optimizer is None: self._build_optimizer() return self._optimizer @property def data_parallel_process_group(self): return distributed_utils.get_data_parallel_group() @property def data_parallel_rank(self): if self.cfg.distributed_training.distributed_world_size == 1: return 0 return distributed_utils.get_data_parallel_rank() @property def is_data_parallel_master(self): # NOTE: this returns true for all model parallel replicas with data # parallel rank 0 return self.data_parallel_rank == 0 @property def checkpoint_suffix(self) -> str: """Suffix to add to the checkpoint file name.""" # if self.cfg.distributed_training.ddp_backend == "fully_sharded": # return self.cfg.checkpoint.checkpoint_suffix + "-shard{0}".format(self.data_parallel_rank) # else: return self.cfg.checkpoint.checkpoint_suffix or "" @property def should_save_checkpoint_on_current_rank(self) -> bool: """Indicates whether to save checkpoints on the current DDP rank.""" if self.zero_enabled: return True else: return self.is_data_parallel_master def save_checkpoint(self, filename, extra_state): """Save all training state in a checkpoint file.""" logger.info(f"Saving checkpoint to {filename}") state_dict = self.state_dict(exclude_model_opt=True) state_dict["extra_state"].update(extra_state) self.model.save_checkpoint(save_dir=filename, client_state=state_dict) logger.info(f"Finished saving checkpoint to {filename}") def state_dict(self, exclude_model_opt=False): state_dict = { "args": None, # legacy "cfg": ( OmegaConf.to_container(self.cfg) if OmegaConf.is_config(self.cfg) else self.cfg ), "model": None if exclude_model_opt else self.model.state_dict(), "criterion": ( self.criterion.state_dict() if utils.has_parameters(self.criterion) else None ), "optimizer_history": (self._optim_history or []) + [ { "criterion_name": self.get_criterion().__class__.__name__, "optimizer_name": self.optimizer.__class__.__name__, "lr_scheduler_state": self.lr_scheduler.state_dict(), "num_updates": self.get_num_updates(), } ], "lr_scheduler_state": self.lr_scheduler.state_dict(), "num_updates": self.get_num_updates(), "task_state": self.task.state_dict() if self.task is not None else {}, "loss_scale": self.scaler.loss_scale, "extra_state": { "metrics": metrics.state_dict(), "previous_training_time": self.cumulative_training_time(), } } if exclude_model_opt and not self.cfg.checkpoint.no_save_optimizer_state: state_dict["last_optimizer_state"] = self.optimizer.state_dict() return state_dict def _catalog_shared_params(module, memo=None, prefix=""): if memo is None: first_call = True memo = {} else: first_call = False for name, param in module._parameters.items(): param_prefix = prefix + ("." if prefix else "") + name if param not in memo: memo[param] = [] memo[param].append(param_prefix) for name, m in module._modules.items(): if m is None: continue submodule_prefix = prefix + ("." if prefix else "") + name _catalog_shared_params(m, memo, submodule_prefix) if first_call: return [x for x in memo.values() if len(x) > 1] def _get_module_by_path(module, path): path = path.split(".") for name in path: module = getattr(module, name) return module def _set_module_by_path(module, path, value): path = path.split(".") for name in path[:-1]: module = getattr(module, name) setattr(module, path[-1], value)
KosmosX-API-main
kosmosX/fairseq/fairseq/ds_trainer.py
# 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 multiprocessing import os import pdb import sys __all__ = ["set_trace"] _stdin = [None] _stdin_lock = multiprocessing.Lock() try: _stdin_fd = sys.stdin.fileno() except Exception: _stdin_fd = None class MultiprocessingPdb(pdb.Pdb): """A Pdb wrapper that works in a multiprocessing environment. Usage: `from fairseq import pdb; pdb.set_trace()` """ def __init__(self): pdb.Pdb.__init__(self, nosigint=True) def _cmdloop(self): stdin_bak = sys.stdin with _stdin_lock: try: if _stdin_fd is not None: if not _stdin[0]: _stdin[0] = os.fdopen(_stdin_fd) sys.stdin = _stdin[0] self.cmdloop() finally: sys.stdin = stdin_bak def set_trace(): pdb = MultiprocessingPdb() pdb.set_trace(sys._getframe().f_back)
KosmosX-API-main
kosmosX/fairseq/fairseq/pdb.py
# 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 re SPACE_NORMALIZER = re.compile(r"\s+") def tokenize_line(line): line = SPACE_NORMALIZER.sub(" ", line) line = line.strip() return line.split()
KosmosX-API-main
kosmosX/fairseq/fairseq/tokenizer.py
#!/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. import argparse import copy import logging import os from typing import Any, Dict, Iterator, List import torch from omegaconf import open_dict from torch import nn from fairseq import utils from fairseq.data import encoders logger = logging.getLogger(__name__) def from_pretrained( model_name_or_path, checkpoint_file="model.pt", data_name_or_path=".", archive_map=None, **kwargs ): from fairseq import checkpoint_utils, file_utils if archive_map is not None: if model_name_or_path in archive_map: model_name_or_path = archive_map[model_name_or_path] if data_name_or_path is not None and data_name_or_path in archive_map: data_name_or_path = archive_map[data_name_or_path] # allow archive_map to set default arg_overrides (e.g., tokenizer, bpe) # for each model if isinstance(model_name_or_path, dict): for k, v in model_name_or_path.items(): if k == "checkpoint_file": checkpoint_file = v elif ( k != "path" # only set kwargs that don't already have overrides and k not in kwargs ): kwargs[k] = v model_name_or_path = model_name_or_path["path"] model_path = file_utils.load_archive_file(model_name_or_path) # convenience hack for loading data and BPE codes from model archive if data_name_or_path.startswith("."): kwargs["data"] = os.path.abspath(os.path.join(model_path, data_name_or_path)) else: kwargs["data"] = file_utils.load_archive_file(data_name_or_path) for file, arg in { "code": "bpe_codes", "bpecodes": "bpe_codes", "sentencepiece.bpe.model": "sentencepiece_model", "merges.txt": "bpe_merges", "vocab.json": "bpe_vocab", }.items(): path = os.path.join(model_path, file) if os.path.exists(path): kwargs[arg] = path if "user_dir" in kwargs: utils.import_user_module(argparse.Namespace(user_dir=kwargs["user_dir"])) models, args, task = checkpoint_utils.load_model_ensemble_and_task( [os.path.join(model_path, cpt) for cpt in checkpoint_file.split(os.pathsep)], arg_overrides=kwargs, ) return { "args": args, "task": task, "models": models, } class GeneratorHubInterface(nn.Module): """ PyTorch Hub interface for generating sequences from a pre-trained translation or language model. """ def __init__(self, cfg, task, models): super().__init__() self.cfg = cfg self.task = task self.models = nn.ModuleList(models) self.src_dict = task.source_dictionary self.tgt_dict = task.target_dictionary # optimize model for generation for model in self.models: model.prepare_for_inference_(cfg) # Load alignment dictionary for unknown word replacement # (None if no unknown word replacement, empty if no path to align dictionary) self.align_dict = utils.load_align_dict(cfg.generation.replace_unk) self.tokenizer = encoders.build_tokenizer(cfg.tokenizer) self.bpe = encoders.build_bpe(cfg.bpe) self.max_positions = utils.resolve_max_positions( self.task.max_positions(), *[model.max_positions() for model in models] ) # this is useful for determining the device self.register_buffer("_float_tensor", torch.tensor([0], dtype=torch.float)) @property def device(self): return self._float_tensor.device def translate( self, sentences: List[str], beam: int = 5, verbose: bool = False, **kwargs ) -> List[str]: return self.sample(sentences, beam, verbose, **kwargs) def sample( self, sentences: List[str], beam: int = 1, verbose: bool = False, **kwargs ) -> List[str]: if isinstance(sentences, str): return self.sample([sentences], beam=beam, verbose=verbose, **kwargs)[0] tokenized_sentences = [self.encode(sentence) for sentence in sentences] batched_hypos = self.generate(tokenized_sentences, beam, verbose, **kwargs) return [self.decode(hypos[0]["tokens"]) for hypos in batched_hypos] def score( self, sentences: List[str], replace_newline_with_eos: bool = False, **kwargs ): if isinstance(sentences, str): return self.score( [sentences], replace_newline_with_eos=replace_newline_with_eos, **kwargs )[0] def encode(sentence): if replace_newline_with_eos: return torch.cat([self.encode(line) for line in sentence.splitlines()]) else: return self.encode(sentence) # NOTE: this doesn't support translation tasks currently tokenized_sentences = [encode(sentence) for sentence in sentences] return [ hypos[0] for hypos in self.generate( tokenized_sentences, score_reference=True, **kwargs ) ] def generate( self, tokenized_sentences: List[torch.LongTensor], beam: int = 5, verbose: bool = False, skip_invalid_size_inputs=False, inference_step_args=None, prefix_allowed_tokens_fn=None, **kwargs ) -> List[List[Dict[str, torch.Tensor]]]: if torch.is_tensor(tokenized_sentences) and tokenized_sentences.dim() == 1: return self.generate( tokenized_sentences.unsqueeze(0), beam=beam, verbose=verbose, **kwargs )[0] # build generator using current args as well as any kwargs gen_args = copy.deepcopy(self.cfg.generation) with open_dict(gen_args): gen_args.beam = beam for k, v in kwargs.items(): setattr(gen_args, k, v) generator = self.task.build_generator( self.models, gen_args, prefix_allowed_tokens_fn=prefix_allowed_tokens_fn, ) inference_step_args = inference_step_args or {} results = [] for batch in self._build_batches(tokenized_sentences, skip_invalid_size_inputs): batch = utils.apply_to_sample(lambda t: t.to(self.device), batch) translations = self.task.inference_step( generator, self.models, batch, **inference_step_args ) for id, hypos in zip(batch["id"].tolist(), translations): results.append((id, hypos)) # sort output to match input order outputs = [hypos for _, hypos in sorted(results, key=lambda x: x[0])] if verbose: def getarg(name, default): return getattr(gen_args, name, getattr(self.cfg, name, default)) for source_tokens, target_hypotheses in zip(tokenized_sentences, outputs): src_str_with_unk = self.string(source_tokens) logger.info("S\t{}".format(src_str_with_unk)) for hypo in target_hypotheses: hypo_str = self.decode(hypo["tokens"]) logger.info("H\t{}\t{}".format(hypo["score"], hypo_str)) logger.info( "P\t{}".format( " ".join( map( lambda x: "{:.4f}".format(x), hypo["positional_scores"].tolist(), ) ) ) ) if hypo["alignment"] is not None and getarg( "print_alignment", False ): logger.info( "A\t{}".format( " ".join( [ "{}-{}".format(src_idx, tgt_idx) for src_idx, tgt_idx in hypo["alignment"] ] ) ) ) return outputs def encode(self, sentence: str) -> torch.LongTensor: sentence = self.tokenize(sentence) sentence = self.apply_bpe(sentence) return self.binarize(sentence) def decode(self, tokens: torch.LongTensor) -> str: sentence = self.string(tokens) sentence = self.remove_bpe(sentence) return self.detokenize(sentence) def tokenize(self, sentence: str) -> str: if self.tokenizer is not None: sentence = self.tokenizer.encode(sentence) return sentence def detokenize(self, sentence: str) -> str: if self.tokenizer is not None: sentence = self.tokenizer.decode(sentence) return sentence def apply_bpe(self, sentence: str) -> str: if self.bpe is not None: sentence = self.bpe.encode(sentence) return sentence def remove_bpe(self, sentence: str) -> str: if self.bpe is not None: sentence = self.bpe.decode(sentence) return sentence def binarize(self, sentence: str) -> torch.LongTensor: return self.src_dict.encode_line(sentence, add_if_not_exist=False).long() def string(self, tokens: torch.LongTensor) -> str: return self.tgt_dict.string(tokens) def _build_batches( self, tokens: List[List[int]], skip_invalid_size_inputs: bool ) -> Iterator[Dict[str, Any]]: lengths = torch.LongTensor([t.numel() for t in tokens]) batch_iterator = self.task.get_batch_iterator( dataset=self.task.build_dataset_for_inference(tokens, lengths), max_tokens=self.cfg.dataset.max_tokens, max_sentences=self.cfg.dataset.batch_size, max_positions=self.max_positions, ignore_invalid_inputs=skip_invalid_size_inputs, disable_iterator_cache=True, ).next_epoch_itr(shuffle=False) return batch_iterator class BPEHubInterface(object): """PyTorch Hub interface for Byte-Pair Encoding (BPE).""" def __init__(self, bpe, **kwargs): super().__init__() args = argparse.Namespace(bpe=bpe, **kwargs) self.bpe = encoders.build_bpe(args) assert self.bpe is not None def encode(self, sentence: str) -> str: return self.bpe.encode(sentence) def decode(self, sentence: str) -> str: return self.bpe.decode(sentence) class TokenizerHubInterface(object): """PyTorch Hub interface for tokenization.""" def __init__(self, tokenizer, **kwargs): super().__init__() args = argparse.Namespace(tokenizer=tokenizer, **kwargs) self.tokenizer = encoders.build_tokenizer(args) assert self.tokenizer is not None def encode(self, sentence: str) -> str: return self.tokenizer.encode(sentence) def decode(self, sentence: str) -> str: return self.tokenizer.decode(sentence)
KosmosX-API-main
kosmosX/fairseq/fairseq/hub_utils.py
# 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 sys import torch from fairseq import utils class SequenceScorer(object): """Scores the target for a given source sentence.""" def __init__( self, tgt_dict, softmax_batch=None, compute_alignment=False, eos=None, symbols_to_strip_from_output=None, ): self.pad = tgt_dict.pad() self.eos = tgt_dict.eos() if eos is None else eos self.softmax_batch = softmax_batch or sys.maxsize assert self.softmax_batch > 0 self.compute_alignment = compute_alignment self.symbols_to_strip_from_output = ( symbols_to_strip_from_output.union({self.eos}) if symbols_to_strip_from_output is not None else {self.eos} ) @torch.no_grad() def generate(self, models, sample, **kwargs): """Score a batch of translations.""" net_input = sample["net_input"] def batch_for_softmax(dec_out, target): # assumes decoder_out[0] is the only thing needed (may not be correct for future models!) first, rest = dec_out[0], dec_out[1:] bsz, tsz, dim = first.shape if bsz * tsz < self.softmax_batch: yield dec_out, target, True else: flat = first.contiguous().view(1, -1, dim) flat_tgt = target.contiguous().view(flat.shape[:-1]) s = 0 while s < flat.size(1): e = s + self.softmax_batch yield (flat[:, s:e],) + rest, flat_tgt[:, s:e], False s = e def gather_target_probs(probs, target): probs = probs.gather( dim=2, index=target.unsqueeze(-1), ) return probs orig_target = sample["target"] # compute scores for each model in the ensemble avg_probs = None avg_attn = None for model in models: model.eval() decoder_out = model(**net_input) attn = decoder_out[1] if len(decoder_out) > 1 else None if type(attn) is dict: attn = attn.get("attn", None) batched = batch_for_softmax(decoder_out, orig_target) probs, idx = None, 0 for bd, tgt, is_single in batched: sample["target"] = tgt curr_prob = model.get_normalized_probs( bd, log_probs=len(models) == 1, sample=sample ).data if is_single: probs = gather_target_probs(curr_prob, orig_target) else: if probs is None: probs = curr_prob.new(orig_target.numel()) step = curr_prob.size(0) * curr_prob.size(1) end = step + idx tgt_probs = gather_target_probs( curr_prob.view(tgt.shape + (curr_prob.size(-1),)), tgt ) probs[idx:end] = tgt_probs.view(-1) idx = end sample["target"] = orig_target probs = probs.view(sample["target"].shape) if avg_probs is None: avg_probs = probs else: avg_probs.add_(probs) if attn is not None: if torch.is_tensor(attn): attn = attn.data else: attn = attn[0] if avg_attn is None: avg_attn = attn else: avg_attn.add_(attn) if len(models) > 1: avg_probs.div_(len(models)) avg_probs.log_() if avg_attn is not None: avg_attn.div_(len(models)) bsz = avg_probs.size(0) hypos = [] start_idxs = sample["start_indices"] if "start_indices" in sample else [0] * bsz for i in range(bsz): # remove padding from ref ref = ( utils.strip_pad(sample["target"][i, start_idxs[i] :], self.pad) if sample["target"] is not None else None ) tgt_len = ref.numel() avg_probs_i = avg_probs[i][start_idxs[i] : start_idxs[i] + tgt_len] score_i = avg_probs_i.sum() / tgt_len if avg_attn is not None: avg_attn_i = avg_attn[i] if self.compute_alignment: alignment = utils.extract_hard_alignment( avg_attn_i, sample["net_input"]["src_tokens"][i], sample["target"][i], self.pad, self.eos, ) else: alignment = None else: avg_attn_i = alignment = None hypos.append( [ { "tokens": ref, "score": score_i, "attention": avg_attn_i, "alignment": alignment, "positional_scores": avg_probs_i, } ] ) return hypos
KosmosX-API-main
kosmosX/fairseq/fairseq/sequence_scorer.py
# 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 uuid from typing import Dict, Optional from torch import Tensor class FairseqIncrementalState(object): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.init_incremental_state() def init_incremental_state(self): self._incremental_state_id = str(uuid.uuid4()) def _get_full_incremental_state_key(self, key: str) -> str: return "{}.{}".format(self._incremental_state_id, key) def get_incremental_state( self, incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]], key: str, ) -> Optional[Dict[str, Optional[Tensor]]]: """Helper for getting incremental state for an nn.Module.""" full_key = self._get_full_incremental_state_key(key) if incremental_state is None or full_key not in incremental_state: return None return incremental_state[full_key] def set_incremental_state( self, incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]], key: str, value: Dict[str, Optional[Tensor]], ) -> Optional[Dict[str, Dict[str, Optional[Tensor]]]]: """Helper for setting incremental state for an nn.Module.""" if incremental_state is not None: full_key = self._get_full_incremental_state_key(key) incremental_state[full_key] = value return incremental_state def with_incremental_state(cls): cls.__bases__ = (FairseqIncrementalState,) + tuple( b for b in cls.__bases__ if b != FairseqIncrementalState ) return cls
KosmosX-API-main
kosmosX/fairseq/fairseq/incremental_decoding_utils.py
# 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 contextlib import copy import importlib import logging import os import sys import warnings from itertools import accumulate from typing import Callable, Dict, List, Optional, TYPE_CHECKING import torch import torch.nn.functional as F from torch import Tensor import collections if TYPE_CHECKING: from fairseq.modules.multihead_attention import MultiheadAttention try: from amp_C import multi_tensor_l2norm multi_tensor_l2norm_available = True except ImportError: multi_tensor_l2norm_available = False try: import torch_xla.core.xla_model as xm except ImportError: xm = None logger = logging.getLogger(__name__) MANIFOLD_PATH_SEP = "|" class FileContentsAction(argparse.Action): def __init__(self, option_strings, dest, nargs=None, **kwargs): if nargs is not None: raise ValueError("nargs not allowed") super(FileContentsAction, self).__init__(option_strings, dest, **kwargs) def __call__(self, parser, namespace, values, option_string=None): from fairseq.file_io import PathManager if PathManager.isfile(values): with PathManager.open(values) as f: argument = f.read().strip() else: argument = values setattr(namespace, self.dest, argument) def split_paths(paths: str, separator=os.pathsep) -> List[str]: return ( paths.split(separator) if "://" not in paths else paths.split(MANIFOLD_PATH_SEP) ) def load_ensemble_for_inference(filenames, task, model_arg_overrides=None): from fairseq import checkpoint_utils deprecation_warning( "utils.load_ensemble_for_inference is deprecated. " "Please use checkpoint_utils.load_model_ensemble instead." ) return checkpoint_utils.load_model_ensemble( filenames, arg_overrides=model_arg_overrides, task=task ) def apply_to_sample(f, sample): if hasattr(sample, "__len__") and len(sample) == 0: return {} def _apply(x): if torch.is_tensor(x): return f(x) elif isinstance(x, collections.OrderedDict): # OrderedDict has attributes that needs to be preserved od = collections.OrderedDict( (key, _apply(value)) for key, value in x.items() ) od.__dict__ = x.__dict__ return od elif isinstance(x, dict): return {key: _apply(value) for key, value in x.items()} elif isinstance(x, list): return [_apply(x) for x in x] elif isinstance(x, tuple): return tuple(_apply(x) for x in x) elif isinstance(x, set): return {_apply(x) for x in x} else: return x return _apply(sample) def move_to_cuda(sample, device=None): device = device or torch.cuda.current_device() def _move_to_cuda(tensor): # non_blocking is ignored if tensor is not pinned, so we can always set # to True (see github.com/PyTorchLightning/pytorch-lightning/issues/620) return tensor.to(device=device, non_blocking=True) return apply_to_sample(_move_to_cuda, sample) def move_to_cpu(sample): def _move_to_cpu(tensor): # PyTorch has poor support for half tensors (float16) on CPU. # Move any such tensors to float32. if tensor.dtype in {torch.bfloat16, torch.float16}: tensor = tensor.to(dtype=torch.float32) return tensor.cpu() return apply_to_sample(_move_to_cpu, sample) def move_to_tpu(sample): import torch_xla.core.xla_model as xm device = xm.xla_device() def _move_to_tpu(tensor): return tensor.to(device) return apply_to_sample(_move_to_tpu, sample) def get_incremental_state( module: "MultiheadAttention", incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]], key: str, ) -> Optional[Dict[str, Optional[Tensor]]]: """Helper for getting incremental state for an nn.Module.""" return module.get_incremental_state(incremental_state, key) def set_incremental_state( module: "MultiheadAttention", incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]], key: str, value: Dict[str, Optional[Tensor]], ) -> Optional[Dict[str, Dict[str, Optional[Tensor]]]]: """Helper for setting incremental state for an nn.Module.""" if incremental_state is not None: result = module.set_incremental_state(incremental_state, key, value) if result is not None: incremental_state = result return incremental_state def load_align_dict(replace_unk): if replace_unk is None: align_dict = None elif isinstance(replace_unk, str) and len(replace_unk) > 0: # Load alignment dictionary for unknown word replacement if it was passed as an argument. align_dict = {} with open(replace_unk, "r") as f: for line in f: cols = line.split() align_dict[cols[0]] = cols[1] else: # No alignment dictionary provided but we still want to perform unknown word replacement by copying the # original source word. align_dict = {} return align_dict def print_embed_overlap(embed_dict, vocab_dict): embed_keys = set(embed_dict.keys()) vocab_keys = set(vocab_dict.symbols) overlap = len(embed_keys & vocab_keys) logger.info("found {}/{} types in embedding file".format(overlap, len(vocab_dict))) def parse_embedding(embed_path): """Parse embedding text file into a dictionary of word and embedding tensors. The first line can have vocabulary size and dimension. The following lines should contain word and embedding separated by spaces. Example: 2 5 the -0.0230 -0.0264 0.0287 0.0171 0.1403 at -0.0395 -0.1286 0.0275 0.0254 -0.0932 """ embed_dict = {} with open(embed_path) as f_embed: next(f_embed) # skip header for line in f_embed: pieces = line.rstrip().split(" ") embed_dict[pieces[0]] = torch.Tensor( [float(weight) for weight in pieces[1:]] ) return embed_dict def load_embedding(embed_dict, vocab, embedding): for idx in range(len(vocab)): token = vocab[idx] if token in embed_dict: embedding.weight.data[idx] = embed_dict[token] return embedding def replace_unk(hypo_str, src_str, alignment, align_dict, unk): from fairseq import tokenizer # Tokens are strings here hypo_tokens = tokenizer.tokenize_line(hypo_str) # TODO: Very rare cases where the replacement is '<eos>' should be handled gracefully src_tokens = tokenizer.tokenize_line(src_str) + ["<eos>"] for i, ht in enumerate(hypo_tokens): if ht == unk: src_token = src_tokens[alignment[i]] # Either take the corresponding value in the aligned dictionary or just copy the original value. hypo_tokens[i] = align_dict.get(src_token, src_token) return " ".join(hypo_tokens) def post_process_prediction( hypo_tokens, src_str, alignment, align_dict, tgt_dict, remove_bpe=None, extra_symbols_to_ignore=None, ): hypo_str = tgt_dict.string( hypo_tokens, remove_bpe, extra_symbols_to_ignore=extra_symbols_to_ignore ) if align_dict is not None: hypo_str = replace_unk( hypo_str, src_str, alignment, align_dict, tgt_dict.unk_string() ) if align_dict is not None or remove_bpe is not None: # Convert back to tokens for evaluating with unk replacement or without BPE # Note that the dictionary can be modified inside the method. hypo_tokens = tgt_dict.encode_line(hypo_str, add_if_not_exist=True) return hypo_tokens, hypo_str, alignment def make_positions(tensor, padding_idx: int, onnx_trace: bool = False): """Replace non-padding symbols with their position numbers. Position numbers begin at padding_idx+1. Padding symbols are ignored. """ # The series of casts and type-conversions here are carefully # balanced to both work with ONNX export and XLA. In particular XLA # prefers ints, cumsum defaults to output longs, and ONNX doesn't know # how to handle the dtype kwarg in cumsum. mask = tensor.ne(padding_idx).int() return (torch.cumsum(mask, dim=1).type_as(mask) * mask).long() + padding_idx def strip_pad(tensor, pad): return tensor[tensor.ne(pad)] def buffered_arange(max): if not hasattr(buffered_arange, "buf"): buffered_arange.buf = torch.LongTensor() if max > buffered_arange.buf.numel(): buffered_arange.buf.resize_(max) torch.arange(max, out=buffered_arange.buf) return buffered_arange.buf[:max] def convert_padding_direction( src_tokens, padding_idx, right_to_left: bool = False, left_to_right: bool = False ): assert right_to_left ^ left_to_right pad_mask = src_tokens.eq(padding_idx) if not pad_mask.any(): # no padding, return early return src_tokens if left_to_right and not pad_mask[:, 0].any(): # already right padded return src_tokens if right_to_left and not pad_mask[:, -1].any(): # already left padded return src_tokens max_len = src_tokens.size(1) buffered = torch.empty(0).long() if max_len > 0: torch.arange(max_len, out=buffered) range = buffered.type_as(src_tokens).expand_as(src_tokens) num_pads = pad_mask.long().sum(dim=1, keepdim=True) if right_to_left: index = torch.remainder(range - num_pads, max_len) else: index = torch.remainder(range + num_pads, max_len) return src_tokens.gather(1, index) def item(tensor): # tpu-comment: making this a no-op for xla devices. if torch.is_tensor(tensor) and tensor.device.type == "xla": return tensor.detach() if hasattr(tensor, "item"): return tensor.item() if hasattr(tensor, "__getitem__"): return tensor[0] return tensor def multi_tensor_total_norm(grads, chunk_size=2048 * 32) -> torch.Tensor: per_device_grads = {} norms = [] for grad in grads: device = grad.device cur_device_grads = per_device_grads.get(device) if cur_device_grads is None: cur_device_grads = [] per_device_grads[device] = cur_device_grads cur_device_grads.append(grad) for device in per_device_grads.keys(): cur_device_grads = per_device_grads[device] if device.type == "cuda": # TODO(msb) return has_inf has_inf = torch.zeros((1, 1), dtype=torch.int, device=device) with torch.cuda.device(device): norm = multi_tensor_l2norm( chunk_size, has_inf, [cur_device_grads], False ) norms.append(norm[0].to(torch.cuda.current_device())) else: norms += [torch.norm(g, p=2, dtype=torch.float32) for g in cur_device_grads] total_norm = torch.norm(torch.stack(norms)) return total_norm @torch.no_grad() def clip_grad_norm_(params, max_norm, aggregate_norm_fn=None) -> torch.Tensor: def grad_exists(p): return p is not None and getattr(p, "grad", None) is not None if isinstance(params, torch.Tensor): params = [params] params = list(params) grads = [ p.grad.detach() for p in params if grad_exists(p) and not hasattr(p, "expert") ] expert_grads = [ p.grad.detach() for p in params if grad_exists(p) and hasattr(p, "expert") ] if len(grads) == 0: if len(params) > 0: return params[0].new_tensor(0.0) else: return torch.tensor(0.0) if len(grads) == 1: total_norm = torch.norm(grads[0], p=2, dtype=torch.float32) else: if multi_tensor_l2norm_available: total_norm = multi_tensor_total_norm(grads) else: if torch.cuda.is_available(): warnings.warn( "amp_C fused kernels unavailable, disabling multi_tensor_l2norm; " "you may get better performance by installing NVIDIA's apex library" ) device = torch.cuda.current_device() elif grads[0].device.type == "xla": device = grads[0].device else: device = torch.device("cpu") total_norm = torch.norm( torch.stack( [torch.norm(g, p=2, dtype=torch.float32).to(device) for g in grads] ) ) if aggregate_norm_fn is not None: total_norm = aggregate_norm_fn(total_norm) if max_norm > 0: max_norm = float(max_norm) clip_coef = (max_norm / (total_norm + 1e-6)).clamp_(max=1) for g in grads + expert_grads: g.mul_(clip_coef) return total_norm def fill_with_neg_inf(t): """FP16-compatible function that fills a tensor with -inf.""" return t.float().fill_(float("-inf")).type_as(t) def _match_types(arg1, arg2): """Convert the numerical argument to the same type as the other argument""" def upgrade(arg_number, arg_structure): if isinstance(arg_structure, tuple): return tuple([arg_number] * len(arg_structure)) elif isinstance(arg_structure, dict): arg = copy.deepcopy(arg_structure) for k in arg: arg[k] = upgrade(arg_number, arg_structure[k]) return arg else: return arg_number if isinstance(arg1, float) or isinstance(arg1, int): return upgrade(arg1, arg2), arg2 elif isinstance(arg2, float) or isinstance(arg2, int): return arg1, upgrade(arg2, arg1) return arg1, arg2 def resolve_max_positions(*args): """Resolve max position constraints from multiple sources.""" def map_value_update(d1, d2): updated_value = copy.deepcopy(d1) for key in d2: if key not in updated_value: updated_value[key] = d2[key] else: updated_value[key] = min(d1[key], d2[key]) return updated_value def nullsafe_min(l): minim = None for item in l: if minim is None: minim = item elif item is not None and item < minim: minim = item return minim max_positions = None for arg in args: if max_positions is None: max_positions = arg elif arg is not None: max_positions, arg = _match_types(max_positions, arg) if isinstance(arg, float) or isinstance(arg, int): max_positions = min(max_positions, arg) elif isinstance(arg, dict): max_positions = map_value_update(max_positions, arg) else: max_positions = tuple(map(nullsafe_min, zip(max_positions, arg))) return max_positions def import_user_module(args): module_path = getattr(args, "user_dir", None) if module_path is not None: module_path = os.path.abspath(args.user_dir) if not os.path.exists(module_path) and not os.path.isfile( os.path.dirname(module_path) ): fairseq_rel_path = os.path.join(os.path.dirname(__file__), args.user_dir) if os.path.exists(fairseq_rel_path): module_path = fairseq_rel_path else: fairseq_rel_path = os.path.join( os.path.dirname(__file__), "..", args.user_dir ) if os.path.exists(fairseq_rel_path): module_path = fairseq_rel_path else: raise FileNotFoundError(module_path) # ensure that user modules are only imported once import_user_module.memo = getattr(import_user_module, "memo", set()) if module_path not in import_user_module.memo: import_user_module.memo.add(module_path) module_parent, module_name = os.path.split(module_path) if module_name not in sys.modules: sys.path.insert(0, module_parent) importlib.import_module(module_name) tasks_path = os.path.join(module_path, "tasks") if os.path.exists(tasks_path): from fairseq.tasks import import_tasks import_tasks(tasks_path, f"{module_name}.tasks") models_path = os.path.join(module_path, "models") if os.path.exists(models_path): from fairseq.models import import_models import_models(models_path, f"{module_name}.models") else: raise ImportError( "Failed to import --user-dir={} because the corresponding module name " "({}) is not globally unique. Please rename the directory to " "something unique and try again.".format(module_path, module_name) ) def softmax(x, dim: int, onnx_trace: bool = False): if onnx_trace: return F.softmax(x.float(), dim=dim) else: return F.softmax(x, dim=dim, dtype=torch.float32) def log_softmax(x, dim: int, onnx_trace: bool = False): if onnx_trace: return F.log_softmax(x.float(), dim=dim) else: return F.log_softmax(x, dim=dim, dtype=torch.float32) def get_perplexity(loss, round=2, base=2): from fairseq.logging.meters import safe_round if loss is None: return 0.0 try: return safe_round(base ** loss, round) except OverflowError: return float("inf") def deprecation_warning(message, stacklevel=3): # don't use DeprecationWarning, since it's ignored by default warnings.warn(message, stacklevel=stacklevel) def relu_squared(x: torch.Tensor): return F.relu(x).pow(2) def get_activation_fn(activation: str) -> Callable: """Returns the activation function corresponding to `activation`""" from fairseq.modules import gelu, gelu_accurate if activation == "relu": return F.relu elif activation == "relu_squared": return relu_squared elif activation == "gelu": return gelu elif activation == "gelu_fast": deprecation_warning( "--activation-fn=gelu_fast has been renamed to gelu_accurate" ) return gelu_accurate elif activation == "gelu_accurate": return gelu_accurate elif activation == "tanh": return torch.tanh elif activation == "linear": return lambda x: x elif activation == "swish": return torch.nn.SiLU else: raise RuntimeError("--activation-fn {} not supported".format(activation)) def get_available_activation_fns() -> List: return [ "relu", "gelu", "gelu_fast", # deprecated "gelu_accurate", "tanh", "linear", ] @contextlib.contextmanager def model_eval(model): is_training = model.training model.eval() yield model.train(is_training) def has_parameters(module): try: next(module.parameters()) return True except StopIteration: return False def get_rng_state(): state = {"torch_rng_state": torch.get_rng_state()} if xm is not None: state["xla_rng_state"] = xm.get_rng_state() if torch.cuda.is_available(): state["cuda_rng_state"] = torch.cuda.get_rng_state() return state def set_rng_state(state): torch.set_rng_state(state["torch_rng_state"]) if xm is not None: xm.set_rng_state(state["xla_rng_state"]) if torch.cuda.is_available(): torch.cuda.set_rng_state(state["cuda_rng_state"]) class set_torch_seed(object): def __init__(self, seed): assert isinstance(seed, int) self.rng_state = get_rng_state() torch.manual_seed(seed) if xm is not None: xm.set_rng_state(seed) if torch.cuda.is_available(): torch.cuda.manual_seed(seed) def __enter__(self): return self def __exit__(self, *exc): set_rng_state(self.rng_state) def parse_alignment(line): """ Parses a single line from the alingment file. Args: line (str): String containing the alignment of the format: <src_idx_1>-<tgt_idx_1> <src_idx_2>-<tgt_idx_2> .. <src_idx_m>-<tgt_idx_m>. All indices are 0 indexed. Returns: torch.IntTensor: packed alignments of shape (2 * m). """ alignments = line.strip().split() parsed_alignment = torch.IntTensor(2 * len(alignments)) for idx, alignment in enumerate(alignments): src_idx, tgt_idx = alignment.split("-") parsed_alignment[2 * idx] = int(src_idx) parsed_alignment[2 * idx + 1] = int(tgt_idx) return parsed_alignment def get_token_to_word_mapping(tokens, exclude_list): n = len(tokens) word_start = [int(token not in exclude_list) for token in tokens] word_idx = list(accumulate(word_start)) token_to_word = {i: word_idx[i] for i in range(n)} return token_to_word def extract_hard_alignment(attn, src_sent, tgt_sent, pad, eos): tgt_valid = ( ((tgt_sent != pad) & (tgt_sent != eos)).nonzero(as_tuple=False).squeeze(dim=-1) ) src_invalid = ( ((src_sent == pad) | (src_sent == eos)).nonzero(as_tuple=False).squeeze(dim=-1) ) src_token_to_word = get_token_to_word_mapping(src_sent, [eos, pad]) tgt_token_to_word = get_token_to_word_mapping(tgt_sent, [eos, pad]) alignment = [] if len(tgt_valid) != 0 and len(src_invalid) < len(src_sent): attn_valid = attn[tgt_valid] attn_valid[:, src_invalid] = float("-inf") _, src_indices = attn_valid.max(dim=1) for tgt_idx, src_idx in zip(tgt_valid, src_indices): alignment.append( ( src_token_to_word[src_idx.item()] - 1, tgt_token_to_word[tgt_idx.item()] - 1, ) ) return alignment def extract_soft_alignment(attn, src_sent, tgt_sent, pad, eos): tgt_valid = ((tgt_sent != pad)).nonzero(as_tuple=False) src_valid = ((src_sent != pad)).nonzero(as_tuple=False).squeeze(dim=-1) alignment = [] if len(tgt_valid) != 0 and len(src_valid) != 0: attn_valid = attn[tgt_valid, src_valid] alignment = [ ["{:.6f}".format(p) for p in src_probs.tolist()] for src_probs in attn_valid ] return alignment def new_arange(x, *size): """ Return a Tensor of `size` filled with a range function on the device of x. If size is empty, using the size of the variable x. """ if len(size) == 0: size = x.size() return torch.arange(size[-1], device=x.device).expand(*size).contiguous() def get_tpu_device(): return xm.xla_device() def tpu_data_loader(itr): import torch_xla.core.xla_model as xm import torch_xla.distributed.parallel_loader as pl from fairseq.data import iterators xm.rendezvous("tpu_data_loader") # wait for all workers xm.mark_step() device = xm.xla_device() return iterators.CountingIterator( pl.ParallelLoader(itr, [device]).per_device_loader(device), start=getattr(itr, "n", 0), total=len(itr), ) def is_xla_tensor(tensor): return torch.is_tensor(tensor) and tensor.device.type == "xla" def index_put(tensor, indices, value): if is_xla_tensor(tensor): for _ in range(indices.dim(), tensor.dim()): indices = indices.unsqueeze(-1) if indices.size(-1) < tensor.size(-1): indices = indices.expand_as(tensor) tensor = torch.mul(tensor, ~indices) + torch.mul(value, indices) else: tensor[indices] = value return tensor def xla_device_to_cpu(dat): import torch_xla.core.xla_model as xm return xm._maybe_convert_to_cpu(dat) class CudaEnvironment(object): def __init__(self): cur_device = torch.cuda.current_device() prop = torch.cuda.get_device_properties("cuda:{}".format(cur_device)) self.name = prop.name self.major = prop.major self.minor = prop.minor self.total_memory_in_GB = prop.total_memory / 1024 / 1024 / 1024 @staticmethod def pretty_print_cuda_env_list(cuda_env_list): """ Given a list of CudaEnviorments, pretty print them """ num_workers = len(cuda_env_list) center = "CUDA enviroments for all {} workers".format(num_workers) banner_len = 40 - len(center) // 2 first_line = "*" * banner_len + center + "*" * banner_len logger.info(first_line) for r, env in enumerate(cuda_env_list): logger.info( "rank {:3d}: ".format(r) + "capabilities = {:2d}.{:<2d} ; ".format(env.major, env.minor) + "total memory = {:.3f} GB ; ".format(env.total_memory_in_GB) + "name = {:40s}".format(env.name) ) logger.info(first_line) def csv_str_list(x): return x.split(",") def eval_str_list(x, type=float): if x is None: return None if isinstance(x, str): x = eval(x) try: return list(map(type, x)) except TypeError: return [type(x)] def eval_str_dict(x, type=dict): if x is None: return None if isinstance(x, str): x = eval(x) return x def eval_bool(x, default=False): if x is None: return default try: return bool(eval(x)) except TypeError: return default def reset_logging(): root = logging.getLogger() for handler in root.handlers: root.removeHandler(handler) root.setLevel(os.environ.get("LOGLEVEL", "INFO").upper()) handler = logging.StreamHandler(sys.stdout) handler.setFormatter( logging.Formatter( fmt="%(asctime)s | %(levelname)s | %(name)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S", ) ) root.addHandler(handler) def safe_getattr(obj, k, default=None): """Returns obj[k] if it exists and is not None, otherwise returns default.""" from omegaconf import OmegaConf if OmegaConf.is_config(obj): return obj[k] if k in obj and obj[k] is not None else default return getattr(obj, k, default) def safe_hasattr(obj, k): """Returns True if the given key exists and is not None.""" return getattr(obj, k, None) is not None
KosmosX-API-main
kosmosX/fairseq/fairseq/utils.py
# 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 ast import collections import contextlib import logging import numpy as np import os import re import time import traceback from collections import OrderedDict from typing import Any, Dict, Optional, Union import torch from fairseq.data import data_utils from fairseq.dataclass.configs import CheckpointConfig from fairseq.dataclass.utils import ( convert_namespace_to_omegaconf, overwrite_args_by_name, ) from fairseq.distributed.fully_sharded_data_parallel import FSDP, has_FSDP from fairseq.distributed import utils as distributed_utils from fairseq.file_io import PathManager from fairseq.models import FairseqDecoder, FairseqEncoder from omegaconf import DictConfig, open_dict, OmegaConf from fairseq.ds_trainer import DeepSpeedTrainer logger = logging.getLogger(__name__) def save_checkpoint(cfg: CheckpointConfig, trainer, epoch_itr, val_loss): from fairseq import meters # only one worker should attempt to create the required dir if trainer.data_parallel_rank == 0: os.makedirs(cfg.save_dir, exist_ok=True) prev_best = getattr(save_checkpoint, "best", val_loss) if val_loss is not None: best_function = max if cfg.maximize_best_checkpoint_metric else min save_checkpoint.best = best_function(val_loss, prev_best) if cfg.no_save: return trainer.consolidate_optimizer() # TODO(SS): do we need this if no_save_optimizer_state extra_state = {"train_iterator": epoch_itr.state_dict(), "val_loss": val_loss} if hasattr(save_checkpoint, "best"): extra_state.update({"best": save_checkpoint.best}) if getattr(epoch_itr, "sharded_checkpoint", False): local_state_dict = extra_state["train_iterator"] all_state_dicts = distributed_utils.all_gather_list( local_state_dict, max_size=getattr(trainer.cfg.common, "all_gather_list_size", 16384), group=trainer.data_parallel_process_group, ) extra_state["train_iterator"] = all_state_dicts if not trainer.should_save_checkpoint_on_current_rank: if trainer.always_call_state_dict_during_save_checkpoint: trainer.state_dict() return write_timer = meters.StopwatchMeter() write_timer.start() epoch = epoch_itr.epoch end_of_epoch = epoch_itr.end_of_epoch() updates = trainer.get_num_updates() logger.info(f"Preparing to save checkpoint for epoch {epoch} @ {updates} updates") def is_better(a, b): return a >= b if cfg.maximize_best_checkpoint_metric else a <= b suffix = trainer.checkpoint_suffix checkpoint_conds = collections.OrderedDict() if isinstance(trainer, DeepSpeedTrainer): checkpoint_conds["checkpoints"] = ( not end_of_epoch and cfg.save_interval_updates > 0 and updates % cfg.save_interval_updates == 0 ) else: checkpoint_conds["checkpoint{}{}.pt".format(epoch, suffix)] = ( end_of_epoch and not cfg.no_epoch_checkpoints and epoch % cfg.save_interval == 0 ) checkpoint_conds["checkpoint_{}_{}{}.pt".format(epoch, updates, suffix)] = ( not end_of_epoch and cfg.save_interval_updates > 0 and updates % cfg.save_interval_updates == 0 ) checkpoint_conds["checkpoint_best{}.pt".format(suffix)] = val_loss is not None and ( not hasattr(save_checkpoint, "best") or is_better(val_loss, save_checkpoint.best) ) if val_loss is not None and cfg.keep_best_checkpoints > 0: worst_best = getattr(save_checkpoint, "best", None) chkpts = checkpoint_paths( cfg.save_dir, pattern=r"checkpoint\.best_{}_(\d+\.?\d*){}\.pt".format( cfg.best_checkpoint_metric, suffix ), ) if len(chkpts) > 0: p = chkpts[-1] if cfg.maximize_best_checkpoint_metric else chkpts[0] worst_best = float(p.rsplit("_")[-1].replace("{}.pt".format(suffix), "")) # add random digits to resolve ties with data_utils.numpy_seed(epoch, updates, val_loss): rand_sfx = np.random.randint(0, cfg.keep_best_checkpoints) checkpoint_conds[ "checkpoint.best_{}_{:.3f}{}{}.pt".format( cfg.best_checkpoint_metric, val_loss, rand_sfx, suffix ) ] = worst_best is None or is_better(val_loss, worst_best) checkpoint_conds[ "checkpoint_last{}.pt".format(suffix) ] = not cfg.no_last_checkpoints checkpoints = [ os.path.join(cfg.save_dir, fn) for fn, cond in checkpoint_conds.items() if cond ] if len(checkpoints) > 0: trainer.save_checkpoint(checkpoints[0], extra_state) if not isinstance(trainer, DeepSpeedTrainer): for cp in checkpoints[1:]: if cfg.write_checkpoints_asynchronously: # TODO[ioPath]: Need to implement a delayed asynchronous # file copying/moving feature. logger.warning( f"ioPath is not copying {checkpoints[0]} to {cp} " "since async write mode is on." ) else: assert PathManager.copy( checkpoints[0], cp, overwrite=True ), f"Failed to copy {checkpoints[0]} to {cp}" write_timer.stop() logger.info( "Saved checkpoint {} (epoch {} @ {} updates, score {}) (writing took {} seconds)".format( checkpoints[0], epoch, updates, val_loss, write_timer.sum ) ) if not end_of_epoch and cfg.keep_interval_updates > 0: # remove old checkpoints; checkpoints are sorted in descending order if cfg.keep_interval_updates_pattern == -1: checkpoints = checkpoint_paths( cfg.save_dir, pattern=r"checkpoint_\d+_(\d+){}\.pt".format(suffix) ) else: checkpoints = checkpoint_paths( cfg.save_dir, pattern=r"checkpoint_\d+_(\d+){}\.pt".format(suffix), keep_match=True, ) checkpoints = [ x[0] for x in checkpoints if x[1] % cfg.keep_interval_updates_pattern != 0 ] for old_chk in checkpoints[cfg.keep_interval_updates :]: if os.path.lexists(old_chk): os.remove(old_chk) elif PathManager.exists(old_chk): PathManager.rm(old_chk) if cfg.keep_last_epochs > 0: # remove old epoch checkpoints; checkpoints are sorted in descending order checkpoints = checkpoint_paths( cfg.save_dir, pattern=r"checkpoint(\d+){}\.pt".format(suffix) ) for old_chk in checkpoints[cfg.keep_last_epochs :]: if os.path.lexists(old_chk): os.remove(old_chk) elif PathManager.exists(old_chk): PathManager.rm(old_chk) if cfg.keep_best_checkpoints > 0: # only keep the best N checkpoints according to validation metric checkpoints = checkpoint_paths( cfg.save_dir, pattern=r"checkpoint\.best_{}_(\d+\.?\d*){}\.pt".format( cfg.best_checkpoint_metric, suffix ), ) if not cfg.maximize_best_checkpoint_metric: checkpoints = checkpoints[::-1] for old_chk in checkpoints[cfg.keep_best_checkpoints :]: if os.path.lexists(old_chk): os.remove(old_chk) elif PathManager.exists(old_chk): PathManager.rm(old_chk) def load_checkpoint(cfg: CheckpointConfig, trainer, **passthrough_args): """ Load a checkpoint and restore the training iterator. *passthrough_args* will be passed through to ``trainer.get_train_iterator``. """ reset_optimizer = cfg.reset_optimizer reset_lr_scheduler = cfg.reset_lr_scheduler optimizer_overrides = ast.literal_eval(cfg.optimizer_overrides) reset_meters = cfg.reset_meters reset_dataloader = cfg.reset_dataloader if cfg.finetune_from_model is not None and ( reset_optimizer or reset_lr_scheduler or reset_meters or reset_dataloader ): raise ValueError( "--finetune-from-model can not be set together with either --reset-optimizer" " or reset_lr_scheduler or reset_meters or reset_dataloader" ) suffix = trainer.checkpoint_suffix if isinstance(trainer, DeepSpeedTrainer): checkpoint_path = os.path.join(cfg.save_dir, "checkpoints/") else: if ( cfg.restore_file == "checkpoint_last.pt" ): # default value of restore_file is 'checkpoint_last.pt' checkpoint_path = os.path.join( cfg.save_dir, "checkpoint_last{}.pt".format(suffix) ) first_launch = not PathManager.exists(checkpoint_path) if cfg.finetune_from_model is not None and first_launch: # if there is no last checkpoint to restore, start the finetune from pretrained model # else just use usual logic to load checkpoint, e.g. restart from last checkpoint and etc. if PathManager.exists(cfg.finetune_from_model): checkpoint_path = cfg.finetune_from_model reset_optimizer = True reset_lr_scheduler = True reset_meters = True reset_dataloader = True logger.info( f"loading pretrained model from {checkpoint_path}: " "optimizer, lr scheduler, meters, dataloader will be reset" ) else: raise ValueError( f"--funetune-from-model {cfg.finetune_from_model} does not exist" ) elif suffix is not None: checkpoint_path = cfg.restore_file.replace(".pt", suffix + ".pt") else: checkpoint_path = cfg.restore_file if cfg.restore_file != "checkpoint_last.pt" and cfg.finetune_from_model: raise ValueError( "--finetune-from-model and --restore-file (non-default value) " "can not be specified together: " + str(cfg) ) extra_state = trainer.load_checkpoint( checkpoint_path, reset_optimizer, reset_lr_scheduler, optimizer_overrides, reset_meters=reset_meters, ) if ( extra_state is not None and "best" in extra_state and not reset_optimizer and not reset_meters ): save_checkpoint.best = extra_state["best"] if extra_state is not None and not reset_dataloader: # restore iterator from checkpoint itr_state = extra_state["train_iterator"] epoch_itr = trainer.get_train_iterator( epoch=itr_state.get("epoch", 1), load_dataset=True, **passthrough_args ) epoch_itr.load_state_dict(itr_state) else: epoch_itr = trainer.get_train_iterator( epoch=1, load_dataset=True, **passthrough_args ) trainer.lr_step(epoch_itr.epoch) return extra_state, epoch_itr def load_checkpoint_to_cpu(path, arg_overrides=None, load_on_all_ranks=False): """Loads a checkpoint to CPU (with upgrading for backward compatibility). If doing single-GPU training or if the checkpoint is only being loaded by at most one process on each node (current default behavior is for only rank 0 to read the checkpoint from disk), load_on_all_ranks should be False to avoid errors from torch.distributed not having been initialized or torch.distributed.barrier() hanging. If all processes on each node may be loading the checkpoint simultaneously, load_on_all_ranks should be set to True to avoid I/O conflicts. There's currently no support for > 1 but < all processes loading the checkpoint on each node. """ local_path = PathManager.get_local_path(path) # The locally cached file returned by get_local_path() may be stale for # remote files that are periodically updated/overwritten (ex: # checkpoint_last.pt) - so we remove the local copy, sync across processes # (if needed), and then download a fresh copy. if local_path != path and PathManager.path_requires_pathmanager(path): try: os.remove(local_path) except FileNotFoundError: # With potentially multiple processes removing the same file, the # file being missing is benign (missing_ok isn't available until # Python 3.8). pass if load_on_all_ranks: torch.distributed.barrier() local_path = PathManager.get_local_path(path) with open(local_path, "rb") as f: state = torch.load(f, map_location=torch.device("cpu")) if "args" in state and state["args"] is not None and arg_overrides is not None: args = state["args"] for arg_name, arg_val in arg_overrides.items(): setattr(args, arg_name, arg_val) if "cfg" in state and state["cfg"] is not None: # hack to be able to set Namespace in dict config. this should be removed when we update to newer # omegaconf version that supports object flags, or when we migrate all existing models from omegaconf import _utils old_primitive = _utils.is_primitive_type _utils.is_primitive_type = lambda _: True state["cfg"] = OmegaConf.create(state["cfg"]) _utils.is_primitive_type = old_primitive OmegaConf.set_struct(state["cfg"], True) if arg_overrides is not None: overwrite_args_by_name(state["cfg"], arg_overrides) state = _upgrade_state_dict(state) return state def load_model_ensemble( filenames, arg_overrides: Optional[Dict[str, Any]] = None, task=None, strict=True, suffix="", num_shards=1, state=None, ): """Loads an ensemble of models. Args: filenames (List[str]): checkpoint files to load arg_overrides (Dict[str,Any], optional): override model args that were used during model training task (fairseq.tasks.FairseqTask, optional): task to use for loading """ assert not ( strict and num_shards > 1 ), "Cannot load state dict with strict=True and checkpoint shards > 1" ensemble, args, _task = load_model_ensemble_and_task( filenames, arg_overrides, task, strict, suffix, num_shards, state, ) return ensemble, args def get_maybe_sharded_checkpoint_filename( filename: str, suffix: str, shard_idx: int, num_shards: int ) -> str: orig_filename = filename filename = filename.replace(".pt", suffix + ".pt") fsdp_filename = filename[:-3] + f"-shard{shard_idx}.pt" model_parallel_filename = orig_filename[:-3] + f"_part{shard_idx}.pt" if PathManager.exists(fsdp_filename): return fsdp_filename elif num_shards > 1: return model_parallel_filename else: return filename def load_model_ensemble_and_task( filenames, arg_overrides: Optional[Dict[str, Any]] = None, task=None, strict=True, suffix="", num_shards=1, state=None, ): assert state is None or len(filenames) == 1 from fairseq import tasks assert not ( strict and num_shards > 1 ), "Cannot load state dict with strict=True and checkpoint shards > 1" ensemble = [] cfg = None for filename in filenames: orig_filename = filename model_shard_state = {"shard_weights": [], "shard_metadata": []} assert num_shards > 0 st = time.time() for shard_idx in range(num_shards): filename = get_maybe_sharded_checkpoint_filename( orig_filename, suffix, shard_idx, num_shards ) if not PathManager.exists(filename): raise IOError("Model file not found: {}".format(filename)) if state is None: state = load_checkpoint_to_cpu(filename, arg_overrides) if "args" in state and state["args"] is not None: cfg = convert_namespace_to_omegaconf(state["args"]) elif "cfg" in state and state["cfg"] is not None: cfg = state["cfg"] else: raise RuntimeError( f"Neither args nor cfg exist in state keys = {state.keys()}" ) if task is None: task = tasks.setup_task(cfg.task) if "task_state" in state: task.load_state_dict(state["task_state"]) if "fsdp_metadata" in state and num_shards > 1: model_shard_state["shard_weights"].append(state["model"]) model_shard_state["shard_metadata"].append(state["fsdp_metadata"]) # check FSDP import before the code goes too far if not has_FSDP: raise ImportError( "Cannot find FullyShardedDataParallel. " "Please install fairscale with: pip install fairscale" ) if shard_idx == num_shards - 1: consolidated_model_state = FSDP.consolidate_shard_weights( shard_weights=model_shard_state["shard_weights"], shard_metadata=model_shard_state["shard_metadata"], ) model = task.build_model(cfg.model) if ( "optimizer_history" in state and len(state["optimizer_history"]) > 0 and "num_updates" in state["optimizer_history"][-1] ): model.set_num_updates( state["optimizer_history"][-1]["num_updates"] ) model.load_state_dict( consolidated_model_state, strict=strict, model_cfg=cfg.model ) else: # model parallel checkpoint or unsharded checkpoint model = task.build_model(cfg.model) if ( "optimizer_history" in state and len(state["optimizer_history"]) > 0 and "num_updates" in state["optimizer_history"][-1] ): model.set_num_updates( state["optimizer_history"][-1]["num_updates"] ) model.load_state_dict( state["model"], strict=strict, model_cfg=cfg.model ) # reset state so it gets loaded for the next model in ensemble state = None if shard_idx % 10 == 0 and shard_idx > 0: elapsed = time.time() - st logger.info( f"Loaded {shard_idx} shards in {elapsed:.2f}s, {elapsed / (shard_idx+1):.2f}s/shard" ) # build model for ensemble ensemble.append(model) return ensemble, cfg, task def checkpoint_paths(path, pattern=r"checkpoint(\d+)\.pt", keep_match=False): """Retrieves all checkpoints found in `path` directory. Checkpoints are identified by matching filename to the specified pattern. If the pattern contains groups, the result will be sorted by the first group in descending order. """ pt_regexp = re.compile(pattern) files = PathManager.ls(path) entries = [] for i, f in enumerate(files): m = pt_regexp.fullmatch(f) if m is not None: idx = float(m.group(1)) if len(m.groups()) > 0 else i entries.append((idx, m.group(0))) if keep_match: return [(os.path.join(path, x[1]), x[0]) for x in sorted(entries, reverse=True)] else: return [os.path.join(path, x[1]) for x in sorted(entries, reverse=True)] def torch_persistent_save(obj, filename, async_write: bool = False): if async_write: with PathManager.opena(filename, "wb") as f: _torch_persistent_save(obj, f) else: if PathManager.supports_rename(filename): # do atomic save with PathManager.open(filename + ".tmp", "wb") as f: _torch_persistent_save(obj, f) PathManager.rename(filename + ".tmp", filename) else: # fallback to non-atomic save with PathManager.open(filename, "wb") as f: _torch_persistent_save(obj, f) def _torch_persistent_save(obj, f): if isinstance(f, str): with PathManager.open(f, "wb") as h: torch_persistent_save(obj, h) return for i in range(3): try: return torch.save(obj, f) except Exception: if i == 2: logger.error(traceback.format_exc()) raise def _upgrade_state_dict(state): """Helper for upgrading old model checkpoints.""" # add optimizer_history if "optimizer_history" not in state: state["optimizer_history"] = [ {"criterion_name": "CrossEntropyCriterion", "best_loss": state["best_loss"]} ] state["last_optimizer_state"] = state["optimizer"] del state["optimizer"] del state["best_loss"] # move extra_state into sub-dictionary if "epoch" in state and "extra_state" not in state: state["extra_state"] = { "epoch": state["epoch"], "batch_offset": state["batch_offset"], "val_loss": state["val_loss"], } del state["epoch"] del state["batch_offset"] del state["val_loss"] # reduce optimizer history's memory usage (only keep the last state) if "optimizer" in state["optimizer_history"][-1]: state["last_optimizer_state"] = state["optimizer_history"][-1]["optimizer"] for optim_hist in state["optimizer_history"]: del optim_hist["optimizer"] # record the optimizer class name if "optimizer_name" not in state["optimizer_history"][-1]: state["optimizer_history"][-1]["optimizer_name"] = "FairseqNAG" # move best_loss into lr_scheduler_state if "lr_scheduler_state" not in state["optimizer_history"][-1]: state["optimizer_history"][-1]["lr_scheduler_state"] = { "best": state["optimizer_history"][-1]["best_loss"] } del state["optimizer_history"][-1]["best_loss"] # keep track of number of updates if "num_updates" not in state["optimizer_history"][-1]: state["optimizer_history"][-1]["num_updates"] = 0 # old model checkpoints may not have separate source/target positions if ( "args" in state and hasattr(state["args"], "max_positions") and not hasattr(state["args"], "max_source_positions") ): state["args"].max_source_positions = state["args"].max_positions state["args"].max_target_positions = state["args"].max_positions # use stateful training data iterator if "train_iterator" not in state["extra_state"]: state["extra_state"]["train_iterator"] = { "epoch": state["extra_state"]["epoch"], "iterations_in_epoch": state["extra_state"].get("batch_offset", 0), } # backward compatibility, cfg updates if "args" in state and state["args"] is not None: # default to translation task if not hasattr(state["args"], "task"): state["args"].task = "translation" # --raw-text and --lazy-load are deprecated if getattr(state["args"], "raw_text", False): state["args"].dataset_impl = "raw" elif getattr(state["args"], "lazy_load", False): state["args"].dataset_impl = "lazy" # epochs start at 1 if state["extra_state"]["train_iterator"] is not None: state["extra_state"]["train_iterator"]["epoch"] = max( state["extra_state"]["train_iterator"].get("epoch", 1), 1 ) # --remove-bpe ==> --postprocess if hasattr(state["args"], "remove_bpe"): state["args"].post_process = state["args"].remove_bpe # --min-lr ==> --stop-min-lr if hasattr(state["args"], "min_lr"): state["args"].stop_min_lr = state["args"].min_lr del state["args"].min_lr # binary_cross_entropy / kd_binary_cross_entropy => wav2vec criterion if ( hasattr(state["args"], "criterion") and state["args"].criterion in [ "binary_cross_entropy", "kd_binary_cross_entropy", ] ): state["args"].criterion = "wav2vec" # remove log_keys if it's None (criteria will supply a default value of []) if hasattr(state["args"], "log_keys") and state["args"].log_keys is None: delattr(state["args"], "log_keys") # speech_pretraining => audio pretraining if ( hasattr(state["args"], "task") and state["args"].task == "speech_pretraining" ): state["args"].task = "audio_pretraining" # audio_cpc => wav2vec if hasattr(state["args"], "arch") and state["args"].arch == "audio_cpc": state["args"].arch = "wav2vec" # convert legacy float learning rate to List[float] if hasattr(state["args"], "lr") and isinstance(state["args"].lr, float): state["args"].lr = [state["args"].lr] # convert task data arg to a string instead of List[string] if ( hasattr(state["args"], "data") and isinstance(state["args"].data, list) and len(state["args"].data) > 0 ): state["args"].data = state["args"].data[0] # remove keys in state["args"] related to teacher-student learning for key in [ "static_teachers", "static_teacher_weights", "dynamic_teachers", "dynamic_teacher_weights", ]: if key in state["args"]: delattr(state["args"], key) state["cfg"] = convert_namespace_to_omegaconf(state["args"]) if "cfg" in state and state["cfg"] is not None: cfg = state["cfg"] with open_dict(cfg): # any upgrades for Hydra-based configs if ( "task" in cfg and "eval_wer_config" in cfg.task and isinstance(cfg.task.eval_wer_config.print_alignment, bool) ): cfg.task.eval_wer_config.print_alignment = "hard" if "generation" in cfg and isinstance(cfg.generation.print_alignment, bool): cfg.generation.print_alignment = "hard" if cfg.generation.print_alignment else None if ( "model" in cfg and "w2v_args" in cfg.model and cfg.model.w2v_args is not None and ( hasattr(cfg.model.w2v_args, "task") or "task" in cfg.model.w2v_args ) and hasattr(cfg.model.w2v_args.task, "eval_wer_config") and cfg.model.w2v_args.task.eval_wer_config is not None and isinstance( cfg.model.w2v_args.task.eval_wer_config.print_alignment, bool ) ): cfg.model.w2v_args.task.eval_wer_config.print_alignment = "hard" return state def prune_state_dict(state_dict, model_cfg: Optional[DictConfig]): """Prune the given state_dict if desired for LayerDrop (https://arxiv.org/abs/1909.11556). Training with LayerDrop allows models to be robust to pruning at inference time. This function prunes state_dict to allow smaller models to be loaded from a larger model and re-maps the existing state_dict for this to occur. It's called by functions that load models from checkpoints and does not need to be called directly. """ arch = None if model_cfg is not None: arch = ( model_cfg._name if isinstance(model_cfg, DictConfig) else getattr(model_cfg, "arch", None) ) if not model_cfg or arch is None or arch == "ptt_transformer": # args should not be none, but don't crash if it is. return state_dict encoder_layers_to_keep = getattr(model_cfg, "encoder_layers_to_keep", None) decoder_layers_to_keep = getattr(model_cfg, "decoder_layers_to_keep", None) if not encoder_layers_to_keep and not decoder_layers_to_keep: return state_dict # apply pruning logger.info( "Pruning model to specified layer configuration - this works best if the model was trained with LayerDrop" ) def create_pruning_pass(layers_to_keep, layer_name): keep_layers = sorted( int(layer_string) for layer_string in layers_to_keep.split(",") ) mapping_dict = {} for i in range(len(keep_layers)): mapping_dict[str(keep_layers[i])] = str(i) regex = re.compile(r"^{layer}.*\.layers\.(\d+)".format(layer=layer_name)) return {"substitution_regex": regex, "mapping_dict": mapping_dict} pruning_passes = [] if encoder_layers_to_keep: pruning_passes.append(create_pruning_pass(encoder_layers_to_keep, "encoder")) if decoder_layers_to_keep: pruning_passes.append(create_pruning_pass(decoder_layers_to_keep, "decoder")) new_state_dict = {} for layer_name in state_dict.keys(): match = re.search(r"\.layers\.(\d+)\.", layer_name) # if layer has no number in it, it is a supporting layer, such as an # embedding if not match: new_state_dict[layer_name] = state_dict[layer_name] continue # otherwise, layer should be pruned. original_layer_number = match.group(1) # figure out which mapping dict to replace from for pruning_pass in pruning_passes: if original_layer_number in pruning_pass["mapping_dict"] and pruning_pass[ "substitution_regex" ].search(layer_name): new_layer_number = pruning_pass["mapping_dict"][original_layer_number] substitution_match = pruning_pass["substitution_regex"].search( layer_name ) new_state_key = ( layer_name[: substitution_match.start(1)] + new_layer_number + layer_name[substitution_match.end(1) :] ) new_state_dict[new_state_key] = state_dict[layer_name] # Since layers are now pruned, *_layers_to_keep are no longer needed. # This is more of "It would make it work fix" rather than a proper fix. if isinstance(model_cfg, DictConfig): context = open_dict(model_cfg) else: context = contextlib.ExitStack() with context: if hasattr(model_cfg, "encoder_layers_to_keep"): model_cfg.encoder_layers_to_keep = None if hasattr(model_cfg, "decoder_layers_to_keep"): model_cfg.decoder_layers_to_keep = None return new_state_dict def load_pretrained_component_from_model( component: Union[FairseqEncoder, FairseqDecoder], checkpoint: str ): """ Load a pretrained FairseqEncoder or FairseqDecoder from checkpoint into the provided `component` object. If state_dict fails to load, there may be a mismatch in the architecture of the corresponding `component` found in the `checkpoint` file. """ if not PathManager.exists(checkpoint): raise IOError("Model file not found: {}".format(checkpoint)) state = load_checkpoint_to_cpu(checkpoint) if isinstance(component, FairseqEncoder): component_type = "encoder" elif isinstance(component, FairseqDecoder): component_type = "decoder" else: raise ValueError( "component to load must be either a FairseqEncoder or " "FairseqDecoder. Loading other component types are not supported." ) component_state_dict = OrderedDict() for key in state["model"].keys(): if key.startswith(component_type): # encoder.input_layers.0.0.weight --> input_layers.0.0.weight component_subkey = key[len(component_type) + 1 :] component_state_dict[component_subkey] = state["model"][key] component.load_state_dict(component_state_dict, strict=True) return component def verify_checkpoint_directory(save_dir: str, rank: int) -> None: if not os.path.exists(save_dir): os.makedirs(save_dir, exist_ok=True) temp_file_path = os.path.join(save_dir, f"dummy-{rank}") try: with open(temp_file_path, "w"): pass except OSError as e: logger.warning( "Unable to access checkpoint save directory: {}".format(save_dir) ) raise e else: os.remove(temp_file_path) def load_ema_from_checkpoint(fpath): """Loads exponential moving averaged (EMA) checkpoint from input and returns a model with ema weights. Args: fpath: A string path of checkpoint to load from. Returns: A dict of string keys mapping to various values. The 'model' key from the returned dict should correspond to an OrderedDict mapping string parameter names to torch Tensors. """ params_dict = collections.OrderedDict() new_state = None with PathManager.open(fpath, 'rb') as f: new_state = torch.load( f, map_location=( lambda s, _: torch.serialization.default_restore_location(s, 'cpu') ), ) # EMA model is stored in a separate "extra state" model_params = new_state['extra_state']['ema'] for key in list(model_params.keys()): p = model_params[key] if isinstance(p, torch.HalfTensor): p = p.float() if key not in params_dict: params_dict[key] = p.clone() # NOTE: clone() is needed in case of p is a shared parameter else: raise ValueError("Key {} is repeated in EMA model params.".format(key)) if len(params_dict) == 0: raise ValueError( f"Input checkpoint path '{fpath}' does not contain " "ema model weights, is this model trained with EMA?" ) new_state['model'] = params_dict return new_state
KosmosX-API-main
kosmosX/fairseq/fairseq/checkpoint_utils.py
# 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 from fairseq.modules.quantization import pq, quantization_options, scalar from omegaconf import DictConfig logger = logging.getLogger(__name__) def quantize_model_scalar(model, model_cfg: DictConfig): quant_noise_scalar = getattr(model_cfg, "quant_noise_scalar", 0) or 0 if quant_noise_scalar > 0: # quantize_model edits the model in place scalar.quantize_model_(model, p=quant_noise_scalar, bits=8, update_step=1000) return model class Quantizer(object): def __init__(self, config_path, max_epoch, max_update): try: import yaml except ImportError: raise ImportError("Please install yaml with: pip install yaml") # parse config if config_path: with open(config_path) as config_file: config = quantization_options.parse_config_yaml( yaml.safe_load(config_file) ) else: config = quantization_options.parse_config_yaml({}) self.n_centroids_config = config["n_centroids"] self.block_sizes_config = config["block_sizes"] self.layers_to_quantize = config["layers_to_quantize"] # We assume that training will run for a fixed number of epochs # (or updates) and that we should train for equal durations # between iterations of PQ. num_iterations = len(self.layers_to_quantize) if max_epoch > 0: assert max_epoch % num_iterations == 0, ( "for iterative PQ, --max-epoch (={}) must be evenly divisible by " "len(layers_to_quantize) (={})".format(max_epoch, num_iterations) ) self.epoch_schedule = max_epoch // num_iterations else: self.epoch_schedule = None if max_update > 0: assert max_update % num_iterations == 0, ( "for iterative PQ, --max-update (={}) must be evenly divisible by " "len(layers_to_quantize) (={})".format(max_update, num_iterations) ) self.update_schedule = max_update // num_iterations else: self.update_schedule = None assert (self.epoch_schedule is not None) ^ ( self.update_schedule is not None ), "for iterative PQ, cannot specify both --max-update and --max-epoch" # 0 is a special value for quantization step, which will force # the first call to begin_epoch() to call step() self.quantization_step = 0 def set_trainer(self, trainer): self.trainer = trainer self.size_tracker = pq.SizeTracker(self.trainer.get_model()) def step(self): """Move to the next stage of quantization.""" if self.quantization_step >= len(self.layers_to_quantize): # Maybe we just finished the last training step or we loaded # a checkpoint for an iterative PQ model which previously # finished training. Either way, don't quantize again. return logger.info( "quantizing model (step={}; layers_to_quantize[step]={})".format( self.quantization_step, self.layers_to_quantize[self.quantization_step] ) ) quantized_layers = pq.quantize_model_( self.trainer.get_model(), self.size_tracker, self.layers_to_quantize, self.block_sizes_config, self.n_centroids_config, step=self.quantization_step, ) logger.info("quantized layers: {}".format(quantized_layers)) logger.info(self.size_tracker) self.quantization_step += 1 # reintialize the Trainer since model parameters have changed self.trainer.reinitialize() def begin_epoch(self, epoch): """Called at the beginning of each epoch (epochs start at 1).""" if ( ( self.epoch_schedule is not None and epoch > 0 and (epoch - 1) % self.epoch_schedule == 0 ) # we always step once in the beginning, even if using # update-based quantization or self.quantization_step == 0 ): self.step() def step_update(self, num_updates): """Called at the end of each step.""" if ( self.update_schedule is not None and num_updates > 0 and num_updates % self.update_schedule == 0 ): self.step() def state_dict(self): return { "n_centroids_config": self.n_centroids_config, "block_sizes_config": self.block_sizes_config, "layers_to_quantize": self.layers_to_quantize, "epoch_schedule": self.epoch_schedule, "update_schedule": self.update_schedule, "quantization_step": self.quantization_step, } def load_state_dict(self, state_dict): self.n_centroids_config = state_dict["n_centroids_config"] self.block_sizes_config = state_dict["block_sizes_config"] self.layers_to_quantize = state_dict["layers_to_quantize"] self.epoch_schedule = state_dict["epoch_schedule"] self.update_schedule = state_dict["update_schedule"] self.quantization_step = state_dict["quantization_step"]
KosmosX-API-main
kosmosX/fairseq/fairseq/quantization_utils.py
# 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. """ Utilities for working with the local dataset cache. This file is adapted from `AllenNLP <https://github.com/allenai/allennlp>`_. and `huggingface <https://github.com/huggingface>`_. """ import fnmatch import json import logging import os import shutil import tarfile import tempfile from functools import partial, wraps from hashlib import sha256 from io import open try: from torch.hub import _get_torch_home torch_cache_home = _get_torch_home() except ImportError: torch_cache_home = os.path.expanduser( os.getenv( "TORCH_HOME", os.path.join(os.getenv("XDG_CACHE_HOME", "~/.cache"), "torch") ) ) default_cache_path = os.path.join(torch_cache_home, "pytorch_fairseq") try: from urllib.parse import urlparse except ImportError: from urlparse import urlparse try: from pathlib import Path PYTORCH_FAIRSEQ_CACHE = Path(os.getenv("PYTORCH_FAIRSEQ_CACHE", default_cache_path)) except (AttributeError, ImportError): PYTORCH_FAIRSEQ_CACHE = os.getenv("PYTORCH_FAIRSEQ_CACHE", default_cache_path) CONFIG_NAME = "config.json" WEIGHTS_NAME = "pytorch_model.bin" logger = logging.getLogger(__name__) # pylint: disable=invalid-name def load_archive_file(archive_file): # redirect to the cache, if necessary try: resolved_archive_file = cached_path(archive_file, cache_dir=None) except EnvironmentError: logger.info( "Archive name '{}' was not found in archive name list. " "We assumed '{}' was a path or URL but couldn't find any file " "associated to this path or URL.".format( archive_file, archive_file, ) ) return None if resolved_archive_file == archive_file: logger.info("loading archive file {}".format(archive_file)) else: logger.info( "loading archive file {} from cache at {}".format( archive_file, resolved_archive_file ) ) # Extract archive to temp dir and replace .tar.bz2 if necessary tempdir = None if not os.path.isdir(resolved_archive_file): tempdir = tempfile.mkdtemp() logger.info( "extracting archive file {} to temp dir {}".format( resolved_archive_file, tempdir ) ) ext = os.path.splitext(archive_file)[1][1:] with tarfile.open(resolved_archive_file, "r:" + ext) as archive: top_dir = os.path.commonprefix(archive.getnames()) archive.extractall(tempdir) os.remove(resolved_archive_file) shutil.move(os.path.join(tempdir, top_dir), resolved_archive_file) shutil.rmtree(tempdir) return resolved_archive_file def url_to_filename(url, etag=None): """ Convert `url` into a hashed filename in a repeatable way. If `etag` is specified, append its hash to the URL's, delimited by a period. """ url_bytes = url.encode("utf-8") url_hash = sha256(url_bytes) filename = url_hash.hexdigest() if etag: etag_bytes = etag.encode("utf-8") etag_hash = sha256(etag_bytes) filename += "." + etag_hash.hexdigest() return filename def filename_to_url(filename, cache_dir=None): """ Return the url and etag (which may be ``None``) stored for `filename`. Raise ``EnvironmentError`` if `filename` or its stored metadata do not exist. """ if cache_dir is None: cache_dir = PYTORCH_FAIRSEQ_CACHE if isinstance(cache_dir, Path): cache_dir = str(cache_dir) cache_path = os.path.join(cache_dir, filename) if not os.path.exists(cache_path): raise EnvironmentError("file {} not found".format(cache_path)) meta_path = cache_path + ".json" if not os.path.exists(meta_path): raise EnvironmentError("file {} not found".format(meta_path)) with open(meta_path, encoding="utf-8") as meta_file: metadata = json.load(meta_file) url = metadata["url"] etag = metadata["etag"] return url, etag def cached_path_from_pm(url_or_filename): """ Tries to cache the specified URL using PathManager class. Returns the cached path if success otherwise failure. """ try: from fairseq.file_io import PathManager local_path = PathManager.get_local_path(url_or_filename) return local_path except Exception: return None def cached_path(url_or_filename, cache_dir=None): """ Given something that might be a URL (or might be a local path), determine which. If it's a URL, download the file and cache it, and return the path to the cached file. If it's already a local path, make sure the file exists and then return the path. """ if cache_dir is None: cache_dir = PYTORCH_FAIRSEQ_CACHE if isinstance(url_or_filename, Path): url_or_filename = str(url_or_filename) if isinstance(cache_dir, Path): cache_dir = str(cache_dir) parsed = urlparse(url_or_filename) if parsed.scheme in ("http", "https", "s3"): # URL, so get it from the cache (downloading if necessary) return get_from_cache(url_or_filename, cache_dir) elif os.path.exists(url_or_filename): # File, and it exists. return url_or_filename elif parsed.scheme == "": # File, but it doesn't exist. raise EnvironmentError("file {} not found".format(url_or_filename)) else: cached_path = cached_path_from_pm(url_or_filename) if cached_path: return cached_path # Something unknown raise ValueError( "unable to parse {} as a URL or as a local path".format(url_or_filename) ) def split_s3_path(url): """Split a full s3 path into the bucket name and path.""" parsed = urlparse(url) if not parsed.netloc or not parsed.path: raise ValueError("bad s3 path {}".format(url)) bucket_name = parsed.netloc s3_path = parsed.path # Remove '/' at beginning of path. if s3_path.startswith("/"): s3_path = s3_path[1:] return bucket_name, s3_path def s3_request(func): """ Wrapper function for s3 requests in order to create more helpful error messages. """ @wraps(func) def wrapper(url, *args, **kwargs): from botocore.exceptions import ClientError try: return func(url, *args, **kwargs) except ClientError as exc: if int(exc.response["Error"]["Code"]) == 404: raise EnvironmentError("file {} not found".format(url)) else: raise return wrapper @s3_request def s3_etag(url): """Check ETag on S3 object.""" import boto3 s3_resource = boto3.resource("s3") bucket_name, s3_path = split_s3_path(url) s3_object = s3_resource.Object(bucket_name, s3_path) return s3_object.e_tag @s3_request def s3_get(url, temp_file): """Pull a file directly from S3.""" import boto3 s3_resource = boto3.resource("s3") bucket_name, s3_path = split_s3_path(url) s3_resource.Bucket(bucket_name).download_fileobj(s3_path, temp_file) def request_wrap_timeout(func, url): import requests for attempt, timeout in enumerate([10, 20, 40, 60, 60]): try: return func(timeout=timeout) except requests.exceptions.Timeout as e: logger.warning( "Request for %s timed-out (attempt %d). Retrying with a timeout of %d secs", url, attempt, timeout, exc_info=e, ) continue raise RuntimeError(f"Unable to fetch file {url}") def http_get(url, temp_file): import requests from tqdm import tqdm req = request_wrap_timeout(partial(requests.get, url, stream=True), url) content_length = req.headers.get("Content-Length") total = int(content_length) if content_length is not None else None progress = tqdm(unit="B", total=total) for chunk in req.iter_content(chunk_size=1024): if chunk: # filter out keep-alive new chunks progress.update(len(chunk)) temp_file.write(chunk) progress.close() def get_from_cache(url, cache_dir=None): """ Given a URL, look for the corresponding dataset in the local cache. If it's not there, download it. Then return the path to the cached file. """ if cache_dir is None: cache_dir = PYTORCH_FAIRSEQ_CACHE if isinstance(cache_dir, Path): cache_dir = str(cache_dir) if not os.path.exists(cache_dir): os.makedirs(cache_dir) # Get eTag to add to filename, if it exists. if url.startswith("s3://"): etag = s3_etag(url) else: try: import requests response = request_wrap_timeout( partial(requests.head, url, allow_redirects=True), url ) if response.status_code != 200: etag = None else: etag = response.headers.get("ETag") except RuntimeError: etag = None filename = url_to_filename(url, etag) # get cache path to put the file cache_path = os.path.join(cache_dir, filename) # If we don't have a connection (etag is None) and can't identify the file # try to get the last downloaded one if not os.path.exists(cache_path) and etag is None: matching_files = fnmatch.filter(os.listdir(cache_dir), filename + ".*") matching_files = list(filter(lambda s: not s.endswith(".json"), matching_files)) if matching_files: cache_path = os.path.join(cache_dir, matching_files[-1]) if not os.path.exists(cache_path): # Download to temporary file, then copy to cache dir once finished. # Otherwise you get corrupt cache entries if the download gets interrupted. with tempfile.NamedTemporaryFile() as temp_file: logger.info("%s not found in cache, downloading to %s", url, temp_file.name) # GET file object if url.startswith("s3://"): s3_get(url, temp_file) else: http_get(url, temp_file) # we are copying the file before closing it, so flush to avoid truncation temp_file.flush() # shutil.copyfileobj() starts at the current position, so go to the start temp_file.seek(0) logger.info("copying %s to cache at %s", temp_file.name, cache_path) with open(cache_path, "wb") as cache_file: shutil.copyfileobj(temp_file, cache_file) logger.info("creating metadata file for %s", cache_path) meta = {"url": url, "etag": etag} meta_path = cache_path + ".json" with open(meta_path, "w") as meta_file: output_string = json.dumps(meta) meta_file.write(output_string) logger.info("removing temp file %s", temp_file.name) return cache_path def read_set_from_file(filename): """ Extract a de-duped collection (set) of text from a file. Expected file format is one item per line. """ collection = set() with open(filename, "r", encoding="utf-8") as file_: for line in file_: collection.add(line.rstrip()) return collection def get_file_extension(path, dot=True, lower=True): ext = os.path.splitext(path)[1] ext = ext if dot else ext[1:] return ext.lower() if lower else ext
KosmosX-API-main
kosmosX/fairseq/fairseq/file_utils.py
# 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 typing import List, Optional import torch import torch.nn as nn from fairseq.token_generation_constraints import ( ConstraintState, OrderedConstraintState, UnorderedConstraintState, ) from torch import Tensor class Search(nn.Module): def __init__(self, tgt_dict): super().__init__() self.pad = tgt_dict.pad() self.unk = tgt_dict.unk() self.eos = tgt_dict.eos() self.vocab_size = len(tgt_dict) self.src_lengths = torch.tensor(-1) self.supports_constraints = False self.stop_on_max_len = False def step( self, step, lprobs, scores, prev_output_tokens=None, original_batch_idxs=None ): """Take a single search step. Args: step: the current search step, starting at 0 lprobs: (bsz x input_beam_size x vocab_size) the model's log-probabilities over the vocabulary at the current step scores: (bsz x input_beam_size x step) the historical model scores of each hypothesis up to this point prev_output_tokens: (bsz x step) the previously generated oputput tokens original_batch_idxs: (bsz) the tensor with the batch indices, in the range [0, bsz) this is useful in case there has been applied a re-ordering and we need to know the orignal indices Return: A tuple of (scores, indices, beams) where: scores: (bsz x output_beam_size) the scores of the chosen elements; output_beam_size can be larger than input_beam_size, e.g., we may return 2*input_beam_size to account for EOS indices: (bsz x output_beam_size) the indices of the chosen elements beams: (bsz x output_beam_size) the hypothesis ids of the chosen elements, in the range [0, input_beam_size) """ raise NotImplementedError @torch.jit.export def set_src_lengths(self, src_lengths): self.src_lengths = src_lengths @torch.jit.export def init_constraints(self, batch_constraints: Optional[Tensor], beam_size: int): """Initialize constraint states for constrained decoding (if supported). Args: batch_constraints: (torch.Tensor, optional) the list of constraints, in packed form beam_size: (int) the beam size Returns: *encoder_out* rearranged according to *new_order* """ pass def prune_sentences(self, batch_idxs: Tensor): """ Removes constraint states for completed sentences (if supported). This is called from sequence_generator._generate() when sentences are deleted from the batch. Args: batch_idxs: Indices of *sentences* whose constraint state should be *kept*. """ pass def update_constraints(self, active_hypos: Tensor): """ Updates the constraint states by selecting the beam items that are retained. This is called at each time step of sequence_generator._generate() when the set of 2 * {beam_size} candidate hypotheses are reduced to the beam size. Args: active_hypos: (batch size, beam size) list of integers denoting, for each sentence, which beam candidate items should be kept. """ pass class BeamSearch(Search): def __init__(self, tgt_dict): super().__init__(tgt_dict) self.constraint_states = None @torch.jit.export def step( self, step: int, lprobs, scores: Optional[Tensor], prev_output_tokens: Optional[Tensor] = None, original_batch_idxs: Optional[Tensor] = None, ): bsz, beam_size, vocab_size = lprobs.size() if step == 0: # at the first step all hypotheses are equally likely, so use # only the first beam lprobs = lprobs[:, ::beam_size, :].contiguous() else: # make probs contain cumulative scores for each hypothesis assert scores is not None lprobs = lprobs + scores[:, :, step - 1].unsqueeze(-1) top_prediction = torch.topk( lprobs.view(bsz, -1), k=min( # Take the best 2 x beam_size predictions. We'll choose the first # beam_size of these which don't predict eos to continue with. beam_size * 2, lprobs.view(bsz, -1).size(1) - 1, # -1 so we never select pad ), ) scores_buf = top_prediction[0] indices_buf = top_prediction[1] # Project back into relative indices and beams beams_buf = indices_buf // vocab_size indices_buf = indices_buf.fmod(vocab_size) # At this point, beams_buf and indices_buf are single-dim and contain relative indices return scores_buf, indices_buf, beams_buf class PrefixConstrainedBeamSearch(Search): def __init__(self, tgt_dict, prefix_allowed_tokens_fn): super().__init__(tgt_dict) self.prefix_allowed_tokens_fn = prefix_allowed_tokens_fn self.stop_on_max_len = True @torch.jit.export def apply_mask(self, x, prev_output_tokens, original_batch_idxs): beam_size = x.shape[0] // original_batch_idxs.shape[0] original_batch_idxs = ( original_batch_idxs.unsqueeze(-1).repeat((1, beam_size)).flatten().tolist() ) mask = torch.full_like(x, -math.inf) for sent_i, (sent, batch_i) in enumerate( zip(prev_output_tokens, original_batch_idxs) ): mask[sent_i, :, self.prefix_allowed_tokens_fn(batch_i, sent)] = 0 return mask @torch.jit.export def step( self, step: int, lprobs: Tensor, scores: Tensor, prev_output_tokens: Tensor, original_batch_idxs: Tensor, ): bsz, beam_size, vocab_size = lprobs.size() lprobs += self.apply_mask( lprobs.view(bsz * beam_size, 1, vocab_size), prev_output_tokens, original_batch_idxs, ).view(bsz, beam_size, vocab_size) if step == 0: # at the first step all hypotheses are equally likely, so use # only the first beam lprobs = lprobs[:, ::beam_size, :].contiguous() else: # make probs contain cumulative scores for each hypothesis assert scores is not None lprobs = lprobs + scores[:, :, step - 1].unsqueeze(-1) top_prediction = torch.topk( lprobs.view(bsz, -1), k=min( # Take the best beam_size predictions. We'll choose the first # beam_size of these which don't predict eos to continue with. beam_size, lprobs.view(bsz, -1).size(1) - 1, # -1 so we never select pad ), ) scores_buf = top_prediction[0] indices_buf = top_prediction[1] beams_buf = indices_buf // vocab_size indices_buf = indices_buf.fmod(vocab_size) return scores_buf, indices_buf, beams_buf class LexicallyConstrainedBeamSearch(Search): """Implements lexically constrained beam search as described in Fast Lexically Constrained Decoding with Dynamic Beam Allocation for Neural Machine Translation. Post & Vilar, NAACL 2018. https://www.aclweb.org/anthology/N18-1119/ and Improved Lexically Constrained Decoding for Translation and Monolingual Rewriting. Hu et al, NAACL 2019. https://www.aclweb.org/anthology/N19-1090/ This is accomplished by maintaining, for each beam hypothesis, a ConstraintState object (see constraints.py) that tracks which constraints have been generated and using this information to shape the beam for each input sentence. """ def __init__(self, tgt_dict, representation): super().__init__(tgt_dict) self.representation = representation self.vocab_size = len(tgt_dict) self.num_cands = 0 self.supports_constraints = True @torch.jit.export def init_constraints(self, batch_constraints: Optional[Tensor], beam_size: int): self.constraint_states = [] for constraint_tensor in batch_constraints: if self.representation == "ordered": constraint_state = OrderedConstraintState.create(constraint_tensor) elif self.representation == "unordered": constraint_state = UnorderedConstraintState.create(constraint_tensor) self.constraint_states.append([constraint_state for i in range(beam_size)]) @torch.jit.export def prune_sentences(self, batch_idxs: Tensor): self.constraint_states = [ self.constraint_states[i] for i in batch_idxs.tolist() ] @torch.jit.export def update_constraints(self, active_hypos: Tensor): if self.constraint_states: batch_size = active_hypos.size(0) for sentid in range(batch_size): self.constraint_states[sentid] = [ self.constraint_states[sentid][i] for i in active_hypos[sentid] ] @torch.jit.export def step( self, step: int, lprobs: Tensor, scores: Optional[Tensor], prev_output_tokens: Optional[Tensor] = None, original_batch_idxs: Optional[Tensor] = None, ): """ A constrained step builds a large candidates list from the following: - the top 2 * {beam_size} items over the whole beam - for each item in the beam - the top {each_k} (default 1) - all next constraints We then compute the constrained state of each beam item, and assign stripe codes: 0 to the best in each bank, 1 to the 2nd-best, and so on. We then sort by (stripe, score), and truncate the list at 2 * beam size. Args: step: the decoder step lprobs: (batch size, beam size, target vocab) the target-vocab distributions for each item in the beam. Retrun: A tuple of (scores, indices, beams, constraints) where: scores: (batch, output beam size) the scores of the chosen elements indices: (batch, output beam size) the target vocab indices of the chosen elements beams: (batch, output beam size) the 0-indexed hypothesis ids of the chosen elements constraints: (batch, output beam size) the new constraint states """ each_k = 1 device = lprobs.device batch_size, beam_size, vocab_size = lprobs.size() self.num_cands = min( # Just take the k-best. We'll get another k from the 1-best from each # row, plus more from the constraints beam_size * 2, lprobs.view(batch_size, -1).size(1) - 1, # -1 so we never select pad ) # STEP 0: Preliminary. Prevent EOS for unfinished hyps across all batch items constraint_states = self.constraint_states if constraint_states and step > 0: not_finished_indices = [] for sentno, sent_constraints in enumerate(constraint_states): for beamno, state in enumerate(sent_constraints): index = sentno * beam_size + beamno if not state.finished: not_finished_indices.append(index) not_finished_indices = torch.tensor(not_finished_indices) if not_finished_indices.numel() > 0: lprobs.view(batch_size * beam_size, -1)[ not_finished_indices, self.eos ] = -math.inf if step == 0: # at the first step all hypotheses are equally likely, so use # only the first beam entry for each batch item lprobs = lprobs[:, ::beam_size, :].contiguous() else: # make probs contain cumulative scores for each hypothesis assert scores is not None lprobs = lprobs + scores[:, :, step - 1].unsqueeze(-1) top_prediction = torch.topk( lprobs.view(batch_size, -1), self.num_cands, ) scores_buf, indices_buf = top_prediction # Project back into relative indices and beams beams_buf = indices_buf // vocab_size indices_buf = indices_buf.fmod(vocab_size) # Short circuit if there are no constraints in this batch if not constraint_states: return scores_buf, indices_buf, beams_buf # STEP 1: get top-1 from each hypothesis across all sentences in the batch if step > 0: top_scores, top_indices = torch.topk( lprobs.view(batch_size * beam_size, -1), k=each_k, dim=1, ) top_scores = top_scores.view(batch_size, -1) top_indices = top_indices.view(batch_size, -1) scores_buf = torch.cat((scores_buf, top_scores), dim=1) indices_buf = torch.cat((indices_buf, top_indices), dim=1) new_beams = torch.arange(0, beam_size, device=device).repeat(batch_size, 1) beams_buf = torch.cat((beams_buf, new_beams), dim=1) # Now, process sentences in the batch one by one. new_scores_buf = torch.zeros((batch_size, 2 * beam_size), device=device) new_indices_buf = torch.zeros((batch_size, 2 * beam_size), device=device).long() new_beams_buf = torch.zeros((batch_size, 2 * beam_size), device=device).long() for sentno, states in enumerate(constraint_states): scores, indices, beams, new_states = self.step_sentence( step, sentno, lprobs[sentno], constraint_states[sentno], beams_buf[sentno].clone(), indices_buf[sentno].clone(), scores_buf[sentno].clone(), ) new_scores_buf[sentno] = scores new_indices_buf[sentno] = indices new_beams_buf[sentno] = beams self.constraint_states[sentno] = new_states return new_scores_buf, new_indices_buf, new_beams_buf @torch.jit.export def step_sentence( self, step: int, sentno: int, lprobs: Tensor, constraint_states: List[List[ConstraintState]], beams_buf: Tensor, indices_buf: Tensor, scores_buf: Tensor, ): """Does per-sentence processing. Adds all constraints for each hypothesis to the list of candidates; then removes duplicates, sorts, and dynamically stripes across the banks. All tensor inputs are collapsed to those pertaining to a single input sentence. """ device = lprobs.device # STEP 2: Add all constraints for each beam item for beamno, state in enumerate(constraint_states): next_tokens = torch.tensor(list(state.next_tokens()), device=device).long() if next_tokens.numel() != 0: indices_buf = torch.cat((indices_buf, next_tokens)) next_beams = ( torch.tensor(beamno, device=device) .repeat(next_tokens.size(0)) .long() ) beams_buf = torch.cat((beams_buf, next_beams)) next_values = lprobs[beamno].take(next_tokens.view(-1)) scores_buf = torch.cat((scores_buf, next_values)) # At the 0th time step, there is just one beam item if step == 0: break # STEP 3: Compute the "bank" for each candidate. This is the # number of constraints it's generated. We need this so that # we can do round-robin allocation of the beam across these # banks. If C is the number of constraints, we select the best # item in bank C, then the best in bank C-1, etc, followed by # the 2nd-best in bank C, the 2nd-best in bank C-1, etc, and so # on, until the maximum beam size. We accomplish this by # creating a sort key and striping across the banks. # Compute the new states for all candidates cands_size = indices_buf.size(0) constraint_states = [ constraint_states[beams_buf[i]].advance(indices_buf[i]) for i in range(cands_size) ] banks = torch.tensor([state.bank for state in constraint_states], device=device) # STEP 4: Sort num_constraint_tokens = len(state.tokens) # Sort by keys (bank, score) (i.e., sort banks together, and scores # within banks). AFAIK pytorch doesn't support either stable sort or # multi-key sorting, so we have to hack this. MAX_SCORE = -100 sort_key = (num_constraint_tokens - banks) * MAX_SCORE + scores_buf sort_values, sort_indices = sort_key.sort(dim=0, descending=True) scores_buf = scores_buf[sort_indices] indices_buf = indices_buf[sort_indices] beams_buf = beams_buf[sort_indices] banks = banks[sort_indices] # Sort the constraints to follow suit constraint_states = [constraint_states[i] for i in sort_indices] # STEP 5: Remove duplicates. The topk calls (overall and # per-row) plus the per-row generation of constraints will # produce duplicates. Here we remove them. def roll(t): """Rolls a 1d tensor left by 1. [0, 1, 2, 3, 4] becomes [4, 0, 1, 2, 3] """ return torch.cat((t[-1].unsqueeze(0), t[0:-1]), dim=0) # We map candidates (beam, token_id) to a single dimension. # This is then shifted by 1. We can then easily identify # duplicates and create a mask that identifies unique # extensions. uniques_mask = beams_buf * (self.vocab_size + 1) + indices_buf uniques_mask = roll(uniques_mask) != uniques_mask # Use the mask to pare down the data structures scores_buf = torch.masked_select(scores_buf, uniques_mask) indices_buf = torch.masked_select(indices_buf, uniques_mask) beams_buf = torch.masked_select(beams_buf, uniques_mask) banks = torch.masked_select(banks, uniques_mask) i = 1 for mask in uniques_mask[1:]: if not mask: constraint_states.pop(i) i += mask # STEP 6: Assign IDs round-robin across banks, sort, and # truncate. Now that the candidates are sorted by (bank, # score) and uniqed, we dynamically allocate the {beam_size} # beam by striping across the candidates. These stripes will # be used as sort keys to do round-robin selection. This is # accomplished in a single pass with offsets. Sorting by # highest-banks (furthest-along hypotheses) first ensures # progress through the constraints. # # e.g., BANKS: 3 3 3 2 2 2 2 1 1 1 0 0 # OLD STRIPES: 0 1 2 0 1 2 3 0 1 2 0 1 # NEW STRIPES: 0 1+4 2+8 0+1 1+5 2+9 3+11 0+2 1+6 2+10 0+3 1+7 # = 0 5 10 1 6 11 13 2 7 12 3 8 # # Sorting by this then gives the following banks: # # 3 2 1 0 3 2 1 0 3 2 1 2 # # We'll take the top {beam_size} of these. stripe_offsets = [offset * (len(banks) + 1) for offset in range(len(banks) + 1)] stripes = torch.zeros_like(banks) cur_bank_count = -1 cur_bank = banks[0] for i, bank in enumerate(banks): if bank != cur_bank: cur_bank_count = 0 cur_bank = bank else: cur_bank_count += 1 stripes[i] = num_constraint_tokens - bank + stripe_offsets[cur_bank_count] # STEP 7: Sort by the stripes values sort_values, sort_indices = stripes.sort(dim=0) scores_buf = scores_buf[sort_indices] indices_buf = indices_buf[sort_indices] beams_buf = beams_buf[sort_indices] constraint_states = [constraint_states[i] for i in sort_indices] # STEP 8: Truncate to the candidates size! scores_buf = scores_buf[: self.num_cands] indices_buf = indices_buf[: self.num_cands] beams_buf = beams_buf[: self.num_cands] return scores_buf, indices_buf, beams_buf, constraint_states class LengthConstrainedBeamSearch(Search): def __init__(self, tgt_dict, min_len_a, min_len_b, max_len_a, max_len_b): super().__init__(tgt_dict) self.min_len_a = min_len_a self.min_len_b = min_len_b self.max_len_a = max_len_a self.max_len_b = max_len_b self.beam = BeamSearch(tgt_dict) self.needs_src_lengths = True def step( self, step: int, lprobs, scores, prev_output_tokens: Optional[Tensor] = None, original_batch_idxs: Optional[Tensor] = None, ): min_lens = self.min_len_a * self.src_lengths + self.min_len_b max_lens = self.max_len_a * self.src_lengths + self.max_len_b lprobs[step < min_lens, :, self.eos] = -math.inf lprobs[step >= max_lens, :, self.eos] = 0 return self.beam.step(step, lprobs, scores) class DiverseBeamSearch(Search): """Diverse Beam Search. See "Diverse Beam Search: Decoding Diverse Solutions from Neural Sequence Models" for details. We only implement the Hamming Diversity penalty here, which performed best in the original paper. """ def __init__(self, tgt_dict, num_groups, diversity_strength): super().__init__(tgt_dict) self.num_groups = num_groups self.diversity_strength = -diversity_strength self.beam = BeamSearch(tgt_dict) @torch.jit.export def step( self, step: int, lprobs, scores, prev_output_tokens: Optional[Tensor] = None, original_batch_idxs: Optional[Tensor] = None, ): bsz, beam_size, vocab_size = lprobs.size() if beam_size % self.num_groups != 0: raise ValueError( "DiverseBeamSearch requires --beam to be divisible by the number of groups" ) # initialize diversity penalty diversity_buf = torch.zeros(lprobs[:, 0, :].size()).to(lprobs) scores_G, indices_G, beams_G = [], [], [] for g in range(self.num_groups): lprobs_g = lprobs[:, g :: self.num_groups, :] scores_g = scores[:, g :: self.num_groups, :] if step > 0 else None # apply diversity penalty if g > 0: lprobs_g = torch.add( lprobs_g, other=diversity_buf.unsqueeze(1), alpha=self.diversity_strength, ) else: lprobs_g = lprobs_g.contiguous() scores_buf, indices_buf, beams_buf = self.beam.step( step, lprobs_g, scores_g ) beams_buf.mul_(self.num_groups).add_(g) scores_G.append(scores_buf.clone()) indices_G.append(indices_buf.clone()) beams_G.append(beams_buf.clone()) # update diversity penalty diversity_buf.scatter_add_( 1, indices_buf, torch.ones(indices_buf.size()).to(diversity_buf) ) # interleave results from different groups scores_buf = torch.stack(scores_G, dim=2).view(bsz, -1) indices_buf = torch.stack(indices_G, dim=2).view(bsz, -1) beams_buf = torch.stack(beams_G, dim=2).view(bsz, -1) return scores_buf, indices_buf, beams_buf class Sampling(Search): sampling_topk: int sampling_topp: float def __init__(self, tgt_dict, sampling_topk=-1, sampling_topp=-1.0): super().__init__(tgt_dict) self.sampling_topk = sampling_topk self.sampling_topp = sampling_topp def _sample_topp(self, lprobs): """Sample among the smallest set of elements whose cumulative probability mass exceeds p. See `"The Curious Case of Neural Text Degeneration" (Holtzman et al., 2019) <https://arxiv.org/abs/1904.09751>`_. Args: lprobs: (bsz x input_beam_size x vocab_size) the model's log-probabilities over the vocabulary at the current step Return: A tuple of (trimed_probs, truncated_indices) where: trimed_probs: (bsz x input_beam_size x ?) the model's probabilities over the elements selected to sample from. The width of the third dimension is determined by top-P. truncated_indices: (bsz x input_beam_size x ?) the indices of the chosen elements. """ probs = lprobs.exp_() # sort the last dimension (vocab dimension) in descending order sorted_probs, sorted_indices = probs.sort(descending=True) # compute a mask to indicate the words to be included in the top-P set. cumsum_probs = sorted_probs.cumsum(dim=2) mask = cumsum_probs.lt(self.sampling_topp) # note that mask was computed by 'lt'. One more word needs to be included # so that the cumulative probability mass can exceed p. cumsum_mask = mask.cumsum(dim=2) last_included = cumsum_mask[:, :, -1:] last_included.clamp_(0, mask.size()[2] - 1) mask = mask.scatter_(2, last_included, 1) # truncate unnecessary dims. max_dim = last_included.max() truncated_mask = mask[:, :, : max_dim + 1] truncated_probs = sorted_probs[:, :, : max_dim + 1] truncated_indices = sorted_indices[:, :, : max_dim + 1] # trim the words that are not in top-P by setting their probabilities # to 0, so that they would not be sampled later. trim_mask = ~truncated_mask trimed_probs = truncated_probs.masked_fill_(trim_mask, 0) return trimed_probs, truncated_indices @torch.jit.export def step( self, step: int, lprobs, scores, prev_output_tokens: Optional[Tensor] = None, original_batch_idxs: Optional[Tensor] = None, ): bsz, beam_size, vocab_size = lprobs.size() if step == 0: # at the first step all hypotheses are equally likely, so use # only the first beam lprobs = lprobs[:, ::beam_size, :].contiguous() if self.sampling_topp > 0: # only sample from the smallest set of words whose cumulative probability mass exceeds p probs, top_indices = self._sample_topp(lprobs) elif self.sampling_topk > 0: # only sample from top-k candidates lprobs, top_indices = lprobs.topk(self.sampling_topk) probs = lprobs.exp_() else: probs = lprobs.exp_() # dummy data to be consistent with true branch for type check top_indices = torch.empty(0).to(probs) # sample if step == 0: indices_buf = torch.multinomial( probs.view(bsz, -1), beam_size, replacement=True, ).view(bsz, beam_size) else: indices_buf = torch.multinomial( probs.view(bsz * beam_size, -1), 1, replacement=True, ).view(bsz, beam_size) if step == 0: # expand to beam size probs = probs.expand(bsz, beam_size, -1) # gather scores scores_buf = torch.gather(probs, dim=2, index=indices_buf.unsqueeze(-1)) scores_buf = scores_buf.log_().view(bsz, -1) # remap indices if using top-k or top-P sampling if self.sampling_topk > 0 or self.sampling_topp > 0: indices_buf = torch.gather( top_indices.expand(bsz, beam_size, -1), dim=2, index=indices_buf.unsqueeze(-1), ).squeeze(2) if step == 0: beams_buf = indices_buf.new_zeros(bsz, beam_size) else: beams_buf = torch.arange(0, beam_size).to(indices_buf).repeat(bsz, 1) # make scores cumulative scores_buf.add_( torch.gather(scores[:, :, step - 1], dim=1, index=beams_buf) ) return scores_buf, indices_buf, beams_buf class DiverseSiblingsSearch(Search): """ Beam search with diverse siblings. See "A Simple, Fast Diverse Decoding Algorithm for Neural Generation" for details. https://arxiv.org/abs/1611.08562 1/ Calculate hypotheses for each beam 2/ Intra-sibling ordering 3/ Rewrite scores 4/ Choose top K hypotheses if diversity_rate == 0 is equivalent to BeamSearch """ def __init__(self, tgt_dict, diversity_rate): super().__init__(tgt_dict) self.diversity_rate = diversity_rate self.beam = BeamSearch(tgt_dict) def step( self, step: int, lprobs, scores, prev_output_tokens: Optional[Tensor] = None, original_batch_idxs: Optional[Tensor] = None, ): bsz, beam_size, vocab_size = lprobs.size() k = min( # Take the best 2 x beam_size predictions. We'll choose the first # beam_size of these which don't predict eos to continue with. beam_size * 2, lprobs.view(bsz, -1).size(1) - 1, # -1 so we never select pad ) s_list: List[Tensor] i_list: List[Tensor] s_list = [torch.empty(0).to(lprobs) for i in range(beam_size)] i_list = [torch.LongTensor().to(device=lprobs.device) for i in range(beam_size)] sibling_score = torch.arange(1, k + 1).to(lprobs) * self.diversity_rate if step == 0: return self.beam.step(step, lprobs, scores) lprobs.add_(scores[:, :, step - 1].unsqueeze(-1)) # 1/ Calculate hypotheses for each beam for i in range(beam_size): torch.topk(lprobs[:, i, :].view(bsz, -1), k, out=(s_list[i], i_list[i])) i_list[i].fmod_(vocab_size) # 2/ Intra-sibling ordering by default from topk + 3/ Rewrite scores s_list[i].sub_(sibling_score) # 4/ Choose top K hypotheses indices = torch.stack(i_list, dim=1).view(bsz, -1) final_scores = torch.empty(0).to(lprobs) final_indices = torch.LongTensor().to(device=lprobs.device) final_beams = torch.LongTensor().to(device=lprobs.device) (final_scores, final_indices) = torch.topk( torch.stack(s_list, dim=1).view(bsz, -1), k, ) final_beams = final_indices // k for i in range(bsz): final_indices[i] = indices[i][final_indices[i]] return final_scores, final_indices, final_beams
KosmosX-API-main
kosmosX/fairseq/fairseq/search.py
#!/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 logging import os import shutil from typing import List, Optional logger = logging.getLogger(__file__) try: from iopath.common.file_io import g_pathmgr as IOPathManager try: # [FB only - for now] AWS PathHandler for PathManager from .fb_pathhandlers import S3PathHandler IOPathManager.register_handler(S3PathHandler()) except KeyError: logging.warning("S3PathHandler already registered.") except ImportError: logging.debug( "S3PathHandler couldn't be imported. Either missing fb-only files, or boto3 module." ) except ImportError: IOPathManager = None class PathManager: """ Wrapper for insulating OSS I/O (using Python builtin operations) from iopath's PathManager abstraction (for transparently handling various internal backends). """ @staticmethod def open( path: str, mode: str = "r", buffering: int = -1, encoding: Optional[str] = None, errors: Optional[str] = None, newline: Optional[str] = None, ): if IOPathManager: return IOPathManager.open( path=path, mode=mode, buffering=buffering, encoding=encoding, errors=errors, newline=newline, ) return open( path, mode=mode, buffering=buffering, encoding=encoding, errors=errors, newline=newline, ) @staticmethod def copy(src_path: str, dst_path: str, overwrite: bool = False) -> bool: if IOPathManager: return IOPathManager.copy( src_path=src_path, dst_path=dst_path, overwrite=overwrite ) return shutil.copyfile(src_path, dst_path) @staticmethod def get_local_path(path: str, **kwargs) -> str: if IOPathManager: return IOPathManager.get_local_path(path, **kwargs) return path @staticmethod def exists(path: str) -> bool: if IOPathManager: return IOPathManager.exists(path) return os.path.exists(path) @staticmethod def isfile(path: str) -> bool: if IOPathManager: return IOPathManager.isfile(path) return os.path.isfile(path) @staticmethod def ls(path: str) -> List[str]: if IOPathManager: return IOPathManager.ls(path) return os.listdir(path) @staticmethod def mkdirs(path: str) -> None: if IOPathManager: return IOPathManager.mkdirs(path) os.makedirs(path, exist_ok=True) @staticmethod def rm(path: str) -> None: if IOPathManager: return IOPathManager.rm(path) os.remove(path) @staticmethod def chmod(path: str, mode: int) -> None: if not PathManager.path_requires_pathmanager(path): os.chmod(path, mode) @staticmethod def register_handler(handler) -> None: if IOPathManager: return IOPathManager.register_handler(handler=handler) @staticmethod def copy_from_local( local_path: str, dst_path: str, overwrite: bool = False, **kwargs ) -> None: if IOPathManager: return IOPathManager.copy_from_local( local_path=local_path, dst_path=dst_path, overwrite=overwrite, **kwargs ) return shutil.copyfile(local_path, dst_path) @staticmethod def path_requires_pathmanager(path: str) -> bool: """Do we require PathManager to access given path?""" if IOPathManager: for p in IOPathManager._path_handlers.keys(): if path.startswith(p): return True return False @staticmethod def supports_rename(path: str) -> bool: # PathManager doesn't yet support renames return not PathManager.path_requires_pathmanager(path) @staticmethod def rename(src: str, dst: str): os.rename(src, dst) """ ioPath async PathManager methods: """ @staticmethod def opena( path: str, mode: str = "r", buffering: int = -1, encoding: Optional[str] = None, errors: Optional[str] = None, newline: Optional[str] = None, ): """ Return file descriptor with asynchronous write operations. """ global IOPathManager if not IOPathManager: logging.info("ioPath is initializing PathManager.") try: from iopath.common.file_io import PathManager IOPathManager = PathManager() except Exception: logging.exception("Failed to initialize ioPath PathManager object.") return IOPathManager.opena( path=path, mode=mode, buffering=buffering, encoding=encoding, errors=errors, newline=newline, ) @staticmethod def async_close() -> bool: """ Wait for files to be written and clean up asynchronous PathManager. NOTE: `PathManager.async_close()` must be called at the end of any script that uses `PathManager.opena(...)`. """ global IOPathManager if IOPathManager: return IOPathManager.async_close() return False
KosmosX-API-main
kosmosX/fairseq/fairseq/file_io.py
# 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. """ Train a network across multiple GPUs. """ import contextlib import logging import os import sys import time from argparse import Namespace from itertools import chain from typing import Any, Dict, List import torch from fairseq import checkpoint_utils, models, optim, utils from fairseq.dataclass.configs import FairseqConfig from fairseq.dataclass.utils import convert_namespace_to_omegaconf from fairseq.distributed import utils as distributed_utils from fairseq.file_io import PathManager from fairseq.logging import meters, metrics # from fairseq.models.ema import build_ema from fairseq.nan_detector import NanDetector from fairseq.optim import lr_scheduler from fairseq.utils import safe_hasattr from omegaconf import OmegaConf logger = logging.getLogger(__name__) class Trainer(object): """Main class for data parallel training. This class supports synchronous distributed data parallel training, where multiple workers each have a full model replica and gradients are accumulated across workers before each update. We use :class:`~torch.nn.parallel.DistributedDataParallel` to handle communication of the gradients across workers. """ def __init__(self, cfg: FairseqConfig, task, model, criterion, quantizer=None): if isinstance(cfg, Namespace): logger.warning( "argparse.Namespace configuration is deprecated! Automatically converting to OmegaConf" ) cfg = convert_namespace_to_omegaconf(cfg) self.cfg = cfg self.task = task # catalog shared parameters shared_params = _catalog_shared_params(model) self.tpu = cfg.common.tpu self.cuda = torch.cuda.is_available() and not cfg.common.cpu and not self.tpu if self.cuda: self.device = torch.device("cuda") elif self.tpu: self.device = utils.get_tpu_device() else: self.device = torch.device("cpu") if self.is_fsdp: import fairscale if self.cfg.common.bf16: raise ValueError( "FullyShardedDataParallel is not compatible with --bf16 or " "--memory-efficient-bf16" ) if self.cfg.distributed_training.zero_sharding != "none": raise ValueError( "FullyShardedDataParallel is not compatible with --zero-sharding " "option (it's already built in)" ) if ( max(self.cfg.optimization.update_freq) > 1 and fairscale.__version__ < "0.4.0" ): raise RuntimeError( "Please update to fairscale 0.4.0 or newer when combining " "--update-freq with FullyShardedDataParallel" ) else: if ( hasattr(self.cfg.distributed_training, "cpu_offload") and self.cfg.distributed_training.cpu_offload ): raise ValueError("--cpu-offload requires --ddp-backend=fully_sharded") # copy model and criterion to current device/dtype self._criterion = criterion self._model = model if not self.is_fsdp: if cfg.common.fp16: assert not cfg.common.amp, "Cannot use fp16 and AMP together" self._criterion = self._criterion.half() self._model = self._model.half() elif cfg.common.bf16: self._criterion = self._criterion.to(dtype=torch.bfloat16) self._model = self._model.to(dtype=torch.bfloat16) elif cfg.common.amp: self._amp_retries = 0 if ( not cfg.distributed_training.pipeline_model_parallel # the DistributedFairseqModel wrapper will handle moving to device, # so only handle cases which don't use the wrapper and not self.use_distributed_wrapper ): self._criterion = self._criterion.to(device=self.device) self._model = self._model.to(device=self.device) self.pipeline_model_parallel = cfg.distributed_training.pipeline_model_parallel self.last_device = None if self.cuda and self.pipeline_model_parallel: self.last_device = torch.device( cfg.distributed_training.pipeline_devices[-1] ) # check that shared parameters are preserved after device transfer for shared_param in shared_params: ref = _get_module_by_path(self._model, shared_param[0]) for path in shared_param[1:]: logger.info( "detected shared parameter: {} <- {}".format(shared_param[0], path) ) _set_module_by_path(self._model, path, ref) self._dummy_batch = None # indicates we don't have a dummy batch at first self._lr_scheduler = None self._num_updates = 0 self._num_xla_compiles = 0 # for TPUs self._optim_history = None self._optimizer = None self._warn_once = set() self._wrapped_criterion = None self._wrapped_model = None self._ema = None # TODO(myleott): support tpu if self.cuda and self.data_parallel_world_size > 1: self._grad_norm_buf = torch.cuda.DoubleTensor(self.data_parallel_world_size) else: self._grad_norm_buf = None self.quantizer = quantizer if self.quantizer is not None: self.quantizer.set_trainer(self) # get detailed cuda environment if self.cuda: self.cuda_env = utils.CudaEnvironment() if self.data_parallel_world_size > 1: self.cuda_env_arr = distributed_utils.all_gather_list( self.cuda_env, group=distributed_utils.get_global_group() ) else: self.cuda_env_arr = [self.cuda_env] if self.data_parallel_rank == 0: utils.CudaEnvironment.pretty_print_cuda_env_list(self.cuda_env_arr) else: self.cuda_env = None self.cuda_env_arr = None metrics.log_start_time("wall", priority=790, round=0) self._start_time = time.time() self._previous_training_time = 0 self._cumulative_training_time = None def reinitialize(self): """Reinitialize the Trainer, typically after model params change.""" self._lr_scheduler = None self._optimizer = None self._wrapped_criterion = None self._wrapped_model = None @property def data_parallel_world_size(self): if self.cfg.distributed_training.distributed_world_size == 1: return 1 return distributed_utils.get_data_parallel_world_size() @property def data_parallel_process_group(self): return distributed_utils.get_data_parallel_group() @property def data_parallel_rank(self): if self.cfg.distributed_training.distributed_world_size == 1: return 0 return distributed_utils.get_data_parallel_rank() @property def is_data_parallel_master(self): # NOTE: this returns true for all model parallel replicas with data # parallel rank 0 return self.data_parallel_rank == 0 @property def use_distributed_wrapper(self) -> bool: return ( self.data_parallel_world_size > 1 and not self.cfg.optimization.use_bmuf ) or (self.is_fsdp and self.cfg.distributed_training.cpu_offload) @property def should_save_checkpoint_on_current_rank(self) -> bool: """Indicates whether to save checkpoints on the current DDP rank.""" if ( self.is_fsdp and self.cfg.distributed_training.use_sharded_state ) or getattr(self.cfg.model, "base_layers", 0) > 0: return True else: return self.is_data_parallel_master @property def always_call_state_dict_during_save_checkpoint(self) -> bool: if self.is_fsdp and not self.cfg.distributed_training.use_sharded_state: # FSDP calls communication collective when consolidating checkpoints return True else: return False @property def checkpoint_suffix(self) -> str: """Suffix to add to the checkpoint file name.""" if self.is_fsdp and self.cfg.distributed_training.use_sharded_state: return self.cfg.checkpoint.checkpoint_suffix + "-shard{0}".format( self.data_parallel_rank ) else: return self.cfg.checkpoint.checkpoint_suffix or "" @property def criterion(self): if self._wrapped_criterion is None: if utils.has_parameters(self._criterion) and self.use_distributed_wrapper: self._wrapped_criterion = models.DistributedFairseqModel( self.cfg.distributed_training, self._criterion, process_group=self.data_parallel_process_group, device=self.device, ) else: self._wrapped_criterion = self._criterion return self._wrapped_criterion @property def model(self): if self._wrapped_model is None: if self.use_distributed_wrapper: self._wrapped_model = models.DistributedFairseqModel( self.cfg.distributed_training, self._model, process_group=self.data_parallel_process_group, device=self.device, ) else: self._wrapped_model = self._model return self._wrapped_model @property def ema(self): if self._ema is None: self._build_ema() return self._ema def _build_ema(self): if self.cfg.ema.store_ema: self._ema = build_ema(self._model, self.cfg.ema, self.device) logger.info("Exponential Moving Average Shadow Model is initialized.") @property def optimizer(self): if self._optimizer is None: self._build_optimizer() return self._optimizer @property def lr_scheduler(self): if self._lr_scheduler is None: self._build_optimizer() # this will initialize self._lr_scheduler return self._lr_scheduler def _build_optimizer(self): params = list( filter( lambda p: p.requires_grad, chain(self.model.parameters(), self.criterion.parameters()), ) ) if self.is_fsdp and self.cfg.common.fp16: # FullyShardedDataParallel always uses MemoryEfficientFP16 wrapper, # mostly for the grad scaling. But if we don't have the # --memory-efficient-fp16 flag set, then we're effectively doing # regular --fp16 and can allow the use of optimizers that would # otherwise be unsupported by MemoryEfficientFP16Optimizer. allow_unsupported = not self.cfg.common.memory_efficient_fp16 self._optimizer = optim.MemoryEfficientFP16Optimizer.build_optimizer( self.cfg, params, allow_unsupported=allow_unsupported ) elif self.cfg.common.fp16 or self.cfg.common.bf16 or self.cfg.common.amp: if self.cuda and torch.cuda.get_device_capability(0)[0] < 7: logger.info( "NOTE: your device does NOT support faster training with --fp16 or --amp, " "please switch to FP32 which is likely to be faster" ) if ( self.cfg.common.memory_efficient_fp16 or self.cfg.common.memory_efficient_bf16 ): self._optimizer = optim.MemoryEfficientFP16Optimizer.build_optimizer( self.cfg, params ) elif self.cfg.common.amp: self._optimizer = optim.AMPOptimizer.build_optimizer(self.cfg, params) else: self._optimizer = optim.FP16Optimizer.build_optimizer(self.cfg, params) else: if self.cuda and torch.cuda.get_device_capability(0)[0] >= 7: logger.info( "NOTE: your device may support faster training with --fp16 or --amp" ) self._optimizer = optim.build_optimizer(self.cfg.optimizer, params) if self.is_fsdp: assert ( not self.cfg.optimization.use_bmuf ), "--ddp-backend=fully_sharded is not compatible with BMUF" assert self._optimizer.supports_flat_params, ( "--ddp-backend=fully_sharded is only compatible with pointwise " "optimizers (e.g., Adam, AdamW, Adadelta, Adamax, SGD, etc.). " "However, the sharding will result in slightly different results when " "using non-pointwise optimizers (e.g., Adagrad, Adafactor, LAMB)" ) if self.cfg.optimization.use_bmuf: self._optimizer = optim.FairseqBMUF( self.cfg.bmuf, self._optimizer, ) if self.cfg.distributed_training.zero_sharding == "os": if ( self.cfg.common.fp16 and not self.cfg.common.memory_efficient_fp16 and not self.cfg.common.memory_efficient_bf16 ) and not self.cfg.common.fp16_no_flatten_grads: raise ValueError( "ZeRO is incomptabile with fp16 and flattened grads. " "Please use --fp16-no-flatten-grads" ) else: optim.shard_(self._optimizer, self.data_parallel_process_group) # We should initialize the learning rate scheduler immediately after # building the optimizer, so that the initial learning rate is set. self._lr_scheduler = lr_scheduler.build_lr_scheduler( self.cfg.lr_scheduler, self.optimizer, ) self._lr_scheduler.step_update(0) @property def is_fsdp(self): return self.cfg.distributed_training.ddp_backend == "fully_sharded" def consolidate_optimizer(self): """For OSS, we need to consolidate the state dict.""" if self.cfg.checkpoint.no_save_optimizer_state: return self._gathered_optim_state = None if hasattr(self.optimizer.optimizer, "consolidate_state_dict"): self.optimizer.optimizer.consolidate_state_dict() elif self.is_fsdp and not self.model.use_sharded_state: st = self.model.gather_full_optim_state_dict( self.optimizer ) # only returns on rank 0 self._gathered_optim_state = st def state_dict(self): state_dict = { "args": None, # legacy "cfg": ( OmegaConf.to_container(self.cfg, resolve=True, enum_to_str=True) if OmegaConf.is_config(self.cfg) else self.cfg ), "model": self.model.state_dict(), "criterion": ( self.criterion.state_dict() if utils.has_parameters(self.criterion) else None ), "optimizer_history": (self._optim_history or []) + [ { "criterion_name": self.get_criterion().__class__.__name__, "optimizer_name": self.optimizer.__class__.__name__, "lr_scheduler_state": self.lr_scheduler.state_dict(), "num_updates": self.get_num_updates(), } ], "task_state": self.task.state_dict() if self.task is not None else {}, "extra_state": { "metrics": metrics.state_dict(), "previous_training_time": self.cumulative_training_time(), }, } if self.cfg.ema.store_ema: # Save EMA model state as extra state state_dict["extra_state"]["ema"] = self.ema.get_model().state_dict() if self.cfg.ema.ema_fp32: # Save EMA params in fp32 state_dict["extra_state"]["ema_fp32_params"] = self.ema.fp32_params if not self.cfg.checkpoint.no_save_optimizer_state: if self._gathered_optim_state is not None: state_dict["last_optimizer_state"] = self._gathered_optim_state self._gathered_optim_state = None else: state_dict["last_optimizer_state"] = self.optimizer.state_dict() if self.is_fsdp: # save meta data for recombining checkpoint upon loading state_dict["fsdp_metadata"] = self.model.local_metadata_dict() return state_dict def save_checkpoint(self, filename, extra_state): """Save all training state in a checkpoint file.""" logger.info(f"Saving checkpoint to {os.path.abspath(filename)}") # call state_dict on all ranks in case it needs internal communication state_dict = utils.move_to_cpu(self.state_dict()) state_dict["extra_state"].update(extra_state) if self.should_save_checkpoint_on_current_rank: checkpoint_utils.torch_persistent_save( state_dict, filename, async_write=self.cfg.checkpoint.write_checkpoints_asynchronously, ) logger.info(f"Finished saving checkpoint to {os.path.abspath(filename)}") def load_checkpoint( self, filename, reset_optimizer=False, reset_lr_scheduler=False, optimizer_overrides=None, reset_meters=False, ): """ Load all training state from a checkpoint file. rank = 0 will load the checkpoint, and then broadcast it to all other ranks. """ extra_state, self._optim_history, last_optim_state = None, [], None logger.info(f"Preparing to load checkpoint {filename}") is_distributed = self.data_parallel_world_size > 1 bexists = PathManager.isfile(filename) if bexists: load_on_all_ranks = ( self.cfg.checkpoint.load_checkpoint_on_all_dp_ranks # TPUs don't support broadcast yet, so load checkpoints # on every worker for now or self.tpu # FSDP requires loading checkpoint shards on all ranks or (self.is_fsdp and self.cfg.distributed_training.use_sharded_state) or getattr(self.cfg.model, "base_layers", 0) > 0 ) if load_on_all_ranks or self.data_parallel_rank == 0: state = checkpoint_utils.load_checkpoint_to_cpu( filename, load_on_all_ranks=load_on_all_ranks ) last_optim_state = state.get("last_optimizer_state", None) # If doing zero_sharding, do not broadcast global optimizer # state. Later we will broadcast sharded states to each rank # to avoid memory from exploding. if ( not load_on_all_ranks and self.cfg.distributed_training.zero_sharding == "os" and "last_optimizer_state" in state and is_distributed ): state["last_optimizer_state"] = "SHARDED" else: last_optim_state = None state = None if is_distributed and not load_on_all_ranks: state = distributed_utils.broadcast_object( state, src_rank=0, group=self.data_parallel_process_group, dist_device=self.device, ) if self.data_parallel_rank > 0: last_optim_state = state.get("last_optimizer_state", None) # load model parameters try: if ( "optimizer_history" in state and len(state["optimizer_history"]) > 0 and "num_updates" in state["optimizer_history"][-1] ): self.model.set_num_updates( state["optimizer_history"][-1]["num_updates"] ) # this is the code related to AdaPrune # In short, it removes redundant heads in multi-head attention module based on heads importance provided # For more info, please refer to the paper: https://openreview.net/forum?id=_CMSV7FTzGI # The idea of prune in mha can be summarized as # Fine tune model (e.g. roberta encoder) on a certain datasets with regularization # After the model is trained. User could use get_reserve_head_index and _adaptive_prune_heads functions to get the top X heads with most importance. # Then user uses the rank to prune a new roberta encoder and save the pruned ckpt manually. # User will fine tune the the new roberta encoder via the ckpt saved above # To get rid of registering different pruned version of Roberta, I use the argument --mha-heads-to-keep to prune the Roberta model into a pruned version which matches the pruned ckpt. if ( safe_hasattr(self.model, "args") and safe_hasattr(self.model.args, "mha_heads_to_keep") and self.model.args.mha_heads_to_keep != -1 ): logger.info( f"Prune model: keep {self.model.args.mha_heads_to_keep} heads for each multihead attention module" ) for layer in self.model.encoder.sentence_encoder.layers: reserve_head_index = layer.self_attn._get_reserve_head_index( num_heads_to_keep=self.model.args.mha_heads_to_keep ) layer.self_attn._adaptive_prune_heads( reserve_head_index=reserve_head_index ) layer.self_attn._set_skip_embed_dim_check() logger.info(self.model) # this is the code related to AdaPrune # In short, it removes redundant units in feedforward layer in each transformer layer based on importance # For more info, please refer to the paper: https://openreview.net/forum?id=_CMSV7FTzGI # The idea of prune in ffn can be summarized as # Fine tune model (e.g. roberta encoder) on a certain datasets with regularization # After the model is trained. User could use _get_fc_rank and _prune_fc_layer functions to get the top X units with most importance. # Then user uses the rank to prune a new roberta encoder and save the pruned ckpt manually. # User will fine tune the the new roberta encoder via the ckpt saved above # To get rid of registering different pruned version of Roberta, I use the argument --ffn-blocks-to-remove to prune the Roberta model into a pruned version which matches the pruned ckpt. if ( safe_hasattr(self.model, "args") and safe_hasattr(self.model.args, "ffn_blocks_to_remove") and self.model.args.ffn_blocks_to_remove != -1 ): logger.info( f"Prune model: remove {self.model.args.ffn_blocks_to_remove} ffn blocks for each transformer layer" ) for layer in self.model.encoder.sentence_encoder.layers: remove_index = layer._get_fc_rank( remove_num=self.model.args.ffn_blocks_to_remove ) layer._prune_fc_layer(remove_index=remove_index) logger.info(self.model) self.model.load_state_dict( state["model"], strict=True, model_cfg=self.cfg.model ) # save memory for later steps del state["model"] if utils.has_parameters(self.get_criterion()): self.get_criterion().load_state_dict( state["criterion"], strict=True ) del state["criterion"] except Exception: raise Exception( "Cannot load model parameters from checkpoint {}; " "please ensure that the architectures match.".format(filename) ) extra_state = state["extra_state"] self._optim_history = state["optimizer_history"] if last_optim_state is not None and not reset_optimizer: # rebuild optimizer after loading model, since params may have changed self._build_optimizer() # only reload optimizer and lr_scheduler if they match last_optim = self._optim_history[-1] assert ( last_optim["criterion_name"] == self.get_criterion().__class__.__name__ ), f"Criterion does not match; please reset the optimizer (--reset-optimizer). {last_optim['criterion_name']} vs {self.get_criterion().__class__.__name__}" assert ( last_optim["optimizer_name"] == self.optimizer.__class__.__name__ ), f"Optimizer does not match; please reset the optimizer (--reset-optimizer). {last_optim['optimizer_name']} vs {self.optimizer.__class__.__name__}" if not reset_lr_scheduler: self.lr_scheduler.load_state_dict(last_optim["lr_scheduler_state"]) if self.is_fsdp and not self.model.use_sharded_state: # if use_sharded_state, the last_optim_state is already sharded, skip this last_optim_state = self.model.get_shard_from_optim_state_dict( last_optim_state ) elif not load_on_all_ranks and is_distributed: last_optim_state = self.optimizer.broadcast_global_state_dict( last_optim_state ) self.optimizer.load_state_dict(last_optim_state, optimizer_overrides) self.set_num_updates(last_optim["num_updates"]) if extra_state is not None: itr_state = extra_state["train_iterator"] if type(itr_state) == list: # assert len(itr_state) == self.data_parallel_world_size itr_state = itr_state[self.data_parallel_rank % len(itr_state)] extra_state["train_iterator"] = itr_state epoch = itr_state.get("epoch", 1) if "previous_training_time" in extra_state: self._previous_training_time = extra_state["previous_training_time"] self._start_time = time.time() self.lr_step(epoch) if ( itr_state.get("version", 1) >= 2 and itr_state["iterations_in_epoch"] == 0 ): # reset meters at start of epoch reset_meters = True if "metrics" in extra_state and not reset_meters: metrics.load_state_dict(extra_state["metrics"]) # reset TimeMeters, since their start times don't make sense anymore for meter in metrics.get_meters("default"): if isinstance(meter, meters.TimeMeter): meter.reset() if self.cfg.ema.store_ema: if "ema" not in extra_state: logger.warn( "EMA not found in checkpoint. But store_ema is True. " "EMA is re-initialized from checkpoint." ) self.ema.restore( state["model"], build_fp32_params=self.cfg.ema.ema_fp32 ) else: logger.info("Loading EMA from checkpoint") self.ema.restore(extra_state["ema"], build_fp32_params=False) if self.cfg.ema.ema_fp32: if "ema_fp32_params" in extra_state: logger.info("Loading EMA fp32 params from checkpoint") self.ema.build_fp32_params(extra_state["ema_fp32_params"]) else: logger.info( "Building EMA fp32 params from EMA model in checkpoint" ) self.ema.build_fp32_params() logger.info( "Loaded checkpoint {} (epoch {} @ {} updates)".format( filename, epoch, self.get_num_updates() ) ) else: logger.info("No existing checkpoint found {}".format(filename)) return extra_state def get_train_iterator( self, epoch, combine=True, load_dataset=True, data_selector=None, shard_batch_itr=True, disable_iterator_cache=False, ): """Return an EpochBatchIterator over the training set for a given epoch.""" if load_dataset: logger.info("loading train data for epoch {}".format(epoch)) self.task.load_dataset( self.cfg.dataset.train_subset, epoch=epoch, combine=combine, data_selector=data_selector, tpu=self.tpu, ) batch_iterator = self.task.get_batch_iterator( dataset=self.task.dataset(self.cfg.dataset.train_subset), max_tokens=self.cfg.dataset.max_tokens, max_sentences=self.cfg.dataset.batch_size, max_positions=utils.resolve_max_positions( self.task.max_positions(), self.model.max_positions(), self.cfg.dataset.max_tokens, ), ignore_invalid_inputs=True, required_batch_size_multiple=self.cfg.dataset.required_batch_size_multiple, seed=(self.cfg.common.seed + epoch) if self.cfg.dataset.update_ordered_indices_seed else self.cfg.common.seed, num_shards=self.data_parallel_world_size if shard_batch_itr else 1, shard_id=self.data_parallel_rank if shard_batch_itr else 0, num_workers=self.cfg.dataset.num_workers, epoch=epoch, data_buffer_size=self.cfg.dataset.data_buffer_size, disable_iterator_cache=disable_iterator_cache, skip_remainder_batch=self.cfg.optimization.skip_remainder_batch, grouped_shuffling=self.cfg.dataset.grouped_shuffling, update_epoch_batch_itr=self.cfg.dataset.update_epoch_batch_itr, ) self.reset_dummy_batch(batch_iterator.first_batch) return batch_iterator def get_valid_iterator( self, subset, disable_iterator_cache=False, ): """Return an EpochBatchIterator over given validation subset for a given epoch.""" batch_iterator = self.task.get_batch_iterator( dataset=self.task.dataset(subset), max_tokens=self.cfg.dataset.max_tokens_valid, max_sentences=self.cfg.dataset.batch_size_valid, max_positions=utils.resolve_max_positions( self.task.max_positions(), self.model.max_positions(), ), ignore_invalid_inputs=self.cfg.dataset.skip_invalid_size_inputs_valid_test, required_batch_size_multiple=self.cfg.dataset.required_batch_size_multiple, seed=self.cfg.common.seed, num_shards=self.data_parallel_world_size, shard_id=self.data_parallel_rank, num_workers=self.cfg.dataset.num_workers, # always pass a fixed "epoch" to keep validation data consistent # across training epochs epoch=1, data_buffer_size=self.cfg.dataset.data_buffer_size, disable_iterator_cache=disable_iterator_cache, skip_remainder_batch=False, ) self.reset_dummy_batch(batch_iterator.first_batch) return batch_iterator def begin_epoch(self, epoch): """Called at the beginning of each epoch.""" logger.info("begin training epoch {}".format(epoch)) self.lr_step_begin_epoch(epoch) if self.quantizer is not None: self.quantizer.begin_epoch(epoch) # task specific setup per epoch self.task.begin_epoch(epoch, self.get_model()) if self.tpu: import torch_xla.core.xla_model as xm xm.rendezvous("begin_epoch") # wait for all workers xm.mark_step() def begin_valid_epoch(self, epoch): """Called at the beginning of each validation epoch.""" # task specific setup per validation epoch self.task.begin_valid_epoch(epoch, self.get_model()) def reset_dummy_batch(self, batch): self._dummy_batch = batch @metrics.aggregate("train") def train_step(self, samples, raise_oom=False): """Do forward, backward and parameter update.""" self._set_seed() self.model.train() self.criterion.train() self.zero_grad() metrics.log_start_time("train_wall", priority=800, round=0) # If EMA is enabled through store_ema=True # and task.uses_ema is True, pass the EMA model as a keyword # argument to the task. extra_kwargs = {} if self.cfg.ema.store_ema and getattr(self.task, "uses_ema", False): extra_kwargs["ema_model"] = self.ema.get_model() # forward and backward pass logging_outputs, sample_size, ooms = [], 0, 0 for i, sample in enumerate(samples): # delayed update loop sample, is_dummy_batch = self._prepare_sample(sample) def maybe_no_sync(): """ Whenever *samples* contains more than one mini-batch, we want to accumulate gradients locally and only call all-reduce in the last backwards pass. """ if ( self.data_parallel_world_size > 1 and hasattr(self.model, "no_sync") and i < len(samples) - 1 # The no_sync context manager results in increased memory # usage with FSDP, since full-size gradients will be # accumulated on each GPU. It's typically a better tradeoff # to do the extra communication with FSDP. and not self.is_fsdp ): return self.model.no_sync() else: return contextlib.ExitStack() # dummy contextmanager try: with maybe_no_sync(): # forward and backward loss, sample_size_i, logging_output = self.task.train_step( sample=sample, model=self.model, criterion=self.criterion, optimizer=self.optimizer, update_num=self.get_num_updates(), ignore_grad=is_dummy_batch, **extra_kwargs, ) del loss logging_outputs.append(logging_output) sample_size += sample_size_i # emptying the CUDA cache after the first step can # reduce the chance of OOM if self.cuda and self.get_num_updates() == 0: torch.cuda.empty_cache() except RuntimeError as e: if "out of memory" in str(e): self._log_oom(e) if raise_oom: raise e logger.warning( "attempting to recover from OOM in forward/backward pass" ) ooms += 1 self.zero_grad() if self.cuda: torch.cuda.empty_cache() if self.cfg.distributed_training.distributed_world_size == 1: return None else: raise e except Exception: self.consolidate_optimizer() self.save_checkpoint( os.path.join(self.cfg.checkpoint.save_dir, "crash.pt"), {} ) raise if self.tpu and i < len(samples) - 1: # tpu-comment: every XLA operation before marking step is # appended to the IR graph, and processing too many batches # before marking step can lead to OOM errors. # To handle gradient accumulation use case, we explicitly # mark step here for every forward pass without a backward pass self._xla_markstep_and_send_to_cpu() if is_dummy_batch: if torch.is_tensor(sample_size): sample_size.zero_() else: sample_size *= 0.0 if torch.is_tensor(sample_size): sample_size = sample_size.float() else: sample_size = float(sample_size) # gather logging outputs from all replicas if self._sync_stats(): train_time = self._local_cumulative_training_time() ( logging_outputs, ( sample_size, ooms, total_train_time, ), ) = self._aggregate_logging_outputs( logging_outputs, sample_size, ooms, train_time, ignore=is_dummy_batch ) self._cumulative_training_time = ( total_train_time / self.data_parallel_world_size ) overflow = False try: with torch.autograd.profiler.record_function("reduce-grads"): # reduce gradients across workers self.optimizer.all_reduce_grads(self.model) if utils.has_parameters(self.criterion): self.optimizer.all_reduce_grads(self.criterion) with torch.autograd.profiler.record_function("multiply-grads"): # multiply gradients by (data_parallel_size / sample_size) since # DDP normalizes by the number of data parallel workers for # improved fp16 precision. # Thus we get (sum_of_gradients / sample_size) at the end. # In case of fp16, this step also undoes loss scaling. # (Debugging note: Some optimizers perform this scaling on the # fly, so inspecting model.parameters() or optimizer.params may # still show the original, unscaled gradients.) numer = ( self.data_parallel_world_size if not self.cfg.optimization.use_bmuf or self._sync_stats() else 1 ) self.optimizer.multiply_grads(numer / (sample_size or 1.0)) # Note: (sample_size or 1.0) handles the case of a zero gradient, in a # way that avoids CPU/device transfers in case sample_size is a GPU or # TPU object. The assumption is that the gradient itself is also 0. with torch.autograd.profiler.record_function("clip-grads"): # clip grads grad_norm = self.clip_grad_norm(self.cfg.optimization.clip_norm) # check that grad norms are consistent across workers # on tpu check tensor is slow if not self.tpu: if ( not self.cfg.optimization.use_bmuf and self.cfg.distributed_training.ddp_backend != "slowmo" ): self._check_grad_norms(grad_norm) if not torch.isfinite(grad_norm).all(): # in case of AMP, if gradients are Nan/Inf then # optimizer step is still required if self.cfg.common.amp: overflow = True else: # check local gradnorm single GPU case, trigger NanDetector raise FloatingPointError("gradients are Nan/Inf") with torch.autograd.profiler.record_function("optimizer"): # take an optimization step self.task.optimizer_step( self.optimizer, model=self.model, update_num=self.get_num_updates() ) if self.cfg.common.amp and overflow: if self._amp_retries == self.cfg.common.amp_batch_retries: logger.info("AMP: skipping this batch.") self._amp_retries = 0 else: self._amp_retries += 1 return self.train_step( samples, raise_oom ) # recursion to feed in same batch except FloatingPointError: self.consolidate_optimizer() self.save_checkpoint( os.path.join(self.cfg.checkpoint.save_dir, "crash.pt"), {} ) # re-run the forward and backward pass with hooks attached to print # out where it fails self.zero_grad() with NanDetector(self.get_model()): for _, sample in enumerate(samples): sample, _ = self._prepare_sample(sample) self.task.train_step( sample, self.model, self.criterion, self.optimizer, self.get_num_updates(), ignore_grad=False, **extra_kwargs, ) raise except OverflowError as e: overflow = True logger.info( f"NOTE: gradient overflow detected, ignoring gradient, {str(e)}" ) grad_norm = torch.tensor(0.0).cuda() self.zero_grad() except RuntimeError as e: if "out of memory" in str(e): self._log_oom(e) logger.error("OOM during optimization, irrecoverable") raise e # Some distributed wrappers (e.g., SlowMo) need access to the optimizer # after the step if hasattr(self.model, "perform_slowmo"): self.model.perform_slowmo( self.optimizer.optimizer, getattr(self.optimizer, "fp32_params", None) ) logging_output = None if not overflow or self.cfg.distributed_training.ddp_backend == "slowmo": self.set_num_updates(self.get_num_updates() + 1) if self.cfg.ema.store_ema: # Step EMA forward with new model. self.ema.step( self.get_model(), self.get_num_updates(), ) metrics.log_scalar( "ema_decay", self.ema.get_decay(), priority=10000, round=5, weight=0, ) if self.tpu: import torch_xla.core.xla_model as xm # mark step on TPUs self._xla_markstep_and_send_to_cpu() # only log stats every log_interval steps # this causes wps to be misreported when log_interval > 1 logging_output = {} if self.get_num_updates() % self.cfg.common.log_interval == 0: # log memory usage mem_info = xm.get_memory_info(self.device) gb_free = mem_info["kb_free"] / 1024 / 1024 gb_total = mem_info["kb_total"] / 1024 / 1024 metrics.log_scalar( "gb_free", gb_free, priority=1500, round=1, weight=0 ) metrics.log_scalar( "gb_total", gb_total, priority=1600, round=1, weight=0 ) logging_outputs = self._xla_markstep_and_send_to_cpu( logging_outputs ) logging_output = self._reduce_and_log_stats( logging_outputs, sample_size, grad_norm ) # log whenever there's an XLA compilation, since these # slow down training and may indicate opportunities for # optimization self._check_xla_compilation() else: if self.cuda and self.cuda_env is not None: # log minimum free memory over the iteration gb_used = torch.cuda.max_memory_allocated() / 1024 / 1024 / 1024 torch.cuda.reset_peak_memory_stats() gb_free = self.cuda_env.total_memory_in_GB - gb_used metrics.log_scalar( "gb_free", gb_free, priority=1500, round=1, weight=0 ) # log stats logging_output = self._reduce_and_log_stats( logging_outputs, sample_size, grad_norm ) # clear CUDA cache to reduce memory fragmentation if ( self.cuda and self.cfg.common.empty_cache_freq > 0 and ( (self.get_num_updates() + self.cfg.common.empty_cache_freq - 1) % self.cfg.common.empty_cache_freq ) == 0 ): torch.cuda.empty_cache() if self.cfg.common.fp16 or self.cfg.common.amp: metrics.log_scalar( "loss_scale", ( self.optimizer.scaler.loss_scale if self.cfg.common.fp16 else self.optimizer.scaler.get_scale() ), priority=700, round=4, weight=0, ) metrics.log_stop_time("train_wall") return logging_output @metrics.aggregate("valid") def valid_step(self, sample, raise_oom=False): """Do forward pass in evaluation mode.""" if self.tpu: import torch_xla.core.xla_model as xm xm.rendezvous("valid_step") # wait for all workers # If EMA is enabled through store_ema=True # and task.uses_ema is True, pass the EMA model as a keyword # argument to the task. extra_kwargs = {} if self.cfg.ema.store_ema and getattr(self.task, "uses_ema", False): extra_kwargs["ema_model"] = self.ema.get_model() with torch.no_grad(): self.model.eval() self.criterion.eval() sample, is_dummy_batch = self._prepare_sample(sample) try: _loss, sample_size, logging_output = self.task.valid_step( sample, self.model, self.criterion, **extra_kwargs ) except RuntimeError as e: if "out of memory" in str(e): self._log_oom(e) if not raise_oom: logger.warning( "ran out of memory in validation step, retrying batch" ) for p in self.model.parameters(): if p.grad is not None: p.grad = None # free some memory if self.cuda: torch.cuda.empty_cache() return self.valid_step(sample, raise_oom=True) raise e logging_outputs = [logging_output] if is_dummy_batch: if torch.is_tensor(sample_size): sample_size.zero_() else: sample_size *= 0.0 # gather logging outputs from all replicas if self.data_parallel_world_size > 1: logging_outputs, (sample_size,) = self._aggregate_logging_outputs( logging_outputs, sample_size, ignore=is_dummy_batch, ) # log validation stats if self.tpu: logging_outputs = self._xla_markstep_and_send_to_cpu(logging_outputs) logging_output = self._reduce_and_log_stats(logging_outputs, sample_size) return logging_output def zero_grad(self): self.optimizer.zero_grad() def lr_step_begin_epoch(self, epoch): """Adjust the learning rate at the beginning of the epoch.""" self.lr_scheduler.step_begin_epoch(epoch) # prefer updating the LR based on the number of steps return self.lr_step_update() def lr_step(self, epoch, val_loss=None): """Adjust the learning rate at the end of the epoch.""" self.lr_scheduler.step(epoch, val_loss) # prefer updating the LR based on the number of steps return self.lr_step_update() def lr_step_update(self): """Update the learning rate after each update.""" new_lr = self.lr_scheduler.step_update(self.get_num_updates()) if isinstance(new_lr, dict): for k, v in new_lr.items(): metrics.log_scalar(f"lr_{k}", v, weight=0, priority=300) new_lr = new_lr.get("default", next(iter(new_lr.values()))) else: metrics.log_scalar("lr", new_lr, weight=0, priority=300) return new_lr def get_lr(self): """Get the current learning rate.""" return self.optimizer.get_lr() def get_model(self): """Get the (non-wrapped) model instance.""" return self._model def get_criterion(self): """Get the (non-wrapped) criterion instance.""" return self._criterion def get_meter(self, name): """[deprecated] Get a specific meter by name.""" from fairseq import meters if "get_meter" not in self._warn_once: self._warn_once.add("get_meter") utils.deprecation_warning( "Trainer.get_meter is deprecated. Please use fairseq.metrics instead." ) train_meters = metrics.get_meters("train") if train_meters is None: train_meters = {} if name == "train_loss" and "loss" in train_meters: return train_meters["loss"] elif name == "train_nll_loss": # support for legacy train.py, which assumed this meter is # always initialized m = train_meters.get("nll_loss", None) return m or meters.AverageMeter() elif name == "wall": # support for legacy train.py, which assumed this meter is # always initialized m = metrics.get_meter("default", "wall") return m or meters.TimeMeter() elif name == "wps": m = metrics.get_meter("train", "wps") return m or meters.TimeMeter() elif name in {"valid_loss", "valid_nll_loss"}: # support for legacy train.py, which assumed these meters # are always initialized k = name[len("valid_") :] m = metrics.get_meter("valid", k) return m or meters.AverageMeter() elif name == "oom": return meters.AverageMeter() elif name in train_meters: return train_meters[name] return None def get_num_updates(self): """Get the number of parameters updates.""" return self._num_updates def set_num_updates(self, num_updates): """Set the number of parameters updates.""" self._num_updates = num_updates self.lr_step_update() if self.quantizer: self.quantizer.step_update(self._num_updates) metrics.log_scalar("num_updates", self._num_updates, weight=0, priority=200) def clip_grad_norm(self, clip_norm): def agg_norm_fn(total_norm): total_norm = total_norm.cuda().float() ** 2 total_norm = distributed_utils.all_reduce( total_norm, group=self.data_parallel_process_group ) return total_norm ** 0.5 should_agg_norm = self.is_fsdp and ( self.data_parallel_process_group is not None or torch.distributed.is_initialized() ) return self.optimizer.clip_grad_norm( clip_norm, aggregate_norm_fn=agg_norm_fn if should_agg_norm else None ) def cumulative_training_time(self): if self._cumulative_training_time is None: # single GPU return self._local_cumulative_training_time() else: return self._cumulative_training_time def _local_cumulative_training_time(self): """Aggregate training time in seconds.""" return time.time() - self._start_time + self._previous_training_time def _fp_convert_sample(self, sample): def apply_half(t): if t.dtype is torch.float32: return t.to(dtype=torch.half) return t def apply_bfloat16(t): if t.dtype is torch.float32: return t.to(dtype=torch.bfloat16) return t if self.cfg.common.fp16: sample = utils.apply_to_sample(apply_half, sample) if self.cfg.common.bf16: sample = utils.apply_to_sample(apply_bfloat16, sample) return sample def _prepare_sample(self, sample, is_dummy=False): if sample == "DUMMY": raise Exception( "Trying to use an uninitialized 'dummy' batch. This usually indicates " "that the total number of batches is smaller than the number of " "participating GPUs. Try reducing the batch size or using fewer GPUs." ) if sample is None or len(sample) == 0: assert ( self._dummy_batch is not None and len(self._dummy_batch) > 0 ), "Invalid dummy batch: {}".format(self._dummy_batch) sample, _ = self._prepare_sample(self._dummy_batch, is_dummy=True) return sample, True # Given that PCIe/NVLink bandwidth is significantly smaller than DRAM bandwidth # it makes sense to do the format conversion on the CPU and then transfer # a smaller buffer to the device. This also saves GPU memory capacity. if self.cfg.common.on_cpu_convert_precision: sample = self._fp_convert_sample(sample) if self.cuda: if self.pipeline_model_parallel: if "target" in sample: sample["target"] = utils.move_to_cuda( sample["target"], device=self.last_device ) else: sample = utils.move_to_cuda(sample) elif self.tpu and is_dummy: # the dummy batch may not be on the appropriate device sample = utils.move_to_cuda(sample, device=self.device) if not self.cfg.common.on_cpu_convert_precision: sample = self._fp_convert_sample(sample) if self._dummy_batch == "DUMMY": self._dummy_batch = sample return sample, False def _set_seed(self): # Set seed based on args.seed and the update number so that we get # reproducible results when resuming from checkpoints seed = self.cfg.common.seed + self.get_num_updates() utils.set_torch_seed(seed) def _sync_stats(self): # Return True if it's using multiple GPUs and DDP or multiple GPUs with # BMUF and it's a bmuf sync with warmup iterations completed before. if self.data_parallel_world_size == 1: return False elif self.cfg.optimization.use_bmuf: return ( self.get_num_updates() + 1 ) % self.cfg.bmuf.global_sync_iter == 0 and ( self.get_num_updates() + 1 ) > self.cfg.bmuf.warmup_iterations else: return True def _log_oom(self, exc): msg = "OOM: Ran out of memory with exception: {}".format(exc) logger.warning(msg) if torch.cuda.is_available() and hasattr(torch.cuda, "memory_summary"): for device_idx in range(torch.cuda.device_count()): logger.warning(torch.cuda.memory_summary(device=device_idx)) sys.stderr.flush() def _aggregate_logging_outputs( self, logging_outputs: List[Dict[str, Any]], *extra_stats_to_sum, ignore=False, ): if self.task.__class__.logging_outputs_can_be_summed(self.get_criterion()): return self._fast_stat_sync_sum( logging_outputs, *extra_stats_to_sum, ignore=ignore ) else: return self._all_gather_list_sync( logging_outputs, *extra_stats_to_sum, ignore=ignore ) def _all_gather_list_sync( self, logging_outputs: List[Dict[str, Any]], *extra_stats_to_sum, ignore=False, ): """ Sync logging outputs across workers. all_gather_list_sync is suitable when logging outputs are complex types. """ if self.tpu: raise NotImplementedError if ignore: logging_outputs = [] results = list( zip( *distributed_utils.all_gather_list( [logging_outputs] + list(extra_stats_to_sum), max_size=getattr(self.cfg.common, "all_gather_list_size", 16384), group=self.data_parallel_process_group, ) ) ) logging_outputs, extra_stats_to_sum = results[0], results[1:] logging_outputs = list(chain.from_iterable(logging_outputs)) extra_stats_to_sum = [sum(s) for s in extra_stats_to_sum] return logging_outputs, extra_stats_to_sum def _fast_stat_sync_sum( self, logging_outputs: List[Dict[str, Any]], *extra_stats_to_sum, ignore=False, ): """ Sync logging outputs across workers. fast_stat_sync_sum is faster than all_gather_list_sync, but is only suitable when logging outputs are scalars and can be summed. Note that *logging_outputs* cannot contain any nested dicts/lists. """ data = {} for i, stat in enumerate(extra_stats_to_sum): data["extra_stats_" + str(i)] = stat if len(logging_outputs) > 0: log_keys = list(logging_outputs[0].keys()) for k in log_keys: if not ignore: v = sum(log[k] for log in logging_outputs if k in log) else: v = logging_outputs[0][k] v = torch.zeros_like(v) if torch.is_tensor(v) else 0 data["logging_outputs_" + k] = v else: log_keys = None data = distributed_utils.all_reduce_dict( data, device=self.device, group=self.data_parallel_process_group ) extra_stats_to_sum = [ data["extra_stats_" + str(i)] for i in range(len(extra_stats_to_sum)) ] if log_keys is not None: logging_outputs = [{k: data["logging_outputs_" + k] for k in log_keys}] else: logging_outputs = [] return logging_outputs, extra_stats_to_sum def _check_grad_norms(self, grad_norm): """Check that grad norms are consistent across workers.""" if self._grad_norm_buf is not None: self._grad_norm_buf.zero_() self._grad_norm_buf[self.data_parallel_rank] = grad_norm distributed_utils.all_reduce( self._grad_norm_buf, group=self.data_parallel_process_group ) def is_consistent(tensor): max_abs_diff = torch.max(torch.abs(tensor - tensor[0])) return ( ( torch.isfinite(tensor).all() and (max_abs_diff / (tensor[0] + 1e-6) < 1e-6).all() ) or (self.cfg.common.amp and not torch.isfinite(tensor).all()) # in case of amp non-finite grads are fine ) if not is_consistent(self._grad_norm_buf): pretty_detail = "\n".join( "rank {:3d} = {:.8f}".format(r, n) for r, n in enumerate(self._grad_norm_buf.tolist()) ) error_detail = "grad_norm across the workers:\n{}\n".format( pretty_detail ) # use FloatingPointError to trigger NanDetector raise FloatingPointError( "Fatal error: gradients are inconsistent between workers. " "Try --ddp-backend=legacy_ddp. " "Or are you mixing up different generation of GPUs in training?" + "\n" + "-" * 80 + "\n{}\n".format(error_detail) + "-" * 80 ) def _reduce_and_log_stats(self, logging_outputs, sample_size, grad_norm=None): if grad_norm is not None and ( not torch.is_tensor(grad_norm) or torch.isfinite(grad_norm) ): metrics.log_speed("ups", 1.0, priority=100, round=2) metrics.log_scalar("gnorm", grad_norm, priority=400, round=3) if self.cfg.optimization.clip_norm > 0: metrics.log_scalar( "clip", torch.where( grad_norm > self.cfg.optimization.clip_norm, grad_norm.new_tensor(100), grad_norm.new_tensor(0), ), priority=500, round=1, ) with metrics.aggregate() as agg: if logging_outputs is not None: self.task.reduce_metrics(logging_outputs, self.get_criterion()) del logging_outputs # extra warning for criterions that don't properly log a loss value if "loss" not in agg: if "loss" not in self._warn_once: self._warn_once.add("loss") logger.warning( "Criterion.reduce_metrics did not log a 'loss' value, " "which may break some functionality" ) metrics.log_scalar("loss", -1) # support legacy interface if self.tpu: logging_output = {} else: logging_output = agg.get_smoothed_values() logging_output["sample_size"] = sample_size for key_to_delete in ["ppl", "wps", "wpb", "bsz"]: if key_to_delete in logging_output: del logging_output[key_to_delete] return logging_output def _check_xla_compilation(self): import torch_xla.debug.metrics as met compile_stats = met.metric_data("CompileTime") if compile_stats is None: return num_xla_compiles = compile_stats[0] if num_xla_compiles > self._num_xla_compiles: logger.warning( "XLA compilation detected on device #{}; too many of these can lead " "to slow training, but we expect a few in the beginning".format( self.cfg.distributed_training.distributed_rank ) ) self._num_xla_compiles = num_xla_compiles def _xla_markstep_and_send_to_cpu(self, data=None): import torch_xla.core.xla_model as xm xm.mark_step() if data is not None: from fairseq.utils import xla_device_to_cpu return xla_device_to_cpu(data) def _catalog_shared_params(module, memo=None, prefix=""): if memo is None: first_call = True memo = {} else: first_call = False for name, param in module._parameters.items(): param_prefix = prefix + ("." if prefix else "") + name if param not in memo: memo[param] = [] memo[param].append(param_prefix) for name, m in module._modules.items(): if m is None: continue submodule_prefix = prefix + ("." if prefix else "") + name _catalog_shared_params(m, memo, submodule_prefix) if first_call: return [x for x in memo.values() if len(x) > 1] def _get_module_by_path(module, path): path = path.split(".") for name in path: module = getattr(module, name) return module def _set_module_by_path(module, path, value): path = path.split(".") for name in path[:-1]: module = getattr(module, name) setattr(module, path[-1], value)
KosmosX-API-main
kosmosX/fairseq/fairseq/trainer.py
# 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 typing as tp from abc import ABC, abstractmethod from collections import Counter from dataclasses import dataclass from multiprocessing import Pool import torch from fairseq.data import Dictionary, indexed_dataset from fairseq.file_chunker_utils import Chunker, find_offsets from fairseq.file_io import PathManager from fairseq.tokenizer import tokenize_line logger = logging.getLogger("binarizer") @dataclass class BinarizeSummary: """ Keep track of what's going on in the binarizer """ num_seq: int = 0 replaced: tp.Optional[Counter] = None num_tok: int = 0 @property def num_replaced(self) -> int: if self.replaced is None: return 0 return sum(self.replaced.values()) @property def replaced_percent(self) -> float: return 100 * self.num_replaced / self.num_tok def __str__(self) -> str: base = f"{self.num_seq} sents, {self.num_tok} tokens" if self.replaced is None: return base return f"{base}, {self.replaced_percent:.3}% replaced" def merge(self, other: "BinarizeSummary"): replaced = None if self.replaced is not None: replaced = self.replaced if other.replaced is not None: if replaced is None: replaced = other.replaced else: replaced += other.replaced self.replaced = replaced self.num_seq += other.num_seq self.num_tok += other.num_tok class Binarizer(ABC): """ a binarizer describes how to take a string and build a tensor out of it """ @abstractmethod def binarize_line( self, line: str, summary: BinarizeSummary, ) -> torch.IntTensor: ... def _worker_prefix(output_prefix: str, worker_id: int): return f"{output_prefix}.pt{worker_id}" class FileBinarizer: """ An file binarizer can take a file, tokenize it, and binarize each line to a tensor """ @classmethod def multiprocess_dataset( cls, input_file: str, dataset_impl: str, binarizer: Binarizer, output_prefix: str, vocab_size=None, num_workers=1, ) -> BinarizeSummary: final_summary = BinarizeSummary() offsets = find_offsets(input_file, num_workers) # find_offsets returns a list of position [pos1, pos2, pos3, pos4] but we would want pairs: # [(pos1, pos2), (pos2, pos3), (pos3, pos4)] to process the chunks with start/end info # we zip the list with itself shifted by one to get all the pairs. (first_chunk, *more_chunks) = zip(offsets, offsets[1:]) pool = None if num_workers > 1: pool = Pool(processes=num_workers - 1) worker_results = [ pool.apply_async( cls._binarize_chunk_and_finalize, args=( binarizer, input_file, start_offset, end_offset, _worker_prefix( output_prefix, worker_id, ), dataset_impl, ), kwds={ "vocab_size": vocab_size, } if vocab_size is not None else {}, ) for worker_id, (start_offset, end_offset) in enumerate( more_chunks, start=1 ) ] pool.close() pool.join() for r in worker_results: summ = r.get() final_summary.merge(summ) # do not close the bin file as we need to merge the worker results in final_ds, summ = cls._binarize_file_chunk( binarizer, input_file, offset_start=first_chunk[0], offset_end=first_chunk[1], output_prefix=output_prefix, dataset_impl=dataset_impl, vocab_size=vocab_size if vocab_size is not None else None, ) final_summary.merge(summ) if num_workers > 1: for worker_id in range(1, num_workers): # merge the worker outputs worker_output_prefix = _worker_prefix( output_prefix, worker_id, ) final_ds.merge_file_(worker_output_prefix) try: os.remove(indexed_dataset.data_file_path(worker_output_prefix)) os.remove(indexed_dataset.index_file_path(worker_output_prefix)) except Exception as e: logger.error( f"couldn't remove {worker_output_prefix}.*", exc_info=e ) # now we can close the file idx_file = indexed_dataset.index_file_path(output_prefix) final_ds.finalize(idx_file) return final_summary @staticmethod def _binarize_file_chunk( binarizer: Binarizer, filename: str, offset_start: int, offset_end: int, output_prefix: str, dataset_impl: str, vocab_size=None, ) -> tp.Tuple[tp.Any, BinarizeSummary]: # (dataset builder, BinarizeSummary) """ creates a dataset builder and append binarized items to it. This function does not finalize the builder, this is useful if you want to do other things with your bin file like appending/merging other files """ bin_file = indexed_dataset.data_file_path(output_prefix) ds = indexed_dataset.make_builder( bin_file, impl=dataset_impl, vocab_size=vocab_size, ) summary = BinarizeSummary() with Chunker( PathManager.get_local_path(filename), offset_start, offset_end ) as line_iterator: for line in line_iterator: ds.add_item(binarizer.binarize_line(line, summary)) return ds, summary @classmethod def _binarize_chunk_and_finalize( cls, binarizer: Binarizer, filename: str, offset_start: int, offset_end: int, output_prefix: str, dataset_impl: str, vocab_size=None, ): """ same as above, but also finalizes the builder """ ds, summ = cls._binarize_file_chunk( binarizer, filename, offset_start, offset_end, output_prefix, dataset_impl, vocab_size=vocab_size, ) idx_file = indexed_dataset.index_file_path(output_prefix) ds.finalize(idx_file) return summ class VocabularyDatasetBinarizer(Binarizer): """ Takes a Dictionary/Vocabulary, assign ids to each token using the dictionary encode_line function. """ def __init__( self, dict: Dictionary, tokenize: tp.Callable[[str], tp.List[str]] = tokenize_line, append_eos: bool = True, reverse_order: bool = False, already_numberized: bool = False, ) -> None: self.dict = dict self.tokenize = tokenize self.append_eos = append_eos self.reverse_order = reverse_order self.already_numberized = already_numberized super().__init__() def binarize_line( self, line: str, summary: BinarizeSummary, ): if summary.replaced is None: summary.replaced = Counter() def replaced_consumer(word, idx): if idx == self.dict.unk_index and word != self.dict.unk_word: summary.replaced.update([word]) if self.already_numberized: id_strings = line.strip().split() id_list = [int(id_string) for id_string in id_strings] if self.reverse_order: id_list.reverse() if self.append_eos: id_list.append(self.dict.eos()) ids = torch.IntTensor(id_list) else: ids = self.dict.encode_line( line=line, line_tokenizer=self.tokenize, add_if_not_exist=False, consumer=replaced_consumer, append_eos=self.append_eos, reverse_order=self.reverse_order, ) summary.num_seq += 1 summary.num_tok += len(ids) return ids class AlignmentDatasetBinarizer(Binarizer): """ binarize by parsing a set of alignments and packing them in a tensor (see utils.parse_alignment) """ def __init__( self, alignment_parser: tp.Callable[[str], torch.IntTensor], ) -> None: super().__init__() self.alignment_parser = alignment_parser def binarize_line( self, line: str, summary: BinarizeSummary, ): ids = self.alignment_parser(line) summary.num_seq += 1 summary.num_tok += len(ids) return ids class LegacyBinarizer: @classmethod def binarize( cls, filename: str, dico: Dictionary, consumer: tp.Callable[[torch.IntTensor], None], tokenize: tp.Callable[[str], tp.List[str]] = tokenize_line, append_eos: bool = True, reverse_order: bool = False, offset: int = 0, end: int = -1, already_numberized: bool = False, ) -> tp.Dict[str, int]: binarizer = VocabularyDatasetBinarizer( dict=dico, tokenize=tokenize, append_eos=append_eos, reverse_order=reverse_order, already_numberized=already_numberized, ) return cls._consume_file( filename, binarizer, consumer, offset_start=offset, offset_end=end, ) @classmethod def binarize_alignments( cls, filename: str, alignment_parser: tp.Callable[[str], torch.IntTensor], consumer: tp.Callable[[torch.IntTensor], None], offset: int = 0, end: int = -1, ) -> tp.Dict[str, int]: binarizer = AlignmentDatasetBinarizer(alignment_parser) return cls._consume_file( filename, binarizer, consumer, offset_start=offset, offset_end=end, ) @staticmethod def _consume_file( filename: str, binarizer: Binarizer, consumer: tp.Callable[[torch.IntTensor], None], offset_start: int, offset_end: int, ) -> tp.Dict[str, int]: summary = BinarizeSummary() with Chunker( PathManager.get_local_path(filename), offset_start, offset_end ) as line_iterator: for line in line_iterator: consumer(binarizer.binarize_line(line, summary)) return { "nseq": summary.num_seq, "nunk": summary.num_replaced, "ntok": summary.num_tok, "replaced": summary.replaced, }
KosmosX-API-main
kosmosX/fairseq/fairseq/binarizer.py
# 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. """Implements tracking of constraints for a beam item. A list of constraints is given as a list of one or more token sequences, each of length at least one token. For example, for an input sentence > Die maschinelle Übersetzung ist schwer zu kontrollieren. We could have the constraints: * to influence * hard There are two implementations: * OrderedConstraintState: Tracks progress through an ordered list of multitoken constraints. * UnorderedConstraintState: Tracks progress through an unordered list of multitoken constraints. The difference is that in the first, the constraints are assumed to be in order; the algorithm will permit zero or more tokens between them. In the second, the constraints are not ordered, so many orderings will be explored. The same sequence can be present any number of times, and will appear that many times in the output. """ from collections import Counter from typing import List, Set import torch class ConstraintState: def __init__(self): pass def pack_constraints(batch_constraints: List[List[torch.Tensor]]) -> torch.Tensor: """Takes a list of list of constraints in tensor form (a list of tensor constraints for each sentence) and transforms it into a packed Tensor. For example, here is a batch of size 3 with 3, 0, and 1 constraints: [ [ [3 1 2], [3], [4 5 6 7], ] [], [ [1 8 9 10 1 4 11 12], ] ] Its corresponding packed structure is: [ [ 3 3 1 2 0 3 0 4 5 6 7 0], [ 0 0 0 0 0 0 0 0 0 0 0 0], [ 1 1 8 9 10 1 4 11 12 0 0 0] ] The packed tensor has shape (batch size, maxlen), where maxlen is defined below. Each row contains concatenated constraint tokens for that sentence, with 0 appended after each constraint. The first item in each row is the number of constraints for that sentence. So maxlen is the maximum of (number of constraints) + (sum length of constraints) + 1. across all sentences in the batch. """ # The maximum word length of concatenated constraints for any sentence max_constraints_len = 1 for sentence_constraints in batch_constraints: if len(sentence_constraints): # number of constraints, plus sum of constrain lens, plus a zero after each constraints_len = ( 1 + sum([c.size(0) for c in sentence_constraints]) + len(sentence_constraints) ) max_constraints_len = max(max_constraints_len, constraints_len) batch_size = len(batch_constraints) constraints_tensor = torch.zeros((batch_size, max_constraints_len)).long() for i, sentence_constraints in enumerate(batch_constraints): constraints_tensor[i, 0] = len(sentence_constraints) offset = 1 for j, constraint in enumerate(sentence_constraints): this_len = constraint.size(0) constraints_tensor[i, offset : offset + this_len] = constraint offset += this_len + 1 return constraints_tensor.long() def unpack_constraints(constraint_tensor: torch.Tensor) -> List[torch.Tensor]: """ Transforms *one row* of a packed constraint tensor (e.g., for one sentence in the batch) into a list of constraint tensors. """ constraint_list = [] num_constraints = constraint_tensor[0] constraints = constraint_tensor.tolist() offset = 1 for i in range(num_constraints): where = constraints.index(0, offset) constraint_list.append(constraint_tensor[offset:where]) offset = where + 1 return constraint_list class ConstraintNode: """ Represents a node in a trie managing unordered constraints. """ def __init__(self, token: int = None, parent=None): # The token associate with this node (None for the root) self.token = int(token) if token is not None else None # The parent (None at the root) self.parent = parent # Whether this node is a completed constraint self.terminal = 0 # List of child nodes self.children = {} # The cumulative number of constraints from this point in the # trie forward self.num_constraints = 0 @property def id(self): return self.token def __str__(self): term = self.terminal != 0 return f"[{self.token}].{term}#{self.num_constraints}" def __getitem__(self, key: int): return self.children.get(key, None) def next_tokens(self) -> Set[int]: """The set of child labels.""" return set(self.children.keys()) @staticmethod def create(constraints: List[List[int]]): root = ConstraintNode() for sequence in constraints: root.add_sequence(sequence) return root @staticmethod def print_graph(node: "ConstraintNode"): if len(node.children) == 0: return str(node) else: s = f"({node}" for child in node.children.values(): s += " " + ConstraintNode.print_graph(child) s += ")" return s def token_counts(self) -> Counter: """Returns a counter of the number of times each token is used in a constraint. """ token_counts = Counter() kids = list(self.children.values()) while len(kids) > 0: kid = kids.pop() token_counts[kid.id] += kid.num_constraints kids += list(kid.children.values()) return token_counts def tokens(self) -> Set[int]: """Returns the set of tokens in constraints.""" return set(self.token_counts().keys()) def add_sequence(self, sequence: List[int]): """Adds a constraint, represented as a list of integers, to the trie.""" assert len(sequence) > 0 token = int(sequence[0]) if token not in self.children: self.children[token] = ConstraintNode(token, parent=self) node = self.children[token] if len(sequence) == 1: node.terminal += 1 node.num_constraints += 1 parent = node.parent while parent is not None: parent.num_constraints += 1 parent = parent.parent else: node.add_sequence(sequence[1:]) class UnorderedConstraintState(ConstraintState): """ Records progress through the set of constraints for each item in the beam using a trie. """ def __init__(self, node: ConstraintNode, copy_from: "ConstraintState" = None): self.node = node if copy_from is None: # The root node self.root = node # The set of states in the graph that have been completed self.completed = Counter() # The... self.generated = Counter() # The list of tokens we need to generate self.needed_tokens = self.root.tokens() else: self.completed = Counter(copy_from.completed) self.generated = Counter(copy_from.generated) self.root = copy_from.root # Mark the node as generated if self.node != self.root: self.generated[node] += 1 @staticmethod def create(constraint_tensor: torch.Tensor): constraint_list = unpack_constraints(constraint_tensor) constraint_trie_root = ConstraintNode.create(constraint_list) return UnorderedConstraintState(constraint_trie_root) def __str__(self): gen_str = ",".join([str(node) for node in self.generated]) return f"{self.name}/{self.bank}({gen_str})x{self.num_completed}" def __copy__(self): copied_state = UnorderedConstraintState(self.node, copy_from=self) return copied_state def copy(self): return self.__copy__() @property def name(self): if self.node.id is None: return "ROOT" else: return str(self.node.id) @property def is_root(self): return self.node == self.root @property def bank(self): return sum(self.generated.values()) @property def num_completed(self): """The number of constraints (not constraint tokens) that are completed. In addition to the already-completed states, we need to account for the current state, which might get marked as completed when another token is generated. """ in_final = self.node.terminal and self.completed[self.node] < self.node.terminal return sum(self.completed.values()) + in_final @property def finished(self): return self.root.num_constraints - self.num_completed == 0 @property def token_counts(self): return self.root.token_counts() @property def tokens(self): return self.root.tokens() @property def num_constraint_tokens(self): return sum(self.token_counts.values()) def next_tokens(self) -> Set[int]: """Returns the list of tokens that could come next. These are (a) all tokens extending the root state and, for non-root states, additionally all tokens extending the current state.""" if self.node != self.root: return self.root.next_tokens().union(self.node.next_tokens()) else: return self.root.next_tokens() def advance(self, token: int): """Reads in a token and advances the state. Here's how it works. We can advance to the next state if: - there is a matching child - its path isn't blocked A path is blocked when all constraints that are descendants of that node have already been generated, in the current state. If we are not able to advance from the current state, we "fall off the graph" and return to the root state. There, we again try to advance, checking the same criteria. In any case, when falling off the graph, we need to do some bookkeeping. We: - check whether any constraints were met (all prefixes of current state) - if one is found, mark it as completed - adjust visited nodes accordingly """ token = int(token) next_state = None child = self.node[token] if child is not None and self.generated[child] < child.num_constraints: next_state = UnorderedConstraintState(child, copy_from=self) def rewind(): """If we're mid-trie and an "illegal" token is chosen next, we need to reset our state to the root state. However, along the way, we need to check whether a prefix of the current trie state represents a state we could mark as completed. """ node = self.node while node != self.root: if node.terminal and self.completed[node] < node.terminal: next_state.completed[node] += 1 return next_state.generated[node] -= 1 node = node.parent # Fall off the graph, check the root if next_state is None and token in self.root.next_tokens(): child = self.root[token] # We can only traverse this edge if it's not saturated if self.generated[child] < child.num_constraints: next_state = UnorderedConstraintState(child, copy_from=self) else: next_state = UnorderedConstraintState(self.root, copy_from=self) # Rewind rewind() elif next_state is None: next_state = UnorderedConstraintState(self.root, copy_from=self) # Rewind rewind() return next_state class ConstraintSequence: def __init__(self, sequences: List[List[int]]): """Represents a set of possibly multitoken constraints by concatenating them and internally recording the end points. """ self.sequences = [] self.endpoints = [] self.num_tokens = 0 self.tokens = set() for sequence in sequences: for token in sequence: self.tokens.add(token) self.num_tokens += len(sequence) self.endpoints += [False for x in range(len(sequence) - 1)] + [True] self.sequences += sequence def __getitem__(self, key: int): return self.sequences[key] def __len__(self): return len(self.sequences) def __str__(self): return str(self.sequences) class OrderedConstraintState(ConstraintState): """ Records progress through the set of linear nonbranching constraints with gaps. """ def __init__(self, sequence: ConstraintSequence, state: int = -1): self.sequence = sequence self.state = state @staticmethod def create(constraint_tensor: torch.Tensor): constraint_list = unpack_constraints(constraint_tensor) return OrderedConstraintState(ConstraintSequence(constraint_list), -1) def __str__(self): return f"{self.state}/{self.bank}x{self.num_completed}" def __copy__(self): return OrderedConstraintState(self.sequence, self.state) def copy(self): return self.__copy__() @property def num_completed(self): if self.state == -1: return 0 count = len( list(filter(lambda x: x, self.sequence.endpoints[0 : self.state + 1])) ) return count @property def is_root(self): return self.state == -1 @property def name(self): if self.state == -1: return "ROOT" else: return str(self.sequence[self.state]) @property def bank(self) -> int: return self.state + 1 @property def finished(self): return self.state + 1 == len(self.sequence) @property def token_counts(self): return self.sequence.token_counts() @property def tokens(self): return self.sequence.tokens @property def num_constraint_tokens(self): return sum(self.token_counts.values()) def next_tokens(self) -> Set[int]: """Returns the list of tokens that could come next. These are (a) all tokens extending the root state and, for non-root states, additionally all tokens extending the current state.""" tokens = set() if self.state > 0: tokens.add(self.sequence[0]) if not self.finished: tokens.add(self.sequence[self.state + 1]) return tokens def advance(self, token: int): """Reads in a token and advances the state. Here's how it works. We can advance to the next state if: - there is a matching child - its path isn't blocked A path is blocked when all constraints that are descendants of that node have already been generated, in the current state. If we are not able to advance from the current state, we "fall off the graph" and return to the root state. There, we again try to advance, checking the same criteria. In any case, when falling off the graph, we need to do some bookkeeping. We: - check whether any constraints were met (all prefixes of current state) - if one is found, mark it as completed - adjust visited nodes accordingly """ token = int(token) # print(f"{self} ADVANCE({token}) {self.sequence} -> ", end="") if self.finished: # Accept anything next_state = self.copy() elif self.sequence[self.state + 1] == token: # Advance to the next token next_state = OrderedConstraintState(self.sequence, self.state + 1) elif self.sequence.endpoints[self.state]: # Accept anything between constraints (*) next_state = self.copy() elif token == self.sequence[0]: # Start over having generated the first token next_state = OrderedConstraintState(self.sequence, 0) else: # Start over from the root next_state = OrderedConstraintState(self.sequence, -1) return next_state
KosmosX-API-main
kosmosX/fairseq/fairseq/token_generation_constraints.py
# 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 from dataclasses import dataclass, field from typing import Optional import torch from omegaconf import II from .dummy_dataset import DummyDataset from fairseq.data import Dictionary from fairseq.dataclass import FairseqDataclass from fairseq.tasks import FairseqTask, register_task logger = logging.getLogger(__name__) @dataclass class DummyMaskedLMConfig(FairseqDataclass): dict_size: int = 49996 dataset_size: int = 100000 tokens_per_sample: int = field( default=512, metadata={ "help": "max number of total tokens over all" " segments per sample for BERT dataset" }, ) batch_size: Optional[int] = II("dataset.batch_size") max_tokens: Optional[int] = II("dataset.max_tokens") max_target_positions: int = II("task.tokens_per_sample") @register_task("dummy_masked_lm", dataclass=DummyMaskedLMConfig) class DummyMaskedLMTask(FairseqTask): def __init__(self, cfg: DummyMaskedLMConfig): super().__init__(cfg) self.dictionary = Dictionary() for i in range(cfg.dict_size): self.dictionary.add_symbol("word{}".format(i)) logger.info("dictionary: {} types".format(len(self.dictionary))) # add mask token self.mask_idx = self.dictionary.add_symbol("<mask>") self.dictionary.pad_to_multiple_(8) # often faster if divisible by 8 mask_idx = 0 pad_idx = 1 seq = torch.arange(cfg.tokens_per_sample) + pad_idx + 1 mask = torch.arange(2, cfg.tokens_per_sample, 7) # ~15% src = seq.clone() src[mask] = mask_idx tgt = torch.full_like(seq, pad_idx) tgt[mask] = seq[mask] self.dummy_src = src self.dummy_tgt = tgt def load_dataset(self, split, epoch=1, combine=False, **kwargs): """Load a given dataset split. Args: split (str): name of the split (e.g., train, valid, test) """ if self.cfg.batch_size is not None: bsz = self.cfg.batch_size else: bsz = max(1, self.cfg.max_tokens // self.cfg.tokens_per_sample) self.datasets[split] = DummyDataset( { "id": 1, "net_input": { "src_tokens": torch.stack([self.dummy_src for _ in range(bsz)]), "src_lengths": torch.full( (bsz,), self.cfg.tokens_per_sample, dtype=torch.long ), }, "target": torch.stack([self.dummy_tgt for _ in range(bsz)]), "nsentences": bsz, "ntokens": bsz * self.cfg.tokens_per_sample, }, num_items=self.cfg.dataset_size, item_size=self.cfg.tokens_per_sample, ) @property def source_dictionary(self): return self.dictionary @property def target_dictionary(self): return self.dictionary
KosmosX-API-main
kosmosX/fairseq/fairseq/benchmark/dummy_masked_lm.py
# 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 from dataclasses import dataclass, field from typing import Optional import torch from .dummy_dataset import DummyDataset from fairseq.data import Dictionary from fairseq.dataclass import FairseqDataclass from fairseq.tasks import FairseqTask, register_task from omegaconf import II logger = logging.getLogger(__name__) @dataclass class DummyLMConfig(FairseqDataclass): dict_size: int = 49996 dataset_size: int = 100000 tokens_per_sample: int = field( default=512, metadata={"help": "max sequence length"} ) add_bos_token: bool = False batch_size: Optional[int] = II("dataset.batch_size") max_tokens: Optional[int] = II("dataset.max_tokens") max_target_positions: int = II("task.tokens_per_sample") @register_task("dummy_lm", dataclass=DummyLMConfig) class DummyLMTask(FairseqTask): def __init__(self, cfg: DummyLMConfig): super().__init__(cfg) # load dictionary self.dictionary = Dictionary() for i in range(cfg.dict_size): self.dictionary.add_symbol("word{}".format(i)) self.dictionary.pad_to_multiple_(8) # often faster if divisible by 8 logger.info("dictionary: {} types".format(len(self.dictionary))) seq = torch.arange(cfg.tokens_per_sample + 1) + self.dictionary.pad() + 1 self.dummy_src = seq[:-1] self.dummy_tgt = seq[1:] def load_dataset(self, split, epoch=1, combine=False, **kwargs): """Load a given dataset split. Args: split (str): name of the split (e.g., train, valid, test) """ if self.cfg.batch_size is not None: bsz = self.cfg.batch_size else: bsz = max(1, self.cfg.max_tokens // self.cfg.tokens_per_sample) self.datasets[split] = DummyDataset( { "id": 1, "net_input": { "src_tokens": torch.stack([self.dummy_src for _ in range(bsz)]), "src_lengths": torch.full( (bsz,), self.cfg.tokens_per_sample, dtype=torch.long ), }, "target": torch.stack([self.dummy_tgt for _ in range(bsz)]), "nsentences": bsz, "ntokens": bsz * self.cfg.tokens_per_sample, }, num_items=self.cfg.dataset_size, item_size=self.cfg.tokens_per_sample, ) @property def source_dictionary(self): return self.dictionary @property def target_dictionary(self): return self.dictionary
KosmosX-API-main
kosmosX/fairseq/fairseq/benchmark/dummy_lm.py
# 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 numpy as np import torch from fairseq.data import Dictionary, FairseqDataset from fairseq.tasks import LegacyFairseqTask, register_task logger = logging.getLogger(__name__) @register_task("dummy_mt") class DummyMTTask(LegacyFairseqTask): @staticmethod def add_args(parser): """Add task-specific arguments to the parser.""" parser.add_argument("--dict-size", default=49996, type=int) parser.add_argument("--dataset-size", default=100000, type=int) parser.add_argument("--src-len", default=30, type=int) parser.add_argument("--tgt-len", default=30, type=int) def __init__(self, args, dictionary): super().__init__(args) self.dictionary = dictionary self.seed = args.seed dictionary.pad_to_multiple_(8) # often faster if divisible by 8 self.dummy_src = torch.arange(args.src_len + 1) + dictionary.pad() + 1 self.dummy_tgt = torch.arange(args.tgt_len + 1) + dictionary.pad() + 1 @classmethod def setup_task(cls, args, **kwargs): """Setup the task.""" dictionary = Dictionary() for i in range(args.dict_size): dictionary.add_symbol("word{}".format(i)) logger.info("dictionary: {} types".format(len(dictionary))) args.max_source_positions = args.src_len + dictionary.pad() + 2 args.max_target_positions = args.tgt_len + dictionary.pad() + 2 return cls(args, dictionary) def load_dataset(self, split, epoch=1, combine=False, **kwargs): """Load a given dataset split. Args: split (str): name of the split (e.g., train, valid, test) """ item_size = max(self.args.src_len, self.args.tgt_len) if self.args.batch_size is not None: bsz = self.args.batch_size else: bsz = max(1, self.args.max_tokens // item_size) tgt = torch.stack([self.dummy_tgt for _ in range(bsz)]) self.datasets[split] = DummyDataset( { "id": 1, "net_input": { "src_tokens": torch.stack([self.dummy_src for _ in range(bsz)]), "src_lengths": torch.full( (bsz,), self.args.src_len, dtype=torch.long ), "prev_output_tokens": tgt.clone(), }, "target": tgt, "nsentences": bsz, "ntokens": bsz * self.args.tgt_len, }, num_items=self.args.dataset_size, item_size=item_size, ) @property def source_dictionary(self): return self.dictionary @property def target_dictionary(self): return self.dictionary class DummyDataset(FairseqDataset): def __init__(self, batch, num_items, item_size): super().__init__() self.batch = batch self.num_items = num_items self.item_size = item_size def __getitem__(self, index): return index def __len__(self): return self.num_items def collater(self, samples): return self.batch @property def sizes(self): return np.array([self.item_size] * self.num_items) def num_tokens(self, index): return self.item_size def size(self, index): return self.item_size def ordered_indices(self): return np.arange(self.num_items) @property def supports_prefetch(self): return False
KosmosX-API-main
kosmosX/fairseq/fairseq/benchmark/dummy_mt.py
# 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 models/tasks to register them from . import dummy_dataset, dummy_lm, dummy_masked_lm, dummy_model, dummy_mt # noqa
KosmosX-API-main
kosmosX/fairseq/fairseq/benchmark/__init__.py