How to use transformer method in mountebank

Best JavaScript code snippet using mountebank

vggtransformer.py

Source:vggtransformer.py Github

copy

Full Screen

1# Copyright (c) Facebook, Inc. and its affiliates.2#3# This source code is licensed under the MIT license found in the4# LICENSE file in the root directory of this source tree.5import argparse6import math7from collections.abc import Iterable8import torch9import torch.nn as nn10from examples.speech_recognition.data.data_utils import lengths_to_encoder_padding_mask11from fairseq import utils12from fairseq.models import (13 FairseqEncoder,14 FairseqEncoderDecoderModel,15 FairseqEncoderModel,16 FairseqIncrementalDecoder,17 register_model,18 register_model_architecture,19)20from fairseq.modules import (21 LinearizedConvolution,22 TransformerDecoderLayer,23 TransformerEncoderLayer,24 VGGBlock,25)26@register_model("asr_vggtransformer")27class VGGTransformerModel(FairseqEncoderDecoderModel):28 """29 Transformers with convolutional context for ASR30 https://arxiv.org/abs/1904.1166031 """32 def __init__(self, encoder, decoder):33 super().__init__(encoder, decoder)34 @staticmethod35 def add_args(parser):36 """Add model-specific arguments to the parser."""37 parser.add_argument(38 "--input-feat-per-channel",39 type=int,40 metavar="N",41 help="encoder input dimension per input channel",42 )43 parser.add_argument(44 "--vggblock-enc-config",45 type=str,46 metavar="EXPR",47 help="""48 an array of tuples each containing the configuration of one vggblock:49 [(out_channels,50 conv_kernel_size,51 pooling_kernel_size,52 num_conv_layers,53 use_layer_norm), ...])54 """,55 )56 parser.add_argument(57 "--transformer-enc-config",58 type=str,59 metavar="EXPR",60 help=""""61 a tuple containing the configuration of the encoder transformer layers62 configurations:63 [(input_dim,64 num_heads,65 ffn_dim,66 normalize_before,67 dropout,68 attention_dropout,69 relu_dropout), ...]')70 """,71 )72 parser.add_argument(73 "--enc-output-dim",74 type=int,75 metavar="N",76 help="""77 encoder output dimension, can be None. If specified, projecting the78 transformer output to the specified dimension""",79 )80 parser.add_argument(81 "--in-channels",82 type=int,83 metavar="N",84 help="number of encoder input channels",85 )86 parser.add_argument(87 "--tgt-embed-dim",88 type=int,89 metavar="N",90 help="embedding dimension of the decoder target tokens",91 )92 parser.add_argument(93 "--transformer-dec-config",94 type=str,95 metavar="EXPR",96 help="""97 a tuple containing the configuration of the decoder transformer layers98 configurations:99 [(input_dim,100 num_heads,101 ffn_dim,102 normalize_before,103 dropout,104 attention_dropout,105 relu_dropout), ...]106 """,107 )108 parser.add_argument(109 "--conv-dec-config",110 type=str,111 metavar="EXPR",112 help="""113 an array of tuples for the decoder 1-D convolution config114 [(out_channels, conv_kernel_size, use_layer_norm), ...]""",115 )116 @classmethod117 def build_encoder(cls, args, task):118 return VGGTransformerEncoder(119 input_feat_per_channel=args.input_feat_per_channel,120 vggblock_config=eval(args.vggblock_enc_config),121 transformer_config=eval(args.transformer_enc_config),122 encoder_output_dim=args.enc_output_dim,123 in_channels=args.in_channels,124 )125 @classmethod126 def build_decoder(cls, args, task):127 return TransformerDecoder(128 dictionary=task.target_dictionary,129 embed_dim=args.tgt_embed_dim,130 transformer_config=eval(args.transformer_dec_config),131 conv_config=eval(args.conv_dec_config),132 encoder_output_dim=args.enc_output_dim,133 )134 @classmethod135 def build_model(cls, args, task):136 """Build a new model instance."""137 # make sure that all args are properly defaulted138 # (in case there are any new ones)139 base_architecture(args)140 encoder = cls.build_encoder(args, task)141 decoder = cls.build_decoder(args, task)142 return cls(encoder, decoder)143 def get_normalized_probs(self, net_output, log_probs, sample=None):144 # net_output['encoder_out'] is a (B, T, D) tensor145 lprobs = super().get_normalized_probs(net_output, log_probs, sample)146 lprobs.batch_first = True147 return lprobs148DEFAULT_ENC_VGGBLOCK_CONFIG = ((32, 3, 2, 2, False),) * 2149DEFAULT_ENC_TRANSFORMER_CONFIG = ((256, 4, 1024, True, 0.2, 0.2, 0.2),) * 2150# 256: embedding dimension151# 4: number of heads152# 1024: FFN153# True: apply layerNorm before (dropout + resiaul) instead of after154# 0.2 (dropout): dropout after MultiheadAttention and second FC155# 0.2 (attention_dropout): dropout in MultiheadAttention156# 0.2 (relu_dropout): dropout after ReLu157DEFAULT_DEC_TRANSFORMER_CONFIG = ((256, 2, 1024, True, 0.2, 0.2, 0.2),) * 2158DEFAULT_DEC_CONV_CONFIG = ((256, 3, True),) * 2159# TODO: repace transformer encoder config from one liner160# to explicit args to get rid of this transformation161def prepare_transformer_encoder_params(162 input_dim,163 num_heads,164 ffn_dim,165 normalize_before,166 dropout,167 attention_dropout,168 relu_dropout,169):170 args = argparse.Namespace()171 args.encoder_embed_dim = input_dim172 args.encoder_attention_heads = num_heads173 args.attention_dropout = attention_dropout174 args.dropout = dropout175 args.activation_dropout = relu_dropout176 args.encoder_normalize_before = normalize_before177 args.encoder_ffn_embed_dim = ffn_dim178 return args179def prepare_transformer_decoder_params(180 input_dim,181 num_heads,182 ffn_dim,183 normalize_before,184 dropout,185 attention_dropout,186 relu_dropout,187):188 args = argparse.Namespace()189 args.encoder_embed_dim = None190 args.decoder_embed_dim = input_dim191 args.decoder_attention_heads = num_heads192 args.attention_dropout = attention_dropout193 args.dropout = dropout194 args.activation_dropout = relu_dropout195 args.decoder_normalize_before = normalize_before196 args.decoder_ffn_embed_dim = ffn_dim197 return args198class VGGTransformerEncoder(FairseqEncoder):199 """VGG + Transformer encoder"""200 def __init__(201 self,202 input_feat_per_channel,203 vggblock_config=DEFAULT_ENC_VGGBLOCK_CONFIG,204 transformer_config=DEFAULT_ENC_TRANSFORMER_CONFIG,205 encoder_output_dim=512,206 in_channels=1,207 transformer_context=None,208 transformer_sampling=None,209 ):210 """constructor for VGGTransformerEncoder211 Args:212 - input_feat_per_channel: feature dim (not including stacked,213 just base feature)214 - in_channel: # input channels (e.g., if stack 8 feature vector215 together, this is 8)216 - vggblock_config: configuration of vggblock, see comments on217 DEFAULT_ENC_VGGBLOCK_CONFIG218 - transformer_config: configuration of transformer layer, see comments219 on DEFAULT_ENC_TRANSFORMER_CONFIG220 - encoder_output_dim: final transformer output embedding dimension221 - transformer_context: (left, right) if set, self-attention will be focused222 on (t-left, t+right)223 - transformer_sampling: an iterable of int, must match with224 len(transformer_config), transformer_sampling[i] indicates sampling225 factor for i-th transformer layer, after multihead att and feedfoward226 part227 """228 super().__init__(None)229 self.num_vggblocks = 0230 if vggblock_config is not None:231 if not isinstance(vggblock_config, Iterable):232 raise ValueError("vggblock_config is not iterable")233 self.num_vggblocks = len(vggblock_config)234 self.conv_layers = nn.ModuleList()235 self.in_channels = in_channels236 self.input_dim = input_feat_per_channel237 self.pooling_kernel_sizes = []238 if vggblock_config is not None:239 for _, config in enumerate(vggblock_config):240 (241 out_channels,242 conv_kernel_size,243 pooling_kernel_size,244 num_conv_layers,245 layer_norm,246 ) = config247 self.conv_layers.append(248 VGGBlock(249 in_channels,250 out_channels,251 conv_kernel_size,252 pooling_kernel_size,253 num_conv_layers,254 input_dim=input_feat_per_channel,255 layer_norm=layer_norm,256 )257 )258 self.pooling_kernel_sizes.append(pooling_kernel_size)259 in_channels = out_channels260 input_feat_per_channel = self.conv_layers[-1].output_dim261 transformer_input_dim = self.infer_conv_output_dim(262 self.in_channels, self.input_dim263 )264 # transformer_input_dim is the output dimension of VGG part265 self.validate_transformer_config(transformer_config)266 self.transformer_context = self.parse_transformer_context(transformer_context)267 self.transformer_sampling = self.parse_transformer_sampling(268 transformer_sampling, len(transformer_config)269 )270 self.transformer_layers = nn.ModuleList()271 if transformer_input_dim != transformer_config[0][0]:272 self.transformer_layers.append(273 Linear(transformer_input_dim, transformer_config[0][0])274 )275 self.transformer_layers.append(276 TransformerEncoderLayer(277 prepare_transformer_encoder_params(*transformer_config[0])278 )279 )280 for i in range(1, len(transformer_config)):281 if transformer_config[i - 1][0] != transformer_config[i][0]:282 self.transformer_layers.append(283 Linear(transformer_config[i - 1][0], transformer_config[i][0])284 )285 self.transformer_layers.append(286 TransformerEncoderLayer(287 prepare_transformer_encoder_params(*transformer_config[i])288 )289 )290 self.encoder_output_dim = encoder_output_dim291 self.transformer_layers.extend(292 [293 Linear(transformer_config[-1][0], encoder_output_dim),294 LayerNorm(encoder_output_dim),295 ]296 )297 def forward(self, src_tokens, src_lengths, **kwargs):298 """299 src_tokens: padded tensor (B, T, C * feat)300 src_lengths: tensor of original lengths of input utterances (B,)301 """302 bsz, max_seq_len, _ = src_tokens.size()303 x = src_tokens.view(bsz, max_seq_len, self.in_channels, self.input_dim)304 x = x.transpose(1, 2).contiguous()305 # (B, C, T, feat)306 for layer_idx in range(len(self.conv_layers)):307 x = self.conv_layers[layer_idx](x)308 bsz, _, output_seq_len, _ = x.size()309 # (B, C, T, feat) -> (B, T, C, feat) -> (T, B, C, feat) -> (T, B, C * feat)310 x = x.transpose(1, 2).transpose(0, 1)311 x = x.contiguous().view(output_seq_len, bsz, -1)312 input_lengths = src_lengths.clone()313 for s in self.pooling_kernel_sizes:314 input_lengths = (input_lengths.float() / s).ceil().long()315 encoder_padding_mask, _ = lengths_to_encoder_padding_mask(316 input_lengths, batch_first=True317 )318 if not encoder_padding_mask.any():319 encoder_padding_mask = None320 subsampling_factor = int(max_seq_len * 1.0 / output_seq_len + 0.5)321 attn_mask = self.lengths_to_attn_mask(input_lengths, subsampling_factor)322 transformer_layer_idx = 0323 for layer_idx in range(len(self.transformer_layers)):324 if isinstance(self.transformer_layers[layer_idx], TransformerEncoderLayer):325 x = self.transformer_layers[layer_idx](326 x, encoder_padding_mask, attn_mask327 )328 if self.transformer_sampling[transformer_layer_idx] != 1:329 sampling_factor = self.transformer_sampling[transformer_layer_idx]330 x, encoder_padding_mask, attn_mask = self.slice(331 x, encoder_padding_mask, attn_mask, sampling_factor332 )333 transformer_layer_idx += 1334 else:335 x = self.transformer_layers[layer_idx](x)336 # encoder_padding_maks is a (T x B) tensor, its [t, b] elements indicate337 # whether encoder_output[t, b] is valid or not (valid=0, invalid=1)338 return {339 "encoder_out": x, # (T, B, C)340 "encoder_padding_mask": encoder_padding_mask.t()341 if encoder_padding_mask is not None342 else None,343 # (B, T) --> (T, B)344 }345 def infer_conv_output_dim(self, in_channels, input_dim):346 sample_seq_len = 200347 sample_bsz = 10348 x = torch.randn(sample_bsz, in_channels, sample_seq_len, input_dim)349 for i, _ in enumerate(self.conv_layers):350 x = self.conv_layers[i](x)351 x = x.transpose(1, 2)352 mb, seq = x.size()[:2]353 return x.contiguous().view(mb, seq, -1).size(-1)354 def validate_transformer_config(self, transformer_config):355 for config in transformer_config:356 input_dim, num_heads = config[:2]357 if input_dim % num_heads != 0:358 msg = (359 "ERROR in transformer config {}: ".format(config)360 + "input dimension {} ".format(input_dim)361 + "not dividable by number of heads {}".format(num_heads)362 )363 raise ValueError(msg)364 def parse_transformer_context(self, transformer_context):365 """366 transformer_context can be the following:367 - None; indicates no context is used, i.e.,368 transformer can access full context369 - a tuple/list of two int; indicates left and right context,370 any number <0 indicates infinite context371 * e.g., (5, 6) indicates that for query at x_t, transformer can372 access [t-5, t+6] (inclusive)373 * e.g., (-1, 6) indicates that for query at x_t, transformer can374 access [0, t+6] (inclusive)375 """376 if transformer_context is None:377 return None378 if not isinstance(transformer_context, Iterable):379 raise ValueError("transformer context must be Iterable if it is not None")380 if len(transformer_context) != 2:381 raise ValueError("transformer context must have length 2")382 left_context = transformer_context[0]383 if left_context < 0:384 left_context = None385 right_context = transformer_context[1]386 if right_context < 0:387 right_context = None388 if left_context is None and right_context is None:389 return None390 return (left_context, right_context)391 def parse_transformer_sampling(self, transformer_sampling, num_layers):392 """393 parsing transformer sampling configuration394 Args:395 - transformer_sampling, accepted input:396 * None, indicating no sampling397 * an Iterable with int (>0) as element398 - num_layers, expected number of transformer layers, must match with399 the length of transformer_sampling if it is not None400 Returns:401 - A tuple with length num_layers402 """403 if transformer_sampling is None:404 return (1,) * num_layers405 if not isinstance(transformer_sampling, Iterable):406 raise ValueError(407 "transformer_sampling must be an iterable if it is not None"408 )409 if len(transformer_sampling) != num_layers:410 raise ValueError(411 "transformer_sampling {} does not match with the number "412 "of layers {}".format(transformer_sampling, num_layers)413 )414 for layer, value in enumerate(transformer_sampling):415 if not isinstance(value, int):416 raise ValueError("Invalid value in transformer_sampling: ")417 if value < 1:418 raise ValueError(419 "{} layer's subsampling is {}.".format(layer, value)420 + " This is not allowed! "421 )422 return transformer_sampling423 def slice(self, embedding, padding_mask, attn_mask, sampling_factor):424 """425 embedding is a (T, B, D) tensor426 padding_mask is a (B, T) tensor or None427 attn_mask is a (T, T) tensor or None428 """429 embedding = embedding[::sampling_factor, :, :]430 if padding_mask is not None:431 padding_mask = padding_mask[:, ::sampling_factor]432 if attn_mask is not None:433 attn_mask = attn_mask[::sampling_factor, ::sampling_factor]434 return embedding, padding_mask, attn_mask435 def lengths_to_attn_mask(self, input_lengths, subsampling_factor=1):436 """437 create attention mask according to sequence lengths and transformer438 context439 Args:440 - input_lengths: (B, )-shape Int/Long tensor; input_lengths[b] is441 the length of b-th sequence442 - subsampling_factor: int443 * Note that the left_context and right_context is specified in444 the input frame-level while input to transformer may already445 go through subsampling (e.g., the use of striding in vggblock)446 we use subsampling_factor to scale the left/right context447 Return:448 - a (T, T) binary tensor or None, where T is max(input_lengths)449 * if self.transformer_context is None, None450 * if left_context is None,451 * attn_mask[t, t + right_context + 1:] = 1452 * others = 0453 * if right_context is None,454 * attn_mask[t, 0:t - left_context] = 1455 * others = 0456 * elsif457 * attn_mask[t, t - left_context: t + right_context + 1] = 0458 * others = 1459 """460 if self.transformer_context is None:461 return None462 maxT = torch.max(input_lengths).item()463 attn_mask = torch.zeros(maxT, maxT)464 left_context = self.transformer_context[0]465 right_context = self.transformer_context[1]466 if left_context is not None:467 left_context = math.ceil(self.transformer_context[0] / subsampling_factor)468 if right_context is not None:469 right_context = math.ceil(self.transformer_context[1] / subsampling_factor)470 for t in range(maxT):471 if left_context is not None:472 st = 0473 en = max(st, t - left_context)474 attn_mask[t, st:en] = 1475 if right_context is not None:476 st = t + right_context + 1477 st = min(st, maxT - 1)478 attn_mask[t, st:] = 1479 return attn_mask.to(input_lengths.device)480 def reorder_encoder_out(self, encoder_out, new_order):481 encoder_out["encoder_out"] = encoder_out["encoder_out"].index_select(482 1, new_order483 )484 if encoder_out["encoder_padding_mask"] is not None:485 encoder_out["encoder_padding_mask"] = encoder_out[486 "encoder_padding_mask"487 ].index_select(1, new_order)488 return encoder_out489class TransformerDecoder(FairseqIncrementalDecoder):490 """491 Transformer decoder consisting of *args.decoder_layers* layers. Each layer492 is a :class:`TransformerDecoderLayer`.493 Args:494 args (argparse.Namespace): parsed command-line arguments495 dictionary (~fairseq.data.Dictionary): decoding dictionary496 embed_tokens (torch.nn.Embedding): output embedding497 no_encoder_attn (bool, optional): whether to attend to encoder outputs.498 Default: ``False``499 left_pad (bool, optional): whether the input is left-padded. Default:500 ``False``501 """502 def __init__(503 self,504 dictionary,505 embed_dim=512,506 transformer_config=DEFAULT_ENC_TRANSFORMER_CONFIG,507 conv_config=DEFAULT_DEC_CONV_CONFIG,508 encoder_output_dim=512,509 ):510 super().__init__(dictionary)511 vocab_size = len(dictionary)512 self.padding_idx = dictionary.pad()513 self.embed_tokens = Embedding(vocab_size, embed_dim, self.padding_idx)514 self.conv_layers = nn.ModuleList()515 for i in range(len(conv_config)):516 out_channels, kernel_size, layer_norm = conv_config[i]517 if i == 0:518 conv_layer = LinearizedConv1d(519 embed_dim, out_channels, kernel_size, padding=kernel_size - 1520 )521 else:522 conv_layer = LinearizedConv1d(523 conv_config[i - 1][0],524 out_channels,525 kernel_size,526 padding=kernel_size - 1,527 )528 self.conv_layers.append(conv_layer)529 if layer_norm:530 self.conv_layers.append(nn.LayerNorm(out_channels))531 self.conv_layers.append(nn.ReLU())532 self.layers = nn.ModuleList()533 if conv_config[-1][0] != transformer_config[0][0]:534 self.layers.append(Linear(conv_config[-1][0], transformer_config[0][0]))535 self.layers.append(536 TransformerDecoderLayer(537 prepare_transformer_decoder_params(*transformer_config[0])538 )539 )540 for i in range(1, len(transformer_config)):541 if transformer_config[i - 1][0] != transformer_config[i][0]:542 self.layers.append(543 Linear(transformer_config[i - 1][0], transformer_config[i][0])544 )545 self.layers.append(546 TransformerDecoderLayer(547 prepare_transformer_decoder_params(*transformer_config[i])548 )549 )550 self.fc_out = Linear(transformer_config[-1][0], vocab_size)551 def forward(self, prev_output_tokens, encoder_out=None, incremental_state=None):552 """553 Args:554 prev_output_tokens (LongTensor): previous decoder outputs of shape555 `(batch, tgt_len)`, for input feeding/teacher forcing556 encoder_out (Tensor, optional): output from the encoder, used for557 encoder-side attention558 incremental_state (dict): dictionary used for storing state during559 :ref:`Incremental decoding`560 Returns:561 tuple:562 - the last decoder layer's output of shape `(batch, tgt_len,563 vocab)`564 - the last decoder layer's attention weights of shape `(batch,565 tgt_len, src_len)`566 """567 target_padding_mask = (568 (prev_output_tokens == self.padding_idx).to(prev_output_tokens.device)569 if incremental_state is None570 else None571 )572 if incremental_state is not None:573 prev_output_tokens = prev_output_tokens[:, -1:]574 # embed tokens575 x = self.embed_tokens(prev_output_tokens)576 # B x T x C -> T x B x C577 x = self._transpose_if_training(x, incremental_state)578 for layer in self.conv_layers:579 if isinstance(layer, LinearizedConvolution):580 x = layer(x, incremental_state)581 else:582 x = layer(x)583 # B x T x C -> T x B x C584 x = self._transpose_if_inference(x, incremental_state)585 # decoder layers586 for layer in self.layers:587 if isinstance(layer, TransformerDecoderLayer):588 x, *_ = layer(589 x,590 (encoder_out["encoder_out"] if encoder_out is not None else None),591 (592 encoder_out["encoder_padding_mask"].t()593 if encoder_out["encoder_padding_mask"] is not None594 else None595 ),596 incremental_state,597 self_attn_mask=(598 self.buffered_future_mask(x)599 if incremental_state is None600 else None601 ),602 self_attn_padding_mask=(603 target_padding_mask if incremental_state is None else None604 ),605 )606 else:607 x = layer(x)608 # T x B x C -> B x T x C609 x = x.transpose(0, 1)610 x = self.fc_out(x)611 return x, None612 def buffered_future_mask(self, tensor):613 dim = tensor.size(0)614 if (615 not hasattr(self, "_future_mask")616 or self._future_mask is None617 or self._future_mask.device != tensor.device618 ):619 self._future_mask = torch.triu(620 utils.fill_with_neg_inf(tensor.new(dim, dim)), 1621 )622 if self._future_mask.size(0) < dim:623 self._future_mask = torch.triu(624 utils.fill_with_neg_inf(self._future_mask.resize_(dim, dim)), 1625 )626 return self._future_mask[:dim, :dim]627 def _transpose_if_training(self, x, incremental_state):628 if incremental_state is None:629 x = x.transpose(0, 1)630 return x631 def _transpose_if_inference(self, x, incremental_state):632 if incremental_state:633 x = x.transpose(0, 1)634 return x635@register_model("asr_vggtransformer_encoder")636class VGGTransformerEncoderModel(FairseqEncoderModel):637 def __init__(self, encoder):638 super().__init__(encoder)639 @staticmethod640 def add_args(parser):641 """Add model-specific arguments to the parser."""642 parser.add_argument(643 "--input-feat-per-channel",644 type=int,645 metavar="N",646 help="encoder input dimension per input channel",647 )648 parser.add_argument(649 "--vggblock-enc-config",650 type=str,651 metavar="EXPR",652 help="""653 an array of tuples each containing the configuration of one vggblock654 [(out_channels, conv_kernel_size, pooling_kernel_size,num_conv_layers), ...]655 """,656 )657 parser.add_argument(658 "--transformer-enc-config",659 type=str,660 metavar="EXPR",661 help="""662 a tuple containing the configuration of the Transformer layers663 configurations:664 [(input_dim,665 num_heads,666 ffn_dim,667 normalize_before,668 dropout,669 attention_dropout,670 relu_dropout), ]""",671 )672 parser.add_argument(673 "--enc-output-dim",674 type=int,675 metavar="N",676 help="encoder output dimension, projecting the LSTM output",677 )678 parser.add_argument(679 "--in-channels",680 type=int,681 metavar="N",682 help="number of encoder input channels",683 )684 parser.add_argument(685 "--transformer-context",686 type=str,687 metavar="EXPR",688 help="""689 either None or a tuple of two ints, indicating left/right context a690 transformer can have access to""",691 )692 parser.add_argument(693 "--transformer-sampling",694 type=str,695 metavar="EXPR",696 help="""697 either None or a tuple of ints, indicating sampling factor in each layer""",698 )699 @classmethod700 def build_model(cls, args, task):701 """Build a new model instance."""702 base_architecture_enconly(args)703 encoder = VGGTransformerEncoderOnly(704 vocab_size=len(task.target_dictionary),705 input_feat_per_channel=args.input_feat_per_channel,706 vggblock_config=eval(args.vggblock_enc_config),707 transformer_config=eval(args.transformer_enc_config),708 encoder_output_dim=args.enc_output_dim,709 in_channels=args.in_channels,710 transformer_context=eval(args.transformer_context),711 transformer_sampling=eval(args.transformer_sampling),712 )713 return cls(encoder)714 def get_normalized_probs(self, net_output, log_probs, sample=None):715 # net_output['encoder_out'] is a (T, B, D) tensor716 lprobs = super().get_normalized_probs(net_output, log_probs, sample)717 # lprobs is a (T, B, D) tensor718 # we need to transoose to get (B, T, D) tensor719 lprobs = lprobs.transpose(0, 1).contiguous()720 lprobs.batch_first = True721 return lprobs722class VGGTransformerEncoderOnly(VGGTransformerEncoder):723 def __init__(724 self,725 vocab_size,726 input_feat_per_channel,727 vggblock_config=DEFAULT_ENC_VGGBLOCK_CONFIG,728 transformer_config=DEFAULT_ENC_TRANSFORMER_CONFIG,729 encoder_output_dim=512,730 in_channels=1,731 transformer_context=None,732 transformer_sampling=None,733 ):734 super().__init__(735 input_feat_per_channel=input_feat_per_channel,736 vggblock_config=vggblock_config,737 transformer_config=transformer_config,738 encoder_output_dim=encoder_output_dim,739 in_channels=in_channels,740 transformer_context=transformer_context,741 transformer_sampling=transformer_sampling,742 )743 self.fc_out = Linear(self.encoder_output_dim, vocab_size)744 def forward(self, src_tokens, src_lengths, **kwargs):745 """746 src_tokens: padded tensor (B, T, C * feat)747 src_lengths: tensor of original lengths of input utterances (B,)748 """749 enc_out = super().forward(src_tokens, src_lengths)750 x = self.fc_out(enc_out["encoder_out"])751 # x = F.log_softmax(x, dim=-1)752 # Note: no need this line, because model.get_normalized_prob will call753 # log_softmax754 return {755 "encoder_out": x, # (T, B, C)756 "encoder_padding_mask": enc_out["encoder_padding_mask"], # (T, B)757 }758 def max_positions(self):759 """Maximum input length supported by the encoder."""760 return (1e6, 1e6) # an arbitrary large number761def Embedding(num_embeddings, embedding_dim, padding_idx):762 m = nn.Embedding(num_embeddings, embedding_dim, padding_idx=padding_idx)763 # nn.init.uniform_(m.weight, -0.1, 0.1)764 # nn.init.constant_(m.weight[padding_idx], 0)765 return m766def Linear(in_features, out_features, bias=True, dropout=0):767 """Linear layer (input: N x T x C)"""768 m = nn.Linear(in_features, out_features, bias=bias)769 # m.weight.data.uniform_(-0.1, 0.1)770 # if bias:771 # m.bias.data.uniform_(-0.1, 0.1)772 return m773def LinearizedConv1d(in_channels, out_channels, kernel_size, dropout=0, **kwargs):774 """Weight-normalized Conv1d layer optimized for decoding"""775 m = LinearizedConvolution(in_channels, out_channels, kernel_size, **kwargs)776 std = math.sqrt((4 * (1.0 - dropout)) / (m.kernel_size[0] * in_channels))777 nn.init.normal_(m.weight, mean=0, std=std)778 nn.init.constant_(m.bias, 0)779 return nn.utils.weight_norm(m, dim=2)780def LayerNorm(embedding_dim):781 m = nn.LayerNorm(embedding_dim)782 return m783# seq2seq models784def base_architecture(args):785 args.input_feat_per_channel = getattr(args, "input_feat_per_channel", 40)786 args.vggblock_enc_config = getattr(787 args, "vggblock_enc_config", DEFAULT_ENC_VGGBLOCK_CONFIG788 )789 args.transformer_enc_config = getattr(790 args, "transformer_enc_config", DEFAULT_ENC_TRANSFORMER_CONFIG791 )792 args.enc_output_dim = getattr(args, "enc_output_dim", 512)793 args.in_channels = getattr(args, "in_channels", 1)794 args.tgt_embed_dim = getattr(args, "tgt_embed_dim", 128)795 args.transformer_dec_config = getattr(796 args, "transformer_dec_config", DEFAULT_ENC_TRANSFORMER_CONFIG797 )798 args.conv_dec_config = getattr(args, "conv_dec_config", DEFAULT_DEC_CONV_CONFIG)799 args.transformer_context = getattr(args, "transformer_context", "None")800@register_model_architecture("asr_vggtransformer", "vggtransformer_1")801def vggtransformer_1(args):802 args.input_feat_per_channel = getattr(args, "input_feat_per_channel", 80)803 args.vggblock_enc_config = getattr(804 args, "vggblock_enc_config", "[(64, 3, 2, 2, True), (128, 3, 2, 2, True)]"805 )806 args.transformer_enc_config = getattr(807 args,808 "transformer_enc_config",809 "((1024, 16, 4096, True, 0.15, 0.15, 0.15),) * 14",810 )811 args.enc_output_dim = getattr(args, "enc_output_dim", 1024)812 args.tgt_embed_dim = getattr(args, "tgt_embed_dim", 128)813 args.conv_dec_config = getattr(args, "conv_dec_config", "((256, 3, True),) * 4")814 args.transformer_dec_config = getattr(815 args,816 "transformer_dec_config",817 "((1024, 16, 4096, True, 0.15, 0.15, 0.15),) * 4",818 )819@register_model_architecture("asr_vggtransformer", "vggtransformer_2")820def vggtransformer_2(args):821 args.input_feat_per_channel = getattr(args, "input_feat_per_channel", 80)822 args.vggblock_enc_config = getattr(823 args, "vggblock_enc_config", "[(64, 3, 2, 2, True), (128, 3, 2, 2, True)]"824 )825 args.transformer_enc_config = getattr(826 args,827 "transformer_enc_config",828 "((1024, 16, 4096, True, 0.15, 0.15, 0.15),) * 16",829 )830 args.enc_output_dim = getattr(args, "enc_output_dim", 1024)831 args.tgt_embed_dim = getattr(args, "tgt_embed_dim", 512)832 args.conv_dec_config = getattr(args, "conv_dec_config", "((256, 3, True),) * 4")833 args.transformer_dec_config = getattr(834 args,835 "transformer_dec_config",836 "((1024, 16, 4096, True, 0.15, 0.15, 0.15),) * 6",837 )838@register_model_architecture("asr_vggtransformer", "vggtransformer_base")839def vggtransformer_base(args):840 args.input_feat_per_channel = getattr(args, "input_feat_per_channel", 80)841 args.vggblock_enc_config = getattr(842 args, "vggblock_enc_config", "[(64, 3, 2, 2, True), (128, 3, 2, 2, True)]"843 )844 args.transformer_enc_config = getattr(845 args, "transformer_enc_config", "((512, 8, 2048, True, 0.15, 0.15, 0.15),) * 12"846 )847 args.enc_output_dim = getattr(args, "enc_output_dim", 512)848 args.tgt_embed_dim = getattr(args, "tgt_embed_dim", 512)849 args.conv_dec_config = getattr(args, "conv_dec_config", "((256, 3, True),) * 4")850 args.transformer_dec_config = getattr(851 args, "transformer_dec_config", "((512, 8, 2048, True, 0.15, 0.15, 0.15),) * 6"852 )853 # Size estimations:854 # Encoder:855 # - vggblock param: 64*1*3*3 + 64*64*3*3 + 128*64*3*3 + 128*128*3 = 258K856 # Transformer:857 # - input dimension adapter: 2560 x 512 -> 1.31M858 # - transformer_layers (x12) --> 37.74M859 # * MultiheadAttention: 512*512*3 (in_proj) + 512*512 (out_proj) = 1.048M860 # * FFN weight: 512*2048*2 = 2.097M861 # - output dimension adapter: 512 x 512 -> 0.26 M862 # Decoder:863 # - LinearizedConv1d: 512 * 256 * 3 + 256 * 256 * 3 * 3864 # - transformer_layer: (x6) --> 25.16M865 # * MultiheadAttention (self-attention): 512*512*3 + 512*512 = 1.048M866 # * MultiheadAttention (encoder-attention): 512*512*3 + 512*512 = 1.048M867 # * FFN: 512*2048*2 = 2.097M868 # Final FC:869 # - FC: 512*5000 = 256K (assuming vocab size 5K)870 # In total:871 # ~65 M872# CTC models873def base_architecture_enconly(args):874 args.input_feat_per_channel = getattr(args, "input_feat_per_channel", 40)875 args.vggblock_enc_config = getattr(876 args, "vggblock_enc_config", "[(32, 3, 2, 2, True)] * 2"877 )878 args.transformer_enc_config = getattr(879 args, "transformer_enc_config", "((256, 4, 1024, True, 0.2, 0.2, 0.2),) * 2"880 )881 args.enc_output_dim = getattr(args, "enc_output_dim", 512)882 args.in_channels = getattr(args, "in_channels", 1)883 args.transformer_context = getattr(args, "transformer_context", "None")884 args.transformer_sampling = getattr(args, "transformer_sampling", "None")885@register_model_architecture("asr_vggtransformer_encoder", "vggtransformer_enc_1")886def vggtransformer_enc_1(args):887 # vggtransformer_1 is the same as vggtransformer_enc_big, except the number888 # of layers is increased to 16889 # keep it here for backward compatiablity purpose890 args.input_feat_per_channel = getattr(args, "input_feat_per_channel", 80)891 args.vggblock_enc_config = getattr(892 args, "vggblock_enc_config", "[(64, 3, 2, 2, True), (128, 3, 2, 2, True)]"893 )894 args.transformer_enc_config = getattr(895 args,896 "transformer_enc_config",897 "((1024, 16, 4096, True, 0.15, 0.15, 0.15),) * 16",898 )...

