import copy import math import numpy as np import torch import torch.nn as nn import torch.nn.functional as fn from torch.nn.init import normal_ class ItemEmbedding(nn.Module): def __init__(self, args, num_items, embedding_dim): super(ItemEmbedding, self).__init__() self.embedding = nn.Embedding(num_items, embedding_dim) self.initializer_range = args.initializer_range # parameters initialization self.apply(self._init_weights) def forward(self, input): embedded_input = self.embedding(input) return embedded_input def _init_weights(self, module): """ Initialize the weights """ if isinstance(module, (nn.Linear, nn.Embedding)): # Slightly different from the TF version which uses truncated_normal for initialization # cf https://github.com/pytorch/pytorch/pull/5617 module.weight.data.normal_(mean=0.0, std=self.initializer_range) elif isinstance(module, nn.LayerNorm): module.bias.data.zero_() module.weight.data.fill_(1.0) if isinstance(module, nn.Linear) and module.bias is not None: module.bias.data.zero_() class SinusoidalPositionEmbeddings(nn.Module): def __init__(self, embedding_dim): super().__init__() self.embedding_dim = embedding_dim def forward(self, timesteps): assert len(timesteps.shape) == 1 half_dim = self.embedding_dim // 2 emb = math.log(10000) / (half_dim - 1) emb = torch.exp(torch.arange(half_dim, dtype=torch.float32) * -emb) emb = emb.to(device=timesteps.device) emb = timesteps.float()[:, None] * emb[None, :] emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1) if self.embedding_dim % 2 == 1: # zero pad emb = torch.nn.functional.pad(emb, (0, 1, 0, 0)) return emb class DiffNet(nn.Module): def __init__(self, config): super().__init__() self.config = config self.hidden_size = config.hidden_size self.temb_mlp = nn.Sequential( SinusoidalPositionEmbeddings(self.hidden_size), nn.Linear(self.hidden_size, self.hidden_size*2), # nn.Dropout(0.1), nn.GELU(), nn.Linear(self.hidden_size*2, self.hidden_size), # nn.Dropout(0.1) ) # self.diffuser = nn.Sequential( # nn.Linear(self.hidden_size*3, self.hidden_size)) self.diffuser = nn.Sequential( nn.Linear(self.hidden_size * 3, self.hidden_size*2), # nn.Dropout(0.1), nn.GELU(), nn.Linear(self.hidden_size*2, self.hidden_size), # nn.Dropout(0.1) ) def forward(self, x, t, guide): # timestep embedding temb = self.temb_mlp(t.reshape(-1)) temb = temb.reshape(t.shape[0], t.shape[1], -1) #(B,L,h) # get the guide vector input = torch.cat([x, temb, guide], axis=-1) # print("input", input.shape) h = self.diffuser(input) return h class MultiHeadAttention(nn.Module): """ Multi-head Self-attention layers, a attention score dropout layer is introduced. Args: input_tensor (torch.Tensor): the input of the multi-head self-attention layer attention_mask (torch.Tensor): the attention mask for input tensor Returns: hidden_states (torch.Tensor): the output of the multi-head self-attention layer """ def __init__(self, n_heads, hidden_size, hidden_dropout_prob, attn_dropout_prob, layer_norm_eps): super(MultiHeadAttention, self).__init__() if hidden_size % n_heads != 0: raise ValueError( "The hidden size (%d) is not a multiple of the number of attention " "heads (%d)" % (hidden_size, n_heads) ) self.num_attention_heads = n_heads self.attention_head_size = int(hidden_size / n_heads) self.all_head_size = self.num_attention_heads * self.attention_head_size self.query = nn.Linear(hidden_size, self.all_head_size) self.key = nn.Linear(hidden_size, self.all_head_size) self.value = nn.Linear(hidden_size, self.all_head_size) self.attn_dropout = nn.Dropout(attn_dropout_prob) self.dense = nn.Linear(hidden_size, hidden_size) self.LayerNorm = nn.LayerNorm(hidden_size, eps=layer_norm_eps) self.out_dropout = nn.Dropout(hidden_dropout_prob) def transpose_for_scores(self, x): new_x_shape = x.size()[:-1] + (self.num_attention_heads, self.attention_head_size) x = x.view(*new_x_shape) return x.permute(0, 2, 1, 3) def forward(self, input_tensor, attention_mask): mixed_query_layer = self.query(input_tensor) mixed_key_layer = self.key(input_tensor) mixed_value_layer = self.value(input_tensor) query_layer = self.transpose_for_scores(mixed_query_layer) key_layer = self.transpose_for_scores(mixed_key_layer) value_layer = self.transpose_for_scores(mixed_value_layer) # Take the dot product between "query" and "key" to get the raw attention scores. attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2)) attention_scores = attention_scores / math.sqrt(self.attention_head_size) # Apply the attention mask is (precomputed for all layers in BertModel forward() function) # [batch_size heads seq_len seq_len] scores # [batch_size 1 1 seq_len] attention_scores = attention_scores + attention_mask # Normalize the attention scores to probabilities. attention_probs = nn.Softmax(dim=-1)(attention_scores) # This is actually dropping out entire tokens to attend to, which might # seem a bit unusual, but is taken from the original Transformer paper. attention_probs = self.attn_dropout(attention_probs) context_layer = torch.matmul(attention_probs, value_layer) context_layer = context_layer.permute(0, 2, 1, 3).contiguous() new_context_layer_shape = context_layer.size()[:-2] + (self.all_head_size,) context_layer = context_layer.view(*new_context_layer_shape) hidden_states = self.dense(context_layer) hidden_states = self.out_dropout(hidden_states) hidden_states = self.LayerNorm(hidden_states + input_tensor) return hidden_states class FeedForward(nn.Module): """ Point-wise feed-forward layer is implemented by two dense layers. Args: input_tensor (torch.Tensor): the input of the point-wise feed-forward layer Returns: hidden_states (torch.Tensor): the output of the point-wise feed-forward layer """ def __init__(self, hidden_size, inner_size, hidden_dropout_prob, hidden_act, layer_norm_eps): super(FeedForward, self).__init__() self.dense_1 = nn.Linear(hidden_size, inner_size) self.intermediate_act_fn = self.get_hidden_act(hidden_act) self.dense_2 = nn.Linear(inner_size, hidden_size) self.LayerNorm = nn.LayerNorm(hidden_size, eps=layer_norm_eps) self.dropout = nn.Dropout(hidden_dropout_prob) def get_hidden_act(self, act): ACT2FN = { "gelu": self.gelu, "relu": fn.relu, "swish": self.swish, "tanh": torch.tanh, "sigmoid": torch.sigmoid, } return ACT2FN[act] def gelu(self, x): """Implementation of the gelu activation function. For information: OpenAI GPT's gelu is slightly different (and gives slightly different results):: 0.5 * x * (1 + torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3)))) Also see https://arxiv.org/abs/1606.08415 """ return x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0))) def swish(self, x): return x * torch.sigmoid(x) def forward(self, input_tensor): hidden_states = self.dense_1(input_tensor) hidden_states = self.intermediate_act_fn(hidden_states) hidden_states = self.dense_2(hidden_states) hidden_states = self.dropout(hidden_states) hidden_states = self.LayerNorm(hidden_states + input_tensor) return hidden_states class TransformerLayer(nn.Module): """ One transformer layer consists of a multi-head self-attention layer and a point-wise feed-forward layer. Args: hidden_states (torch.Tensor): the input of the multi-head self-attention sublayer attention_mask (torch.Tensor): the attention mask for the multi-head self-attention sublayer Returns: feedforward_output (torch.Tensor): The output of the point-wise feed-forward sublayer, is the output of the transformer layer. """ def __init__( self, n_heads, hidden_size, intermediate_size, hidden_dropout_prob, attn_dropout_prob, hidden_act, layer_norm_eps ): super(TransformerLayer, self).__init__() self.multi_head_attention = MultiHeadAttention( n_heads, hidden_size, hidden_dropout_prob, attn_dropout_prob, layer_norm_eps ) self.feed_forward = FeedForward(hidden_size, intermediate_size, hidden_dropout_prob, hidden_act, layer_norm_eps) def forward(self, hidden_states, attention_mask): attention_output = self.multi_head_attention(hidden_states, attention_mask) feedforward_output = self.feed_forward(attention_output) return feedforward_output class TransformerEncoder(nn.Module): r""" One TransformerEncoder consists of several TransformerLayers. - n_layers(num): num of transformer layers in transformer encoder. Default: 2 - n_heads(num): num of attention heads for multi-head attention layer. Default: 2 - hidden_size(num): the input and output hidden size. Default: 64 - inner_size(num): the dimensionality in feed-forward layer. Default: 256 - hidden_dropout_prob(float): probability of an element to be zeroed. Default: 0.5 - attn_dropout_prob(float): probability of an attention score to be zeroed. Default: 0.5 - hidden_act(str): activation function in feed-forward layer. Default: 'gelu' candidates: 'gelu', 'relu', 'swish', 'tanh', 'sigmoid' - layer_norm_eps(float): a value added to the denominator for numerical stability. Default: 1e-12 """ def __init__( self, n_layers=2, n_heads=2, hidden_size=64, inner_size=256, hidden_dropout_prob=0.5, attn_dropout_prob=0.5, hidden_act='gelu', layer_norm_eps=1e-12 ): super(TransformerEncoder, self).__init__() layer = TransformerLayer( n_heads, hidden_size, inner_size, hidden_dropout_prob, attn_dropout_prob, hidden_act, layer_norm_eps ) self.layer = nn.ModuleList([copy.deepcopy(layer) for _ in range(n_layers)]) def forward(self, hidden_states, attention_mask, output_all_encoded_layers=True): """ Args: hidden_states (torch.Tensor): the input of the TransformerEncoder attention_mask (torch.Tensor): the attention mask for the input hidden_states output_all_encoded_layers (Bool): whether output all transformer layers' output Returns: all_encoder_layers (list): if output_all_encoded_layers is True, return a list consists of all transformer layers' output, otherwise return a list only consists of the output of last transformer layer. """ all_encoder_layers = [] for layer_module in self.layer: hidden_states = layer_module(hidden_states, attention_mask) if output_all_encoded_layers: all_encoder_layers.append(hidden_states) if not output_all_encoded_layers: all_encoder_layers.append(hidden_states) return all_encoder_layers def info_nce(z_i, z_j, temp, batch_size, sim='dot'): """ We do not sample negative examples explicitly. Instead, given a positive pair, similar to (Chen et al., 2017), we treat the other 2(N − 1) augmented examples within a minibatch as negative examples. """ N = 2 * batch_size z = torch.cat((z_i, z_j), dim=0) # print("debug z",z.shape ) if sim == 'cos': sim = nn.functional.cosine_similarity(z.unsqueeze(1), z.unsqueeze(0), dim=2) / temp elif sim == 'dot': sim = torch.mm(z, z.T) / temp sim_i_j = torch.diag(sim, batch_size) sim_j_i = torch.diag(sim, -batch_size) positive_samples = torch.cat((sim_i_j, sim_j_i), dim=0).reshape(N, 1) mask = mask_correlated_samples(batch_size) negative_samples = sim[mask].reshape(N, -1) labels = torch.zeros(N).to(positive_samples.device).long() logits = torch.cat((positive_samples, negative_samples), dim=1) return logits, labels def mask_correlated_samples(batch_size): N = 2 * batch_size mask = torch.ones((N, N), dtype=bool) mask = mask.fill_diagonal_(0) for i in range(batch_size): mask[i, batch_size + i] = 0 mask[batch_size + i, i] = 0 return mask