Full Screen

Full Screen

FromOptionsTransformer.js

Source:FromOptionsTransformer.js Github

copy

Full Screen

...59 constructor(reporter, idGenerator = new UniqueIdentifierGenerator()) {60 super(reporter, options.validate);61 var append = (transformer) => {62 this.append((tree) => {63 return new transformer(idGenerator, reporter).transformAny(tree);64 });65 };66 if (transformOptions.blockBinding) {67 this.append((tree) => {68 validateConst(tree, reporter);69 return tree;70 });71 }72 // Issue errors for any unbound variables73 if (options.freeVariableChecker) {74 this.append((tree) => {75 validateFreeVariables(tree, reporter);76 return tree;77 });...

Full Screen

Full Screen

QueryBuilderTest.js

Source:QueryBuilderTest.js Github

copy

Full Screen

1/*global ngDescribe, describe, it, expect, angular*/2ngDescribe({3 name: 'QueryBuilder',4 modules: 'lucidworksView.utils.queryBuilder',5 inject: ['QueryBuilder'],6 tests: function (deps) {7 describe('urls should be created from objects.', function(){8 it('should turn a flat object into a string', function(){9 var obj = {q: 'test', start: 0, rows: 10, wt: 'json'};10 expect(deps.QueryBuilder.objectToURLString(obj)).toEqual('q=test&start=0&rows=10&wt=json');11 });12 it('should turn hash tags into uri encoded components', function(){13 var obj = {q: '#1', start: 0, rows: 10, wt: 'json'};14 expect(deps.QueryBuilder.objectToURLString(obj)).toEqual('q=%231&start=0&rows=10&wt=json');15 obj = {q: '1#2#3', start: 0, rows: 10, wt: 'json'};16 expect(deps.QueryBuilder.objectToURLString(obj)).toEqual('q=1%232%233&start=0&rows=10&wt=json');17 });18 });19 describe('transformers should be registered', function() {20 it('should be able to register keyValue transformers', function(){21 deps.QueryBuilder.registerTransformer('keyValue', 'test', keyValueTransformerFunc);22 function keyValueTransformerFunc(key, value){23 return key +'!!!'+ value;24 }25 var obj = {q: '#1', start: 0, rows: 10, wt: 'json', fq: [{key: 'department', transformer: 'test', values: ['VIDEO/COMPACT DISC']}]};26 expect(deps.QueryBuilder.objectToURLString(obj)).toEqual('q=%231&start=0&rows=10&wt=json&fq=department!!!VIDEO/COMPACT DISC');27 });28 it('should be able to register preEncodeWrapper transformers', function(){29 deps.QueryBuilder.registerTransformer('preEncodeWrapper', 'test', preEncodeWrapperTransformerFunc);30 function preEncodeWrapperTransformerFunc(data){31 return '!!!'+data+'!!!';32 }33 var obj = {q: '#1', start: 0, rows: 10, wt: 'json', fq: [{key: 'department', transformer: 'test', values: ['VIDEO/COMPACT DISC']}]};34 expect(deps.QueryBuilder.objectToURLString(obj)).toEqual('q=%231&start=0&rows=10&wt=json&fq=department=!!!VIDEO/COMPACT DISC!!!');35 });36 it('should be able to register encode transformers', function(){37 deps.QueryBuilder.registerTransformer('preEncodeWrapper', 'test', preEncodeWrapperTransformerFunc);38 deps.QueryBuilder.registerTransformer('encode', 'test', encodeTransformerFunc);39 function preEncodeWrapperTransformerFunc(data){40 return '!!!'+data+'!!!';41 }42 function encodeTransformerFunc(data){43 return 'XXX'+data+'XXX';44 }45 var obj = {q: '#1', start: 0, rows: 10, wt: 'json', fq: [{key: 'department', transformer: 'test', values: ['VIDEO/COMPACT DISC']}]};46 expect(deps.QueryBuilder.objectToURLString(obj)).toEqual('q=%231&start=0&rows=10&wt=json&fq=department=XXX!!!VIDEO/COMPACT DISC!!!XXX');47 });48 it('should be able to register wrapper transformers', function(){49 deps.QueryBuilder.registerTransformer('preEncodeWrapper', 'test', preEncodeWrapperTransformerFunc);50 deps.QueryBuilder.registerTransformer('encode', 'test', encodeTransformerFunc);51 deps.QueryBuilder.registerTransformer('wrapper', 'test', wrapperTransformerFunc);52 function preEncodeWrapperTransformerFunc(data){53 return '!!!'+data+'!!!';54 }55 function encodeTransformerFunc(data){56 return 'XXX'+data+'XXX';57 }58 function wrapperTransformerFunc(data){59 return '['+data+']';60 }61 var obj = {q: '#1', start: 0, rows: 10, wt: 'json', fq: [{key: 'department', transformer: 'test', values: ['VIDEO/COMPACT DISC']}]};62 expect(deps.QueryBuilder.objectToURLString(obj)).toEqual('q=%231&start=0&rows=10&wt=json&fq=department=[XXX!!!VIDEO/COMPACT DISC!!!XXX]');63 });64 it('should be able to register join transformers', function(){65 deps.QueryBuilder.registerTransformer('preEncodeWrapper', 'test', preEncodeWrapperTransformerFunc);66 deps.QueryBuilder.registerTransformer('encode', 'test', encodeTransformerFunc);67 deps.QueryBuilder.registerTransformer('wrapper', 'test', wrapperTransformerFunc);68 deps.QueryBuilder.registerTransformer('join', 'test', joinTransformerFunc);69 function preEncodeWrapperTransformerFunc(data){70 return '!!!'+data+'!!!';71 }72 function encodeTransformerFunc(data){73 return 'XXX'+data+'XXX';74 }75 function wrapperTransformerFunc(data){76 return '['+data+']';77 }78 function joinTransformerFunc(str, arr){79 var join = '~';80 if(angular.isString(arr)){81 return str + join + arr;82 }83 return _.reduce(arr, arrayJoinStringReducer, str);84 function arrayJoinStringReducer(str, value){85 return str + ((str!=='')?join:'') + value;86 }87 }88 var obj = {q: '#1', start: 0, rows: 10, wt: 'json', fq: [{key: 'department', transformer: 'test', values: ['VIDEO/COMPACT DISC', 'LASERCAT']}]};89 expect(deps.QueryBuilder.objectToURLString(obj)).toEqual('q=%231&start=0&rows=10&wt=json&fq=department=[XXX!!!VIDEO/COMPACT DISC!!!XXX]~department=[XXX!!!LASERCAT!!!XXX]');90 });91 });92 describe('helper functions should help transform', function(){93 it('should escape special characters used in lucene', function(){94 expect(deps.QueryBuilder.escapeSpecialChars('string + - && { } has special chars ? * ||')).toEqual('string \\+ \\- \\\&\\\& \\{ \\} has special chars \\? \\* \\|\\|');95 });96 it('should be able to encode URI components with plusses', function(){97 expect(deps.QueryBuilder.encodeURIComponentPlus('string % $ has spaces')).toEqual('string+%25+%24+has+spaces');98 });99 it('should help build key value string', function(){100 expect(deps.QueryBuilder.keyValueString('key', 'value', 'join')).toEqual('keyjoinvalue');101 });102 it('should help build an array joined string', function(){103 expect(deps.QueryBuilder.arrayJoinString('', ['in','the','array'], '~')).toEqual('in~the~array');104 expect(deps.QueryBuilder.arrayJoinString('thisIsAString', ['in','the','array'], '~')).toEqual('thisIsAString~in~the~array');105 });106 });107 }...

Full Screen

Full Screen

ConvertingTransformersModule.ts

Source:ConvertingTransformersModule.ts Github

copy

Full Screen

1import { ContainerModule, interfaces } from 'inversify';2import { InversifyContainerFacade } from '../../InversifyContainerFacade';3import { ServiceIdentifiers } from '../../ServiceIdentifiers';4import { INodeTransformer } from '../../../interfaces/node-transformers/INodeTransformer';5import { IObjectExpressionExtractor } from '../../../interfaces/node-transformers/converting-transformers/object-expression-extractors/IObjectExpressionExtractor';6import { NodeTransformer } from '../../../enums/node-transformers/NodeTransformer';7import { ObjectExpressionExtractor } from '../../../enums/node-transformers/converting-transformers/properties-extractors/ObjectExpressionExtractor';8import { BasePropertiesExtractor } from '../../../node-transformers/converting-transformers/object-expression-extractors/BasePropertiesExtractor';9import { BooleanLiteralTransformer } from '../../../node-transformers/converting-transformers/BooleanLiteralTransformer';10import { ExportSpecifierTransformer } from '../../../node-transformers/converting-transformers/ExportSpecifierTransformer';11import { MemberExpressionTransformer } from '../../../node-transformers/converting-transformers/MemberExpressionTransformer';12import { ClassFieldTransformer } from '../../../node-transformers/converting-transformers/ClassFieldTransformer';13import { NumberLiteralTransformer } from '../../../node-transformers/converting-transformers/NumberLiteralTransformer';14import { NumberToNumericalExpressionTransformer } from '../../../node-transformers/converting-transformers/NumberToNumericalExpressionTransformer';15import { ObjectExpressionKeysTransformer } from '../../../node-transformers/converting-transformers/ObjectExpressionKeysTransformer';16import { ObjectExpressionToVariableDeclarationExtractor } from '../../../node-transformers/converting-transformers/object-expression-extractors/ObjectExpressionToVariableDeclarationExtractor';17import { ObjectExpressionTransformer } from '../../../node-transformers/converting-transformers/ObjectExpressionTransformer';18import { ObjectPatternPropertiesTransformer } from '../../../node-transformers/converting-transformers/ObjectPatternPropertiesTransformer';19import { SplitStringTransformer } from '../../../node-transformers/converting-transformers/SplitStringTransformer';20import { TemplateLiteralTransformer } from '../../../node-transformers/converting-transformers/TemplateLiteralTransformer';21export const convertingTransformersModule: interfaces.ContainerModule = new ContainerModule((bind: interfaces.Bind) => {22 // converting transformers23 bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)24 .to(BooleanLiteralTransformer)25 .whenTargetNamed(NodeTransformer.BooleanLiteralTransformer);26 bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)27 .to(ExportSpecifierTransformer)28 .whenTargetNamed(NodeTransformer.ExportSpecifierTransformer);29 bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)30 .to(MemberExpressionTransformer)31 .whenTargetNamed(NodeTransformer.MemberExpressionTransformer);32 bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)33 .to(ClassFieldTransformer)34 .whenTargetNamed(NodeTransformer.ClassFieldTransformer);35 bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)36 .to(NumberLiteralTransformer)37 .whenTargetNamed(NodeTransformer.NumberLiteralTransformer);38 bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)39 .to(NumberToNumericalExpressionTransformer)40 .whenTargetNamed(NodeTransformer.NumberToNumericalExpressionTransformer);41 bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)42 .to(ObjectExpressionKeysTransformer)43 .whenTargetNamed(NodeTransformer.ObjectExpressionKeysTransformer);44 bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)45 .to(ObjectExpressionTransformer)46 .whenTargetNamed(NodeTransformer.ObjectExpressionTransformer);47 bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)48 .to(ObjectPatternPropertiesTransformer)49 .whenTargetNamed(NodeTransformer.ObjectPatternPropertiesTransformer);50 bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)51 .to(SplitStringTransformer)52 .whenTargetNamed(NodeTransformer.SplitStringTransformer);53 bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)54 .to(TemplateLiteralTransformer)55 .whenTargetNamed(NodeTransformer.TemplateLiteralTransformer);56 // object expression extractors57 bind<IObjectExpressionExtractor>(ServiceIdentifiers.IObjectExpressionExtractor)58 .to(ObjectExpressionToVariableDeclarationExtractor)59 .whenTargetNamed(ObjectExpressionExtractor.ObjectExpressionToVariableDeclarationExtractor);60 bind<IObjectExpressionExtractor>(ServiceIdentifiers.IObjectExpressionExtractor)61 .to(BasePropertiesExtractor)62 .whenTargetNamed(ObjectExpressionExtractor.BasePropertiesExtractor);63 // object expression extractor factory64 bind<IObjectExpressionExtractor>(ServiceIdentifiers.Factory__IObjectExpressionExtractor)65 .toFactory<IObjectExpressionExtractor>(InversifyContainerFacade66 .getCacheFactory<ObjectExpressionExtractor, IObjectExpressionExtractor>(67 ServiceIdentifiers.IObjectExpressionExtractor68 ));...

Full Screen

Full Screen

DefaultValueTransformers.ts

Source:DefaultValueTransformers.ts Github

copy

Full Screen

1/* eslint-disable @typescript-eslint/explicit-module-boundary-types */2/* eslint-disable @typescript-eslint/no-explicit-any */3import { ValueTransformer } from './ValueTransformer';4export class JsonTransformer implements ValueTransformer {5 toDB(input: any): string | null {6 return input == undefined ? null : JSON.stringify(input);7 }8 fromDB(input: string | null): any {9 return input == null ? undefined : JSON.parse(input);10 }11}12export class BooleanTextTransformer implements ValueTransformer {13 toDB(input: boolean | undefined): string | null {14 return input == undefined ? null : !input ? '0' : '1';15 }16 fromDB(input: string | null): boolean | undefined {17 if (input == null) {18 return undefined;19 }20 if (input === '0' || input === 'false') {21 return false;22 } else if (input === '1' || input === 'true') {23 return true;24 }25 return undefined;26 }27}28export class BooleanNumberTransformer implements ValueTransformer {29 toDB(input: boolean | undefined): number | null {30 return input == undefined ? null : !input ? 0 : 1;31 }32 fromDB(input: number | null): boolean | undefined {33 return input == null ? undefined : !input ? false : true;34 }35}36export class DateTextTransformer implements ValueTransformer {37 toDB(input: Date | undefined): string | null {38 return input == undefined ? null : input.toISOString();39 }40 fromDB(input: string | null): Date | undefined {41 return input == null ? undefined : new Date(Date.parse(input));42 }43}44export class DateIntegerAsSecondsTransformer implements ValueTransformer {45 toDB(input: Date | undefined): number | null {46 return input == undefined ? null : Math.floor(input.getTime() / 1000);47 }48 fromDB(input: number | null): Date | undefined {49 return input == null ? undefined : new Date(Number.isInteger(input) ? input * 1000 : NaN);50 }51}52export class DateIntegerAsMillisecondsTransformer implements ValueTransformer {53 toDB(input: Date | undefined): number | null {54 return input == undefined ? null : input.getTime();55 }56 fromDB(input: number | null): Date | undefined {57 return input == null ? undefined : new Date(Number.isInteger(input) ? input : NaN);58 }59}60export class NumberTextTransformer implements ValueTransformer {61 toDB(input: number | undefined): string | null {62 return input == undefined ? null : String(input);63 }64 fromDB(input: string | null): number | undefined {65 return input == null ? undefined : Number(input);66 }67}68export class NumberDefaultTransformer implements ValueTransformer {69 toDB(input: number | undefined): number | null {70 return input == undefined ? null : Number(input);71 }72 fromDB(input: number | null): number | undefined {73 return input == null ? undefined : Number(input);74 }75}76export class StringDefaultTransformer implements ValueTransformer {77 toDB(input: string | undefined): string | null {78 return input == undefined ? null : String(input);79 }80 fromDB(input: string | null): string | undefined {81 return input == null ? undefined : String(input);82 }83}84export class StringNumberTransformer implements ValueTransformer {85 toDB(input: string | undefined): number | null {86 return input == undefined ? null : Number(input);87 }88 fromDB(input: number | null): string | undefined {89 return input == null ? undefined : String(input);90 }91}92export class UnknownDefaultTransformer implements ValueTransformer {93 /* istanbul ignore next */94 toDB(input: any | undefined): string | null {95 return input == undefined ? null : input;96 }97 /* istanbul ignore next */98 fromDB(input: string | null): string | undefined {99 return input == null ? undefined : input;100 }101}102export class DefaultValueTransformers {103 readonly json: ValueTransformer = new JsonTransformer();104 readonly booleanText: ValueTransformer = new BooleanTextTransformer();105 readonly booleanNumber: ValueTransformer = new BooleanNumberTransformer();106 readonly dateText: ValueTransformer = new DateTextTransformer();107 readonly dateIntegerAsSeconds: ValueTransformer = new DateIntegerAsSecondsTransformer();108 readonly dateIntegerAsMilliseconds: ValueTransformer = new DateIntegerAsMillisecondsTransformer();109 readonly numberText: ValueTransformer = new NumberTextTransformer();110 readonly numberDefault: ValueTransformer = new NumberDefaultTransformer();111 readonly stringNumber: ValueTransformer = new StringNumberTransformer();112 readonly stringDefault: ValueTransformer = new StringDefaultTransformer();113 readonly unknownDefault: ValueTransformer = new UnknownDefaultTransformer();114}...

Full Screen

Full Screen

index.ts

Source:index.ts Github

copy

Full Screen

1import { callStatementTransformer } from "./call-statement";2import { doWhileStatementTransformer } from "./do-while-statement";3import { forOfStatementTransformer } from "./for-of-statement";4import { ifStatementTransformer } from "./if-statement";5import { promiseAllStatementTransformer } from "./promise-all-statement";6import { removeUnnecessaryExpressionsTransformer } from "./remove-unneccesary-expressionts";7import { switchStatementTransformer } from "./switch-statement";8import { throwStatementTransformer } from "./throw-statement";9import { tryStatementTransformer } from "./try-statement";10import { unsupportedStatementTransformer } from "./unsupported";11import { whileStatementTransformer } from "./while-statement";12import { consoleLogStatementTransformer } from "./log-statement";13import { arrayFilterTransformer } from "./array-filter-statement";14import { arrayMapTransformer } from "./array-map-statement";15import { arrayLengthTransformer } from "./array-length-statement";16import { ConverterOptions } from "../../convert";17import { resolveExpressionsTransformer } from "./array-initializer";18import { stringTemplateTransformer } from "./string-template";19import { literalExpressionTransformer } from "./resolve-literal-expressions";20import { deployTimeStatementTransformer } from "./deploy-time-replacements";21import { stringConversionTransformer } from "./string-conversion";22import { ICompilerHost } from "../../compiler-host";23import { evalConstTransformer } from "./eval-const";24import { enumValueTransformer } from "./enum-values";25import { nullCoalescingStatementTransformer } from "./null-coalescing-statement";26import { newDateTransformer } from "./datetime-stmt";27export const createTransformers = (converterOptions: ConverterOptions = {}, host: ICompilerHost) => {28 return [29 evalConstTransformer(host.typeChecker),30 enumValueTransformer(host.typeChecker),31 removeUnnecessaryExpressionsTransformer(converterOptions),32 unsupportedStatementTransformer(converterOptions),33 ifStatementTransformer(converterOptions),34 arrayFilterTransformer(converterOptions),35 arrayMapTransformer(converterOptions),36 arrayLengthTransformer(converterOptions),37 switchStatementTransformer(converterOptions),38 tryStatementTransformer(converterOptions),39 resolveExpressionsTransformer(converterOptions),40 //variableStatementTransformer(converterOptions),41 promiseAllStatementTransformer(converterOptions),42 consoleLogStatementTransformer(converterOptions),43 newDateTransformer,44 callStatementTransformer(converterOptions),45 nullCoalescingStatementTransformer(converterOptions),46 deployTimeStatementTransformer,47 literalExpressionTransformer,48 stringTemplateTransformer,49 stringConversionTransformer,50 throwStatementTransformer(converterOptions),51 whileStatementTransformer(converterOptions),52 doWhileStatementTransformer(converterOptions),53 forOfStatementTransformer(converterOptions),54 ];...

Full Screen

Full Screen

NodeTransformer.ts

Source:NodeTransformer.ts Github

copy

Full Screen

1export enum NodeTransformer {2 BooleanLiteralTransformer = 'BooleanLiteralTransformer',3 BlockStatementControlFlowTransformer = 'BlockStatementControlFlowTransformer',4 BlockStatementSimplifyTransformer = 'BlockStatementSimplifyTransformer',5 ClassFieldTransformer = 'ClassFieldTransformer',6 CommentsTransformer = 'CommentsTransformer',7 CustomCodeHelpersTransformer = 'CustomCodeHelpersTransformer',8 DeadCodeInjectionIdentifiersTransformer = 'DeadCodeInjectionIdentifiersTransformer',9 DeadCodeInjectionTransformer = 'DeadCodeInjectionTransformer',10 DirectivePlacementTransformer = 'DirectivePlacementTransformer',11 EscapeSequenceTransformer = 'EscapeSequenceTransformer',12 EvalCallExpressionTransformer = 'EvalCallExpressionTransformer',13 ExportSpecifierTransformer = 'ExportSpecifierTransformer',14 ExpressionStatementsMergeTransformer = 'ExpressionStatementsMergeTransformer',15 FunctionControlFlowTransformer = 'FunctionControlFlowTransformer',16 IfStatementSimplifyTransformer = 'IfStatementSimplifyTransformer',17 LabeledStatementTransformer = 'LabeledStatementTransformer',18 MemberExpressionTransformer = 'MemberExpressionTransformer',19 MetadataTransformer = 'MetadataTransformer',20 NumberLiteralTransformer = 'NumberLiteralTransformer',21 NumberToNumericalExpressionTransformer = 'NumberToNumericalExpressionTransformer',22 ObfuscatingGuardsTransformer = 'ObfuscatingGuardsTransformer',23 ObjectExpressionKeysTransformer = 'ObjectExpressionKeysTransformer',24 ObjectExpressionTransformer = 'ObjectExpressionTransformer',25 ObjectPatternPropertiesTransformer = 'ObjectPatternPropertiesTransformer',26 ParentificationTransformer = 'ParentificationTransformer',27 RenamePropertiesTransformer = 'RenamePropertiesTransformer',28 ScopeIdentifiersTransformer = 'ScopeIdentifiersTransformer',29 ScopeThroughIdentifiersTransformer = 'ScopeThroughIdentifiersTransformer',30 SplitStringTransformer = 'SplitStringTransformer',31 StringArrayTransformer = 'StringArrayTransformer',32 StringArrayRotateFunctionTransformer = 'StringArrayRotateFunctionTransformer',33 StringArrayScopeCallsWrapperTransformer = 'StringArrayScopeCallsWrapperTransformer',34 TemplateLiteralTransformer = 'TemplateLiteralTransformer',35 VariableDeclarationsMergeTransformer = 'VariableDeclarationsMergeTransformer',36 VariablePreserveTransformer = 'VariablePreserveTransformer'...

Full Screen

Full Screen

__init__.py

Source:__init__.py Github

copy

Full Screen

1# Copyright (c) Facebook Inc. and its affiliates.2#3# This source code is licensed under the MIT license found in the4# LICENSE file in the root directory of this source tree.5"""isort:skip_file"""6from .transformer_config import (7 TransformerConfig,8 DEFAULT_MAX_SOURCE_POSITIONS,9 DEFAULT_MAX_TARGET_POSITIONS,10 DEFAULT_MIN_PARAMS_TO_WRAP,11)12from .transformer_decoder import TransformerDecoder, TransformerDecoderBase, Linear13from .transformer_encoder import TransformerEncoder, TransformerEncoderBase14from .transformer_legacy import (15 TransformerModel,16 base_architecture,17 tiny_architecture,18 transformer_iwslt_de_en,19 transformer_wmt_en_de,20 transformer_vaswani_wmt_en_de_big,21 transformer_vaswani_wmt_en_fr_big,22 transformer_wmt_en_de_big,23 transformer_wmt_en_de_big_t2t,24)25from .transformer_base import TransformerModelBase, Embedding26__all__ = [27 "TransformerModelBase",28 "TransformerConfig",29 "TransformerDecoder",30 "TransformerDecoderBase",31 "TransformerEncoder",32 "TransformerEncoderBase",33 "TransformerModel",34 "Embedding",35 "Linear",36 "base_architecture",37 "tiny_architecture",38 "transformer_iwslt_de_en",39 "transformer_wmt_en_de",40 "transformer_vaswani_wmt_en_de_big",41 "transformer_vaswani_wmt_en_fr_big",42 "transformer_wmt_en_de_big",43 "transformer_wmt_en_de_big_t2t",44 "DEFAULT_MAX_SOURCE_POSITIONS",45 "DEFAULT_MAX_TARGET_POSITIONS",46 "DEFAULT_MIN_PARAMS_TO_WRAP",...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var imposter = {2 {3 {4 "is": {5 "headers": {6 },7 }8 }9 }10};11var http = require('http'),12 options = {13 headers: {14 }15 },16 req = http.request(options, function (res) {17 res.setEncoding('utf8');18 res.on('data', function (chunk) {19 console.log("body: " + chunk);20 });21 });22req.on('error', function (e) {23 console.log('problem with request: ' + e.message);24});25req.write(JSON.stringify(imposter));26req.end();27var imposter = {28 {29 {30 "proxy": {31 }32 }33 }34};35var http = require('http'),36 options = {37 headers: {38 }39 },40 req = http.request(options, function (res) {41 res.setEncoding('utf8');42 res.on('data', function (chunk) {43 console.log("body: " + chunk);44 });45 });46req.on('error', function (e) {47 console.log('problem with request: ' + e.message);48});49req.write(JSON.stringify(imposter));50req.end();51var http = require('http');52var server = http.createServer(function(req, res) {53 res.writeHead(200, { 'Content-Type': 'application/json' });54 res.end(JSON.stringify

Full Screen

Using AI Code Generation

copy

Full Screen

1var assert = require('assert'),2 mb = require('mountebank');3var imposter = {4 {5 {6 is: {7 }8 }9 }10};11mb.create(imposter).then(function (server) {12 return mb.get('/hello', { port: server.port }).then(function (response) {13 assert.equal(response.body, 'Hello world!');14 server.close();15 });16});17var assert = require('assert'),18 mb = require('mountebank');19var imposter = {20 {21 {22 proxy: {23 }24 }25 }26};27mb.create(imposter).then(function (server) {28 return mb.get('/hello', { port: server.port }).then(function (response) {29 assert.equal(response.statusCode, 200);30 server.close();31 });32});33var assert = require('assert'),34 mb = require('mountebank');35var imposter = {36 {37 {38 proxy: {39 }40 }41 }42};43mb.create(imposter).then(function (server) {44 return mb.get('/hello', { port: server.port }).then(function (response) {45 assert.equal(response.statusCode, 200);46 server.close();47 });48});49var assert = require('assert'),50 mb = require('mountebank');51var imposter = {52 {53 {54 proxy: {55 }56 }57 }58};59mb.create(imposter).then(function (server) {60 return mb.get('/hello', { port: server.port

Full Screen

Using AI Code Generation

copy

Full Screen

1var imposter = require('mountebank').create({2}, function () {3 imposter.post('/imposters', {4 "stubs": [{5 "responses": [{6 "is": {7 "headers": {8 },9 "body": {

Full Screen

Using AI Code Generation

copy

Full Screen

1var mb = require('mountebank');2var fs = require('fs');3var path = require('path');4var imposter = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'imposter.json'), 'utf8'));5mb.create({port:2525, pidfile: 'mb.pid', logfile: 'mb.log'}, function (error, mbServer) {6 if (error) {7 console.log('Error starting server: ', error);8 }9 else {10 mbServer.createImposter(imposter, function (error, imposter) {11 if (error) {12 console.log('Error creating imposter: ', error);13 }14 else {15 console.log('Imposter created: ', imposter);16 }17 });18 }19});20{21 {22 {23 "is": {24 "headers": {25 },26 }27 }28 {29 "equals": {30 }31 }32 }33}

Full Screen

Using AI Code Generation

copy

Full Screen

1const mb = require('mountebank');2const fs = require('fs');3const path = require('path');4const port = 2525;5const imposters = JSON.parse(fs.readFileSync(path.join(__dirname, 'imposters.json'), 'utf8'));6mb.start({7}, function () {8 mb.createImposters(imposters, function (err, imposters) {9 if (err) {10 console.error(err);11 return;12 }13 console.log(imposters);14 });15});16{17 {18 {19 {20 "equals": {21 }22 }23 {24 "is": {25 "headers": {26 },27 }28 }29 {30 "transformer": {31 "input": {32 },33 "output": {34 }35 }36 }37 }38 }39}40const mb = require('mountebank');41const fs = require('fs');42const path = require('path');43const port = 2525;44const imposters = JSON.parse(fs.readFileSync(path.join(__dirname, 'imposters.json'), 'utf8'));45mb.start({46}, function () {47 mb.createImposters(imposters, function (err, imposters) {48 if (err) {49 console.error(err);50 return;51 }

Full Screen

Using AI Code Generation

copy

Full Screen

1var request = require('request');2var fs = require('fs');3var transformer = require('mountebank').transformer;4var imposterPort = 2525;5var imposterProtocol = 'http';6var imposterHost = 'localhost';7var imposterName = 'test';8var imposters = require('mountebank').imposters;9var imposter = imposters.create(imposterPort, imposterProtocol);10var stubs = require('mountebank').stubs;11var stub = stubs.create();12var predicates = require('mountebank').predicates;13var predicate = predicates.create();14var response = require('mountebank').response;15var response = response.create();16var fs = require('fs');17var transformer = require('mountebank').transformer;18var imposterPort = 2525;19var imposterProtocol = 'http';20var imposterHost = 'localhost';21var imposterName = 'test';22var imposters = require('mountebank').imposters;23var imposter = imposters.create(imposterPort, imposterProtocol);24var stubs = require('mountebank').stubs;25var stub = stubs.create();26var predicates = require('mountebank').predicates;27var predicate = predicates.create();28var response = require('mountebank').response;29var response = response.create();30var fs = require('fs');31var transformer = require('mountebank').transformer;32var imposterPort = 2525;33var imposterProtocol = 'http';34var imposterHost = 'localhost';35var imposterName = 'test';36var imposters = require('mountebank').imposters;37var imposter = imposters.create(imposterPort, imposterProtocol);38var stubs = require('mountebank').stubs;39var stub = stubs.create();40var predicates = require('mountebank').predicates;41var predicate = predicates.create();42var response = require('mountebank').response;

Full Screen

Using AI Code Generation

copy

Full Screen

1const { createTransformer } = require('mountebank');2const transformer = createTransformer();3const { transform } = transformer;4transform({5 config: {6 },7 {8 {9 {10 equals: {11 }12 }13 {14 is: {15 }16 }17 }18 }19});20const { createTransformer } = require('mountebank');21const transformer = createTransformer();22const { transform } = transformer;23transform({24 config: {25 },26 {27 {28 {29 equals: {30 }31 }32 {33 is: {34 }35 }36 }37 }38});39const { createTransformer } = require('mountebank');40const transformer = createTransformer();41const { transform } = transformer;42transform({43 config: {44 },45 {46 {47 {48 equals: {49 }50 }51 {52 is: {53 }54 }55 }56 }57});58const { createTransformer } = require('mountebank');59const transformer = createTransformer();60const { transform } = transformer;61transform({62 config: {63 },64 {

Full Screen

Using AI Code Generation

copy

Full Screen

1const { createTransformer } = require('mountebank');2const transformer = createTransformer();3module.exports = transformer.createResponseTransformer({4 response: {5 headers: {6 },7 body: {8 {9 },10 {11 }12 }13 }14});15module.exports = transformer.createResponseTransformer({16 response: {17 headers: {18 },19 body: {20 {21 },22 {23 }24 }25 }26});27module.exports = transformer.createResponseTransformer({28 response: {29 headers: {30 },31 body: {32 {33 },34 {35 }36 }37 }38});

Full Screen

Using AI Code Generation

copy

Full Screen

1const transformer = require('mountebank').transformer;2function requestTransform (request) {3 request.path = '/api/v1/transformed';4 request.query = { transformed: 'true' };5 return request;6}7function responseTransform (response) {8 response.statusCode = 200;9 response.body = 'transformed';10 return response;11}12module.exports = transformer({13});14{15 {16 {17 "is": {18 "headers": {19 },20 }21 }22 }23 {24 }25}26const assert = require('assert');27const mb = require('mountebank');28describe('transformer', () => {29 let imposter;30 const port = 4545;31 beforeEach(() => {32 return mb.create().then((mbServer) => {33 imposter = mbServer;34 return imposter.post('/imposters', require('./imposter'));35 });36 });37 afterEach(() => {38 return imposter.del('/imposters');39 });40 it('should transform request and response', () => {41 return imposter.get('/api/v1/transformed?transformed=true').then(response => {42 assert.strictEqual(response.statusCode, 200);43 assert.strictEqual(response.body, 'transformed');44 });45 });46});47{48 {49 {50 "is": {51 "headers": {52 },53 }54 }55 }

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run mountebank automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful