python_code
stringlengths
0
108k
# helpers def exists(val): return val is not None def batched_index_select(values, indices): last_dim = values.shape[-1] return values.gather(1, indices[:, :, None].expand(-1, -1, last_dim)) # helper classes class Residual(nn.Module): def __init__(self, fn): super().__init__() self.fn = fn def forward(self, x, **kwargs): return self.fn(x, **kwargs) + x class PreNorm(nn.Module): def __init__(self, dim, fn): super().__init__() self.fn = fn self.norm = nn.LayerNorm(dim) def forward(self, x, **kwargs): return self.fn(self.norm(x), **kwargs) class FeedForward(nn.Module): def __init__(self, dim, mult = 4, dropout = 0.): super().__init__() self.net = nn.Sequential( nn.Linear(dim, dim * mult), nn.GELU(), nn.Dropout(dropout), nn.Linear(dim * mult, dim) ) def forward(self, x, **kwargs): return self.net(x) # adjacent attention class class AdjacentAttention(nn.Module): def __init__( self, *, dim, dim_head = 64, heads = 4, dropout = 0. ): super().__init__() inner_dim = dim_head * heads self.scale = dim_head ** -0.5 self.heads = heads self.to_qkv = nn.Linear(dim, inner_dim * 3, bias = False) self.to_out = nn.Linear(inner_dim, dim) self.null_k = nn.Parameter(torch.randn(heads, dim_head)) self.null_v = nn.Parameter(torch.randn(heads, dim_head)) self.dropout = nn.Dropout(dropout) def forward( self, x, adj_kv_indices, mask ): b, n, d, h = *x.shape, self.heads flat_indices = repeat(adj_kv_indices, 'b n a -> (b h) (n a)', h = h) # derive query, key, value q, k, v = self.to_qkv(x).chunk(3, dim = -1) q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = h), (q, k, v)) # gather keys and values according to adjacency matrix k, v = map(lambda t: rearrange(t, 'b h n d -> (b h) n d'), (k, v)) k = batched_index_select(k, flat_indices) v = batched_index_select(v, flat_indices) k, v = map(lambda t: rearrange(t, '(b h) (n a) d -> b h n a d', h = h, n = n), (k, v)) # add null key / value, so a node can attend to nothing # have come across this in GNN literature as some other name nk, nv = map(lambda t: rearrange(t, 'h d -> () h () () d').expand(b, -1, n, 1, -1), (self.null_k, self.null_v)) k = torch.cat((nk, k), dim = -2) v = torch.cat((nv, v), dim = -2) mask = F.pad(mask, (1, 0), value = 1) # similarity of each node to its neighbors sim = einsum('b h n d, b h n a d -> b h n a', q, k) * self.scale # mask out neighbors that are just padding mask_value = -torch.finfo(sim.dtype).max mask = rearrange(mask.bool(), 'b n a -> b () n a') sim.masked_fill_(~mask.bool(), mask_value) # attention attn = sim.softmax(dim = -1) # dropout attn = self.dropout(attn) # get weighted average of the values of all neighbors out = einsum('b h n a, b h n a d -> b h n d', attn, v) out = rearrange(out, 'b h n d -> b n (h d)') # combine output return self.to_out(out) # adjacent network (layers of adjacent attention) class AdjacentAttentionNetwork(nn.Module): def __init__( self, *, dim, depth, dim_head = 64, heads = 4, num_neighbors_cutoff = None, num_global_nodes = 0, attn_dropout = 0., ff_dropout = 0. ): super().__init__() self.num_neighbors_cutoff = num_neighbors_cutoff self.layers = nn.ModuleList([]) for _ in range(depth): global_attn = PreNorm(dim, ISAB( dim = dim, heads = heads, num_induced_points = num_global_nodes )) if num_global_nodes > 0 else None self.layers.append(nn.ModuleList([ Residual(PreNorm(dim, AdjacentAttention( dim = dim, dim_head = dim_head, heads = heads, dropout = attn_dropout ))), global_attn, Residual(PreNorm(dim, FeedForward( dim = dim, dropout = ff_dropout ))) ])) def forward(self, x, adjacency_mat, mask = None): device, n = x.device, x.shape[1] diag = torch.eye(adjacency_mat.shape[-1], device = device).bool() adjacency_mat |= diag # nodes should pay attention itself (self-interacting) # zero out points on adjacency matrix # where the nodes are just padding if exists(mask): adjacency_mat &= (mask[:, :, None] * mask[:, None, :]) adj_mat = adjacency_mat.float() # if we don't set a hard limit to the number of neighbors: # - get the maximum number of neighbors and pad the rest of the nodes with less than that number of neighbors # else: # - randomly sample the cutoff number of neighbors for any node that exceeds the max # - this would be similar to random sparse attention (bigbird) # get the maximum number of neighbors max_neighbors = int(adj_mat.sum(dim = -1).max()) if exists(self.num_neighbors_cutoff) and max_neighbors > self.num_neighbors_cutoff: # to randomly sample the neighbors, add a small uniform noise to the mask and topk noise = torch.empty((n, n), device = device).uniform_(-0.01, 0.01) adj_mat = adj_mat + noise adj_mask, adj_kv_indices = adj_mat.topk(dim = -1, k = self.num_neighbors_cutoff) # cast the mask back to 0s and 1s adj_mask = (adj_mask > 0.5).float() else: # todo - get distribution of number of neighbors, and strategically break up attention (message passing) to multiple steps # - start with a bimodal num neighbors test case, then generalize # use topk to get all the neighbors # also pass the mask into the attention, as some neighbors will be just padding and not actually neighbors adj_mask, adj_kv_indices = adj_mat.topk(dim = -1, k = max_neighbors) for attn, global_attn, ff in self.layers: x = attn( x, adj_kv_indices = adj_kv_indices, mask = adj_mask ) if exists(global_attn): out, _ = global_attn(x, mask = mask) x = x + out x = ff(x) return x
logging.set_verbosity_error() def exists(val): return val is not None def map_values(fn, dictionary): return {k: fn(v) for k, v in dictionary.items()} CONTEXT_EMBED_USE_CPU = os.getenv('CONTEXT_EMBED_USE_CPU', None) is not None if CONTEXT_EMBED_USE_CPU: print('calculating context embed only on cpu') MODELS = dict( pubmed = dict( dim = 768, path = 'microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract', ) ) GLOBAL_VARIABLES = dict(model = None, tokenizer = None) def get_contextual_dim(model_name): assert model_name in MODELS return MODELS[model_name]['dim'] @run_once('init_transformer') def init_transformer(model_name): path = MODELS[model_name]['path'] GLOBAL_VARIABLES['tokenizer'] = AutoTokenizer.from_pretrained(path) model = AutoModelForMaskedLM.from_pretrained(path) if not CONTEXT_EMBED_USE_CPU: model = model.cuda() GLOBAL_VARIABLES['model'] = model @torch.no_grad() def tokenize_text( text, max_length = 256, model_name = 'pubmed', hidden_state_index = -1, return_cls_token = True ): init_transformer(model_name) model = GLOBAL_VARIABLES['model'] tokenizer = GLOBAL_VARIABLES['tokenizer'] encoding = tokenizer.batch_encode_plus( [text], add_special_tokens = True, padding = True, truncation = True, max_length = max_length, return_attention_mask = True, return_tensors = 'pt' ) if not CONTEXT_EMBED_USE_CPU: encoding = map_values(lambda t: t.cuda(), encoding) model.eval() with torch.no_grad(): outputs = model(**encoding, output_hidden_states = True) hidden_state = outputs.hidden_states[hidden_state_index][0] if return_cls_token: return hidden_state[0] return hidden_state.mean(dim = 0) def get_text_repr( texts, *, device, max_length = 256, model_name = 'pubmed', hidden_state_index = -1, return_cls_token = True, ): assert model_name in MODELS, f'{model_name} not found in available text transformers to use' if isinstance(texts, str): texts = [texts] get_context_repr_fn = cache_fn(tokenize_text, path = f'contexts/{model_name}') representations = [get_context_repr_fn(text, max_length = max_length, model_name = model_name, hidden_state_index = hidden_state_index, return_cls_token = return_cls_token) for text in texts] return torch.stack(representations).to(device)
# helpers functions def exists(x): return x is not None def default(val, d): if exists(val): return val return d() if callable(d) else d def cycle(dl): while True: for data in dl: yield data def has_int_squareroot(num): return (math.sqrt(num) ** 2) == num def num_to_groups(num, divisor): groups = num // divisor remainder = num % divisor arr = [divisor] * groups if remainder > 0: arr.append(remainder) return arr def convert_image_to(img_type, image): if image.mode != img_type: return image.convert(img_type) return image # small helper modules class Residual(nn.Module): def __init__(self, fn): super().__init__() self.fn = fn def forward(self, x, *args, **kwargs): return self.fn(x, *args, **kwargs) + x def Upsample(dim, dim_out = None): return nn.Sequential( nn.Upsample(scale_factor = 2, mode = 'nearest'), nn.Conv2d(dim, default(dim_out, dim), 3, padding = 1) ) def Downsample(dim, dim_out = None): return nn.Conv2d(dim, default(dim_out, dim), 4, 2, 1) class LayerNorm(nn.Module): def __init__(self, dim): super().__init__() self.g = nn.Parameter(torch.ones(1, dim, 1, 1)) def forward(self, x): eps = 1e-5 if x.dtype == torch.float32 else 1e-3 var = torch.var(x, dim = 1, unbiased = False, keepdim = True) mean = torch.mean(x, dim = 1, keepdim = True) return (x - mean) * (var + eps).rsqrt() * self.g class PreNorm(nn.Module): def __init__(self, dim, fn): super().__init__() self.fn = fn self.norm = LayerNorm(dim) def forward(self, x): x = self.norm(x) return self.fn(x) # positional embeds class LearnedSinusoidalPosEmb(nn.Module): def __init__(self, dim): super().__init__() assert (dim % 2) == 0 half_dim = dim // 2 self.weights = nn.Parameter(torch.randn(half_dim)) def forward(self, x): x = rearrange(x, 'b -> b 1') freqs = x * rearrange(self.weights, 'd -> 1 d') * 2 * math.pi fouriered = torch.cat((freqs.sin(), freqs.cos()), dim = -1) fouriered = torch.cat((x, fouriered), dim = -1) return fouriered # building block modules class Block(nn.Module): def __init__(self, dim, dim_out, groups = 8): super().__init__() self.proj = nn.Conv2d(dim, dim_out, 3, padding = 1) self.norm = nn.GroupNorm(groups, dim_out) self.act = nn.SiLU() def forward(self, x, scale_shift = None): x = self.proj(x) x = self.norm(x) if exists(scale_shift): scale, shift = scale_shift x = x * (scale + 1) + shift x = self.act(x) return x class ResnetBlock(nn.Module): def __init__(self, dim, dim_out, *, time_emb_dim = None, groups = 8): super().__init__() self.mlp = nn.Sequential( nn.SiLU(), nn.Linear(time_emb_dim, dim_out * 2) ) if exists(time_emb_dim) else None self.block1 = Block(dim, dim_out, groups = groups) self.block2 = Block(dim_out, dim_out, groups = groups) self.res_conv = nn.Conv2d(dim, dim_out, 1) if dim != dim_out else nn.Identity() def forward(self, x, time_emb = None): scale_shift = None if exists(self.mlp) and exists(time_emb): time_emb = self.mlp(time_emb) time_emb = rearrange(time_emb, 'b c -> b c 1 1') scale_shift = time_emb.chunk(2, dim = 1) h = self.block1(x, scale_shift = scale_shift) h = self.block2(h) return h + self.res_conv(x) class LinearAttention(nn.Module): def __init__(self, dim, heads = 4, dim_head = 32): super().__init__() self.scale = dim_head ** -0.5 self.heads = heads hidden_dim = dim_head * heads self.to_qkv = nn.Conv2d(dim, hidden_dim * 3, 1, bias = False) self.to_out = nn.Sequential( nn.Conv2d(hidden_dim, dim, 1), LayerNorm(dim) ) def forward(self, x): b, c, h, w = x.shape qkv = self.to_qkv(x).chunk(3, dim = 1) q, k, v = map(lambda t: rearrange(t, 'b (h c) x y -> b h c (x y)', h = self.heads), qkv) q = q.softmax(dim = -2) k = k.softmax(dim = -1) q = q * self.scale v = v / (h * w) context = torch.einsum('b h d n, b h e n -> b h d e', k, v) out = torch.einsum('b h d e, b h d n -> b h e n', context, q) out = rearrange(out, 'b h c (x y) -> b (h c) x y', h = self.heads, x = h, y = w) return self.to_out(out) class Attention(nn.Module): def __init__(self, dim, heads = 4, dim_head = 32): super().__init__() self.scale = dim_head ** -0.5 self.heads = heads hidden_dim = dim_head * heads self.to_qkv = nn.Conv2d(dim, hidden_dim * 3, 1, bias = False) self.to_out = nn.Conv2d(hidden_dim, dim, 1) def forward(self, x): b, c, h, w = x.shape qkv = self.to_qkv(x).chunk(3, dim = 1) q, k, v = map(lambda t: rearrange(t, 'b (h c) x y -> b h c (x y)', h = self.heads), qkv) q = q * self.scale sim = einsum('b h d i, b h d j -> b h i j', q, k) attn = sim.softmax(dim = -1) out = einsum('b h i j, b h d j -> b h i d', attn, v) out = rearrange(out, 'b h (x y) d -> b (h d) x y', x = h, y = w) return self.to_out(out) # model class Unet(nn.Module): def __init__( self, dim, init_dim = None, dim_mults=(1, 2, 4, 8), channels = 3, resnet_block_groups = 8, learned_sinusoidal_dim = 16 ): super().__init__() # determine dimensions self.channels = channels input_channels = channels * 2 init_dim = default(init_dim, dim) self.init_conv = nn.Conv2d(input_channels, init_dim, 7, padding = 3) dims = [init_dim, *map(lambda m: dim * m, dim_mults)] in_out = list(zip(dims[:-1], dims[1:])) block_klass = partial(ResnetBlock, groups = resnet_block_groups) # time embeddings time_dim = dim * 4 sinu_pos_emb = LearnedSinusoidalPosEmb(learned_sinusoidal_dim) fourier_dim = learned_sinusoidal_dim + 1 self.time_mlp = nn.Sequential( sinu_pos_emb, nn.Linear(fourier_dim, time_dim), nn.GELU(), nn.Linear(time_dim, time_dim) ) # layers self.downs = nn.ModuleList([]) self.ups = nn.ModuleList([]) num_resolutions = len(in_out) for ind, (dim_in, dim_out) in enumerate(in_out): is_last = ind >= (num_resolutions - 1) self.downs.append(nn.ModuleList([ block_klass(dim_in, dim_in, time_emb_dim = time_dim), block_klass(dim_in, dim_in, time_emb_dim = time_dim), Residual(PreNorm(dim_in, LinearAttention(dim_in))), Downsample(dim_in, dim_out) if not is_last else nn.Conv2d(dim_in, dim_out, 3, padding = 1) ])) mid_dim = dims[-1] self.mid_block1 = block_klass(mid_dim, mid_dim, time_emb_dim = time_dim) self.mid_attn = Residual(PreNorm(mid_dim, Attention(mid_dim))) self.mid_block2 = block_klass(mid_dim, mid_dim, time_emb_dim = time_dim) for ind, (dim_in, dim_out) in enumerate(reversed(in_out)): is_last = ind == (len(in_out) - 1) self.ups.append(nn.ModuleList([ block_klass(dim_out + dim_in, dim_out, time_emb_dim = time_dim), block_klass(dim_out + dim_in, dim_out, time_emb_dim = time_dim), Residual(PreNorm(dim_out, LinearAttention(dim_out))), Upsample(dim_out, dim_in) if not is_last else nn.Conv2d(dim_out, dim_in, 3, padding = 1) ])) self.final_res_block = block_klass(dim * 2, dim, time_emb_dim = time_dim) self.final_conv = nn.Conv2d(dim, channels, 1) def forward(self, x, time, x_self_cond = None): x_self_cond = default(x_self_cond, lambda: torch.zeros_like(x)) x = torch.cat((x_self_cond, x), dim = 1) x = self.init_conv(x) r = x.clone() t = self.time_mlp(time) h = [] for block1, block2, attn, downsample in self.downs: x = block1(x, t) h.append(x) x = block2(x, t) x = attn(x) h.append(x) x = downsample(x) x = self.mid_block1(x, t) x = self.mid_attn(x) x = self.mid_block2(x, t) for block1, block2, attn, upsample in self.ups: x = torch.cat((x, h.pop()), dim = 1) x = block1(x, t) x = torch.cat((x, h.pop()), dim = 1) x = block2(x, t) x = attn(x) x = upsample(x) x = torch.cat((x, r), dim = 1) x = self.final_res_block(x, t) return self.final_conv(x) # chroma class def log(t, eps = 1e-20): return torch.log(t.clamp(min = eps)) def right_pad_dims_to(x, t): padding_dims = x.ndim - t.ndim if padding_dims <= 0: return t return t.view(*t.shape, *((1,) * padding_dims)) def beta_linear_log_snr(t): return -torch.log(expm1(1e-4 + 10 * (t ** 2))) def alpha_cosine_log_snr(t, s: float = 0.008): return -log((torch.cos((t + s) / (1 + s) * math.pi * 0.5) ** -2) - 1, eps = 1e-5) # not sure if this accounts for beta being clipped to 0.999 in discrete version def log_snr_to_alpha_sigma(log_snr): return torch.sqrt(torch.sigmoid(log_snr)), torch.sqrt(torch.sigmoid(-log_snr)) class Chroma(nn.Module): def __init__( self, model, *, image_size, timesteps = 1000, use_ddim = False, noise_schedule = 'cosine', time_difference = 0. ): super().__init__() self.model = model self.channels = self.model.channels self.image_size = image_size if noise_schedule == "linear": self.log_snr = beta_linear_log_snr elif noise_schedule == "cosine": self.log_snr = alpha_cosine_log_snr else: raise ValueError(f'invalid noise schedule {noise_schedule}') self.timesteps = timesteps self.use_ddim = use_ddim # proposed in the paper, summed to time_next # as a way to fix a deficiency in self-conditioning and lower FID when the number of sampling timesteps is < 400 self.time_difference = time_difference @property def device(self): return next(self.model.parameters()).device def get_sampling_timesteps(self, batch, *, device): times = torch.linspace(1., 0., self.timesteps + 1, device = device) times = repeat(times, 't -> b t', b = batch) times = torch.stack((times[:, :-1], times[:, 1:]), dim = 0) times = times.unbind(dim = -1) return times @torch.no_grad() def ddpm_sample(self, shape, time_difference = None): batch, device = shape[0], self.device time_difference = default(time_difference, self.time_difference) time_pairs = self.get_sampling_timesteps(batch, device = device) img = torch.randn(shape, device=device) x_start = None for time, time_next in tqdm(time_pairs, desc = 'sampling loop time step', total = self.timesteps): # add the time delay time_next = (time_next - self.time_difference).clamp(min = 0.) noise_cond = self.log_snr(time) # get predicted x0 x_start = self.model(img, noise_cond, x_start) # clip x0 x_start.clamp_(-1., 1.) # get log(snr) log_snr = self.log_snr(time) log_snr_next = self.log_snr(time_next) log_snr, log_snr_next = map(partial(right_pad_dims_to, img), (log_snr, log_snr_next)) # get alpha sigma of time and next time alpha, sigma = log_snr_to_alpha_sigma(log_snr) alpha_next, sigma_next = log_snr_to_alpha_sigma(log_snr_next) # derive posterior mean and variance c = -expm1(log_snr - log_snr_next) mean = alpha_next * (img * (1 - c) / alpha + c * x_start) variance = (sigma_next ** 2) * c log_variance = log(variance) # get noise noise = torch.where( rearrange(time_next > 0, 'b -> b 1 1 1'), torch.randn_like(img), torch.zeros_like(img) ) img = mean + (0.5 * log_variance).exp() * noise return img @torch.no_grad() def ddim_sample(self, shape, time_difference = None): batch, device = shape[0], self.device time_difference = default(time_difference, self.time_difference) time_pairs = self.get_sampling_timesteps(batch, device = device) img = torch.randn(shape, device = device) x_start = None for times, times_next in tqdm(time_pairs, desc = 'sampling loop time step'): # get times and noise levels log_snr = self.log_snr(times) log_snr_next = self.log_snr(times_next) padded_log_snr, padded_log_snr_next = map(partial(right_pad_dims_to, img), (log_snr, log_snr_next)) alpha, sigma = log_snr_to_alpha_sigma(padded_log_snr) alpha_next, sigma_next = log_snr_to_alpha_sigma(padded_log_snr_next) # add the time delay times_next = (times_next - time_difference).clamp(min = 0.) # predict x0 x_start = self.model(img, log_snr, x_start) # clip x0 x_start.clamp_(-1., 1.) # get predicted noise pred_noise = (img - alpha * x_start) / sigma.clamp(min = 1e-8) # calculate x next img = x_start * alpha_next + pred_noise * sigma_next return img @torch.no_grad() def sample(self, batch_size = 16): image_size, channels = self.image_size, self.channels sample_fn = self.ddpm_sample if not self.use_ddim else self.ddim_sample return sample_fn((batch_size, channels, image_size, image_size)) def forward(self, img, *args, **kwargs): batch, c, h, w, device, img_size, = *img.shape, img.device, self.image_size assert h == img_size and w == img_size, f'height and width of image must be {img_size}' # sample random times times = torch.zeros((batch,), device = device).float().uniform_(0, 1.) # noise sample noise = torch.randn_like(img) noise_level = self.log_snr(times) padded_noise_level = right_pad_dims_to(img, noise_level) alpha, sigma = log_snr_to_alpha_sigma(padded_noise_level) noised_img = alpha * img + sigma * noise # if doing self-conditioning, 50% of the time, predict x_start from current set of times # and condition with unet with that # this technique will slow down training by 25%, but seems to lower FID significantly self_cond = None if random() < 0.5: with torch.no_grad(): self_cond = self.model(noised_img, noise_level).detach_() # predict and take gradient step pred = self.model(noised_img, noise_level, self_cond) return F.mse_loss(pred, img) # trainer class class Trainer(object): def __init__( self, diffusion_model, folder, *, train_batch_size = 16, gradient_accumulate_every = 1, augment_horizontal_flip = True, train_lr = 1e-4, train_num_steps = 100000, ema_update_every = 10, ema_decay = 0.995, adam_betas = (0.9, 0.99), save_and_sample_every = 1000, num_samples = 25, results_folder = './results', amp = False, fp16 = False, split_batches = True, convert_image_to = None ): super().__init__() self.accelerator = Accelerator( split_batches = split_batches, mixed_precision = 'fp16' if fp16 else 'no' ) self.accelerator.native_amp = amp self.model = diffusion_model assert has_int_squareroot(num_samples), 'number of samples must have an integer square root' self.num_samples = num_samples self.save_and_sample_every = save_and_sample_every self.batch_size = train_batch_size self.gradient_accumulate_every = gradient_accumulate_every self.train_num_steps = train_num_steps self.image_size = diffusion_model.image_size # dataset and dataloader self.ds = Dataset(folder, self.image_size, augment_horizontal_flip = augment_horizontal_flip, convert_image_to = convert_image_to) dl = DataLoader(self.ds, batch_size = train_batch_size, shuffle = True, pin_memory = True, num_workers = cpu_count()) dl = self.accelerator.prepare(dl) self.dl = cycle(dl) # optimizer self.opt = Adam(diffusion_model.parameters(), lr = train_lr, betas = adam_betas) # for logging results in a folder periodically if self.accelerator.is_main_process: self.ema = EMA(diffusion_model, beta = ema_decay, update_every = ema_update_every) self.results_folder = Path(results_folder) self.results_folder.mkdir(exist_ok = True) # step counter state self.step = 0 # prepare model, dataloader, optimizer with accelerator self.model, self.opt = self.accelerator.prepare(self.model, self.opt) def save(self, milestone): if not self.accelerator.is_local_main_process: return data = { 'step': self.step, 'model': self.accelerator.get_state_dict(self.model), 'opt': self.opt.state_dict(), 'ema': self.ema.state_dict(), 'scaler': self.accelerator.scaler.state_dict() if exists(self.accelerator.scaler) else None } torch.save(data, str(self.results_folder / f'model-{milestone}.pt')) def load(self, milestone): data = torch.load(str(self.results_folder / f'model-{milestone}.pt')) model = self.accelerator.unwrap_model(self.model) model.load_state_dict(data['model']) self.step = data['step'] self.opt.load_state_dict(data['opt']) self.ema.load_state_dict(data['ema']) if exists(self.accelerator.scaler) and exists(data['scaler']): self.accelerator.scaler.load_state_dict(data['scaler']) def train(self): accelerator = self.accelerator device = accelerator.device with tqdm(initial = self.step, total = self.train_num_steps, disable = not accelerator.is_main_process) as pbar: while self.step < self.train_num_steps: total_loss = 0. for _ in range(self.gradient_accumulate_every): data = next(self.dl).to(device) with self.accelerator.autocast(): loss = self.model(data) loss = loss / self.gradient_accumulate_every total_loss += loss.item() self.accelerator.backward(loss) pbar.set_description(f'loss: {total_loss:.4f}') accelerator.wait_for_everyone() self.opt.step() self.opt.zero_grad() accelerator.wait_for_everyone() if accelerator.is_main_process: self.ema.to(device) self.ema.update() if self.step != 0 and self.step % self.save_and_sample_every == 0: self.ema.ema_model.eval() with torch.no_grad(): milestone = self.step // self.save_and_sample_every batches = num_to_groups(self.num_samples, self.batch_size) all_images_list = list(map(lambda n: self.ema.ema_model.sample(batch_size=n), batches)) all_images = torch.cat(all_images_list, dim = 0) utils.save_image(all_images, str(self.results_folder / f'sample-{milestone}.png'), nrow = int(math.sqrt(self.num_samples))) self.save(milestone) self.step += 1 pbar.update(1) accelerator.print('training complete')
terminate = False def signal_handling(signum,frame): global terminate terminate = True num_attempts = 4 for attempt in range(num_attempts): dream = Imagine( text = "an armchair in the form of pikachu\\an armchair imitating pikachu\\abstract", text_min = "blur\\zoom", lr = 7e-2, image_size = 512, gradient_accumulate_every = 1, save_every = 50, epochs = 5, iterations = 50, save_progress = False, bilinear = False, open_folder = False, seed = None, torch_deterministic = False, max_classes = 20, class_temperature = 2., save_date_time = False, save_best = True, experimental_resample = True, ema_decay = 0.99 ) dream() shutil.copy(dream.textpath + ".best.png", f"{attempt}.png") try: time.sleep(2) del dream time.sleep(2) torch.cuda.empty_cache() except Exception: torch.cuda.empty_cache()
__version__ = '0.9.1'
"""Good differentiable image resampling for PyTorch.""" def sinc(x): return torch.where(x != 0, torch.sin(math.pi * x) / (math.pi * x), x.new_ones([])) def lanczos(x, a): cond = torch.logical_and(-a < x, x < a) out = torch.where(cond, sinc(x) * sinc(x/a), x.new_zeros([])) return out / out.sum() def ramp(ratio, width): n = math.ceil(width / ratio + 1) out = torch.empty([n]) cur = 0 for i in range(out.shape[0]): out[i] = cur cur += ratio return torch.cat([-out[1:].flip([0]), out])[1:-1] def odd(fn): return update_wrapper(lambda x: torch.sign(x) * fn(abs(x)), fn) def _to_linear_srgb(input): cond = input <= 0.04045 a = input / 12.92 b = ((input + 0.055) / 1.055)**2.4 return torch.where(cond, a, b) def _to_nonlinear_srgb(input): cond = input <= 0.0031308 a = 12.92 * input b = 1.055 * input**(1/2.4) - 0.055 return torch.where(cond, a, b) to_linear_srgb = odd(_to_linear_srgb) to_nonlinear_srgb = odd(_to_nonlinear_srgb) def resample(input, size, align_corners=True, is_srgb=False): n, c, h, w = input.shape dh, dw = size if is_srgb: input = to_linear_srgb(input) input = input.view([n * c, 1, h, w]) if dh < h: kernel_h = lanczos(ramp(dh / h, 3), 3).to(input.device, input.dtype) pad_h = (kernel_h.shape[0] - 1) // 2 input = F.pad(input, (0, 0, pad_h, pad_h), 'reflect') input = F.conv2d(input, kernel_h[None, None, :, None]) if dw < w: kernel_w = lanczos(ramp(dw / w, 3), 3).to(input.device, input.dtype) pad_w = (kernel_w.shape[0] - 1) // 2 input = F.pad(input, (pad_w, pad_w, 0, 0), 'reflect') input = F.conv2d(input, kernel_w[None, None, None, :]) input = input.view([n, c, h, w]) input = F.interpolate(input, size, mode='bicubic', align_corners=align_corners) if is_srgb: input = to_nonlinear_srgb(input) return input
# Exponential Moving Average (from https://gist.github.com/crowsonkb/76b94d5238272722290734bf4725d204) """Exponential moving average for PyTorch. Adapted from https://www.zijianhu.com/post/pytorch/ema/ by crowsonkb """ class EMA(nn.Module): def __init__(self, model, decay): super().__init__() self.model = model self.decay = decay self.register_buffer('accum', torch.tensor(1.)) self._biased = deepcopy(self.model) self.average = deepcopy(self.model) for param in self._biased.parameters(): param.detach_().zero_() for param in self.average.parameters(): param.detach_().zero_() self.update() @torch.no_grad() def update(self): assert self.training, 'Update should only be called during training' self.accum *= self.decay model_params = dict(self.model.named_parameters()) biased_params = dict(self._biased.named_parameters()) average_params = dict(self.average.named_parameters()) assert model_params.keys() == biased_params.keys() == average_params.keys(), f'Model parameter keys incompatible with EMA stored parameter keys' for name, param in model_params.items(): biased_params[name].mul_(self.decay) biased_params[name].add_((1 - self.decay) * param) average_params[name].copy_(biased_params[name]) average_params[name].div_(1 - self.accum) model_buffers = dict(self.model.named_buffers()) biased_buffers = dict(self._biased.named_buffers()) average_buffers = dict(self.average.named_buffers()) assert model_buffers.keys() == biased_buffers.keys() == average_buffers.keys() for name, buffer in model_buffers.items(): biased_buffers[name].copy_(buffer) average_buffers[name].copy_(buffer) def forward(self, *args, **kwargs): if self.training: return self.model(*args, **kwargs) return self.average(*args, **kwargs)
# this code is a copy from huggingface # with some minor modifications try: from urllib.parse import urlparse except ImportError: from urlparse import urlparse try: from pathlib import Path PYTORCH_PRETRAINED_BIGGAN_CACHE = Path(os.getenv('PYTORCH_PRETRAINED_BIGGAN_CACHE', Path.home() / '.pytorch_pretrained_biggan')) except (AttributeError, ImportError): PYTORCH_PRETRAINED_BIGGAN_CACHE = os.getenv('PYTORCH_PRETRAINED_BIGGAN_CACHE', os.path.join(os.path.expanduser("~"), '.pytorch_pretrained_biggan')) logger = logging.getLogger(__name__) # pylint: disable=invalid-name PRETRAINED_MODEL_ARCHIVE_MAP = { 'biggan-deep-128': "https://s3.amazonaws.com/models.huggingface.co/biggan/biggan-deep-128-pytorch_model.bin", 'biggan-deep-256': "https://s3.amazonaws.com/models.huggingface.co/biggan/biggan-deep-256-pytorch_model.bin", 'biggan-deep-512': "https://s3.amazonaws.com/models.huggingface.co/biggan/biggan-deep-512-pytorch_model.bin", } PRETRAINED_CONFIG_ARCHIVE_MAP = { 'biggan-deep-128': "https://s3.amazonaws.com/models.huggingface.co/biggan/biggan-deep-128-config.json", 'biggan-deep-256': "https://s3.amazonaws.com/models.huggingface.co/biggan/biggan-deep-256-config.json", 'biggan-deep-512': "https://s3.amazonaws.com/models.huggingface.co/biggan/biggan-deep-512-config.json", } WEIGHTS_NAME = 'pytorch_model.bin' CONFIG_NAME = 'config.json' 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_PRETRAINED_BIGGAN_CACHE if sys.version_info[0] == 3 and 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(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_PRETRAINED_BIGGAN_CACHE if sys.version_info[0] == 3 and isinstance(url_or_filename, Path): url_or_filename = str(url_or_filename) if sys.version_info[0] == 3 and 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: # 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): 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.""" 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.""" 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 http_get(url, temp_file): req = requests.get(url, stream=True) 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_PRETRAINED_BIGGAN_CACHE if sys.version_info[0] == 3 and 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: response = requests.head(url, allow_redirects=True) if response.status_code != 200: raise IOError("HEAD request failed for url {} with status code {}" .format(url, response.status_code)) etag = response.headers.get("ETag") filename = url_to_filename(url, etag) # get cache path to put the file cache_path = os.path.join(cache_dir, filename) 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', encoding="utf-8") as meta_file: json.dump(meta, meta_file) 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 class BigGANConfig(object): """ Configuration class to store the configuration of a `BigGAN`. Defaults are for the 128x128 model. layers tuple are (up-sample in the layer ?, input channels, output channels) """ def __init__(self, output_dim=128, z_dim=128, class_embed_dim=128, channel_width=128, num_classes=1000, layers=[(False, 16, 16), (True, 16, 16), (False, 16, 16), (True, 16, 8), (False, 8, 8), (True, 8, 4), (False, 4, 4), (True, 4, 2), (False, 2, 2), (True, 2, 1)], attention_layer_position=8, eps=1e-4, n_stats=51): """Constructs BigGANConfig. """ self.output_dim = output_dim self.z_dim = z_dim self.class_embed_dim = class_embed_dim self.channel_width = channel_width self.num_classes = num_classes self.layers = layers self.attention_layer_position = attention_layer_position self.eps = eps self.n_stats = n_stats @classmethod def from_dict(cls, json_object): """Constructs a `BigGANConfig` from a Python dictionary of parameters.""" config = BigGANConfig() for key, value in json_object.items(): config.__dict__[key] = value return config @classmethod def from_json_file(cls, json_file): """Constructs a `BigGANConfig` from a json file of parameters.""" with open(json_file, "r", encoding='utf-8') as reader: text = reader.read() return cls.from_dict(json.loads(text)) def __repr__(self): return str(self.to_json_string()) def to_dict(self): """Serializes this instance to a Python dictionary.""" output = copy.deepcopy(self.__dict__) return output def to_json_string(self): """Serializes this instance to a JSON string.""" return json.dumps(self.to_dict(), indent=2, sort_keys=True) + "\n" def snconv2d(eps=1e-12, **kwargs): return nn.utils.spectral_norm(nn.Conv2d(**kwargs), eps=eps) def snlinear(eps=1e-12, **kwargs): return nn.utils.spectral_norm(nn.Linear(**kwargs), eps=eps) def sn_embedding(eps=1e-12, **kwargs): return nn.utils.spectral_norm(nn.Embedding(**kwargs), eps=eps) class SelfAttn(nn.Module): """ Self attention Layer""" def __init__(self, in_channels, eps=1e-12): super(SelfAttn, self).__init__() self.in_channels = in_channels self.snconv1x1_theta = snconv2d(in_channels=in_channels, out_channels=in_channels//8, kernel_size=1, bias=False, eps=eps) self.snconv1x1_phi = snconv2d(in_channels=in_channels, out_channels=in_channels//8, kernel_size=1, bias=False, eps=eps) self.snconv1x1_g = snconv2d(in_channels=in_channels, out_channels=in_channels//2, kernel_size=1, bias=False, eps=eps) self.snconv1x1_o_conv = snconv2d(in_channels=in_channels//2, out_channels=in_channels, kernel_size=1, bias=False, eps=eps) self.maxpool = nn.MaxPool2d(2, stride=2, padding=0) self.softmax = nn.Softmax(dim=-1) self.gamma = nn.Parameter(torch.zeros(1)) def forward(self, x): _, ch, h, w = x.size() # Theta path theta = self.snconv1x1_theta(x) theta = theta.view(-1, ch//8, h*w) # Phi path phi = self.snconv1x1_phi(x) phi = self.maxpool(phi) phi = phi.view(-1, ch//8, h*w//4) # Attn map attn = torch.bmm(theta.permute(0, 2, 1), phi) attn = self.softmax(attn) # g path g = self.snconv1x1_g(x) g = self.maxpool(g) g = g.view(-1, ch//2, h*w//4) # Attn_g - o_conv attn_g = torch.bmm(g, attn.permute(0, 2, 1)) attn_g = attn_g.view(-1, ch//2, h, w) attn_g = self.snconv1x1_o_conv(attn_g) # Out out = x + self.gamma*attn_g return out class BigGANBatchNorm(nn.Module): """ This is a batch norm module that can handle conditional input and can be provided with pre-computed activation means and variances for various truncation parameters. We cannot just rely on torch.batch_norm since it cannot handle batched weights (pytorch 1.0.1). We computate batch_norm our-self without updating running means and variances. If you want to train this model you should add running means and variance computation logic. """ def __init__(self, num_features, condition_vector_dim=None, n_stats=51, eps=1e-4, conditional=True): super(BigGANBatchNorm, self).__init__() self.num_features = num_features self.eps = eps self.conditional = conditional # We use pre-computed statistics for n_stats values of truncation between 0 and 1 self.register_buffer('running_means', torch.zeros(n_stats, num_features)) self.register_buffer('running_vars', torch.ones(n_stats, num_features)) self.step_size = 1.0 / (n_stats - 1) if conditional: assert condition_vector_dim is not None self.scale = snlinear(in_features=condition_vector_dim, out_features=num_features, bias=False, eps=eps) self.offset = snlinear(in_features=condition_vector_dim, out_features=num_features, bias=False, eps=eps) else: self.weight = torch.nn.Parameter(torch.Tensor(num_features)) self.bias = torch.nn.Parameter(torch.Tensor(num_features)) def forward(self, x, truncation, condition_vector=None): # Retreive pre-computed statistics associated to this truncation coef, start_idx = math.modf(truncation / self.step_size) start_idx = int(start_idx) if coef != 0.0: # Interpolate running_mean = self.running_means[start_idx] * coef + self.running_means[start_idx + 1] * (1 - coef) running_var = self.running_vars[start_idx] * coef + self.running_vars[start_idx + 1] * (1 - coef) else: running_mean = self.running_means[start_idx] running_var = self.running_vars[start_idx] if self.conditional: running_mean = running_mean.unsqueeze(0).unsqueeze(-1).unsqueeze(-1) running_var = running_var.unsqueeze(0).unsqueeze(-1).unsqueeze(-1) weight = 1 + self.scale(condition_vector).unsqueeze(-1).unsqueeze(-1) bias = self.offset(condition_vector).unsqueeze(-1).unsqueeze(-1) out = (x - running_mean) / torch.sqrt(running_var + self.eps) * weight + bias else: out = F.batch_norm(x, running_mean, running_var, self.weight, self.bias, training=False, momentum=0.0, eps=self.eps) return out class GenBlock(nn.Module): def __init__(self, in_size, out_size, condition_vector_dim, reduction_factor=4, up_sample=False, n_stats=51, eps=1e-12): super(GenBlock, self).__init__() self.up_sample = up_sample self.drop_channels = (in_size != out_size) middle_size = in_size // reduction_factor self.bn_0 = BigGANBatchNorm(in_size, condition_vector_dim, n_stats=n_stats, eps=eps, conditional=True) self.conv_0 = snconv2d(in_channels=in_size, out_channels=middle_size, kernel_size=1, eps=eps) self.bn_1 = BigGANBatchNorm(middle_size, condition_vector_dim, n_stats=n_stats, eps=eps, conditional=True) self.conv_1 = snconv2d(in_channels=middle_size, out_channels=middle_size, kernel_size=3, padding=1, eps=eps) self.bn_2 = BigGANBatchNorm(middle_size, condition_vector_dim, n_stats=n_stats, eps=eps, conditional=True) self.conv_2 = snconv2d(in_channels=middle_size, out_channels=middle_size, kernel_size=3, padding=1, eps=eps) self.bn_3 = BigGANBatchNorm(middle_size, condition_vector_dim, n_stats=n_stats, eps=eps, conditional=True) self.conv_3 = snconv2d(in_channels=middle_size, out_channels=out_size, kernel_size=1, eps=eps) self.relu = nn.ReLU() def forward(self, x, cond_vector, truncation): x0 = x x = self.bn_0(x, truncation, cond_vector) x = self.relu(x) x = self.conv_0(x) x = self.bn_1(x, truncation, cond_vector) x = self.relu(x) if self.up_sample: x = F.interpolate(x, scale_factor=2, mode='nearest') x = self.conv_1(x) x = self.bn_2(x, truncation, cond_vector) x = self.relu(x) x = self.conv_2(x) x = self.bn_3(x, truncation, cond_vector) x = self.relu(x) x = self.conv_3(x) if self.drop_channels: new_channels = x0.shape[1] // 2 x0 = x0[:, :new_channels, ...] if self.up_sample: x0 = F.interpolate(x0, scale_factor=2, mode='nearest') out = x + x0 return out class Generator(nn.Module): def __init__(self, config): super(Generator, self).__init__() self.config = config ch = config.channel_width condition_vector_dim = config.z_dim * 2 self.gen_z = snlinear(in_features=condition_vector_dim, out_features=4 * 4 * 16 * ch, eps=config.eps) layers = [] for i, layer in enumerate(config.layers): if i == config.attention_layer_position: layers.append(SelfAttn(ch*layer[1], eps=config.eps)) layers.append(GenBlock(ch*layer[1], ch*layer[2], condition_vector_dim, up_sample=layer[0], n_stats=config.n_stats, eps=config.eps)) self.layers = nn.ModuleList(layers) self.bn = BigGANBatchNorm(ch, n_stats=config.n_stats, eps=config.eps, conditional=False) self.relu = nn.ReLU() self.conv_to_rgb = snconv2d(in_channels=ch, out_channels=ch, kernel_size=3, padding=1, eps=config.eps) self.tanh = nn.Tanh() def forward(self, cond_vector, truncation): z = self.gen_z(cond_vector[0].unsqueeze(0)) # We use this conversion step to be able to use TF weights: # TF convention on shape is [batch, height, width, channels] # PT convention on shape is [batch, channels, height, width] z = z.view(-1, 4, 4, 16 * self.config.channel_width) z = z.permute(0, 3, 1, 2).contiguous() next_available_latent_index = 1 for layer in self.layers: if isinstance(layer, GenBlock): z = layer(z, cond_vector[next_available_latent_index].unsqueeze(0), truncation) next_available_latent_index += 1 else: z = layer(z) z = self.bn(z, truncation) z = self.relu(z) z = self.conv_to_rgb(z) z = z[:, :3, ...] z = self.tanh(z) return z class BigGAN(nn.Module): """BigGAN Generator.""" @classmethod def from_pretrained(cls, pretrained_model_name_or_path, cache_dir=None, *inputs, **kwargs): if pretrained_model_name_or_path in PRETRAINED_MODEL_ARCHIVE_MAP: model_file = PRETRAINED_MODEL_ARCHIVE_MAP[pretrained_model_name_or_path] config_file = PRETRAINED_CONFIG_ARCHIVE_MAP[pretrained_model_name_or_path] else: model_file = os.path.join(pretrained_model_name_or_path, WEIGHTS_NAME) config_file = os.path.join(pretrained_model_name_or_path, CONFIG_NAME) try: resolved_model_file = cached_path(model_file, cache_dir=cache_dir) resolved_config_file = cached_path(config_file, cache_dir=cache_dir) except EnvironmentError: logger.error("Wrong model name, should be a valid path to a folder containing " "a {} file and a {} file or a model name in {}".format( WEIGHTS_NAME, CONFIG_NAME, PRETRAINED_MODEL_ARCHIVE_MAP.keys())) raise logger.info("loading model {} from cache at {}".format(pretrained_model_name_or_path, resolved_model_file)) # Load config config = BigGANConfig.from_json_file(resolved_config_file) logger.info("Model config {}".format(config)) # Instantiate model. model = cls(config, *inputs, **kwargs) state_dict = torch.load(resolved_model_file, map_location='cpu' if not torch.cuda.is_available() else None) model.load_state_dict(state_dict, strict=False) return model def __init__(self, config): super(BigGAN, self).__init__() self.config = config self.embeddings = nn.Linear(config.num_classes, config.z_dim, bias=False) self.generator = Generator(config) def forward(self, z, class_label, truncation): assert 0 < truncation <= 1 embed = self.embeddings(class_label) cond_vector = torch.cat((z, embed), dim=1) z = self.generator(cond_vector, truncation) return z
def train( text=None, img=None, text_min="", lr = .07, image_size = 512, gradient_accumulate_every = 1, epochs = 20, iterations = 1050, save_every = 50, overwrite = False, save_progress = False, save_date_time = False, bilinear = False, open_folder = True, seed = 0, append_seed = False, random = False, torch_deterministic = False, max_classes = None, class_temperature = 2., save_best = False, experimental_resample = False, ema_decay = 0.5, num_cutouts = 128, center_bias = False, larger_model = False ): print(f'Starting up... v{__version__}') if random: seed = rnd.randint(0, 1e6) imagine = Imagine( text=text, img=img, text_min=text_min, lr = lr, image_size = image_size, gradient_accumulate_every = gradient_accumulate_every, epochs = epochs, iterations = iterations, save_every = save_every, save_progress = save_progress, bilinear = bilinear, seed = seed, append_seed = append_seed, torch_deterministic = torch_deterministic, open_folder = open_folder, max_classes = max_classes, class_temperature = class_temperature, save_date_time = save_date_time, save_best = save_best, experimental_resample = experimental_resample, ema_decay = ema_decay, num_cutouts = num_cutouts, center_bias = center_bias, larger_clip = larger_model ) if not overwrite and imagine.filename.exists(): answer = input('Imagined image already exists, do you want to overwrite? (y/n) ').lower() if answer not in ('yes', 'y'): exit() imagine() def main(): fire.Fire(train)
assert torch.cuda.is_available(), 'CUDA must be available in order to use Big Sleep' # graceful keyboard interrupt terminate = False def signal_handling(signum,frame): print('detecting keyboard interrupt, gracefully exiting') global terminate terminate = True signal.signal(signal.SIGINT,signal_handling) # helpers def exists(val): return val is not None def open_folder(path): if os.path.isfile(path): path = os.path.dirname(path) if not os.path.isdir(path): return cmd_list = None if sys.platform == 'darwin': cmd_list = ['open', '--', path] elif sys.platform == 'linux2' or sys.platform == 'linux': cmd_list = ['xdg-open', path] elif sys.platform in ['win32', 'win64']: cmd_list = ['explorer', path.replace('/','\\')] if cmd_list == None: return try: subprocess.check_call(cmd_list) except subprocess.CalledProcessError: pass except OSError: pass def create_text_path(text=None, img=None, encoding=None): input_name = "" if text is not None: input_name += text if img is not None: if isinstance(img, str): img_name = "".join(img.split(".")[:-1]) # replace spaces by underscores, remove img extension img_name = img_name.split("/")[-1] # only take img name, not path else: img_name = "PIL_img" input_name += "_" + img_name if encoding is not None: input_name = "your_encoding" return input_name.replace("-", "_").replace(",", "").replace(" ", "_").replace("|", "--").strip('-_')[:255] # tensor helpers def differentiable_topk(x, k, temperature=1.): n, dim = x.shape topk_tensors = [] for i in range(k): is_last = i == (k - 1) values, indices = (x / temperature).softmax(dim=-1).topk(1, dim=-1) topks = torch.zeros_like(x).scatter_(-1, indices, values) topk_tensors.append(topks) if not is_last: x = x.scatter(-1, indices, float('-inf')) topks = torch.cat(topk_tensors, dim=-1) return topks.reshape(n, k, dim).sum(dim = 1) def create_clip_img_transform(image_width): clip_mean = [0.48145466, 0.4578275, 0.40821073] clip_std = [0.26862954, 0.26130258, 0.27577711] transform = T.Compose([ #T.ToPILImage(), T.Resize(image_width), T.CenterCrop((image_width, image_width)), T.ToTensor(), T.Normalize(mean=clip_mean, std=clip_std) ]) return transform def rand_cutout(image, size, center_bias=False, center_focus=2): width = image.shape[-1] min_offset = 0 max_offset = width - size if center_bias: # sample around image center center = max_offset / 2 std = center / center_focus offset_x = int(random.gauss(mu=center, sigma=std)) offset_y = int(random.gauss(mu=center, sigma=std)) # resample uniformly if over boundaries offset_x = random.randint(min_offset, max_offset) if (offset_x > max_offset or offset_x < min_offset) else offset_x offset_y = random.randint(min_offset, max_offset) if (offset_y > max_offset or offset_y < min_offset) else offset_y else: offset_x = random.randint(min_offset, max_offset) offset_y = random.randint(min_offset, max_offset) cutout = image[:, :, offset_x:offset_x + size, offset_y:offset_y + size] return cutout # load biggan class Latents(torch.nn.Module): def __init__( self, num_latents = 15, num_classes = 1000, z_dim = 128, max_classes = None, class_temperature = 2. ): super().__init__() self.normu = torch.nn.Parameter(torch.zeros(num_latents, z_dim).normal_(std = 1)) self.cls = torch.nn.Parameter(torch.zeros(num_latents, num_classes).normal_(mean = -3.9, std = .3)) self.register_buffer('thresh_lat', torch.tensor(1)) assert not exists(max_classes) or max_classes > 0 and max_classes <= num_classes, f'max_classes must be between 0 and {num_classes}' self.max_classes = max_classes self.class_temperature = class_temperature def forward(self): if exists(self.max_classes): classes = differentiable_topk(self.cls, self.max_classes, temperature = self.class_temperature) else: classes = torch.sigmoid(self.cls) return self.normu, classes class Model(nn.Module): def __init__( self, image_size, max_classes = None, class_temperature = 2., ema_decay = 0.99 ): super().__init__() assert image_size in (128, 256, 512), 'image size must be one of 128, 256, or 512' self.biggan = BigGAN.from_pretrained(f'biggan-deep-{image_size}') self.max_classes = max_classes self.class_temperature = class_temperature self.ema_decay\ = ema_decay self.init_latents() def init_latents(self): latents = Latents( num_latents = len(self.biggan.config.layers) + 1, num_classes = self.biggan.config.num_classes, z_dim = self.biggan.config.z_dim, max_classes = self.max_classes, class_temperature = self.class_temperature ) self.latents = EMA(latents, self.ema_decay) def forward(self): self.biggan.eval() out = self.biggan(*self.latents(), 1) return (out + 1) / 2 class BigSleep(nn.Module): def __init__( self, num_cutouts = 128, loss_coef = 100, image_size = 512, bilinear = False, max_classes = None, class_temperature = 2., experimental_resample = False, ema_decay = 0.99, center_bias = False, larger_clip = False ): super().__init__() self.loss_coef = loss_coef self.image_size = image_size self.num_cutouts = num_cutouts self.experimental_resample = experimental_resample self.center_bias = center_bias self.interpolation_settings = {'mode': 'bilinear', 'align_corners': False} if bilinear else {'mode': 'nearest'} model_name = 'ViT-B/32' if not larger_clip else 'ViT-L/14' self.perceptor, self.normalize_image = load(model_name, jit = False) self.model = Model( image_size = image_size, max_classes = max_classes, class_temperature = class_temperature, ema_decay = ema_decay ) def reset(self): self.model.init_latents() def sim_txt_to_img(self, text_embed, img_embed, text_type="max"): sign = -1 if text_type == "min": sign = 1 return sign * self.loss_coef * torch.cosine_similarity(text_embed, img_embed, dim = -1).mean() def forward(self, text_embeds, text_min_embeds=[], return_loss = True): width, num_cutouts = self.image_size, self.num_cutouts out = self.model() if not return_loss: return out pieces = [] for ch in range(num_cutouts): # sample cutout size size = int(width * torch.zeros(1,).normal_(mean=.8, std=.3).clip(.5, .95)) # get cutout apper = rand_cutout(out, size, center_bias=self.center_bias) if (self.experimental_resample): apper = resample(apper, (224, 224)) else: apper = F.interpolate(apper, (224, 224), **self.interpolation_settings) pieces.append(apper) into = torch.cat(pieces) into = self.normalize_image(into) image_embed = self.perceptor.encode_image(into) latents, soft_one_hot_classes = self.model.latents() num_latents = latents.shape[0] latent_thres = self.model.latents.model.thresh_lat lat_loss = torch.abs(1 - torch.std(latents, dim=1)).mean() + \ torch.abs(torch.mean(latents, dim = 1)).mean() + \ 4 * torch.max(torch.square(latents).mean(), latent_thres) for array in latents: mean = torch.mean(array) diffs = array - mean var = torch.mean(torch.pow(diffs, 2.0)) std = torch.pow(var, 0.5) zscores = diffs / std skews = torch.mean(torch.pow(zscores, 3.0)) kurtoses = torch.mean(torch.pow(zscores, 4.0)) - 3.0 lat_loss = lat_loss + torch.abs(kurtoses) / num_latents + torch.abs(skews) / num_latents cls_loss = ((50 * torch.topk(soft_one_hot_classes, largest = False, dim = 1, k = 999)[0]) ** 2).mean() results = [] for txt_embed in text_embeds: results.append(self.sim_txt_to_img(txt_embed, image_embed)) for txt_min_embed in text_min_embeds: results.append(self.sim_txt_to_img(txt_min_embed, image_embed, "min")) sim_loss = sum(results).mean() return out, (lat_loss, cls_loss, sim_loss) class Imagine(nn.Module): def __init__( self, *, text=None, img=None, encoding=None, text_min = "", lr = .07, image_size = 512, gradient_accumulate_every = 1, save_every = 50, epochs = 20, iterations = 1050, save_progress = False, bilinear = False, open_folder = True, seed = None, append_seed = False, torch_deterministic = False, max_classes = None, class_temperature = 2., save_date_time = False, save_best = False, experimental_resample = False, ema_decay = 0.99, num_cutouts = 128, center_bias = False, larger_clip = False ): super().__init__() if torch_deterministic: assert not bilinear, 'the deterministic (seeded) operation does not work with interpolation (PyTorch 1.7.1)' torch.set_deterministic(True) self.seed = seed self.append_seed = append_seed if exists(seed): print(f'setting seed of {seed}') if seed == 0: print('you can override this with --seed argument in the command line, or --random for a randomly chosen one') torch.manual_seed(seed) self.epochs = epochs self.iterations = iterations model = BigSleep( image_size = image_size, bilinear = bilinear, max_classes = max_classes, class_temperature = class_temperature, experimental_resample = experimental_resample, ema_decay = ema_decay, num_cutouts = num_cutouts, center_bias = center_bias, larger_clip = larger_clip ).cuda() self.model = model self.lr = lr self.optimizer = Adam(model.model.latents.model.parameters(), lr) self.gradient_accumulate_every = gradient_accumulate_every self.save_every = save_every self.save_progress = save_progress self.save_date_time = save_date_time self.save_best = save_best self.current_best_score = 0 self.open_folder = open_folder self.total_image_updates = (self.epochs * self.iterations) / self.save_every self.encoded_texts = { "max": [], "min": [] } # create img transform self.clip_transform = create_clip_img_transform(224) # create starting encoding self.set_clip_encoding(text=text, img=img, encoding=encoding, text_min=text_min) @property def seed_suffix(self): return f'.{self.seed}' if self.append_seed and exists(self.seed) else '' def set_text(self, text): self.set_clip_encoding(text = text) def create_clip_encoding(self, text=None, img=None, encoding=None): self.text = text self.img = img if encoding is not None: encoding = encoding.cuda() #elif self.create_story: # encoding = self.update_story_encoding(epoch=0, iteration=1) elif text is not None and img is not None: encoding = (self.create_text_encoding(text) + self.create_img_encoding(img)) / 2 elif text is not None: encoding = self.create_text_encoding(text) elif img is not None: encoding = self.create_img_encoding(img) return encoding def create_text_encoding(self, text): tokenized_text = tokenize(text).cuda() with torch.no_grad(): text_encoding = self.model.perceptor.encode_text(tokenized_text).detach() return text_encoding def create_img_encoding(self, img): if isinstance(img, str): img = Image.open(img) normed_img = self.clip_transform(img).unsqueeze(0).cuda() with torch.no_grad(): img_encoding = self.model.perceptor.encode_image(normed_img).detach() return img_encoding def encode_multiple_phrases(self, text, img=None, encoding=None, text_type="max"): if text is not None and "|" in text: self.encoded_texts[text_type] = [self.create_clip_encoding(text=prompt_min, img=img, encoding=encoding) for prompt_min in text.split("|")] else: self.encoded_texts[text_type] = [self.create_clip_encoding(text=text, img=img, encoding=encoding)] def encode_max_and_min(self, text, img=None, encoding=None, text_min=""): self.encode_multiple_phrases(text, img=img, encoding=encoding) if text_min is not None and text_min != "": self.encode_multiple_phrases(text_min, img=img, encoding=encoding, text_type="min") def set_clip_encoding(self, text=None, img=None, encoding=None, text_min=""): self.current_best_score = 0 self.text = text self.text_min = text_min if len(text_min) > 0: text = text + "_wout_" + text_min[:255] if text is not None else "wout_" + text_min[:255] text_path = create_text_path(text=text, img=img, encoding=encoding) if self.save_date_time: text_path = datetime.now().strftime("%y%m%d-%H%M%S-") + text_path self.text_path = text_path self.filename = Path(f'./{text_path}{self.seed_suffix}.png') self.encode_max_and_min(text, img=img, encoding=encoding, text_min=text_min) # Tokenize and encode each prompt def reset(self): self.model.reset() self.model = self.model.cuda() self.optimizer = Adam(self.model.model.latents.parameters(), self.lr) def train_step(self, epoch, i, pbar=None): total_loss = 0 for _ in range(self.gradient_accumulate_every): out, losses = self.model(self.encoded_texts["max"], self.encoded_texts["min"]) loss = sum(losses) / self.gradient_accumulate_every total_loss += loss loss.backward() self.optimizer.step() self.model.model.latents.update() self.optimizer.zero_grad() if (i + 1) % self.save_every == 0: with torch.no_grad(): self.model.model.latents.eval() out, losses = self.model(self.encoded_texts["max"], self.encoded_texts["min"]) top_score, best = torch.topk(losses[2], k=1, largest=False) image = self.model.model()[best].cpu() self.model.model.latents.train() save_image(image, str(self.filename)) if pbar is not None: pbar.update(1) else: print(f'image updated at "./{str(self.filename)}"') if self.save_progress: total_iterations = epoch * self.iterations + i num = total_iterations // self.save_every save_image(image, Path(f'./{self.text_path}.{num}{self.seed_suffix}.png')) if self.save_best and top_score.item() < self.current_best_score: self.current_best_score = top_score.item() save_image(image, Path(f'./{self.text_path}{self.seed_suffix}.best.png')) return out, total_loss def forward(self): penalizing = "" if len(self.text_min) > 0: penalizing = f'penalizing "{self.text_min}"' print(f'Imagining "{self.text_path}" {penalizing}...') with torch.no_grad(): self.model(self.encoded_texts["max"][0]) # one warmup step due to issue with CLIP and CUDA if self.open_folder: open_folder('./') self.open_folder = False image_pbar = tqdm(total=self.total_image_updates, desc='image update', position=2, leave=True) epoch_pbar = trange(self.epochs, desc = ' epochs', position=0, leave=True) for epoch in (ep for ep in epoch_pbar if not terminate): pbar = trange(self.iterations, desc=' iteration', position=1, leave=True) image_pbar.update(0) for i in (it for it in pbar if not terminate): out, loss = self.train_step(epoch, i, image_pbar) pbar.set_description(f'loss: {loss.item():04.2f}')
_MODELS = { "RN50": "https://openaipublic.azureedge.net/clip/models/afeb0e10f9e5a86da6080e35cf09123aca3b358a0c3e3b6c78a7b63bc04b6762/RN50.pt", "RN101": "https://openaipublic.azureedge.net/clip/models/8fa8567bab74a42d41c5915025a8e4538c3bdbe8804a470a72f30b0d94fab599/RN101.pt", "RN50x4": "https://openaipublic.azureedge.net/clip/models/7e526bd135e493cef0776de27d5f42653e6b4c8bf9e0f653bb11773263205fdd/RN50x4.pt", "ViT-B/32": "https://openaipublic.azureedge.net/clip/models/40d365715913c9da98579312b702a82c18be219cc2a73407c4526f58eba950af/ViT-B-32.pt", "ViT-L/14": "https://openaipublic.azureedge.net/clip/models/b8cca3fd41ae0c99ba7e8951adf17d267cdb84cd88be6f7c2e0eca1737a03836/ViT-L-14.pt" } def _download(url: str, root: str = os.path.expanduser("~/.cache/clip")): os.makedirs(root, exist_ok=True) filename = os.path.basename(url) expected_sha256 = url.split("/")[-2] download_target = os.path.join(root, filename) if os.path.exists(download_target) and not os.path.isfile(download_target): raise RuntimeError(f"{download_target} exists and is not a regular file") if os.path.isfile(download_target): if hashlib.sha256(open(download_target, "rb").read()).hexdigest() == expected_sha256: return download_target else: warnings.warn(f"{download_target} exists, but the SHA256 checksum does not match; re-downloading the file") with urllib.request.urlopen(url) as source, open(download_target, "wb") as output: with tqdm(total=int(source.info().get("Content-Length")), ncols=80, unit='iB', unit_scale=True) as loop: while True: buffer = source.read(8192) if not buffer: break output.write(buffer) loop.update(len(buffer)) if hashlib.sha256(open(download_target, "rb").read()).hexdigest() != expected_sha256: raise RuntimeError(f"Model has been downloaded but the SHA256 checksum does not not match") return download_target def _transform(): return Compose([ Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)), ]) def available_models() -> List[str]: """Returns the names of available CLIP models""" return list(_MODELS.keys()) def load(name: str, device: Union[str, torch.device] = "cuda" if torch.cuda.is_available() else "cpu", jit=True): """Load a CLIP model Parameters ---------- name : str A model name listed by `clip.available_models()`, or the path to a model checkpoint containing the state_dict device : Union[str, torch.device] The device to put the loaded model jit : bool Whether to load the optimized JIT model (default) or more hackable non-JIT model. Returns ------- model : torch.nn.Module The CLIP model preprocess : Callable[[PIL.Image], torch.Tensor] A torchvision transform that converts a PIL image into a tensor that the returned model can take as its input """ if name in _MODELS: model_path = _download(_MODELS[name]) elif os.path.isfile(name): model_path = name else: raise RuntimeError(f"Model {name} not found; available models = {available_models()}") try: # loading JIT archive model = torch.jit.load(model_path, map_location=device if jit else "cpu").eval() state_dict = None except RuntimeError: # loading saved state dict if jit: warnings.warn(f"File {model_path} is not a JIT archive. Loading as a state dict instead") jit = False state_dict = torch.load(model_path, map_location="cpu") if not jit: model = build_model(state_dict or model.state_dict()).to(device) if str(device) == "cpu": model.float() return model, _transform() # patch the device names device_holder = torch.jit.trace(lambda: torch.ones([]).to(torch.device(device)), example_inputs=[]) device_node = [n for n in device_holder.graph.findAllNodes("prim::Constant") if "Device" in repr(n)][-1] def patch_device(module): graphs = [module.graph] if hasattr(module, "graph") else [] if hasattr(module, "forward1"): graphs.append(module.forward1.graph) for graph in graphs: for node in graph.findAllNodes("prim::Constant"): if "value" in node.attributeNames() and str(node["value"]).startswith("cuda"): node.copyAttributes(device_node) model.apply(patch_device) patch_device(model.encode_image) patch_device(model.encode_text) # patch dtype to float32 on CPU if str(device) == "cpu": float_holder = torch.jit.trace(lambda: torch.ones([]).float(), example_inputs=[]) float_input = list(float_holder.graph.findNode("aten::to").inputs())[1] float_node = float_input.node() def patch_float(module): graphs = [module.graph] if hasattr(module, "graph") else [] if hasattr(module, "forward1"): graphs.append(module.forward1.graph) for graph in graphs: for node in graph.findAllNodes("aten::to"): inputs = list(node.inputs()) for i in [1, 2]: # dtype can be the second or third argument to aten::to() if inputs[i].node()["value"] == 5: inputs[i].node().copyAttributes(float_node) model.apply(patch_float) patch_float(model.encode_image) patch_float(model.encode_text) model.float() return model, _transform() def tokenize(texts: Union[str, List[str]], context_length: int = 77) -> torch.LongTensor: """ Returns the tokenized representation of given input string(s) Parameters ---------- texts : Union[str, List[str]] An input string or a list of input strings to tokenize context_length : int The context length to use; all CLIP models use 77 as the context length Returns ------- A two-dimensional tensor containing the resulting tokens, shape = [number of input strings, context_length] """ if isinstance(texts, str): texts = [texts] sot_token = _tokenizer.encoder["<|startoftext|>"] eot_token = _tokenizer.encoder["<|endoftext|>"] all_tokens = [[sot_token] + _tokenizer.encode(text) + [eot_token] for text in texts] result = torch.zeros(len(all_tokens), context_length, dtype=torch.long) for i, tokens in enumerate(all_tokens): if len(tokens) > context_length: raise RuntimeError(f"Input {texts[i]} is too long for context length {context_length}") result[i, :len(tokens)] = torch.tensor(tokens) return result class Bottleneck(nn.Module): expansion = 4 def __init__(self, inplanes, planes, stride=1): super().__init__() # all conv layers have stride 1. an avgpool is performed after the second convolution when stride > 1 self.conv1 = nn.Conv2d(inplanes, planes, 1, bias=False) self.bn1 = nn.BatchNorm2d(planes) self.conv2 = nn.Conv2d(planes, planes, 3, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(planes) self.avgpool = nn.AvgPool2d(stride) if stride > 1 else nn.Identity() self.conv3 = nn.Conv2d(planes, planes * self.expansion, 1, bias=False) self.bn3 = nn.BatchNorm2d(planes * self.expansion) self.relu = nn.ReLU(inplace=True) self.downsample = None self.stride = stride if stride > 1 or inplanes != planes * Bottleneck.expansion: # downsampling layer is prepended with an avgpool, and the subsequent convolution has stride 1 self.downsample = nn.Sequential(OrderedDict([ ("-1", nn.AvgPool2d(stride)), ("0", nn.Conv2d(inplanes, planes * self.expansion, 1, stride=1, bias=False)), ("1", nn.BatchNorm2d(planes * self.expansion)) ])) def forward(self, x: torch.Tensor): identity = x out = self.relu(self.bn1(self.conv1(x))) out = self.relu(self.bn2(self.conv2(out))) out = self.avgpool(out) out = self.bn3(self.conv3(out)) if self.downsample is not None: identity = self.downsample(x) out += identity out = self.relu(out) return out class AttentionPool2d(nn.Module): def __init__(self, spacial_dim: int, embed_dim: int, num_heads: int, output_dim: int = None): super().__init__() self.positional_embedding = nn.Parameter(torch.randn(spacial_dim ** 2 + 1, embed_dim) / embed_dim ** 0.5) self.k_proj = nn.Linear(embed_dim, embed_dim) self.q_proj = nn.Linear(embed_dim, embed_dim) self.v_proj = nn.Linear(embed_dim, embed_dim) self.c_proj = nn.Linear(embed_dim, output_dim or embed_dim) self.num_heads = num_heads def forward(self, x): x = x.reshape(x.shape[0], x.shape[1], x.shape[2] * x.shape[3]).permute(2, 0, 1) # NCHW -> (HW)NC x = torch.cat([x.mean(dim=0, keepdim=True), x], dim=0) # (HW+1)NC x = x + self.positional_embedding[:, None, :].to(x.dtype) # (HW+1)NC x, _ = F.multi_head_attention_forward( query=x, key=x, value=x, embed_dim_to_check=x.shape[-1], num_heads=self.num_heads, q_proj_weight=self.q_proj.weight, k_proj_weight=self.k_proj.weight, v_proj_weight=self.v_proj.weight, in_proj_weight=None, in_proj_bias=torch.cat([self.q_proj.bias, self.k_proj.bias, self.v_proj.bias]), bias_k=None, bias_v=None, add_zero_attn=False, dropout_p=0, out_proj_weight=self.c_proj.weight, out_proj_bias=self.c_proj.bias, use_separate_proj_weight=True, training=self.training, need_weights=False ) return x[0] class ModifiedResNet(nn.Module): """ A ResNet class that is similar to torchvision's but contains the following changes: - There are now 3 "stem" convolutions as opposed to 1, with an average pool instead of a max pool. - Performs anti-aliasing strided convolutions, where an avgpool is prepended to convolutions with stride > 1 - The final pooling layer is a QKV attention instead of an average pool """ def __init__(self, layers, output_dim, heads, input_resolution=224, width=64): super().__init__() self.output_dim = output_dim self.input_resolution = input_resolution # the 3-layer stem self.conv1 = nn.Conv2d(3, width // 2, kernel_size=3, stride=2, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(width // 2) self.conv2 = nn.Conv2d(width // 2, width // 2, kernel_size=3, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(width // 2) self.conv3 = nn.Conv2d(width // 2, width, kernel_size=3, padding=1, bias=False) self.bn3 = nn.BatchNorm2d(width) self.avgpool = nn.AvgPool2d(2) self.relu = nn.ReLU(inplace=True) # residual layers self._inplanes = width # this is a *mutable* variable used during construction self.layer1 = self._make_layer(width, layers[0]) self.layer2 = self._make_layer(width * 2, layers[1], stride=2) self.layer3 = self._make_layer(width * 4, layers[2], stride=2) self.layer4 = self._make_layer(width * 8, layers[3], stride=2) embed_dim = width * 32 # the ResNet feature dimension self.attnpool = AttentionPool2d(input_resolution // 32, embed_dim, heads, output_dim) def _make_layer(self, planes, blocks, stride=1): layers = [Bottleneck(self._inplanes, planes, stride)] self._inplanes = planes * Bottleneck.expansion for _ in range(1, blocks): layers.append(Bottleneck(self._inplanes, planes)) return nn.Sequential(*layers) def forward(self, x): def stem(x): for conv, bn in [(self.conv1, self.bn1), (self.conv2, self.bn2), (self.conv3, self.bn3)]: x = self.relu(bn(conv(x))) x = self.avgpool(x) return x x = x.type(self.conv1.weight.dtype) x = stem(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) x = self.attnpool(x) return x class LayerNorm(nn.LayerNorm): """Subclass torch's LayerNorm to handle fp16.""" def forward(self, x: torch.Tensor): orig_type = x.dtype ret = super().forward(x.type(torch.float32)) return ret.type(orig_type) class QuickGELU(nn.Module): def forward(self, x: torch.Tensor): return x * torch.sigmoid(1.702 * x) class ResidualAttentionBlock(nn.Module): def __init__(self, d_model: int, n_head: int, attn_mask: torch.Tensor = None): super().__init__() self.attn = nn.MultiheadAttention(d_model, n_head) self.ln_1 = LayerNorm(d_model) self.mlp = nn.Sequential(OrderedDict([ ("c_fc", nn.Linear(d_model, d_model * 4)), ("gelu", QuickGELU()), ("c_proj", nn.Linear(d_model * 4, d_model)) ])) self.ln_2 = LayerNorm(d_model) self.attn_mask = attn_mask def attention(self, x: torch.Tensor): self.attn_mask = self.attn_mask.to(dtype=x.dtype, device=x.device) if self.attn_mask is not None else None return self.attn(x, x, x, need_weights=False, attn_mask=self.attn_mask)[0] def forward(self, x: torch.Tensor): x = x + self.attention(self.ln_1(x)) x = x + self.mlp(self.ln_2(x)) return x class Transformer(nn.Module): def __init__(self, width: int, layers: int, heads: int, attn_mask: torch.Tensor = None): super().__init__() self.width = width self.layers = layers self.resblocks = nn.Sequential(*[ResidualAttentionBlock(width, heads, attn_mask) for _ in range(layers)]) def forward(self, x: torch.Tensor): return self.resblocks(x) class VisualTransformer(nn.Module): def __init__(self, input_resolution: int, patch_size: int, width: int, layers: int, heads: int, output_dim: int): super().__init__() self.input_resolution = input_resolution 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((input_resolution // patch_size) ** 2 + 1, width)) self.ln_pre = LayerNorm(width) self.transformer = Transformer(width, layers, heads) self.ln_post = LayerNorm(width) self.proj = nn.Parameter(scale * torch.randn(width, output_dim)) 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) x = x.permute(1, 0, 2) # LND -> NLD x = self.ln_post(x[:, 0, :]) if self.proj is not None: x = x @ self.proj return x class CLIP(nn.Module): def __init__(self, embed_dim: int, # vision image_resolution: int, vision_layers: Union[Tuple[int, int, int, int], int], vision_width: int, vision_patch_size: int, # text context_length: int, vocab_size: int, transformer_width: int, transformer_heads: int, transformer_layers: int ): super().__init__() self.context_length = context_length if isinstance(vision_layers, (tuple, list)): vision_heads = vision_width * 32 // 64 self.visual = ModifiedResNet( layers=vision_layers, output_dim=embed_dim, heads=vision_heads, input_resolution=image_resolution, width=vision_width ) else: vision_heads = vision_width // 64 self.visual = VisualTransformer( input_resolution=image_resolution, patch_size=vision_patch_size, width=vision_width, layers=vision_layers, heads=vision_heads, output_dim=embed_dim ) self.transformer = Transformer( width=transformer_width, layers=transformer_layers, heads=transformer_heads, attn_mask=self.build_attention_mask() ) self.vocab_size = vocab_size self.token_embedding = nn.Embedding(vocab_size, transformer_width) self.positional_embedding = nn.Parameter(torch.empty(self.context_length, transformer_width)) self.ln_final = LayerNorm(transformer_width) self.text_projection = nn.Parameter(torch.empty(transformer_width, embed_dim)) self.logit_scale = nn.Parameter(torch.ones([])) self.initialize_parameters() def initialize_parameters(self): nn.init.normal_(self.token_embedding.weight, std=0.02) nn.init.normal_(self.positional_embedding, std=0.01) if isinstance(self.visual, ModifiedResNet): if self.visual.attnpool is not None: std = self.visual.attnpool.c_proj.in_features ** -0.5 nn.init.normal_(self.visual.attnpool.q_proj.weight, std=std) nn.init.normal_(self.visual.attnpool.k_proj.weight, std=std) nn.init.normal_(self.visual.attnpool.v_proj.weight, std=std) nn.init.normal_(self.visual.attnpool.c_proj.weight, std=std) for resnet_block in [self.visual.layer1, self.visual.layer2, self.visual.layer3, self.visual.layer4]: for name, param in resnet_block.named_parameters(): if name.endswith("bn3.weight"): nn.init.zeros_(param) proj_std = (self.transformer.width ** -0.5) * ((2 * self.transformer.layers) ** -0.5) attn_std = self.transformer.width ** -0.5 fc_std = (2 * self.transformer.width) ** -0.5 for block in self.transformer.resblocks: nn.init.normal_(block.attn.in_proj_weight, std=attn_std) nn.init.normal_(block.attn.out_proj.weight, std=proj_std) nn.init.normal_(block.mlp.c_fc.weight, std=fc_std) nn.init.normal_(block.mlp.c_proj.weight, std=proj_std) if self.text_projection is not None: nn.init.normal_(self.text_projection, std=self.transformer.width ** -0.5) def build_attention_mask(self): # lazily create causal attention mask, with full attention between the vision tokens # pytorch uses additive attention mask; fill with -inf mask = torch.empty(self.context_length, self.context_length) mask.fill_(float("-inf")) mask.triu_(1) # zero out the lower diagonal return mask @property def dtype(self): return self.visual.conv1.weight.dtype def encode_image(self, image): return self.visual(image.type(self.dtype)) def encode_text(self, text): x = self.token_embedding(text).type(self.dtype) # [batch_size, n_ctx, d_model] x = x + self.positional_embedding.type(self.dtype) x = x.permute(1, 0, 2) # NLD -> LND x = self.transformer(x) x = x.permute(1, 0, 2) # LND -> NLD x = self.ln_final(x).type(self.dtype) # x.shape = [batch_size, n_ctx, transformer.width] # take features from the eot embedding (eot_token is the highest number in each sequence) x = x[torch.arange(x.shape[0]), text.argmax(dim=-1)] @ self.text_projection return x def forward(self, image, text): image_features = self.encode_image(image) text_features = self.encode_text(text) # normalized features image_features = image_features / image_features.norm(dim=-1, keepdim=True) text_features = text_features / text_features.norm(dim=-1, keepdim=True) # cosine similarity as logits logit_scale = self.logit_scale.exp() logits_per_image = logit_scale * image_features @ text_features.t() logits_per_text = logit_scale * text_features @ image_features.t() # shape = [global_batch_size, global_batch_size] return logits_per_image, logits_per_text def convert_weights(model: nn.Module): """Convert applicable model parameters to fp16""" def _convert_weights_to_fp16(l): if isinstance(l, (nn.Conv1d, nn.Conv2d, nn.Linear)): l.weight.data = l.weight.data.half() if l.bias is not None: l.bias.data = l.bias.data.half() if isinstance(l, nn.MultiheadAttention): for attr in [*[f"{s}_proj_weight" for s in ["in", "q", "k", "v"]], "in_proj_bias", "bias_k", "bias_v"]: tensor = getattr(l, attr) if tensor is not None: tensor.data = tensor.data.half() for name in ["text_projection", "proj"]: if hasattr(l, name): attr = getattr(l, name) if attr is not None: attr.data = attr.data.half() model.apply(_convert_weights_to_fp16) def build_model(state_dict: dict): vit = "visual.proj" in state_dict if vit: vision_width = state_dict["visual.conv1.weight"].shape[0] vision_layers = len([k for k in state_dict.keys() if k.startswith("visual.") and k.endswith(".attn.in_proj_weight")]) vision_patch_size = state_dict["visual.conv1.weight"].shape[-1] grid_size = round((state_dict["visual.positional_embedding"].shape[0] - 1) ** 0.5) image_resolution = vision_patch_size * grid_size else: counts: list = [len(set(k.split(".")[2] for k in state_dict if k.startswith(f"visual.layer{b}"))) for b in [1, 2, 3, 4]] vision_layers = tuple(counts) vision_width = state_dict["visual.layer1.0.conv1.weight"].shape[0] output_width = round((state_dict["visual.attnpool.positional_embedding"].shape[0] - 1) ** 0.5) vision_patch_size = None assert output_width ** 2 + 1 == state_dict["visual.attnpool.positional_embedding"].shape[0] image_resolution = output_width * 32 embed_dim = state_dict["text_projection"].shape[1] context_length = state_dict["positional_embedding"].shape[0] vocab_size = state_dict["token_embedding.weight"].shape[0] transformer_width = state_dict["ln_final.weight"].shape[0] transformer_heads = transformer_width // 64 transformer_layers = len(set(k.split(".")[2] for k in state_dict if k.startswith(f"transformer.resblocks"))) model = CLIP( embed_dim, image_resolution, vision_layers, vision_width, vision_patch_size, context_length, vocab_size, transformer_width, transformer_heads, transformer_layers ) for key in ["input_resolution", "context_length", "vocab_size"]: if key in state_dict: del state_dict[key] convert_weights(model) model.load_state_dict(state_dict) return model.eval() @lru_cache() def default_bpe(): return os.path.join(os.path.dirname(os.path.abspath(__file__)), "data/bpe_simple_vocab_16e6.txt") @lru_cache() def bytes_to_unicode(): """ Returns list of utf-8 byte and a corresponding list of unicode strings. The reversible bpe codes work on unicode strings. This means you need a large # of unicode characters in your vocab if you want to avoid UNKs. When you're at something like a 10B token dataset you end up needing around 5K for decent coverage. This is a signficant percentage of your normal, say, 32K bpe vocab. To avoid that, we want lookup tables between utf-8 bytes and unicode strings. And avoids mapping to whitespace/control characters the bpe code barfs on. """ bs = list(range(ord("!"), ord("~")+1))+list(range(ord("¡"), ord("¬")+1))+list(range(ord("®"), ord("ÿ")+1)) cs = bs[:] n = 0 for b in range(2**8): if b not in bs: bs.append(b) cs.append(2**8+n) n += 1 cs = [chr(n) for n in cs] return dict(zip(bs, cs)) def get_pairs(word): """Return set of symbol pairs in a word. Word is represented as tuple of symbols (symbols being variable-length strings). """ pairs = set() prev_char = word[0] for char in word[1:]: pairs.add((prev_char, char)) prev_char = char return pairs def basic_clean(text): text = ftfy.fix_text(text) text = html.unescape(html.unescape(text)) return text.strip() def whitespace_clean(text): text = re.sub(r'\s+', ' ', text) text = text.strip() return text class SimpleTokenizer(object): def __init__(self, bpe_path: str = default_bpe()): self.byte_encoder = bytes_to_unicode() self.byte_decoder = {v: k for k, v in self.byte_encoder.items()} merges = Path(bpe_path).read_text(encoding='utf8').split('\n') merges = merges[1:49152-256-2+1] merges = [tuple(merge.split()) for merge in merges] vocab = list(bytes_to_unicode().values()) vocab = vocab + [v+'</w>' for v in vocab] for merge in merges: vocab.append(''.join(merge)) vocab.extend(['<|startoftext|>', '<|endoftext|>']) self.encoder = dict(zip(vocab, range(len(vocab)))) self.decoder = {v: k for k, v in self.encoder.items()} self.bpe_ranks = dict(zip(merges, range(len(merges)))) self.cache = {'<|startoftext|>': '<|startoftext|>', '<|endoftext|>': '<|endoftext|>'} self.pat = re.compile(r"""<\|startoftext\|>|<\|endoftext\|>|'s|'t|'re|'ve|'m|'ll|'d|[\p{L}]+|[\p{N}]|[^\s\p{L}\p{N}]+""", re.IGNORECASE) def bpe(self, token): if token in self.cache: return self.cache[token] word = tuple(token[:-1]) + ( token[-1] + '</w>',) pairs = get_pairs(word) if not pairs: return token+'</w>' while True: bigram = min(pairs, key = lambda pair: self.bpe_ranks.get(pair, float('inf'))) if bigram not in self.bpe_ranks: break first, second = bigram new_word = [] i = 0 while i < len(word): try: j = word.index(first, i) new_word.extend(word[i:j]) i = j except: new_word.extend(word[i:]) break if word[i] == first and i < len(word)-1 and word[i+1] == second: new_word.append(first+second) i += 2 else: new_word.append(word[i]) i += 1 new_word = tuple(new_word) word = new_word if len(word) == 1: break else: pairs = get_pairs(word) word = ' '.join(word) self.cache[token] = word return word def encode(self, text): bpe_tokens = [] text = whitespace_clean(basic_clean(text)).lower() for token in re.findall(self.pat, text): token = ''.join(self.byte_encoder[b] for b in token.encode('utf-8')) bpe_tokens.extend(self.encoder[bpe_token] for bpe_token in self.bpe(token).split(' ')) return bpe_tokens def decode(self, tokens): text = ''.join([self.decoder[token] for token in tokens]) text = bytearray([self.byte_decoder[c] for c in text]).decode('utf-8', errors="replace").replace('</w>', ' ') return text import gzip _tokenizer = SimpleTokenizer()
class AxialPositionalEmbedding(nn.Module): def __init__(self, dim, axial_shape, axial_dims = None): super().__init__() self.dim = dim self.shape = axial_shape self.max_seq_len = reduce(mul, axial_shape, 1) self.summed = axial_dims is None axial_dims = ((dim,) * len(axial_shape)) if self.summed else axial_dims assert len(self.shape) == len(axial_dims), 'number of axial dimensions must equal the number of dimensions in the shape' assert self.summed or not self.summed and sum(axial_dims) == dim, f'axial dimensions must sum up to the target dimension {dim}' self.weights = ParameterList(self, 'weights', len(axial_shape)) for ind, (shape, axial_dim) in enumerate(zip(self.shape, axial_dims)): ax_shape = [1] * len(self.shape) ax_shape[ind] = shape ax_shape = (1, *ax_shape, axial_dim) ax_emb = nn.Parameter(torch.zeros(ax_shape).normal_(0, 1)) self.weights.append(ax_emb) def forward(self, x): b, t, e = x.shape assert (t <= self.max_seq_len), f'Sequence length ({t}) must be less than the maximum sequence length allowed ({self.max_seq_len})' embs = [] for ax_emb in self.weights.to_list(): axial_dim = ax_emb.shape[-1] expand_shape = (b, *self.shape, axial_dim) emb = ax_emb.expand(expand_shape).reshape(b, self.max_seq_len, axial_dim) embs.append(emb) pos_emb = sum(embs) if self.summed else torch.cat(embs, dim=-1) return pos_emb[:, :t].to(x) # a mock parameter list object until below issue is resolved # https://github.com/pytorch/pytorch/issues/36035 class ParameterList(object): def __init__(self, kls, prefix, length): self.ind = 0 self.kls = kls self.prefix = prefix self.length = length def _keyname(self, prefix, ind): return f'{prefix}_{ind}' def append(self, x): setattr(self.kls, self._keyname(self.prefix, self.ind), x) self.ind += 1 def to_list(self): return [getattr(self.kls, self._keyname(self.prefix, i)) for i in range(self.length)] # Axial Positional Embedding for Images class AxialPositionalEmbeddingImage(nn.Module): def __init__(self, dim, axial_shape, axial_dims = None): super().__init__() assert len(axial_shape) == 2, 'Axial shape must have 2 dimensions for images' self.pos_emb = AxialPositionalEmbedding(dim, axial_shape, axial_dims) def forward(self, img): b, c, h, w = img.shape img = img.permute(0, 2, 3, 1).reshape(b, h * w, c) pos_emb = self.pos_emb(img) return pos_emb.reshape(b, h, w, c).permute(0, 3, 1, 2)
# constants DEVICE = None # defaults to cuda if available, else cpu NUM_BATCHES = int(1e5) GRADIENT_ACCUMULATE_EVERY = 16 LEARNING_RATE = 3e-4 IGNORE_INDEX = -100 THRESHOLD_LENGTH = 250 # set device DISTOGRAM_BUCKETS = constants.DISTOGRAM_BUCKETS DEVICE = constants.DEVICE # helpers def cycle(loader, cond = lambda x: True): while True: for data in loader: if not cond(data): continue yield data # get data data = scn.load( casp_version = 12, thinning = 30, with_pytorch = 'dataloaders', batch_size = 1, dynamic_batching = False ) data = iter(data['train']) data_cond = lambda t: t[1].shape[1] < THRESHOLD_LENGTH dl = cycle(data, data_cond) # model model = Alphafold2( dim = 256, depth = 1, heads = 8, dim_head = 64 ).to(DEVICE) # optimizer optim = Adam(model.parameters(), lr = LEARNING_RATE) # training loop for _ in range(NUM_BATCHES): for _ in range(GRADIENT_ACCUMULATE_EVERY): batch = next(dl) seq, coords, mask = batch.seqs, batch.crds, batch.msks b, l, _ = seq.shape # prepare mask, labels seq, coords, mask = seq.argmax(dim = -1).to(DEVICE), coords.to(DEVICE), mask.to(DEVICE).bool() coords = rearrange(coords, 'b (l c) d -> b l c d', l = l) discretized_distances = get_bucketed_distance_matrix(coords[:, :, 1], mask, DISTOGRAM_BUCKETS, IGNORE_INDEX) # predict distogram = model(seq, mask = mask) distogram = rearrange(distogram, 'b i j c -> b c i j') # loss loss = F.cross_entropy( distogram, discretized_distances, ignore_index = IGNORE_INDEX ) loss.backward() print('loss:', loss.item()) optim.step() optim.zero_grad()
# data # models # constants FEATURES = "esm" # one of ["esm", "msa", "msa_transformer", None] DEVICE = None # defaults to cuda if available, else cpu NUM_BATCHES = int(1e5) GRADIENT_ACCUMULATE_EVERY = 16 LEARNING_RATE = 3e-4 IGNORE_INDEX = -100 THRESHOLD_LENGTH = 250 TO_PDB = False SAVE_DIR = "" # set device DEVICE = constants.DEVICE DISTOGRAM_BUCKETS = constants.DISTOGRAM_BUCKETS # set emebdder model from esm if appropiate - Load ESM-1b model if FEATURES == "esm": # from pytorch hub (almost 30gb) embedd_model, alphabet = torch.hub.load("facebookresearch/esm", "esm1b_t33_650M_UR50S") batch_converter = alphabet.get_batch_converter() ##  alternatively do # import esm # after installing esm # model, alphabet = esm.pretrained.esm1b_t33_650M_UR50S() batch_converter = alphabet.get_batch_converter() # helpers def cycle(loader, cond = lambda x: True): while True: for data in loader: if not cond(data): continue yield data # get data data = scn.load( casp_version = 12, thinning = 30, with_pytorch = 'dataloaders', batch_size = 1, dynamic_batching = False ) data = iter(data['train']) data_cond = lambda t: t[1].shape[1] < THRESHOLD_LENGTH dl = cycle(data, data_cond) # model model = Alphafold2( dim = 256, depth = 1, heads = 8, dim_head = 64, predict_coords = True, structure_module_dim = 8, structure_module_depth = 2, structure_module_heads = 4, structure_module_dim_head = 16, structure_module_refinement_iters = 2 ).to(DEVICE) # optimizer dispersion_weight = 0.1 criterion = nn.MSELoss() optim = Adam(model.parameters(), lr = LEARNING_RATE) # training loop for _ in range(NUM_BATCHES): for _ in range(GRADIENT_ACCUMULATE_EVERY): batch = next(dl) seq, coords, mask = batch.seqs, batch.crds, batch.msks b, l, _ = seq.shape # prepare data and mask labels seq, coords, mask = seq.argmax(dim = -1).to(DEVICE), coords.to(DEVICE), mask.to(DEVICE) # coords = rearrange(coords, 'b (l c) d -> b l c d', l = l) # no need to rearrange for now # mask the atoms and backbone positions for each residue # sequence embedding (msa / esm / attn / or nothing) msa, embedds = None # get embedds if FEATURES == "esm": embedds = get_esm_embedd(seq, embedd_model, batch_converter) # get msa here elif FEATURES == "msa": pass # no embeddings else: pass # predict - out is (batch, L * 3, 3) refined = model( seq, msa = msa, embedds = embedds, mask = mask ) # build SC container. set SC points to CA and optionally place carbonyl O proto_sidechain = sidechain_container(coords_3d, n_aa=batch, cloud_mask=cloud_mask, place_oxygen=False) # rotate / align coords_aligned, labels_aligned = Kabsch(refined, coords[flat_cloud_mask]) # atom mask cloud_mask = scn_cloud_mask(seq, boolean = False) flat_cloud_mask = rearrange(cloud_mask, 'b l c -> b (l c)') # chain_mask is all atoms that will be backpropped thru -> existing + trainable chain_mask = (mask * cloud_mask)[cloud_mask] flat_chain_mask = rearrange(chain_mask, 'b l c -> b (l c)') # save pdb files for visualization if TO_PDB: # idx from batch to save prot and label idx = 0 coords2pdb(seq[idx, :, 0], coords_aligned[idx], cloud_mask, prefix=SAVE_DIR, name="pred.pdb") coords2pdb(seq[idx, :, 0], labels_aligned[idx], cloud_mask, prefix=SAVE_DIR, name="label.pdb") # loss - RMSE + distogram_dispersion loss = torch.sqrt(criterion(coords_aligned[flat_chain_mask], labels_aligned[flat_chain_mask])) + \ dispersion_weight * torch.norm( (1/weights)-1 ) loss.backward() print('loss:', loss.item()) optim.step() optim.zero_grad()
# helpers def exists(val): return val is not None @contextmanager def null_context(): yield def split_at_index(dim, index, t): pre_slices = (slice(None),) * dim l = (*pre_slices, slice(None, index)) r = (*pre_slices, slice(index, None)) return t[l], t[r] # function wrapper for determinism on backwards class Deterministic(nn.Module): def __init__(self, net): super().__init__() self.net = net self.cpu_state = None self.cuda_in_fwd = None self.gpu_devices = None self.gpu_states = None def record_rng(self, *args): self.cpu_state = torch.get_rng_state() if torch.cuda._initialized: self.cuda_in_fwd = True self.gpu_devices, self.gpu_states = get_device_states(*args) def forward(self, *args, record_rng = False, set_rng = False, **kwargs): if record_rng: self.record_rng(*args) if not set_rng: return self.net(*args, **kwargs) rng_devices = [] if self.cuda_in_fwd: rng_devices = self.gpu_devices with torch.random.fork_rng(devices=rng_devices, enabled=True): torch.set_rng_state(self.cpu_state) if self.cuda_in_fwd: set_device_states(self.gpu_devices, self.gpu_states) return self.net(*args, **kwargs) # reversible self attention block class ReversibleSelfAttnBlock(nn.Module): def __init__(self, f, g, j, k): super().__init__() self.f = Deterministic(f) self.g = Deterministic(g) self.j = Deterministic(j) self.k = Deterministic(k) def forward(self, x, m, mask = None, msa_mask = None, seq_shape = None, msa_shape = None, seq_pos_emb = None, msa_pos_emb = None, _reverse = True, **kwargs): x1, x2 = torch.chunk(x, 2, dim = 2) m1, m2 = torch.chunk(m, 2, dim = 2) y1, y2, n1, n2 = None, None, None, None context = torch.no_grad if _reverse else null_context record_rng = self.training and _reverse with context(): y1 = x1 + self.f(x2, shape = seq_shape, record_rng = record_rng, mask = mask, rotary_emb = seq_pos_emb) y2 = x2 + self.g(y1, shape = seq_shape, record_rng = record_rng) n1 = m1 + self.j(m2, shape = msa_shape, record_rng = record_rng, mask = msa_mask, rotary_emb = msa_pos_emb) n2 = m2 + self.k(n1, record_rng = record_rng) return torch.cat((y1, y2), dim = 2), torch.cat((n1, n2), dim = 2) def backward_pass(self, y, n, dy, dn, mask = None, msa_mask = None, seq_shape = None, msa_shape = None, seq_pos_emb = None, msa_pos_emb = None, **kwargs): y1, y2 = torch.chunk(y, 2, dim = 2) del y dy1, dy2 = torch.chunk(dy, 2, dim = 2) del dy with torch.enable_grad(): y1.requires_grad = True gy1 = self.g(y1, shape = seq_shape, set_rng = True) torch.autograd.backward(gy1, dy2) with torch.no_grad(): x2 = y2 - gy1 del y2, gy1 dx1 = dy1 + y1.grad del dy1 y1.grad = None with torch.enable_grad(): x2.requires_grad = True fx2 = self.f(x2, shape = seq_shape, set_rng = True, mask = mask, rotary_emb = seq_pos_emb) torch.autograd.backward(fx2, dx1, retain_graph = True) with torch.no_grad(): x1 = y1 - fx2 del y1, fx2 dx2 = dy2 + x2.grad del dy2 x2.grad = None x = torch.cat([x1, x2.detach()], dim = 2) dx = torch.cat([dx1, dx2], dim = 2) n1, n2 = torch.chunk(n, 2, dim = 2) del n dn1, dn2 = torch.chunk(dn, 2, dim = 2) del dn with torch.enable_grad(): n1.requires_grad = True gn1 = self.k(n1, set_rng = True) torch.autograd.backward(gn1, dn2) with torch.no_grad(): m2 = n2 - gn1 del n2, gn1 dm1 = dn1 + n1.grad del dn1 n1.grad = None with torch.enable_grad(): m2.requires_grad = True fm2 = self.j(m2, shape = msa_shape, set_rng = True, mask = msa_mask, rotary_emb = msa_pos_emb) torch.autograd.backward(fm2, dm1, retain_graph=True) with torch.no_grad(): m1 = n1 - fm2 del n1, fm2 dm2 = dn2 + m2.grad del dn2 m2.grad = None m = torch.cat([m1, m2.detach()], dim = 2) dm = torch.cat([dm1, dm2], dim = 2) return x, m, dx, dm # reversible cross attention block class ReversibleCrossAttnBlock(nn.Module): def __init__(self, f, g, j, k): super().__init__() self.f = Deterministic(f) self.g = Deterministic(g) self.j = Deterministic(j) self.k = Deterministic(k) def forward(self, x, m, mask = None, msa_mask = None, seq_shape = None, msa_shape = None, seq_to_msa_pos_emb = None, msa_to_seq_pos_emb = None, _reverse = True, **kwargs): x1, x2 = torch.chunk(x, 2, dim = 2) m1, m2 = torch.chunk(m, 2, dim = 2) y1, y2, n1, n2 = None, None, None, None context = torch.no_grad if _reverse else null_context record_rng = self.training and _reverse with context(): y1 = x1 + self.f(x2, m2, record_rng = record_rng, mask = mask, context_mask = msa_mask, shape = seq_shape, context_shape = msa_shape, rotary_emb = seq_to_msa_pos_emb) y2 = x2 + self.k(y1, shape = seq_shape, record_rng = record_rng) n1 = m1 + self.j(m2, y2, record_rng = record_rng, mask = msa_mask, context_mask = mask, shape = msa_shape, context_shape = seq_shape, rotary_emb = msa_to_seq_pos_emb) n2 = m2 + self.g(n1, record_rng = record_rng) return torch.cat((y1, y2), dim = 2), torch.cat((n1, n2), dim = 2) def backward_pass(self, y, n, dy, dn, mask = None, msa_mask = None, seq_shape = None, msa_shape = None, seq_to_msa_pos_emb = None, msa_to_seq_pos_emb = None, **kwargs): n1, n2 = torch.chunk(n, 2, dim = 2) del n dn1, dn2 = torch.chunk(dn, 2, dim = 2) del dn y1, y2 = torch.chunk(y, 2, dim = 2) del y dy1, dy2 = torch.chunk(dy, 2, dim = 2) del dy with torch.enable_grad(): n1.requires_grad = True gn1 = self.g(n1, set_rng = True) torch.autograd.backward(gn1, dn2) with torch.no_grad(): m2 = n2 - gn1 del n2, gn1 dm1 = dn1 + n1.grad del dn1 n1.grad = None with torch.enable_grad(): m2.requires_grad = True y2.requires_grad = True fm2 = self.j(m2, y2, set_rng=True, mask = msa_mask, context_mask = mask, shape = msa_shape, context_shape = seq_shape, rotary_emb = msa_to_seq_pos_emb) torch.autograd.backward(fm2, dm1) with torch.no_grad(): m1 = n1 - fm2 del n1, fm2 dm2 = dn2 + m2.grad dx2 = dy2 + y2.grad del dn2 del dy2 m2.grad = None y2.grad = None with torch.enable_grad(): y1.requires_grad = True gy1 = self.k(y1, shape = seq_shape, set_rng = True) torch.autograd.backward(gy1, dx2) with torch.no_grad(): x2 = y2 - gy1 del y2, gy1 dx1 = dy1 + y1.grad del dy1 y1.grad = None with torch.enable_grad(): x2.requires_grad = True m2.requires_grad = True fx2 = self.f(x2, m2, set_rng = True, mask = mask, context_mask = msa_mask, shape = seq_shape, context_shape = msa_shape, rotary_emb = seq_to_msa_pos_emb) torch.autograd.backward(fx2, dx1) with torch.no_grad(): x1 = y1 - fx2 del y1, fx2 dx2 = dx2 + x2.grad dm2 = dm2 + m2.grad x2.grad = None m2.grad = None with torch.no_grad(): m = torch.cat([m1, m2.detach()], dim = 2) dm = torch.cat([dm1, dm2], dim = 2) x = torch.cat([x1, x2.detach()], dim = 2) dx = torch.cat([dx1, dx2], dim = 2) return x, m, dx, dm # reverse and non reverse functions class ReversibleFunction(Function): @staticmethod def forward(ctx, inp, ind, blocks, kwargs): x, m = split_at_index(1, ind, inp) for block in blocks: x, m = block(x, m, _reverse = True, **kwargs) ctx.blocks = blocks ctx.kwargs = kwargs ctx.ind = ind ctx.save_for_backward(x.detach(), m.detach()) return torch.cat((x, m), dim = 1) @staticmethod def backward(ctx, d): ind = ctx.ind blocks = ctx.blocks kwargs = ctx.kwargs dy, dn = split_at_index(1, ind, d) y, n = ctx.saved_tensors for block in blocks[::-1]: y, n, dy, dn = block.backward_pass(y, n, dy, dn, **kwargs) d = torch.cat((dy, dn), dim = 1) return d, None, None, None reversible_apply = ReversibleFunction.apply def irreversible_apply(inputs, ind, blocks, kwargs): x, m = split_at_index(1, ind, inputs) for block in blocks: x, m = block(x, m, _reverse = False, **kwargs) return torch.cat((x, m), dim = 1) # main reversible sequence class class ReversibleSequence(nn.Module): def __init__(self, input_blocks, block_types): super().__init__() self.block_types = block_types blocks = nn.ModuleList([]) for block, block_type in zip(input_blocks, block_types): if block_type == 'self': reversible_klass = ReversibleSelfAttnBlock elif block_type == 'cross': reversible_klass = ReversibleCrossAttnBlock elif block_type == 'conv': reversible_klass = ReversibleSelfAttnBlock blocks.append(reversible_klass(*block)) self.blocks = blocks def forward( self, seq, msa, seq_shape = None, msa_shape = None, mask = None, msa_mask = None, seq_pos_emb = None, msa_pos_emb = None, seq_to_msa_pos_emb = None, msa_to_seq_pos_emb = None, reverse = True ): assert exists(msa), 'reversibility does not work with no MSA sequences yet' blocks = self.blocks seq, msa = list(map(lambda t: torch.cat((t, t), dim = -1), (seq, msa))) kwargs = {'mask': mask, 'msa_mask': msa_mask, 'seq_shape': seq_shape, 'msa_shape': msa_shape, 'seq_pos_emb': seq_pos_emb, 'msa_pos_emb': msa_pos_emb, 'seq_to_msa_pos_emb': seq_to_msa_pos_emb, 'msa_to_seq_pos_emb': msa_to_seq_pos_emb} fn = reversible_apply if reverse else irreversible_apply ind = seq.shape[1] inp = torch.cat((seq, msa), dim = 1) out = fn(inp, ind, blocks, kwargs) seq, msa = split_at_index(1, ind, out) return list(map(lambda t: reduce(t, 'b n (c d) -> b n d', 'mean', c = 2), (seq, msa)))
# MSA MLM def get_mask_subset_with_prob(mask, prob): batch, seq_len, device = *mask.shape, mask.device max_masked = math.ceil(prob * seq_len) num_tokens = mask.sum(dim=-1, keepdim=True) mask_excess = (mask.cumsum(dim=-1) > (num_tokens * prob).ceil()) mask_excess = mask_excess[:, :max_masked] rand = torch.rand((batch, seq_len), device=device).masked_fill(~mask, -1e9) _, sampled_indices = rand.topk(max_masked, dim=-1) sampled_indices = (sampled_indices + 1).masked_fill_(mask_excess, 0) new_mask = torch.zeros((batch, seq_len + 1), device=device) new_mask.scatter_(-1, sampled_indices, 1) return new_mask[:, 1:].bool() class MLM(nn.Module): def __init__( self, dim, num_tokens, mask_id, mask_prob = 0.15, random_replace_token_prob = 0.1, keep_token_same_prob = 0.1, exclude_token_ids = (0,) ): super().__init__() self.to_logits = nn.Linear(dim, num_tokens) self.mask_id = mask_id self.mask_prob = mask_prob self.exclude_token_ids = exclude_token_ids self.keep_token_same_prob = keep_token_same_prob self.random_replace_token_prob = random_replace_token_prob def noise(self, seq, mask): num_msa = seq.shape[1] seq = rearrange(seq, 'b n ... -> (b n) ...') mask = rearrange(mask, 'b n ... -> (b n) ...') # prepare masks for noising sequence excluded_tokens_mask = mask for token_id in self.exclude_token_ids: excluded_tokens_mask = excluded_tokens_mask & (seq != token_id) mlm_mask = get_mask_subset_with_prob(excluded_tokens_mask, self.mask_prob) # keep some tokens the same replace_token_with_mask = get_mask_subset_with_prob(mlm_mask, 1. - self.keep_token_same_prob) # replace with mask seq = seq.masked_fill(mlm_mask, self.mask_id) # generate random tokens random_replace_token_prob_mask = get_mask_subset_with_prob(mlm_mask, (1 - self.keep_token_same_prob) * self.random_replace_token_prob) random_tokens = torch.randint(1, constants.NUM_AMINO_ACIDS, seq.shape).to(seq.device) for token_id in self.exclude_token_ids: random_replace_token_prob_mask = random_replace_token_prob_mask & (random_tokens != token_id) # make sure you never substitute a token with an excluded token type (pad, start, end) # noise sequence noised_seq = torch.where(random_replace_token_prob_mask, random_tokens, seq) noised_seq = rearrange(noised_seq, '(b n) ... -> b n ...', n = num_msa) mlm_mask = rearrange(mlm_mask, '(b n) ... -> b n ...', n = num_msa) return noised_seq, mlm_mask def forward(self, seq_embed, original_seq, mask): logits = self.to_logits(seq_embed) seq_logits = logits[mask] seq_labels = original_seq[mask] loss = F.cross_entropy(seq_logits, seq_labels, reduction = 'mean') return loss
# constants MAX_NUM_MSA = 20 MAX_NUM_TEMPLATES = 10 NUM_AMINO_ACIDS = 21 NUM_EMBEDDS_TR = 1280 # best esm model NUM_EMBEDDS_T5 = 1024 # best t5 model NUM_COORDS_PER_RES = 14 DISTOGRAM_BUCKETS = 37 THETA_BUCKETS = 25 PHI_BUCKETS = 13 OMEGA_BUCKETS = 25 # embedding related constants MSA_EMBED_DIM = 768 MSA_MODEL_PATH = ["facebookresearch/esm", "esm_msa1_t12_100M_UR50S"] ESM_EMBED_DIM = 1280 ESM_MODEL_PATH = ["facebookresearch/esm", "esm1b_t33_650M_UR50S"] PROTTRAN_EMBED_DIM = 1024 # default device DEVICE_NAME = 'cuda' if torch.cuda.is_available() else 'cpu' DEVICE = torch.device(DEVICE_NAME) # aminoacid data AA_DATA = { 'A': { 'bonds': [[0,1], [1,2], [2,3], [1,4]] }, 'R': { 'bonds': [[0,1], [1,2], [2,3], [2,4], [4,5], [5,6], [6,7], [7,8], [8,9], [8,10]] }, 'N': { 'bonds': [[0,1], [1,2], [2,3], [1,4], [4,5], [5,6], [5,7]] }, 'D': { 'bonds': [[0,1], [1,2], [2,3], [1,4], [4,5], [5,6], [5,7]] }, 'C': { 'bonds': [[0,1], [1,2], [2,3], [1,4], [4,5]] }, 'Q': { 'bonds': [[0,1], [1,2], [2,3], [1,4], [4,5], [5,6], [6,7], [6,8]] }, 'E': { 'bonds': [[0,1], [1,2], [2,3], [1,4], [4,5], [5,6], [6,7], [7,8]] }, 'G': { 'bonds': [[0,1], [1,2], [2,3]] }, 'H': { 'bonds': [[0,1], [1,2], [2,3], [1,4], [4,5], [5,6], [6,7], [7,8], [8,9], [5,9]] }, 'I': { 'bonds': [[0,1], [1,2], [2,3], [1,4], [4,5], [5,6], [4,7]] }, 'L': { 'bonds': [[0,1], [1,2], [2,3], [1,4], [4,5], [5,6], [5,7]] }, 'K': { 'bonds': [[0,1], [1,2], [2,3], [1,4], [4,5], [5,6], [6,7], [7,8]] }, 'M': { 'bonds': [[0,1], [1,2], [2,3], [1,4], [4,5], [5,6], [6,7]] }, 'F': { 'bonds': [[0,1], [1,2], [2,3], [1,4], [4,5], [5,6], [6,7], [7,8], [8,9], [9,10], [5,10]] }, 'P': { 'bonds': [[0,1], [1,2], [2,3], [1,4], [4,5], [5,6], [0,6]] }, 'S': { 'bonds': [[0,1], [1,2], [2,3], [1,4], [4,5]] }, 'T': { 'bonds': [[0,1], [1,2], [2,3], [1,4], [4,5], [4,6]] }, 'W': { 'bonds': [[0,1], [1,2], [2,3], [1,4], [4,5], [5,6], [6,7], [7,8], [8,9], [9,10], [10,11], [11,12], [12, 13], [5,13], [8,13]] }, 'Y': { 'bonds': [[0,1], [1,2], [2,3], [1,4], [4,5], [5,6], [6,7], [7,8], [8,9], [8,10], [10,11], [5,11]] }, 'V': { 'bonds': [[0,1], [1,2], [2,3], [1,4], [4,5], [4,6]] }, '_': { 'bonds': [] } }
# utils for working with 3d-protein structures # import torch_sparse # only needed for sparse nth_deg adj calculation # bio # sidechainnet # custom # build vocabulary VOCAB = ProteinVocabulary() # constants # helpers def exists(val): return val is not None # constants: same as in alphafold2.py DISTANCE_THRESHOLDS = torch.linspace(2, 20, steps = constants.DISTOGRAM_BUCKETS) # distance binning function def get_bucketed_distance_matrix(coords, mask, num_buckets = constants.DISTOGRAM_BUCKETS, ignore_index = -100): distances = torch.cdist(coords, coords, p=2) boundaries = torch.linspace(2, 20, steps = num_buckets, device = coords.device) discretized_distances = torch.bucketize(distances, boundaries[:-1]) discretized_distances.masked_fill_(~(mask[..., None] & mask[..., None, :]), ignore_index) return discretized_distances # decorators def set_backend_kwarg(fn): @wraps(fn) def inner(*args, backend = 'auto', **kwargs): if backend == 'auto': backend = 'torch' if isinstance(args[0], torch.Tensor) else 'numpy' kwargs.update(backend = backend) return fn(*args, **kwargs) return inner def expand_dims_to(t, length = 3): if length == 0: return t return t.reshape(*((1,) * length), *t.shape) # will work with both torch and numpy def expand_arg_dims(dim_len = 3): """ pack here for reuse. turns input into (B x D x N) """ def outer(fn): @wraps(fn) def inner(x, y, **kwargs): assert len(x.shape) == len(y.shape), "Shapes of A and B must match." remaining_len = dim_len - len(x.shape) x = expand_dims_to(x, length = remaining_len) y = expand_dims_to(y, length = remaining_len) return fn(x, y, **kwargs) return inner return outer def invoke_torch_or_numpy(torch_fn, numpy_fn): def outer(fn): @wraps(fn) def inner(*args, **kwargs): backend = kwargs.pop('backend') passed_args = fn(*args, **kwargs) passed_args = list(passed_args) if isinstance(passed_args[-1], dict): passed_kwargs = passed_args.pop() else: passed_kwargs = {} backend_fn = torch_fn if backend == 'torch' else numpy_fn return backend_fn(*passed_args, **passed_kwargs) return inner return outer @contextlib.contextmanager def torch_default_dtype(dtype): prev_dtype = torch.get_default_dtype() torch.set_default_dtype(dtype) yield torch.set_default_dtype(prev_dtype) # preprocess data def get_atom_ids_dict(): """ Get's a dict mapping each atom to a token. """ ids = set(["", "N", "CA", "C", "O"]) for k,v in SC_BUILD_INFO.items(): for name in v["atom-names"]: ids.add(name) return {k: i for i,k in enumerate(sorted(ids))} def make_cloud_mask(aa): """ relevent points will be 1. paddings will be 0. """ mask = np.zeros(constants.NUM_COORDS_PER_RES) # early stop if padding token if aa == "_": return mask # get num of atoms in aa n_atoms = 4+len( SC_BUILD_INFO[ ONE_TO_THREE_LETTER_MAP[aa] ]["atom-names"] ) mask[:n_atoms] = 1 return mask def make_atom_id_embedds(aa, atom_ids): """ Return the tokens for each atom in the aa. """ mask = np.zeros(constants.NUM_COORDS_PER_RES) # early stop if padding token if aa == "_": return mask # get atom id atom_list = ["N", "CA", "C", "O"] + SC_BUILD_INFO[ ONE_TO_THREE_LETTER_MAP[aa] ]["atom-names"] for i,atom in enumerate(atom_list): mask[i] = ATOM_IDS[atom] return mask ATOM_IDS = get_atom_ids_dict() CUSTOM_INFO = {k: {"cloud_mask": make_cloud_mask(k), "atom_id_embedd": make_atom_id_embedds(k, atom_ids=ATOM_IDS), } for k in "ARNDCQEGHILKMFPSTWYV_"} # common utils # parsing to pdb for easier visualization - other example from sidechainnet is: # https://github.com/jonathanking/sidechainnet/tree/master/sidechainnet/structure def download_pdb(name, route): """ Downloads a PDB entry from the RCSB PDB. Inputs: * name: str. the PDB entry id. 4 characters, capitalized. * route: str. route of the destin file. usually ".pdb" extension Output: route of destin file """ os.system(f"curl https://files.rcsb.org/download/{name}.pdb > {route}") return route def clean_pdb(name, route=None, chain_num=None): """ Cleans the structure to only leave the important part. Inputs: * name: str. route of the input .pdb file * route: str. route of the output. will overwrite input if not provided * chain_num: int. index of chain to select (1-indexed as pdb files) Output: route of destin file. """ import mdtraj destin = route if route is not None else name # read input raw_prot = mdtraj.load_pdb(name) # iterate over prot and select the specified chains idxs = [] for chain in raw_prot.topology.chains: # if arg passed, only select that chain if chain_num is not None: if chain_num != chain.index: continue # select indexes of chain chain_idxs = raw_prot.topology.select(f"chainid == {str(chain.index)}") idxs.extend( chain_idxs.tolist() ) # sort: topology and xyz selection are ordered idxs = sorted(idxs) # get new trajectory from the sleected subset of indexes and save prot = mdtraj.Trajectory(xyz=raw_prot.xyz[:, idxs], topology=raw_prot.topology.subset(idxs)) prot.save(destin) return destin def custom2pdb(coords, proteinnet_id, route): """ Takes a custom representation and turns into a .pdb file. Inputs: * coords: array/tensor of shape (3 x N) or (N x 3). in Angstroms. same order as in the proteinnnet is assumed (same as raw pdb file) * proteinnet_id: str. proteinnet id format (<class>#<pdb_id>_<chain_number>_<chain_id>) see: https://github.com/aqlaboratory/proteinnet/ * route: str. destin route. Output: tuple of routes: (original, generated) for the structures. """ import mdtraj # convert to numpy if isinstance(coords, torch.Tensor): coords = coords.detach().cpu().numpy() # ensure (1, N, 3) if coords.shape[1] == 3: coords = coords.T coords = np.newaxis(coords, axis=0) # get pdb id and chain num pdb_name, chain_num = proteinnet_id.split("#")[-1].split("_")[:-1] pdb_destin = "/".join(route.split("/")[:-1])+"/"+pdb_name+".pdb" # download pdb file and select appropiate download_pdb(pdb_name, pdb_destin) clean_pdb(pdb_destin, chain_num=chain_num) # load trajectory scaffold and replace coordinates - assumes same order scaffold = mdtraj.load_pdb(pdb_destin) scaffold.xyz = coords scaffold.save(route) return pdb_destin, route def coords2pdb(seq, coords, cloud_mask, prefix="", name="af2_struct.pdb"): """ Turns coordinates into PDB files ready to be visualized. Inputs: * seq: (L,) tensor of ints (sidechainnet aa-key pairs) * coords: (3, N) coords of atoms * cloud_mask: (L, C) boolean mask of occupied spaces in scn format * prefix: str. directory to save files. * name: str. name of destin file (ex: pred1.pdb) """ scaffold = torch.zeros( cloud_mask.shape, 3 ) scaffold[cloud_mask] = coords.cpu().float() # build structures and save pred = scn.StructureBuilder( seq, crd=scaffold ) pred.to_pdb(prefix+name) # adapted from https://github.com/facebookresearch/esm def remove_insertions(sequence: str) -> str: """ Removes any insertions into the sequence. Needed to load aligned sequences in an MSA. """ deletekeys = dict.fromkeys(string.ascii_lowercase) deletekeys["."] = None deletekeys["*"] = None translation = str.maketrans(deletekeys) return sequence.translate(translation) def read_msa(filename: str, nseq: int): """ Reads the first nseq sequences from an MSA file, automatically removes insertions.""" return [(record.description, remove_insertions(str(record.seq))) for record in itertools.islice(SeqIO.parse(filename, "fasta"), nseq)] # sidechainnet / MSA / other data utils def ids_to_embed_input(x): """ Returns the amino acid string input for calculating the ESM and MSA transformer embeddings Inputs: * x: any deeply nested list of integers that correspond with amino acid id """ assert isinstance(x, list), 'input must be a list' id2aa = VOCAB._int2char out = [] for el in x: if isinstance(el, list): out.append(ids_to_embed_input(el)) elif isinstance(el, int): out.append(id2aa[el]) else: raise TypeError('type must be either list or character') if all(map(lambda c: isinstance(c, str), out)): return (None, ''.join(out)) return out def ids_to_prottran_input(x): """ Returns the amino acid string input for calculating the ESM and MSA transformer embeddings Inputs: * x: any deeply nested list of integers that correspond with amino acid id """ assert isinstance(x, list), 'input must be a list' id2aa = VOCAB._int2char out = [] for ids in x: chars = ' '.join([id2aa[i] for i in ids]) chars = re.sub(r"[UZOB]", "X", chars) out.append(chars) return out def get_prottran_embedd(seq, model, tokenizer, device = None): from transformers import pipeline fe = pipeline('feature-extraction', model = model, tokenizer = tokenizer, device = (-1 if not exists(device) else device.index)) max_seq_len = seq.shape[1] embedd_inputs = ids_to_prottran_input(seq.cpu().tolist()) embedding = fe(embedd_inputs) embedding = torch.tensor(embedding, device = device) return embedding[:, 1:(max_seq_len + 1)] def get_msa_embedd(msa, embedd_model, batch_converter, device = None): """ Returns the MSA_tr embeddings for a protein. Inputs: * seq: ( (b,) L,) tensor of ints (in sidechainnet int-char convention) * embedd_model: MSA_tr model (see train_end2end.py for an example) * batch_converter: MSA_tr batch converter (see train_end2end.py for an example) Outputs: tensor of (batch, n_seqs, L, embedd_dim) * n_seqs: number of sequences in the MSA * embedd_dim: number of embedding dimensions. 768 for MSA_Transformer """ # use MSA transformer REPR_LAYER_NUM = 12 device = seq.device max_seq_len = msa.shape[-1] embedd_inputs = ids_to_embed_input(msa.cpu().tolist()) msa_batch_labels, msa_batch_strs, msa_batch_tokens = batch_converter(embedd_inputs) with torch.no_grad(): results = embedd_model(msa_batch_tokens.to(device), repr_layers=[REPR_LAYER_NUM], return_contacts=False) # index 0 is for start token. so take from 1 one token_reps = results["representations"][REPR_LAYER_NUM][..., 1:max_seq_len+1, :] return token_reps def get_esm_embedd(seq, embedd_model, batch_converter, msa_data=None): """ Returns the ESM embeddings for a protein. Inputs: * seq: ( (b,) L,) tensor of ints (in sidechainnet int-char convention) * embedd_model: ESM model (see train_end2end.py for an example) * batch_converter: ESM batch converter (see train_end2end.py for an example) Outputs: tensor of (batch, n_seqs, L, embedd_dim) * n_seqs: number of sequences in the MSA. 1 for ESM-1b * embedd_dim: number of embedding dimensions. 1280 for ESM-1b """ # use ESM transformer device = seq.device REPR_LAYER_NUM = 33 max_seq_len = seq.shape[-1] embedd_inputs = ids_to_embed_input(seq.cpu().tolist()) batch_labels, batch_strs, batch_tokens = batch_converter(embedd_inputs) with torch.no_grad(): results = embedd_model(batch_tokens.to(device), repr_layers=[REPR_LAYER_NUM], return_contacts=False) # index 0 is for start token. so take from 1 one token_reps = results["representations"][REPR_LAYER_NUM][..., 1:max_seq_len+1, :].unsqueeze(dim=1) return token_reps def get_t5_embedd(seq, tokenizer, encoder, msa_data=None, device=None): """ Returns the ProtT5-XL-U50 embeddings for a protein. Inputs: * seq: ( (b,) L,) tensor of ints (in sidechainnet int-char convention) * tokenizer: tokenizer model: T5Tokenizer * encoder: encoder model: T5EncoderModel ex: from transformers import T5EncoderModel, T5Tokenizer model_name = "Rostlab/prot_t5_xl_uniref50" tokenizer = T5Tokenizer.from_pretrained(model_name, do_lower_case=False ) model = T5EncoderModel.from_pretrained(model_name) # prepare model model = model.to(device) model = model.eval() if torch.cuda.is_available(): model = model.half() Outputs: tensor of (batch, n_seqs, L, embedd_dim) * n_seqs: number of sequences in the MSA. 1 for T5 models * embedd_dim: number of embedding dimensions. 1024 for T5 models """ # get params and prepare device = seq.device if device is None else device embedd_inputs = ids_to_prottran_input(seq.cpu().tolist()) # embedd - https://huggingface.co/Rostlab/prot_t5_xl_uniref50 inputs_embedding = [] shift_left, shift_right = 0, -1 ids = tokenizer.batch_encode_plus(embedd_inputs, add_special_tokens=True, padding=True, return_tensors="pt") with torch.no_grad(): embedding = encoder(input_ids=torch.tensor(ids['input_ids']).to(device), attention_mask=torch.tensor(ids["attention_mask"]).to(device)) # return (batch, seq_len, embedd_dim) token_reps = embedding.last_hidden_state[:, shift_left:shift_right].to(device) token_reps = expand_dims_to(token_reps, 4-len(token_reps.shape)) return token_reps.float() def get_all_protein_ids(dataloader, verbose=False): """ Given a sidechainnet dataloader for a CASP version, Returns all the ids belonging to proteins. Inputs: * dataloader: a sidechainnet dataloader for a CASP version Outputs: a set containing the ids for all protein entries. """ # store ids here ids = set([]) # iterate for all batches for i,batch in tqdm(enumerate(dataloaders['train'])): # for breaking from 2 loops at once try: for i in range(batch.int_seqs.shape[0]): # check if all fragments are : 4_LETTER_PDB + NUM + CHAIN max_len_10 = len(batch.pids[i]) < 10 fragments = [len(x) <= 4 for x in batch.pids[i].split("_")] fragments_under_4 = sum(fragments) == len(fragments) # AND CONDITION # record id if max_len_10 and fragments_under_4: ids.add(batch.pids[i]) else: if verbose: print("skip:", batch.pids[i], "under 4", fragments) except StopIteration: break # returns set of ids return ids def scn_cloud_mask(scn_seq, boolean=True, coords=None): """ Gets the boolean mask atom positions (not all aas have same atoms). Inputs: * scn_seq: (batch, length) sequence as provided by Sidechainnet package * boolean: whether to return as array of idxs or boolean values * coords: optional .(batch, lc, 3). sidechainnet coords. returns the true mask (solves potential atoms that might not be provided) Outputs: (batch, length, NUM_COORDS_PER_RES) boolean mask """ scn_seq = expand_dims_to(scn_seq, 2 - len(scn_seq.shape)) # early check for coords mask if coords is not None: batch_mask = ( rearrange(coords, '... (l c) d -> ... l c d', c=constants.NUM_COORDS_PER_RES) == 0 ).sum(dim=-1) < coords.shape[-1] if boolean: return batch_mask.bool() else: return batch_mask.nonzero() # do loop in cpu device = scn_seq.device batch_mask = [] scn_seq = scn_seq.cpu().tolist() for i, seq in enumerate(scn_seq): # get masks for each prot (points for each aa) batch_mask.append( torch.tensor([CUSTOM_INFO[VOCAB._int2char[aa]]['cloud_mask'] \ for aa in seq]).bool().to(device) ) # concat in last dim batch_mask = torch.stack(batch_mask, dim=0) # return mask (boolean or indexes) if boolean: return batch_mask.bool() else: return batch_mask.nonzero() def scn_backbone_mask(scn_seq, boolean=True, n_aa=3): """ Gets the boolean mask for N and CA positions. Inputs: * scn_seq: sequence(s) as provided by Sidechainnet package (int tensor/s) * n_aa: number of atoms in a backbone. (may include cbeta as 4th pos) * bool: whether to return as array of idxs or boolean values Outputs: (N_mask, CA_mask, C_mask) """ wrapper = torch.zeros(*scn_seq.shape, n_aa).to(scn_seq.device) # N is the first atom in every AA. CA is the 2nd. wrapper[..., 0] = 1 wrapper[..., 1] = 2 wrapper[..., 2] = 3 wrapper = rearrange(wrapper, '... l c -> ... (l c)') # find idxs N_mask = wrapper == 1 CA_mask = wrapper == 2 C_mask = wrapper == 3 if boolean: return N_mask, CA_mask, C_mask return torch.nonzero(N_mask), torch.nonzero(CA_mask), torch.nonzero(C_mask) def scn_atom_embedd(scn_seq): """ Returns the token for each atom in the aa. Inputs: * scn_seq: sequence(s) as provided by Sidechainnet package (int tensor/s) """ device = scn_seq.device batch_tokens = [] # do loop in cpu scn_seq = scn_seq.cpu().tolist() for i,seq in enumerate(scn_seq): batch_tokens.append( torch.tensor([CUSTOM_INFO[VOCAB.int2char(aa)]["atom_id_embedd"] \ for aa in seq]) ) batch_tokens = torch.stack(batch_tokens, dim=0).long().to(device) return batch_tokens def mat_input_to_masked(x, x_mask=None, edges_mat=None, edges=None, edge_mask=None, edge_attr_mat=None, edge_attr=None): """ Turns the padded input and edges + mask into the non-padded inputs and edges. At least one of (edges_mat, edges) must be provided. The same format for edges and edge_attr must be provided (either adj matrix form or flattened form). Inputs: * x: ((batch), N, D) a tensor of N nodes and D dims for each one * x_mask: ((batch), N,) boolean mask for x * edges: (2, E) optional. indices of the corresponding adjancecy matrix. * edges_mat: ((batch), N, N) optional. adjacency matrix for x * edge_mask: optional. boolean mask of the same shape of either "edge_mat" or "edges". * edge_attr: (E, D_edge) optional. edge attributes of D_edge dims. * edge_attr_mat: ((batch), N, N) optional. adjacency matrix with features Outputs: * x: (N_, D) the masked node features * edge_index: (2, E_) the masked x-indices for the edges * edge_attr: (E_, D_edge) the masked edge attributes * batch: (N_,) the corresponding index in the batch for each node """ # collapse batch dimension if len(x.shape) == 3: batch_dim = x.shape[1] # collapse for x and its mask x = rearrange(x, 'b n d ... -> (b n) d ...') if x_mask is not None: x_mask = rearrange(x_mask, 'b n ... -> (b n) ...') else: x_mask = torch.ones_like(x[..., 0]).bool() # collapse for edge indexes and attributes if needed if edges_mat is not None and edges is None: edges = torch.nonzero(edges_mat, as_tuple=False).t() edges = edges[1:] + edges[:1]*batch_dim # get the batch identifier for each node batch = (torch.arange(x.shape[0], device=x.device) // batch_dim)[x_mask] else: # edges to indices format if edges_mat is not None and edges is None: edges = torch.nonzero(edges_mat, as_tuple=False).t() # get the batch identifier for each node batch = torch.zeros(x.shape[0], device=x.device).to(x.device) # adapt edge attrs if provided if edge_attr_mat is not None and edge_attr is None: edge_attr = edge_attr[edges_mat.bool()] # gen edge_mask if not provided if edge_mask is None: edge_mask = torch.ones_like(edges[-1]).bool() # begin applying masks x = x[x_mask] # process edge indexes: get square mat and remove all non-coding atoms max_num = edges.max().item()+1 wrapper = torch.zeros(max_num, max_num).to(x.device) wrapper[edges[0][edge_mask], edges[1][edge_mask]] = 1 wrapper = wrapper[x_mask, :][:, x_mask] edge_index = torch.nonzero(wrapper, as_tuple=False).t() # process edge attr edge_attr = edge_attr[edge_mask] if edge_attr is not None else None return x, edge_index, edge_attr, batch def nth_deg_adjacency(adj_mat, n=1, sparse=False): """ Calculates the n-th degree adjacency matrix. Performs mm of adj_mat and adds the newly added. Default is dense. Mods for sparse version are done when needed. Inputs: * adj_mat: (N, N) adjacency tensor * n: int. degree of the output adjacency * sparse: bool. whether to use torch-sparse module Outputs: * edge_idxs: ij positions of the adjacency matrix * edge_attrs: degree of connectivity (1 for neighs, 2 for neighs^2, ... ) """ adj_mat = adj_mat.float() attr_mat = torch.zeros_like(adj_mat) new_adj_mat = adj_mat.clone() for i in range(n): if i == 0: attr_mat += adj_mat continue if i == 1 and sparse: idxs = adj_mat.nonzero().t() vals = adj_mat[idxs[0], idxs[1]] new_idxs = idxs.clone() new_vals = vals.clone() m, k, n = 3 * [adj_mat.shape[0]] # (m, n) * (n, k) , but adj_mats are squared: m=n=k if sparse: new_idxs, new_vals = torch_sparse.spspmm(new_idxs, new_vals, idxs, vals, m=m, k=k, n=n) new_vals = new_vals.bool().float() # fill by indexes bc it's faster in sparse mode - will need an intersection function previous = attr_mat[new_idxs[0], new_idxs[1]].bool().float() attr_mat[new_idxs[0], new_idxs[1]] = (1 - previous)*(i+1) else: new_adj_mat = (new_adj_mat @ adj_mat).bool().float() attr_mat.masked_fill( (new_adj_mat - attr_mat.bool().float()).bool(), i+1 ) return new_adj_mat, attr_mat def prot_covalent_bond(seqs, adj_degree=1, cloud_mask=None, mat=True, sparse=False): """ Returns the idxs of covalent bonds for a protein. Inputs * seq: (b, n) torch long. * adj_degree: int. adjacency degree * cloud_mask: mask selecting the present atoms. * mat: whether to return as indexes of only atoms (PyG version) or matrices of masked atoms (for batched training). for indexes, only 1 seq is supported. * sparse: bool. whether to use torch_sparse for adj_mat calc Outputs: edge_idxs, edge_types (degree of adjacency). """ device = seqs.device # set up container adj_mat (will get trimmed - less than 14) next_aa = NUM_COORDS_PER_RES adj_mat = torch.zeros(seqs.shape[0], *[seqs.shape[1]*NUM_COORDS_PER_RES]*2) # not needed to device since it's only for indices seq_list = seqs.cpu().tolist() for s,seq in enumerate(seq_list): next_idx = 0 for i,idx in enumerate(seq): aa_bonds = constants.AA_DATA[VOCAB._int2char[idx]]['bonds'] # if no edges -> padding token -> finish bond creation for this seq if len(aa_bonds) == 0: break # correct next position. for indexes functionality next_aa = max(aa_bonds, key=lambda x: max(x))[-1] # offset by pos in chain ( intra-aa bonds + with next aa ) bonds = next_idx + torch.tensor( aa_bonds + [[2, next_aa]] ).t() next_idx += next_aa # delete link with next if final AA in seq if i == seqs.shape[1] - 1: bonds = bonds[:, :-1] # modify adj mat adj_mat[s, bonds[0], bonds[1]] = 1 # convert to undirected adj_mat[s] = adj_mat[s] + adj_mat[s].t() # do N_th degree adjacency adj_mat, attr_mat = nth_deg_adjacency(adj_mat, n=adj_degree, sparse=sparse) if mat: # return the full matrix/tensor return attr_mat.bool().to(seqs.device), attr_mat.to(device) else: edge_idxs = attr_mat[0].nonzero().t().long() edge_types = attr_mat[0, edge_idxs[0], edge_idxs[1]] return edge_idxs.to(seqs.device), edge_types.to(seqs.device) def sidechain_container(seqs, backbones, atom_mask, cloud_mask=None, padding_tok=20): """ Gets a backbone of the protein, returns the whole coordinates with sidechains (same format as sidechainnet). Keeps differentiability. Inputs: * seqs: (batch, L) either tensor or list * backbones: (batch, L*n_aa, 3): assume batch=1 (could be extended (?not tested)). Coords for (N-term, C-alpha, C-term, (c_beta)) of every aa. * atom_mask: (14,). int or bool tensor specifying which atoms are passed. * cloud_mask: (batch, l, c). optional. cloud mask from scn_cloud_mask`. sets point outside of mask to 0. if passed, else c_alpha * padding: int. padding token. same as in sidechainnet: 20 Outputs: whole coordinates of shape (batch, L, 14, 3) """ atom_mask = atom_mask.bool().cpu().detach() cum_atom_mask = atom_mask.cumsum(dim=-1).tolist() device = backbones.device batch, length = backbones.shape[0], backbones.shape[1] // cum_atom_mask[-1] predicted = rearrange(backbones, 'b (l back) d -> b l back d', l=length) # early check if whole chain is already pred if cum_atom_mask[-1] == 14: return predicted # build scaffold from (N, CA, C, CB) - do in cpu new_coords = torch.zeros(batch, length, constants.NUM_COORDS_PER_RES, 3) predicted = predicted.cpu() if predicted.is_cuda else predicted # fill atoms if they have been passed for i,atom in enumerate(atom_mask.tolist()): if atom: new_coords[:, :, i] = predicted[:, :, cum_atom_mask[i]-1] # generate sidechain if not passed for s,seq in enumerate(seqs): # format seq accordingly if isinstance(seq, torch.Tensor): padding = (seq == padding_tok).sum().item() seq_str = ''.join([VOCAB._int2char[aa] for aa in seq.cpu().numpy()[:-padding or None]]) elif isinstance(seq, str): padding = 0 seq_str = seq # get scaffolds - will overwrite oxygen since its position is fully determined by N-C-CA scaffolds = mp_nerf.proteins.build_scaffolds_from_scn_angles(seq_str, angles=None, device="cpu") coords, _ = mp_nerf.proteins.sidechain_fold(wrapper = new_coords[s, :-padding or None].detach(), **scaffolds, c_beta = cum_atom_mask[4]==5) # add detached scn for i,atom in enumerate(atom_mask.tolist()): if not atom: new_coords[:, :-padding or None, i] = coords[:, i] new_coords = new_coords.to(device) if cloud_mask is not None: new_coords[torch.logical_not(cloud_mask)] = 0. # replace any nan-s with previous point location (or N if pos is 13th of AA) nan_mask = list(torch.nonzero(new_coords!=new_coords, as_tuple=True)) new_coords[nan_mask[0], nan_mask[1], nan_mask[2]] = new_coords[nan_mask[0], nan_mask[1], (nan_mask[-2]+1) % new_coords.shape[-1]] return new_coords.to(device) # distance utils (distogram to dist mat + masking) def center_distogram_torch(distogram, bins=DISTANCE_THRESHOLDS, min_t=1., center="mean", wide="std"): """ Returns the central estimate of a distogram. Median for now. Inputs: * distogram: (batch, N, N, B) where B is the number of buckets. * bins: (B,) containing the cutoffs for the different buckets * min_t: float. lower bound for distances. Outputs: * central: (batch, N, N) * dispersion: (batch, N, N) * weights: (batch, N, N) """ shape, device = distogram.shape, distogram.device # threshold to weights and find mean value of each bin n_bins = ( bins - 0.5 * (bins[2] - bins[1]) ).to(device) n_bins[0] = 1.5 n_bins[-1] = 1.33*bins[-1] # above last threshold is ignored max_bin_allowed = torch.tensor(n_bins.shape[0]-1).to(device).long() # calculate measures of centrality and dispersion - magnitudes = distogram.sum(dim=-1) if center == "median": cum_dist = torch.cumsum(distogram, dim=-1) medium = 0.5 * cum_dist[..., -1:] central = torch.searchsorted(cum_dist, medium).squeeze() central = n_bins[ torch.min(central, max_bin_allowed) ] elif center == "mean": central = (distogram * n_bins).sum(dim=-1) / magnitudes # create mask for last class - (IGNORE_INDEX) mask = (central <= bins[-2].item()).float() # mask diagonal to 0 dist - don't do masked filling to avoid inplace errors diag_idxs = np.arange(shape[-2]) central = expand_dims_to(central, 3 - len(central.shape)) central[:, diag_idxs, diag_idxs] *= 0. # provide weights if wide == "var": dispersion = (distogram * (n_bins - central.unsqueeze(-1))**2).sum(dim=-1) / magnitudes elif wide == "std": dispersion = ((distogram * (n_bins - central.unsqueeze(-1))**2).sum(dim=-1) / magnitudes).sqrt() else: dispersion = torch.zeros_like(central, device=device) # rescale to 0-1. lower std / var --> weight=1. set potential nan's to 0 weights = mask / (1+dispersion) weights[weights != weights] *= 0. weights[:, diag_idxs, diag_idxs] *= 0. return central, weights # distance matrix to 3d coords: https://github.com/scikit-learn/scikit-learn/blob/42aff4e2e/sklearn/manifold/_mds.py#L279 def mds_torch(pre_dist_mat, weights=None, iters=10, tol=1e-5, eigen=False, verbose=2): """ Gets distance matrix. Outputs 3d. See below for wrapper. Assumes (for now) distogram is (N x N) and symmetric Outs: * best_3d_coords: (batch x 3 x N) * historic_stresses: (batch x steps) """ device, dtype = pre_dist_mat.device, pre_dist_mat.type() # ensure batched MDS pre_dist_mat = expand_dims_to(pre_dist_mat, length = ( 3 - len(pre_dist_mat.shape) )) # start batch, N, _ = pre_dist_mat.shape diag_idxs = np.arange(N) his = [torch.tensor([np.inf]*batch, device=device)] # initialize by eigendecomposition: https://www.lptmc.jussieu.fr/user/lesne/bioinformatics.pdf # follow : https://www.biorxiv.org/content/10.1101/2020.11.27.401232v1.full.pdf D = pre_dist_mat**2 M = 0.5 * (D[:, :1, :] + D[:, :, :1] - D) # do loop svd bc it's faster: (2-3x in CPU and 1-2x in GPU) # https://discuss.pytorch.org/t/batched-svd-lowrank-being-much-slower-than-loop-implementation-both-cpu-and-gpu/119336 svds = [torch.svd_lowrank(mi) for mi in M] u = torch.stack([svd[0] for svd in svds], dim=0) s = torch.stack([svd[1] for svd in svds], dim=0) v = torch.stack([svd[2] for svd in svds], dim=0) best_3d_coords = torch.bmm(u, torch.diag_embed(s).abs().sqrt())[..., :3] # only eigen - way faster but not weights if weights is None and eigen==True: return torch.transpose( best_3d_coords, -1, -2), torch.zeros_like(torch.stack(his, dim=0)) elif eigen==True: if verbose: print("Can't use eigen flag if weights are active. Fallback to iterative") # continue the iterative way if weights is None: weights = torch.ones_like(pre_dist_mat) # iterative updates: for i in range(iters): # compute distance matrix of coords and stress best_3d_coords = best_3d_coords.contiguous() dist_mat = torch.cdist(best_3d_coords, best_3d_coords, p=2).clone() stress = ( weights * (dist_mat - pre_dist_mat)**2 ).sum(dim=(-1,-2)) * 0.5 # perturb - update X using the Guttman transform - sklearn-like dist_mat[ dist_mat <= 0 ] += 1e-7 ratio = weights * (pre_dist_mat / dist_mat) B = -ratio B[:, diag_idxs, diag_idxs] += ratio.sum(dim=-1) # update coords = (1. / N * torch.matmul(B, best_3d_coords)) dis = torch.norm(coords, dim=(-1, -2)) if verbose >= 2: print('it: %d, stress %s' % (i, stress)) # update metrics if relative improvement above tolerance if (his[-1] - stress / dis).mean() <= tol: if verbose: print('breaking at iteration %d with stress %s' % (i, stress / dis)) break best_3d_coords = coords his.append( stress / dis ) return torch.transpose(best_3d_coords, -1,-2), torch.stack(his, dim=0) def mds_numpy(pre_dist_mat, weights=None, iters=10, tol=1e-5, eigen=False, verbose=2): """ Gets distance matrix. Outputs 3d. See below for wrapper. Assumes (for now) distrogram is (N x N) and symmetric Out: * best_3d_coords: (3 x N) * historic_stress """ if weights is None: weights = np.ones_like(pre_dist_mat) # ensure batched MDS pre_dist_mat = expand_dims_to(pre_dist_mat, length = ( 3 - len(pre_dist_mat.shape) )) # start batch, N, _ = pre_dist_mat.shape his = [np.inf] # init random coords best_stress = np.inf * np.ones(batch) best_3d_coords = 2*np.random.rand(batch, 3, N) - 1 # iterative updates: for i in range(iters): # compute distance matrix of coords and stress dist_mat = np.linalg.norm(best_3d_coords[:, :, :, None] - best_3d_coords[:, :, None, :], axis=-3) stress = (( weights * (dist_mat - pre_dist_mat) )**2).sum(axis=(-1, -2)) * 0.5 # perturb - update X using the Guttman transform - sklearn-like dist_mat[dist_mat == 0] = 1e-7 ratio = weights * (pre_dist_mat / dist_mat) B = -ratio B[:, np.arange(N), np.arange(N)] += ratio.sum(axis=-1) # update - double transpose. TODO: consider fix coords = (1. / N * np.matmul(best_3d_coords, B)) dis = np.linalg.norm(coords, axis=(-1, -2)) if verbose >= 2: print('it: %d, stress %s' % (i, stress)) # update metrics if relative improvement above tolerance if (best_stress - stress / dis).mean() <= tol: if verbose: print('breaking at iteration %d with stress %s' % (i, stress / dis)) break best_3d_coords = coords best_stress = stress / dis his.append(best_stress) return best_3d_coords, np.array(his) def get_dihedral_torch(c1, c2, c3, c4): """ Returns the dihedral angle in radians. Will use atan2 formula from: https://en.wikipedia.org/wiki/Dihedral_angle#In_polymer_physics Can't use torch.dot bc it does not broadcast Inputs: * c1: (batch, 3) or (3,) * c1: (batch, 3) or (3,) * c1: (batch, 3) or (3,) * c1: (batch, 3) or (3,) """ u1 = c2 - c1 u2 = c3 - c2 u3 = c4 - c3 return torch.atan2( ( (torch.norm(u2, dim=-1, keepdim=True) * u1) * torch.cross(u2,u3, dim=-1) ).sum(dim=-1) , ( torch.cross(u1,u2, dim=-1) * torch.cross(u2, u3, dim=-1) ).sum(dim=-1) ) def get_dihedral_numpy(c1, c2, c3, c4): """ Returns the dihedral angle in radians. Will use atan2 formula from: https://en.wikipedia.org/wiki/Dihedral_angle#In_polymer_physics Inputs: * c1: (batch, 3) or (3,) * c1: (batch, 3) or (3,) * c1: (batch, 3) or (3,) * c1: (batch, 3) or (3,) """ u1 = c2 - c1 u2 = c3 - c2 u3 = c4 - c3 return np.arctan2( ( (np.linalg.norm(u2, axis=-1, keepdims=True) * u1) * np.cross(u2,u3, axis=-1)).sum(axis=-1), ( np.cross(u1,u2, axis=-1) * np.cross(u2, u3, axis=-1) ).sum(axis=-1) ) def calc_phis_torch(pred_coords, N_mask, CA_mask, C_mask=None, prop=True, verbose=0): """ Filters mirrors selecting the 1 with most N of negative phis. Used as part of the MDScaling wrapper if arg is passed. See below. Angle Phi between planes: (Cterm{-1}, N, Ca{0}) and (N{0}, Ca{+1}, Cterm{+1}) Inputs: * pred_coords: (batch, 3, N) predicted coordinates * N_mask: (batch, N) boolean mask for N-term positions * CA_mask: (batch, N) boolean mask for C-alpha positions * C_mask: (batch, N) or None. boolean mask for C-alpha positions or automatically calculate from N_mask and CA_mask if None. * prop: bool. whether to return as a proportion of negative phis. * verbose: bool. verbosity level Output: (batch, N) containing the phi angles or (batch,) containing the proportions. Note: use [0] since all prots in batch have same backbone """ # detach gradients for angle calculation - mirror selection pred_coords_ = torch.transpose(pred_coords.detach(), -1 , -2).cpu() # ensure dims N_mask = expand_dims_to( N_mask, 2-len(N_mask.shape) ) CA_mask = expand_dims_to( CA_mask, 2-len(CA_mask.shape) ) if C_mask is not None: C_mask = expand_dims_to( C_mask, 2-len(C_mask.shape) ) else: C_mask = torch.logical_not(torch.logical_or(N_mask,CA_mask)) # select points n_terms = pred_coords_[:, N_mask[0].squeeze()] c_alphas = pred_coords_[:, CA_mask[0].squeeze()] c_terms = pred_coords_[:, C_mask[0].squeeze()] # compute phis for every pritein in the batch phis = [get_dihedral_torch(c_terms[i, :-1], n_terms[i, 1:], c_alphas[i, 1:], c_terms[i, 1:]) for i in range(pred_coords.shape[0])] # return percentage of lower than 0 if prop: return torch.stack([(x<0).float().mean() for x in phis], dim=0 ) return phis def calc_phis_numpy(pred_coords, N_mask, CA_mask, C_mask=None, prop=True, verbose=0): """ Filters mirrors selecting the 1 with most N of negative phis. Used as part of the MDScaling wrapper if arg is passed. See below. Angle Phi between planes: (Cterm{-1}, N, Ca{0}) and (N{0}, Ca{+1}, Cterm{+1}) Inputs: * pred_coords: (batch, 3, N) predicted coordinates * N_mask: (N, ) boolean mask for N-term positions * CA_mask: (N, ) boolean mask for C-alpha positions * C_mask: (N, ) or None. boolean mask for C-alpha positions or automatically calculate from N_mask and CA_mask if None. * prop: bool. whether to return as a proportion of negative phis. * verbose: bool. verbosity level Output: (batch, N) containing the phi angles or (batch,) containing the proportions. """ # detach gradients for angle calculation - mirror selection pred_coords_ = np.transpose(pred_coords, (0, 2, 1)) n_terms = pred_coords_[:, N_mask.squeeze()] c_alphas = pred_coords_[:, CA_mask.squeeze()] # select c_term auto if not passed if C_mask is not None: c_terms = pred_coords_[:, C_mask] else: c_terms = pred_coords_[:, (np.ones_like(N_mask)-N_mask-CA_mask).squeeze().astype(bool) ] # compute phis for every pritein in the batch phis = [get_dihedral_numpy(c_terms[i, :-1], n_terms[i, 1:], c_alphas[i, 1:], c_terms[i, 1:]) for i in range(pred_coords.shape[0])] # return percentage of lower than 0 if prop: return np.array( [(x<0).mean() for x in phis] ) return phis # alignment by centering + rotation to compute optimal RMSD # adapted from : https://github.com/charnley/rmsd/ def kabsch_torch(X, Y, cpu=True): """ Kabsch alignment of X into Y. Assumes X,Y are both (Dims x N_points). See below for wrapper. """ device = X.device # center X and Y to the origin X_ = X - X.mean(dim=-1, keepdim=True) Y_ = Y - Y.mean(dim=-1, keepdim=True) # calculate convariance matrix (for each prot in the batch) C = torch.matmul(X_, Y_.t()).detach() if cpu: C = C.cpu() # Optimal rotation matrix via SVD if int(torch.__version__.split(".")[1]) < 8: # warning! int torch 1.<8 : W must be transposed V, S, W = torch.svd(C) W = W.t() else: V, S, W = torch.linalg.svd(C) # determinant sign for direction correction d = (torch.det(V) * torch.det(W)) < 0.0 if d: S[-1] = S[-1] * (-1) V[:, -1] = V[:, -1] * (-1) # Create Rotation matrix U U = torch.matmul(V, W).to(device) # calculate rotations X_ = torch.matmul(X_.t(), U).t() # return centered and aligned return X_, Y_ def kabsch_numpy(X, Y): """ Kabsch alignment of X into Y. Assumes X,Y are both (Dims x N_points). See below for wrapper. """ # center X and Y to the origin X_ = X - X.mean(axis=-1, keepdims=True) Y_ = Y - Y.mean(axis=-1, keepdims=True) # calculate convariance matrix (for each prot in the batch) C = np.dot(X_, Y_.transpose()) # Optimal rotation matrix via SVD V, S, W = np.linalg.svd(C) # determinant sign for direction correction d = (np.linalg.det(V) * np.linalg.det(W)) < 0.0 if d: S[-1] = S[-1] * (-1) V[:, -1] = V[:, -1] * (-1) # Create Rotation matrix U U = np.dot(V, W) # calculate rotations X_ = np.dot(X_.T, U).T # return centered and aligned return X_, Y_ # metrics - more formulas here: http://predictioncenter.org/casp12/doc/help.html def distmat_loss_torch(X=None, Y=None, X_mat=None, Y_mat=None, p=2, q=2, custom=None, distmat_mask=None, clamp=None): """ Calculates a loss on the distance matrix - no need to align structs. Inputs: * X: (N, d) tensor. the predicted structure. One of (X, X_mat) is needed. * X_mat: (N, N) tensor. the predicted distance matrix. Optional () * Y: (N, d) tensor. the true structure. One of (Y, Y_mat) is needed. * Y_mat: (N, N) tensor. the predicted distance matrix. Optional () * p: int. power for the distance calculation (2 for euclidean) * q: float. power for the scaling of the loss (2 for MSE, 1 for MAE, etc) * custom: func or None. custom loss over distance matrices. ex: lambda x,y: 1 - 1/ (1 + ((x-y))**2) (1 is very bad. 0 is good) * distmat_mask: (N, N) mask (boolean or weights for each ij pos). optional. * clamp: tuple of (min,max) values for clipping distance matrices. ex: (0,150) """ assert (X is not None or X_mat is not None) and \ (Y is not None or Y_mat is not None), "The true and predicted coords or dist mats must be provided" # calculate distance matrices if X_mat is None: X = X.squeeze() if clamp is not None: X = torch.clamp(X, *clamp) X_mat = torch.cdist(X, X, p=p) if Y_mat is None: Y = Y.squeeze() if clamp is not None: Y = torch.clamp(Y, *clamp) Y_mat = torch.cdist(Y, Y, p=p) if distmat_mask is None: distmat_mask = torch.ones_like(Y_mat).bool() # do custom expression if passed if custom is not None: return custom(X_mat.squeeze(), Y_mat.squeeze()).mean() # **2 ensures always positive. Later scale back to desired power else: loss = ( X_mat - Y_mat )**2 if q != 2: loss = loss**(q/2) return loss[distmat_mask].mean() def rmsd_torch(X, Y): """ Assumes x,y are both (B x D x N). See below for wrapper. """ return torch.sqrt( torch.mean((X - Y)**2, axis=(-1, -2)) ) def rmsd_numpy(X, Y): """ Assumes x,y are both (B x D x N). See below for wrapper. """ return np.sqrt( np.mean((X - Y)**2, axis=(-1, -2)) ) def gdt_torch(X, Y, cutoffs, weights=None): """ Assumes x,y are both (B x D x N). see below for wrapper. * cutoffs is a list of `K` thresholds * weights is a list of `K` weights (1 x each threshold) """ device = X.device if weights is None: weights = torch.ones(1,len(cutoffs)) else: weights = torch.tensor([weights]).to(device) # set zeros and fill with values GDT = torch.zeros(X.shape[0], len(cutoffs), device=device) dist = ((X - Y)**2).sum(dim=1).sqrt() # iterate over thresholds for i,cutoff in enumerate(cutoffs): GDT[:, i] = (dist <= cutoff).float().mean(dim=-1) # weighted mean return (GDT*weights).mean(-1) def gdt_numpy(X, Y, cutoffs, weights=None): """ Assumes x,y are both (B x D x N). see below for wrapper. * cutoffs is a list of `K` thresholds * weights is a list of `K` weights (1 x each threshold) """ if weights is None: weights = np.ones( (1,len(cutoffs)) ) else: weights = np.array([weights]) # set zeros and fill with values GDT = np.zeros( (X.shape[0], len(cutoffs)) ) dist = np.sqrt( ((X - Y)**2).sum(axis=1) ) # iterate over thresholds for i,cutoff in enumerate(cutoffs): GDT[:, i] = (dist <= cutoff).mean(axis=-1) # weighted mean return (GDT*weights).mean(-1) def tmscore_torch(X, Y): """ Assumes x,y are both (B x D x N). see below for wrapper. """ L = max(15, X.shape[-1]) d0 = 1.24 * (L - 15)**(1/3) - 1.8 # get distance dist = ((X - Y)**2).sum(dim=1).sqrt() # formula (see wrapper for source): return (1 / (1 + (dist/d0)**2)).mean(dim=-1) def tmscore_numpy(X, Y): """ Assumes x,y are both (B x D x N). see below for wrapper. """ L = max(15, X.shape[-1]) d0 = 1.24 * np.cbrt(L - 15) - 1.8 # get distance dist = np.sqrt( ((X - Y)**2).sum(axis=1) ) # formula (see wrapper for source): return (1 / (1 + (dist/d0)**2)).mean(axis=-1) def mdscaling_torch(pre_dist_mat, weights=None, iters=10, tol=1e-5, fix_mirror=True, N_mask=None, CA_mask=None, C_mask=None, eigen=False, verbose=2): """ Handles the specifics of MDS for proteins (mirrors, ...) """ # batched mds for full parallel preds, stresses = mds_torch(pre_dist_mat, weights=weights,iters=iters, tol=tol, eigen=eigen, verbose=verbose) if not fix_mirror: return preds, stresses # no need to caculate multiple mirrors - just correct Z axis phi_ratios = calc_phis_torch(preds, N_mask, CA_mask, C_mask, prop=True) to_correct = torch.nonzero( (phi_ratios < 0.5)).view(-1) # fix mirrors by (-1)*Z if more (+) than (-) phi angles preds[to_correct, -1] = (-1)*preds[to_correct, -1] if verbose == 2: print("Corrected mirror idxs:", to_correct) return preds, stresses def mdscaling_numpy(pre_dist_mat, weights=None, iters=10, tol=1e-5, fix_mirror=True, N_mask=None, CA_mask=None, C_mask=None, verbose=2): """ Handles the specifics of MDS for proteins (mirrors, ...) """ # batched mds for full parallel preds, stresses = mds_numpy(pre_dist_mat, weights=weights,iters=iters, tol=tol, verbose=verbose) if not fix_mirror: return preds, stresses # no need to caculate multiple mirrors - just correct Z axis phi_ratios = calc_phis_numpy(preds, N_mask, CA_mask, C_mask, prop=True) for i,pred in enumerate(preds): # fix mirrors by (-1)*Z if more (+) than (-) phi angles if phi_ratios < 0.5: preds[i, -1] = (-1)*preds[i, -1] if verbose == 2: print("Corrected mirror in struct no.", i) return preds, stresses def lddt_ca_torch(true_coords, pred_coords, cloud_mask, r_0=15.): """ Computes the lddt score for each C_alpha. https://academic.oup.com/bioinformatics/article/29/21/2722/195896 Inputs: * true_coords: (b, l, c, d) in sidechainnet format. * pred_coords: (b, l, c, d) in sidechainnet format. * cloud_mask : (b, l, c) adapted for scn format. * r_0: float. maximum inclusion radius in reference struct. Outputs: * (b, l) lddt for c_alpha scores (ranging between 0 and 1) See wrapper below. """ device, dtype = true_coords.device, true_coords.type() thresholds = torch.tensor([0.5, 1, 2, 4], device=device).type(dtype) # adapt masks cloud_mask = cloud_mask.bool().cpu() c_alpha_mask = torch.zeros(cloud_mask.shape[1:], device=device).bool() # doesn't have batch dim c_alpha_mask[..., 1] = True # container for c_alpha scores (between 0,1) wrapper = torch.zeros(true_coords.shape[:2], device=device).type(dtype) for bi, seq in enumerate(true_coords): # select atoms for study c_alphas = cloud_mask[bi]*c_alpha_mask # only pick c_alpha positions selected_pred = pred_coords[bi, c_alphas, :] selected_target = true_coords[bi, c_alphas, :] # get number under distance dist_mat_pred = torch.cdist(selected_pred, selected_pred, p=2) dist_mat_target = torch.cdist(selected_target, selected_target, p=2) under_r0_target = dist_mat_target < r_0 compare_dists = torch.abs(dist_mat_pred - dist_mat_target)[under_r0_target] # measure diff below threshold score = torch.zeros_like(under_r0_target).float() max_score = torch.zeros_like(under_r0_target).float() max_score[under_r0_target] = 4. # measure under how many thresholds score[under_r0_target] = thresholds.shape[0] - \ torch.bucketize( compare_dists, boundaries=thresholds ).float() # dont include diagonal l_mask = c_alphas.float().sum(dim=-1).bool() wrapper[bi, l_mask] = ( score.sum(dim=-1) - thresholds.shape[0] ) / \ ( max_score.sum(dim=-1) - thresholds.shape[0] ) return wrapper ################ ### WRAPPERS ### ################ @set_backend_kwarg @invoke_torch_or_numpy(mdscaling_torch, mdscaling_numpy) def MDScaling(pre_dist_mat, **kwargs): """ Gets distance matrix (-ces). Outputs 3d. Assumes (for now) distrogram is (N x N) and symmetric. For support of ditograms: see `center_distogram_torch()` Inputs: * pre_dist_mat: (1, N, N) distance matrix. * weights: optional. (N x N) pairwise relative weights . * iters: number of iterations to run the algorithm on * tol: relative tolerance at which to stop the algorithm if no better improvement is achieved * backend: one of ["numpy", "torch", "auto"] for backend choice * fix_mirror: int. number of iterations to run the 3d generation and pick the best mirror (highest number of negative phis) * N_mask: indexing array/tensor for indices of backbone N. Only used if fix_mirror > 0. * CA_mask: indexing array/tensor for indices of backbone C_alpha. Only used if fix_mirror > 0. * verbose: whether to print logs Outputs: * best_3d_coords: (3 x N) * historic_stress: (timesteps, ) """ pre_dist_mat = expand_dims_to(pre_dist_mat, 3 - len(pre_dist_mat.shape)) return pre_dist_mat, kwargs @expand_arg_dims(dim_len = 2) @set_backend_kwarg @invoke_torch_or_numpy(kabsch_torch, kabsch_numpy) def Kabsch(A, B): """ Returns Kabsch-rotated matrices resulting from aligning A into B. Adapted from: https://github.com/charnley/rmsd/ * Inputs: * A,B are (3 x N) * backend: one of ["numpy", "torch", "auto"] for backend choice * Outputs: tensor/array of shape (3 x N) """ # run calcs - pick the 0th bc an additional dim was created return A, B @expand_arg_dims() @set_backend_kwarg @invoke_torch_or_numpy(rmsd_torch, rmsd_numpy) def RMSD(A, B): """ Returns RMSD score as defined here (lower is better): https://en.wikipedia.org/wiki/ Root-mean-square_deviation_of_atomic_positions * Inputs: * A,B are (B x 3 x N) or (3 x N) * backend: one of ["numpy", "torch", "auto"] for backend choice * Outputs: tensor/array of size (B,) """ return A, B @expand_arg_dims() @set_backend_kwarg @invoke_torch_or_numpy(gdt_torch, gdt_numpy) def GDT(A, B, *, mode="TS", cutoffs=[1,2,4,8], weights=None): """ Returns GDT score as defined here (highre is better): Supports both TS and HA http://predictioncenter.org/casp12/doc/help.html * Inputs: * A,B are (B x 3 x N) (np.array or torch.tensor) * cutoffs: defines thresholds for gdt * weights: list containing the weights * mode: one of ["numpy", "torch", "auto"] for backend * Outputs: tensor/array of size (B,) """ # define cutoffs for each type of gdt and weights cutoffs = [0.5,1,2,4] if mode in ["HA", "ha"] else [1,2,4,8] # calculate GDT return A, B, cutoffs, {'weights': weights} @expand_arg_dims() @set_backend_kwarg @invoke_torch_or_numpy(tmscore_torch, tmscore_numpy) def TMscore(A, B): """ Returns TMscore as defined here (higher is better): >0.5 (likely) >0.6 (highly likely) same folding. = 0.2. https://en.wikipedia.org/wiki/Template_modeling_score Warning! It's not exactly the code in: https://zhanglab.ccmb.med.umich.edu/TM-score/TMscore.cpp but will suffice for now. Inputs: * A,B are (B x 3 x N) (np.array or torch.tensor) * mode: one of ["numpy", "torch", "auto"] for backend Outputs: tensor/array of size (B,) """ return A, B
# structure module # constants @dataclass class Recyclables: coords: torch.Tensor single_msa_repr_row: torch.Tensor pairwise_repr: torch.Tensor @dataclass class ReturnValues: distance: torch.Tensor = None theta: torch.Tensor = None phi: torch.Tensor = None omega: torch.Tensor = None msa_mlm_loss: torch.Tensor = None recyclables: Recyclables = None # helpers def exists(val): return val is not None def default(val, d): if exists(val): return val return d() if isfunction(d) else d def cast_tuple(val, depth = 1): return val if isinstance(val, tuple) else (val,) * depth def init_zero_(layer): nn.init.constant_(layer.weight, 0.) if exists(layer.bias): nn.init.constant_(layer.bias, 0.) # helper classes class Always(nn.Module): def __init__(self, val): super().__init__() self.val = val def forward(self, x): return self.val # feed forward class GEGLU(nn.Module): def forward(self, x): x, gates = x.chunk(2, dim = -1) return x * F.gelu(gates) class FeedForward(nn.Module): def __init__( self, dim, mult = 4, dropout = 0. ): super().__init__() self.norm = nn.LayerNorm(dim) self.net = nn.Sequential( nn.Linear(dim, dim * mult * 2), GEGLU(), nn.Dropout(dropout), nn.Linear(dim * mult, dim) ) init_zero_(self.net[-1]) def forward(self, x, **kwargs): x = self.norm(x) return self.net(x) # attention class Attention(nn.Module): def __init__( self, dim, seq_len = None, heads = 8, dim_head = 64, dropout = 0., gating = True ): super().__init__() inner_dim = dim_head * heads self.seq_len = seq_len self.heads= heads self.scale = dim_head ** -0.5 self.to_q = nn.Linear(dim, inner_dim, bias = False) self.to_kv = nn.Linear(dim, inner_dim * 2, bias = False) self.to_out = nn.Linear(inner_dim, dim) self.gating = nn.Linear(dim, inner_dim) nn.init.constant_(self.gating.weight, 0.) nn.init.constant_(self.gating.bias, 1.) self.dropout = nn.Dropout(dropout) init_zero_(self.to_out) def forward(self, x, mask = None, attn_bias = None, context = None, context_mask = None, tie_dim = None): device, orig_shape, h, has_context = x.device, x.shape, self.heads, exists(context) context = default(context, x) q, k, v = (self.to_q(x), *self.to_kv(context).chunk(2, dim = -1)) i, j = q.shape[-2], k.shape[-2] q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = h), (q, k, v)) # scale q = q * self.scale # query / key similarities if exists(tie_dim): # as in the paper, for the extra MSAs # they average the queries along the rows of the MSAs # they named this particular module MSAColumnGlobalAttention q, k = map(lambda t: rearrange(t, '(b r) ... -> b r ...', r = tie_dim), (q, k)) q = q.mean(dim = 1) dots = einsum('b h i d, b r h j d -> b r h i j', q, k) dots = rearrange(dots, 'b r ... -> (b r) ...') else: dots = einsum('b h i d, b h j d -> b h i j', q, k) # add attention bias, if supplied (for pairwise to msa attention communication) if exists(attn_bias): dots = dots + attn_bias # masking if exists(mask): mask = default(mask, lambda: torch.ones(1, i, device = device).bool()) context_mask = mask if not has_context else default(context_mask, lambda: torch.ones(1, k.shape[-2], device = device).bool()) mask_value = -torch.finfo(dots.dtype).max mask = mask[:, None, :, None] * context_mask[:, None, None, :] dots = dots.masked_fill(~mask, mask_value) # attention attn = dots.softmax(dim = -1) attn = self.dropout(attn) # aggregate out = einsum('b h i j, b h j d -> b h i d', attn, v) # merge heads out = rearrange(out, 'b h n d -> b n (h d)') # gating gates = self.gating(x) out = out * gates.sigmoid() # combine to out out = self.to_out(out) return out class AxialAttention(nn.Module): def __init__( self, dim, heads, row_attn = True, col_attn = True, accept_edges = False, global_query_attn = False, **kwargs ): super().__init__() assert not (not row_attn and not col_attn), 'row or column attention must be turned on' self.row_attn = row_attn self.col_attn = col_attn self.global_query_attn = global_query_attn self.norm = nn.LayerNorm(dim) self.attn = Attention(dim = dim, heads = heads, **kwargs) self.edges_to_attn_bias = nn.Sequential( nn.Linear(dim, heads, bias = False), Rearrange('b i j h -> b h i j') ) if accept_edges else None def forward(self, x, edges = None, mask = None): assert self.row_attn ^ self.col_attn, 'has to be either row or column attention, but not both' b, h, w, d = x.shape x = self.norm(x) # axial attention if self.col_attn: axial_dim = w mask_fold_axial_eq = 'b h w -> (b w) h' input_fold_eq = 'b h w d -> (b w) h d' output_fold_eq = '(b w) h d -> b h w d' elif self.row_attn: axial_dim = h mask_fold_axial_eq = 'b h w -> (b h) w' input_fold_eq = 'b h w d -> (b h) w d' output_fold_eq = '(b h) w d -> b h w d' x = rearrange(x, input_fold_eq) if exists(mask): mask = rearrange(mask, mask_fold_axial_eq) attn_bias = None if exists(self.edges_to_attn_bias) and exists(edges): attn_bias = self.edges_to_attn_bias(edges) attn_bias = repeat(attn_bias, 'b h i j -> (b x) h i j', x = axial_dim) tie_dim = axial_dim if self.global_query_attn else None out = self.attn(x, mask = mask, attn_bias = attn_bias, tie_dim = tie_dim) out = rearrange(out, output_fold_eq, h = h, w = w) return out class TriangleMultiplicativeModule(nn.Module): def __init__( self, *, dim, hidden_dim = None, mix = 'ingoing' ): super().__init__() assert mix in {'ingoing', 'outgoing'}, 'mix must be either ingoing or outgoing' hidden_dim = default(hidden_dim, dim) self.norm = nn.LayerNorm(dim) self.left_proj = nn.Linear(dim, hidden_dim) self.right_proj = nn.Linear(dim, hidden_dim) self.left_gate = nn.Linear(dim, hidden_dim) self.right_gate = nn.Linear(dim, hidden_dim) self.out_gate = nn.Linear(dim, hidden_dim) # initialize all gating to be identity for gate in (self.left_gate, self.right_gate, self.out_gate): nn.init.constant_(gate.weight, 0.) nn.init.constant_(gate.bias, 1.) if mix == 'outgoing': self.mix_einsum_eq = '... i k d, ... j k d -> ... i j d' elif mix == 'ingoing': self.mix_einsum_eq = '... k j d, ... k i d -> ... i j d' self.to_out_norm = nn.LayerNorm(hidden_dim) self.to_out = nn.Linear(hidden_dim, dim) def forward(self, x, mask = None): assert x.shape[1] == x.shape[2], 'feature map must be symmetrical' if exists(mask): mask = rearrange(mask, 'b i j -> b i j ()') x = self.norm(x) left = self.left_proj(x) right = self.right_proj(x) if exists(mask): left = left * mask right = right * mask left_gate = self.left_gate(x).sigmoid() right_gate = self.right_gate(x).sigmoid() out_gate = self.out_gate(x).sigmoid() left = left * left_gate right = right * right_gate out = einsum(self.mix_einsum_eq, left, right) out = self.to_out_norm(out) out = out * out_gate return self.to_out(out) # evoformer blocks class OuterMean(nn.Module): def __init__( self, dim, hidden_dim = None, eps = 1e-5 ): super().__init__() self.eps = eps self.norm = nn.LayerNorm(dim) hidden_dim = default(hidden_dim, dim) self.left_proj = nn.Linear(dim, hidden_dim) self.right_proj = nn.Linear(dim, hidden_dim) self.proj_out = nn.Linear(hidden_dim, dim) def forward(self, x, mask = None): x = self.norm(x) left = self.left_proj(x) right = self.right_proj(x) outer = rearrange(left, 'b m i d -> b m i () d') * rearrange(right, 'b m j d -> b m () j d') if exists(mask): # masked mean, if there are padding in the rows of the MSA mask = rearrange(mask, 'b m i -> b m i () ()') * rearrange(mask, 'b m j -> b m () j ()') outer = outer.masked_fill(~mask, 0.) outer = outer.mean(dim = 1) / (mask.sum(dim = 1) + self.eps) else: outer = outer.mean(dim = 1) return self.proj_out(outer) class PairwiseAttentionBlock(nn.Module): def __init__( self, dim, seq_len, heads, dim_head, dropout = 0., global_column_attn = False ): super().__init__() self.outer_mean = OuterMean(dim) self.triangle_attention_outgoing = AxialAttention(dim = dim, heads = heads, dim_head = dim_head, row_attn = True, col_attn = False, accept_edges = True) self.triangle_attention_ingoing = AxialAttention(dim = dim, heads = heads, dim_head = dim_head, row_attn = False, col_attn = True, accept_edges = True, global_query_attn = global_column_attn) self.triangle_multiply_outgoing = TriangleMultiplicativeModule(dim = dim, mix = 'outgoing') self.triangle_multiply_ingoing = TriangleMultiplicativeModule(dim = dim, mix = 'ingoing') def forward( self, x, mask = None, msa_repr = None, msa_mask = None ): if exists(msa_repr): x = x + self.outer_mean(msa_repr, mask = msa_mask) x = self.triangle_multiply_outgoing(x, mask = mask) + x x = self.triangle_multiply_ingoing(x, mask = mask) + x x = self.triangle_attention_outgoing(x, edges = x, mask = mask) + x x = self.triangle_attention_ingoing(x, edges = x, mask = mask) + x return x class MsaAttentionBlock(nn.Module): def __init__( self, dim, seq_len, heads, dim_head, dropout = 0. ): super().__init__() self.row_attn = AxialAttention(dim = dim, heads = heads, dim_head = dim_head, row_attn = True, col_attn = False, accept_edges = True) self.col_attn = AxialAttention(dim = dim, heads = heads, dim_head = dim_head, row_attn = False, col_attn = True) def forward( self, x, mask = None, pairwise_repr = None ): x = self.row_attn(x, mask = mask, edges = pairwise_repr) + x x = self.col_attn(x, mask = mask) + x return x # main evoformer class class EvoformerBlock(nn.Module): def __init__( self, *, dim, seq_len, heads, dim_head, attn_dropout, ff_dropout, global_column_attn = False ): super().__init__() self.layer = nn.ModuleList([ PairwiseAttentionBlock(dim = dim, seq_len = seq_len, heads = heads, dim_head = dim_head, dropout = attn_dropout, global_column_attn = global_column_attn), FeedForward(dim = dim, dropout = ff_dropout), MsaAttentionBlock(dim = dim, seq_len = seq_len, heads = heads, dim_head = dim_head, dropout = attn_dropout), FeedForward(dim = dim, dropout = ff_dropout), ]) def forward(self, inputs): x, m, mask, msa_mask = inputs attn, ff, msa_attn, msa_ff = self.layer # msa attention and transition m = msa_attn(m, mask = msa_mask, pairwise_repr = x) m = msa_ff(m) + m # pairwise attention and transition x = attn(x, mask = mask, msa_repr = m, msa_mask = msa_mask) x = ff(x) + x return x, m, mask, msa_mask class Evoformer(nn.Module): def __init__( self, *, depth, **kwargs ): super().__init__() self.layers = nn.ModuleList([EvoformerBlock(**kwargs) for _ in range(depth)]) def forward( self, x, m, mask = None, msa_mask = None ): inp = (x, m, mask, msa_mask) x, m, *_ = checkpoint_sequential(self.layers, 1, inp) return x, m class Alphafold2(nn.Module): def __init__( self, *, dim, max_seq_len = 2048, depth = 6, heads = 8, dim_head = 64, max_rel_dist = 32, num_tokens = constants.NUM_AMINO_ACIDS, num_embedds = constants.NUM_EMBEDDS_TR, max_num_msas = constants.MAX_NUM_MSA, max_num_templates = constants.MAX_NUM_TEMPLATES, extra_msa_evoformer_layers = 4, attn_dropout = 0., ff_dropout = 0., templates_dim = 32, templates_embed_layers = 4, templates_angles_feats_dim = 55, predict_angles = False, symmetrize_omega = False, predict_coords = False, # structure module related keyword arguments below structure_module_depth = 4, structure_module_heads = 1, structure_module_dim_head = 4, disable_token_embed = False, mlm_mask_prob = 0.15, mlm_random_replace_token_prob = 0.1, mlm_keep_token_same_prob = 0.1, mlm_exclude_token_ids = (0,), recycling_distance_buckets = 32 ): super().__init__() self.dim = dim # token embedding self.token_emb = nn.Embedding(num_tokens + 1, dim) if not disable_token_embed else Always(0) self.to_pairwise_repr = nn.Linear(dim, dim * 2) self.disable_token_embed = disable_token_embed # positional embedding self.max_rel_dist = max_rel_dist self.pos_emb = nn.Embedding(max_rel_dist * 2 + 1, dim) # extra msa embedding self.extra_msa_evoformer = Evoformer( dim = dim, depth = extra_msa_evoformer_layers, seq_len = max_seq_len, heads = heads, dim_head = dim_head, attn_dropout = attn_dropout, ff_dropout = ff_dropout, global_column_attn = True ) # template embedding self.to_template_embed = nn.Linear(templates_dim, dim) self.templates_embed_layers = templates_embed_layers self.template_pairwise_embedder = PairwiseAttentionBlock( dim = dim, dim_head = dim_head, heads = heads, seq_len = max_seq_len ) self.template_pointwise_attn = Attention( dim = dim, dim_head = dim_head, heads = heads, dropout = attn_dropout ) self.template_angle_mlp = nn.Sequential( nn.Linear(templates_angles_feats_dim, dim), nn.GELU(), nn.Linear(dim, dim) ) # projection for angles, if needed self.predict_angles = predict_angles self.symmetrize_omega = symmetrize_omega if predict_angles: self.to_prob_theta = nn.Linear(dim, constants.THETA_BUCKETS) self.to_prob_phi = nn.Linear(dim, constants.PHI_BUCKETS) self.to_prob_omega = nn.Linear(dim, constants.OMEGA_BUCKETS) # custom embedding projection self.embedd_project = nn.Linear(num_embedds, dim) # main trunk modules self.net = Evoformer( dim = dim, depth = depth, seq_len = max_seq_len, heads = heads, dim_head = dim_head, attn_dropout = attn_dropout, ff_dropout = ff_dropout ) # MSA SSL MLM self.mlm = MLM( dim = dim, num_tokens = num_tokens, mask_id = num_tokens, # last token of embedding is used for masking mask_prob = mlm_mask_prob, keep_token_same_prob = mlm_keep_token_same_prob, random_replace_token_prob = mlm_random_replace_token_prob, exclude_token_ids = mlm_exclude_token_ids ) # calculate distogram logits self.to_distogram_logits = nn.Sequential( nn.LayerNorm(dim), nn.Linear(dim, constants.DISTOGRAM_BUCKETS) ) # to coordinate output self.predict_coords = predict_coords self.structure_module_depth = structure_module_depth self.msa_to_single_repr_dim = nn.Linear(dim, dim) self.trunk_to_pairwise_repr_dim = nn.Linear(dim, dim) with torch_default_dtype(torch.float32): self.ipa_block = IPABlock( dim = dim, heads = structure_module_heads, ) self.to_quaternion_update = nn.Linear(dim, 6) init_zero_(self.ipa_block.attn.to_out) self.to_points = nn.Linear(dim, 3) # aux confidence measure self.lddt_linear = nn.Linear(dim, 1) # recycling params self.recycling_msa_norm = nn.LayerNorm(dim) self.recycling_pairwise_norm = nn.LayerNorm(dim) self.recycling_distance_embed = nn.Embedding(recycling_distance_buckets, dim) self.recycling_distance_buckets = recycling_distance_buckets def forward( self, seq, msa = None, mask = None, msa_mask = None, extra_msa = None, extra_msa_mask = None, seq_index = None, seq_embed = None, msa_embed = None, templates_feats = None, templates_mask = None, templates_angles = None, embedds = None, recyclables = None, return_trunk = False, return_confidence = False, return_recyclables = False, return_aux_logits = False ): assert not (self.disable_token_embed and not exists(seq_embed)), 'sequence embedding must be supplied if one has disabled token embedding' assert not (self.disable_token_embed and not exists(msa_embed)), 'msa embedding must be supplied if one has disabled token embedding' # if MSA is not passed in, just use the sequence itself if not exists(msa): msa = rearrange(seq, 'b n -> b () n') msa_mask = rearrange(mask, 'b n -> b () n') # assert on sequence length assert msa.shape[-1] == seq.shape[-1], 'sequence length of MSA and primary sequence must be the same' # variables b, n, device = *seq.shape[:2], seq.device n_range = torch.arange(n, device = device) # unpack (AA_code, atom_pos) if isinstance(seq, (list, tuple)): seq, seq_pos = seq # embed main sequence x = self.token_emb(seq) if exists(seq_embed): x += seq_embed # mlm for MSAs if self.training and exists(msa): original_msa = msa msa_mask = default(msa_mask, lambda: torch.ones_like(msa).bool()) noised_msa, replaced_msa_mask = self.mlm.noise(msa, msa_mask) msa = noised_msa # embed multiple sequence alignment (msa) if exists(msa): m = self.token_emb(msa) if exists(msa_embed): m = m + msa_embed # add single representation to msa representation m = m + rearrange(x, 'b n d -> b () n d') # get msa_mask to all ones if none was passed msa_mask = default(msa_mask, lambda: torch.ones_like(msa).bool()) elif exists(embedds): m = self.embedd_project(embedds) # get msa_mask to all ones if none was passed msa_mask = default(msa_mask, lambda: torch.ones_like(embedds[..., -1]).bool()) else: raise Error('either MSA or embeds must be given') # derive pairwise representation x_left, x_right = self.to_pairwise_repr(x).chunk(2, dim = -1) x = rearrange(x_left, 'b i d -> b i () d') + rearrange(x_right, 'b j d-> b () j d') # create pair-wise residue embeds x_mask = rearrange(mask, 'b i -> b i ()') * rearrange(mask, 'b j -> b () j') if exists(mask) else None # add relative positional embedding seq_index = default(seq_index, lambda: torch.arange(n, device = device)) seq_rel_dist = rearrange(seq_index, 'i -> () i ()') - rearrange(seq_index, 'j -> () () j') seq_rel_dist = seq_rel_dist.clamp(-self.max_rel_dist, self.max_rel_dist) + self.max_rel_dist rel_pos_emb = self.pos_emb(seq_rel_dist) x = x + rel_pos_emb # add recyclables, if present if exists(recyclables): m[:, 0] = m[:, 0] + self.recycling_msa_norm(recyclables.single_msa_repr_row) x = x + self.recycling_pairwise_norm(recyclables.pairwise_repr) distances = torch.cdist(recyclables.coords, recyclables.coords, p=2) boundaries = torch.linspace(2, 20, steps = self.recycling_distance_buckets, device = device) discretized_distances = torch.bucketize(distances, boundaries[:-1]) distance_embed = self.recycling_distance_embed(discretized_distances) x = x + distance_embed # embed templates, if present if exists(templates_feats): _, num_templates, *_ = templates_feats.shape # embed template t = self.to_template_embed(templates_feats) t_mask_crossed = rearrange(templates_mask, 'b t i -> b t i ()') * rearrange(templates_mask, 'b t j -> b t () j') t = rearrange(t, 'b t ... -> (b t) ...') t_mask_crossed = rearrange(t_mask_crossed, 'b t ... -> (b t) ...') for _ in range(self.templates_embed_layers): t = self.template_pairwise_embedder(t, mask = t_mask_crossed) t = rearrange(t, '(b t) ... -> b t ...', t = num_templates) t_mask_crossed = rearrange(t_mask_crossed, '(b t) ... -> b t ...', t = num_templates) # template pos emb x_point = rearrange(x, 'b i j d -> (b i j) () d') t_point = rearrange(t, 'b t i j d -> (b i j) t d') x_mask_point = rearrange(x_mask, 'b i j -> (b i j) ()') t_mask_point = rearrange(t_mask_crossed, 'b t i j -> (b i j) t') template_pooled = self.template_pointwise_attn( x_point, context = t_point, mask = x_mask_point, context_mask = t_mask_point ) template_pooled_mask = rearrange(t_mask_point.sum(dim = -1) > 0, 'b -> b () ()') template_pooled = template_pooled * template_pooled_mask template_pooled = rearrange(template_pooled, '(b i j) () d -> b i j d', i = n, j = n) x = x + template_pooled # add template angle features to MSAs by passing through MLP and then concat if exists(templates_angles): t_angle_feats = self.template_angle_mlp(templates_angles) m = torch.cat((m, t_angle_feats), dim = 1) msa_mask = torch.cat((msa_mask, templates_mask), dim = 1) # embed extra msa, if present if exists(extra_msa): extra_m = self.token_emb(msa) extra_msa_mask = default(extra_msa_mask, torch.ones_like(extra_m).bool()) x, extra_m = self.extra_msa_evoformer( x, extra_m, mask = x_mask, msa_mask = extra_msa_mask ) # trunk x, m = self.net( x, m, mask = x_mask, msa_mask = msa_mask ) # ready output container ret = ReturnValues() # calculate theta and phi before symmetrization if self.predict_angles: ret.theta_logits = self.to_prob_theta(x) ret.phi_logits = self.to_prob_phi(x) # embeds to distogram trunk_embeds = (x + rearrange(x, 'b i j d -> b j i d')) * 0.5 # symmetrize distance_pred = self.to_distogram_logits(trunk_embeds) ret.distance = distance_pred # calculate mlm loss, if training msa_mlm_loss = None if self.training and exists(msa): num_msa = original_msa.shape[1] msa_mlm_loss = self.mlm(m[:, :num_msa], original_msa, replaced_msa_mask) # determine angles, if specified if self.predict_angles: omega_input = trunk_embeds if self.symmetrize_omega else x ret.omega_logits = self.to_prob_omega(omega_input) if not self.predict_coords or return_trunk: return ret # derive single and pairwise embeddings for structural refinement single_msa_repr_row = m[:, 0] single_repr = self.msa_to_single_repr_dim(single_msa_repr_row) pairwise_repr = self.trunk_to_pairwise_repr_dim(x) # prepare float32 precision for equivariance original_dtype = single_repr.dtype single_repr, pairwise_repr = map(lambda t: t.float(), (single_repr, pairwise_repr)) # iterative refinement with equivariant transformer in high precision with torch_default_dtype(torch.float32): quaternions = torch.tensor([1., 0., 0., 0.], device = device) # initial rotations quaternions = repeat(quaternions, 'd -> b n d', b = b, n = n) translations = torch.zeros((b, n, 3), device = device) # go through the layers and apply invariant point attention and feedforward for i in range(self.structure_module_depth): is_last = i == (self.structure_module_depth - 1) # the detach comes from # https://github.com/deepmind/alphafold/blob/0bab1bf84d9d887aba5cfb6d09af1e8c3ecbc408/alphafold/model/folding.py#L383 rotations = quaternion_to_matrix(quaternions) if not is_last: rotations = rotations.detach() single_repr = self.ipa_block( single_repr, mask = mask, pairwise_repr = pairwise_repr, rotations = rotations, translations = translations ) # update quaternion and translation quaternion_update, translation_update = self.to_quaternion_update(single_repr).chunk(2, dim = -1) quaternion_update = F.pad(quaternion_update, (1, 0), value = 1.) quaternions = quaternion_multiply(quaternions, quaternion_update) translations = translations + einsum('b n c, b n c r -> b n r', translation_update, rotations) points_local = self.to_points(single_repr) rotations = quaternion_to_matrix(quaternions) coords = einsum('b n c, b n c d -> b n d', points_local, rotations) + translations coords.type(original_dtype) if return_recyclables: coords, single_msa_repr_row, pairwise_repr = map(torch.detach, (coords, single_msa_repr_row, pairwise_repr)) ret.recyclables = Recyclables(coords, single_msa_repr_row, pairwise_repr) if return_aux_logits: return coords, ret if return_confidence: return coords, self.lddt_linear(single_repr.float()) return coords
class ProtTranEmbedWrapper(nn.Module): def __init__(self, *, alphafold2): super().__init__() from transformers import AutoTokenizer, AutoModel self.alphafold2 = alphafold2 self.project_embed = nn.Linear(PROTTRAN_EMBED_DIM, alphafold2.dim) self.tokenizer = AutoTokenizer.from_pretrained('Rostlab/prot_bert', do_lower_case=False) self.model = AutoModel.from_pretrained('Rostlab/prot_bert') def forward(self, seq, msa, msa_mask = None, **kwargs): device = seq.device num_msa = msa.shape[1] msa_flat = rearrange(msa, 'b m n -> (b m) n') seq_embed = get_prottran_embedd(seq, self.model, self.tokenizer, device = device) msa_embed = get_prottran_embedd(msa_flat, self.model, self.tokenizer, device = device) seq_embed, msa_embed = map(self.project_embed, (seq_embed, msa_embed)) msa_embed = rearrange(msa_embed, '(b m) n d -> b m n d', m = num_msa) return self.alphafold2(seq, msa, seq_embed = seq_embed, msa_embed = msa_embed, msa_mask = msa_mask, **kwargs) class MSAEmbedWrapper(nn.Module): def __init__(self, *, alphafold2): super().__init__() self.alphafold2 = alphafold2 model, alphabet = torch.hub.load(*MSA_MODEL_PATH) batch_converter = alphabet.get_batch_converter() self.model = model self.batch_converter = batch_converter self.project_embed = nn.Linear(MSA_EMBED_DIM, alphafold2.dim) if MSA_EMBED_DIM != alphafold2.dim else nn.Identity() def forward(self, seq, msa, msa_mask = None, **kwargs): assert seq.shape[-1] == msa.shape[-1], 'sequence and msa must have the same length if you wish to use MSA transformer embeddings' model, batch_converter, device = self.model, self.batch_converter, seq.device seq_and_msa = torch.cat((seq.unsqueeze(1), msa), dim = 1) if exists(msa_mask): # in the event that there are rows in the MSA that are completely padding # process each batch element individually, so that padding isn't processed # with row-tied attention num_msa = msa_mask.any(dim = -1).sum(dim = -1).tolist() seq_and_msa_list = seq_and_msa.unbind(dim = 0) num_rows = seq_and_msa.shape[1] embeds = [] for num, batch_el in zip(num_msa, seq_and_msa_list): batch_el = rearrange(batch_el, '... -> () ...') batch_el = batch_el[:, :num] embed = get_msa_embedd(batch_el, model, batch_converter, device = device) embed = F.pad(embed, (0, 0, 0, 0, 0, num_rows - num), value = 0.) embeds.append(embed) embeds = torch.cat(embeds, dim = 0) else: embeds = get_msa_embedd(seq_and_msa, model, batch_converter, device = device) embeds = self.project_embed(embeds) seq_embed, msa_embed = embeds[:, 0], embeds[:, 1:] return self.alphafold2(seq, msa, seq_embed = seq_embed, msa_embed = msa_embed, msa_mask = msa_mask, **kwargs) class ESMEmbedWrapper(nn.Module): def __init__(self, *, alphafold2): super().__init__() self.alphafold2 = alphafold2 model, alphabet = torch.hub.load(*ESM_MODEL_PATH) batch_converter = alphabet.get_batch_converter() self.model = model self.batch_converter = batch_converter self.project_embed = nn.Linear(ESM_EMBED_DIM, alphafold2.dim) if ESM_EMBED_DIM != alphafold2.dim else nn.Identity() def forward(self, seq, msa=None, **kwargs): model, batch_converter, device = self.model, self.batch_converter, seq.device seq_embeds = get_esm_embedd(seq, model, batch_converter, device = device) seq_embeds = self.project_embed(seq_embeds) if msa is not None: flat_msa = rearrange(msa, 'b m n -> (b m) n') msa_embeds = get_esm_embedd(flat_msa, model, batch_converter, device = device) msa_embeds = rearrange(msa_embeds, '(b m) n d -> b m n d') msa_embeds = self.project_embed(msa_embeds) else: msa_embeds = None return self.alphafold2(seq, msa, seq_embed = seq_embeds, msa_embed = msa_embeds, **kwargs)
# rotary embedding helpers def rotate_every_two(x): x = rearrange(x, '... (d j) -> ... d j', j = 2) x1, x2 = x.unbind(dim = -1) x = torch.stack((-x2, x1), dim = -1) return rearrange(x, '... d j -> ... (d j)') def apply_rotary_pos_emb(x, sinu_pos): sin, cos = map(lambda t: rearrange(t, 'b ... -> b () ...'), sinu_pos) rot_dim = sin.shape[-1] x, x_pass = x[..., :rot_dim], x[..., rot_dim:] x = x * cos + rotate_every_two(x) * sin return torch.cat((x, x_pass), dim = -1) # positional embeddings class DepthWiseConv1d(nn.Module): def __init__(self, dim_in, dim_out, kernel_size, padding = 0, stride = 1, bias = True, groups = None): super().__init__() groups = default(groups, dim_in) self.net = nn.Sequential( nn.Conv1d(dim_in, dim_in, kernel_size = kernel_size, padding = padding, groups = groups, stride = stride, bias = bias), nn.Conv1d(dim_in, dim_out, 1, bias = bias) ) def forward(self, x): return self.net(x) class FixedPositionalEmbedding(nn.Module): def __init__(self, dim): super().__init__() inv_freq = 1. / (10000 ** (torch.arange(0, dim, 2).float() / dim)) self.register_buffer('inv_freq', inv_freq) def forward(self, n, device): seq = torch.arange(n, device = device).type_as(self.inv_freq) freqs = einsum('i , j -> i j', seq, self.inv_freq) freqs = repeat(freqs, 'i j -> () i (j r)', r = 2) return [freqs.sin(), freqs.cos()] class AxialRotaryEmbedding(nn.Module): def __init__(self, dim, max_freq = 10): super().__init__() self.dim = dim // 2 inv_freq = 1. / (10000 ** (torch.arange(0, self.dim, 2).float() / self.dim)) self.register_buffer('inv_freq', inv_freq) def forward(self, n, device): seq = torch.arange(n, device = device).type_as(self.inv_freq) x = einsum('n, d -> n d', seq, self.inv_freq) y = einsum('n, d -> n d', seq, self.inv_freq) x_sinu = repeat(x, 'i d -> i j d', j = n) y_sinu = repeat(y, 'j d -> i j d', i = n) sin = torch.cat((x_sinu.sin(), y_sinu.sin()), dim = -1) cos = torch.cat((x_sinu.cos(), y_sinu.cos()), dim = -1) sin, cos = map(lambda t: repeat(t, 'i j d -> () (i j) (d r)', r = 2), (sin, cos)) return [sin, cos]
def test_mat_to_masked(): # nodes x = torch.ones(19, 3) x_mask = torch.randn(19) > -0.3 # edges edges_mat = torch.randn(19, 19) < 1 edges = torch.nonzero(edges_mat, as_tuple=False).t() # test normal edges / nodes cleaned = mat_input_to_masked(x, x_mask, edges=edges) cleaned_2 = mat_input_to_masked(x, x_mask, edges_mat=edges_mat) # test batch dimension x_ = torch.stack([x]*2, dim=0) x_mask_ = torch.stack([x_mask]*2, dim=0) edges_mat_ = torch.stack([edges_mat]*2, dim=0) cleaned_3 = mat_input_to_masked(x_, x_mask_, edges_mat=edges_mat_) assert True def test_center_distogram_median(): distogram = torch.randn(1, 128, 128, 37) distances, weights = center_distogram_torch(distogram, center = 'median') assert True def test_masks(): seqs = torch.randint(20, size=(2, 50)) # cloud point mask - can't test bc it needs sidechainnet installed # cloud_masks = scn_cloud_mask(seqs, boolean=True) # atom masking N_mask, CA_mask, C_mask = scn_backbone_mask(seqs, boolean = True) assert True def test_mds_and_mirrors(): distogram = torch.randn(2, 32*3, 32*3, 37) distances, weights = center_distogram_torch(distogram) # set out some points (due to padding) paddings = [7,0] for i,pad in enumerate(paddings): if pad > 0: weights[i, -pad:, -pad:] = 0. # masks masker = torch.arange(distogram.shape[1]) % 3 N_mask = (masker==0).bool() CA_mask = (masker==1).bool() coords_3d, _ = MDScaling(distances, weights = weights, iters = 5, fix_mirror = 2, N_mask = N_mask, CA_mask = CA_mask, C_mask = None ) assert list(coords_3d.shape) == [2, 3, 32*3], 'coordinates must be of the right shape after MDS' def test_sidechain_container(): seqs = torch.tensor([[0]*137, [3]*137]).long() bb = torch.randn(2, 137*4, 3) atom_mask = torch.tensor( [1]*4 + [0]*(14-4) ) proto_3d = sidechain_container(seqs, bb, atom_mask=atom_mask) assert list(proto_3d.shape) == [2, 137, 14, 3] def test_distmat_loss(): a = torch.randn(2, 137, 14, 3) b = torch.randn(2, 137, 14, 3) loss = distmat_loss_torch(a, b, p=2, q=2) # mse on distmat assert True def test_lddt(): a = torch.randn(2, 137, 14, 3) b = torch.randn(2, 137, 14, 3) cloud_mask = torch.ones(a.shape[:-1]).bool() lddt_result = lddt_ca_torch(a, b, cloud_mask) assert list(lddt_result.shape) == [2, 137] def test_kabsch(): a = torch.randn(3, 8) b = torch.randn(3, 8) a_, b_ = Kabsch(a,b) assert a.shape == a_.shape def test_tmscore(): a = torch.randn(2, 3, 8) b = torch.randn(2, 3, 8) out = TMscore(a, b) assert True def test_gdt(): a = torch.randn(1, 3, 8) b = torch.randn(1, 3, 8) GDT(a, b, weights = 1) assert True
def test_main(): model = Alphafold2( dim = 32, depth = 2, heads = 2, dim_head = 32 ) seq = torch.randint(0, 21, (2, 128)) msa = torch.randint(0, 21, (2, 5, 128)) mask = torch.ones_like(seq).bool() msa_mask = torch.ones_like(msa).bool() distogram = model( seq, msa, mask = mask, msa_mask = msa_mask ) assert True def test_no_msa(): model = Alphafold2( dim = 32, depth = 2, heads = 2, dim_head = 32 ) seq = torch.randint(0, 21, (2, 128)) mask = torch.ones_like(seq).bool() distogram = model( seq, mask = mask ) assert True def test_anglegrams(): model = Alphafold2( dim = 32, depth = 2, heads = 2, dim_head = 32, predict_angles = True ) seq = torch.randint(0, 21, (2, 128)) msa = torch.randint(0, 21, (2, 5, 128)) mask = torch.ones_like(seq).bool() msa_mask = torch.ones_like(msa).bool() ret = model( seq, msa, mask = mask, msa_mask = msa_mask ) assert True def test_templates(): model = Alphafold2( dim = 32, depth = 2, heads = 2, dim_head = 32, templates_dim = 32, templates_angles_feats_dim = 32 ) seq = torch.randint(0, 21, (2, 16)) mask = torch.ones_like(seq).bool() msa = torch.randint(0, 21, (2, 5, 16)) msa_mask = torch.ones_like(msa).bool() templates_feats = torch.randn(2, 3, 16, 16, 32) templates_angles = torch.randn(2, 3, 16, 32) templates_mask = torch.ones(2, 3, 16).bool() distogram = model( seq, msa, mask = mask, msa_mask = msa_mask, templates_feats = templates_feats, templates_angles = templates_angles, templates_mask = templates_mask ) assert True def test_extra_msa(): model = Alphafold2( dim = 128, depth = 2, heads = 2, dim_head = 32, predict_coords = True ) seq = torch.randint(0, 21, (2, 4)) mask = torch.ones_like(seq).bool() msa = torch.randint(0, 21, (2, 5, 4)) msa_mask = torch.ones_like(msa).bool() extra_msa = torch.randint(0, 21, (2, 5, 4)) extra_msa_mask = torch.ones_like(extra_msa).bool() coords = model( seq, msa, mask = mask, msa_mask = msa_mask, extra_msa = extra_msa, extra_msa_mask = extra_msa_mask ) assert True def test_embeddings(): model = Alphafold2( dim = 32, depth = 2, heads = 2, dim_head = 32 ) seq = torch.randint(0, 21, (2, 16)) mask = torch.ones_like(seq).bool() embedds = torch.randn(2, 1, 16, 1280) # without mask distogram = model( seq, mask = mask, embedds = embedds, msa_mask = None ) # with mask embedds_mask = torch.ones_like(embedds[..., -1]).bool() distogram = model( seq, mask = mask, embedds = embedds, msa_mask = embedds_mask ) assert True def test_coords(): model = Alphafold2( dim = 32, depth = 2, heads = 2, dim_head = 32, predict_coords = True, structure_module_depth = 1, structure_module_heads = 1, structure_module_dim_head = 1, ) seq = torch.randint(0, 21, (2, 16)) mask = torch.ones_like(seq).bool() msa = torch.randint(0, 21, (2, 5, 16)) msa_mask = torch.ones_like(msa).bool() coords = model( seq, msa, mask = mask, msa_mask = msa_mask ) assert coords.shape == (2, 16, 3), 'must output coordinates' def test_coords_backbone_with_cbeta(): model = Alphafold2( dim = 32, depth = 2, heads = 2, dim_head = 32, predict_coords = True, structure_module_depth = 1, structure_module_heads = 1, structure_module_dim_head = 1, ) seq = torch.randint(0, 21, (2, 16)) mask = torch.ones_like(seq).bool() msa = torch.randint(0, 21, (2, 5, 16)) msa_mask = torch.ones_like(msa).bool() coords = model( seq, msa, mask = mask, msa_mask = msa_mask ) assert coords.shape == (2, 16, 3), 'must output coordinates' def test_coords_all_atoms(): model = Alphafold2( dim = 32, depth = 2, heads = 2, dim_head = 32, predict_coords = True, structure_module_depth = 1, structure_module_heads = 1, structure_module_dim_head = 1, ) seq = torch.randint(0, 21, (2, 16)) mask = torch.ones_like(seq).bool() msa = torch.randint(0, 21, (2, 5, 16)) msa_mask = torch.ones_like(msa).bool() coords = model( seq, msa, mask = mask, msa_mask = msa_mask ) assert coords.shape == (2, 16, 3), 'must output coordinates' def test_mds(): model = Alphafold2( dim = 32, depth = 2, heads = 2, dim_head = 32, predict_coords = True, structure_module_depth = 1, structure_module_heads = 1, structure_module_dim_head = 1, ) seq = torch.randint(0, 21, (2, 16)) mask = torch.ones_like(seq).bool() msa = torch.randint(0, 21, (2, 5, 16)) msa_mask = torch.ones_like(msa).bool() coords = model( seq, msa, mask = mask, msa_mask = msa_mask ) assert coords.shape == (2, 16, 3), 'must output coordinates' def test_edges_to_equivariant_network(): model = Alphafold2( dim = 32, depth = 1, heads = 2, dim_head = 32, predict_coords = True, predict_angles = True ) seq = torch.randint(0, 21, (2, 32)) mask = torch.ones_like(seq).bool() msa = torch.randint(0, 21, (2, 5, 32)) msa_mask = torch.ones_like(msa).bool() coords, confidences = model( seq, msa, mask = mask, msa_mask = msa_mask, return_confidence = True ) assert True, 'should run without errors' def test_coords_backwards(): model = Alphafold2( dim = 256, depth = 2, heads = 2, dim_head = 32, predict_coords = True, structure_module_depth = 1, structure_module_heads = 1, structure_module_dim_head = 1, ) seq = torch.randint(0, 21, (2, 16)) mask = torch.ones_like(seq).bool() msa = torch.randint(0, 21, (2, 5, 16)) msa_mask = torch.ones_like(msa).bool() coords = model( seq, msa, mask = mask, msa_mask = msa_mask ) coords.sum().backward() assert True, 'must be able to go backwards through MDS and center distogram' def test_confidence(): model = Alphafold2( dim = 256, depth = 1, heads = 2, dim_head = 32, predict_coords = True ) seq = torch.randint(0, 21, (2, 16)) mask = torch.ones_like(seq).bool() msa = torch.randint(0, 21, (2, 5, 16)) msa_mask = torch.ones_like(msa).bool() coords, confidences = model( seq, msa, mask = mask, msa_mask = msa_mask, return_confidence = True ) assert coords.shape[:-1] == confidences.shape[:-1] def test_recycling(): model = Alphafold2( dim = 128, depth = 2, heads = 2, dim_head = 32, predict_coords = True, ) seq = torch.randint(0, 21, (2, 4)) mask = torch.ones_like(seq).bool() msa = torch.randint(0, 21, (2, 5, 4)) msa_mask = torch.ones_like(msa).bool() extra_msa = torch.randint(0, 21, (2, 5, 4)) extra_msa_mask = torch.ones_like(extra_msa).bool() coords, ret = model( seq, msa, mask = mask, msa_mask = msa_mask, extra_msa = extra_msa, extra_msa_mask = extra_msa_mask, return_aux_logits = True, return_recyclables = True ) coords, ret = model( seq, msa, mask = mask, msa_mask = msa_mask, extra_msa = extra_msa, extra_msa_mask = extra_msa_mask, recyclables = ret.recyclables, return_aux_logits = True, return_recyclables = True ) assert True
try: import pytorch_lightning as pl LightningDataModule = pl.LightningDataModule except ImportError: LightningDataModule = object CACHE_PATH = Path("~/.cache/alphafold2_pytorch").expanduser() DATA_DIR = CACHE_PATH / "trrosetta" / "trrosetta" URL = "http://s3.amazonaws.com/proteindata/data_pytorch/trrosetta.tar.gz" REMOVE_KEYS = dict.fromkeys(string.ascii_lowercase) REMOVE_KEYS["."] = None REMOVE_KEYS["*"] = None translation = str.maketrans(REMOVE_KEYS) DEFAULT_VOCAB = ProteinVocabulary() def default_tokenize(seq: str) -> List[int]: return [DEFAULT_VOCAB[ch] for ch in seq] def read_fasta(filename: str) -> List[Tuple[str, str]]: def remove_insertions(sequence: str) -> str: return sequence.translate(translation) return [ (record.description, remove_insertions(str(record.seq))) for record in SeqIO.parse(filename, "fasta") ] def read_pdb(pdb: str): ag = prody.parsePDB(pdb) for chain in ag.iterChains(): angles, coords, seq = get_seq_coords_and_angles(chain) return angles, coords, seq def download_file(url, filename=None, root=CACHE_PATH): import os import urllib root.mkdir(exist_ok=True, parents=True) filename = filename or os.path.basename(url) download_target = root / filename download_target_tmp = root / f"tmp.{filename}" if download_target.exists() and not download_target.is_file(): raise RuntimeError(f"{download_target} exists and is not a regular file") if download_target.is_file(): return download_target with urllib.request.urlopen(url) as source, open( download_target_tmp, "wb" ) as output: with tqdm(total=int(source.info().get("Content-Length")), ncols=80) as loop: while True: buffer = source.read(8192) if not buffer: break output.write(buffer) loop.update(len(buffer)) download_target_tmp.rename(download_target) return download_target def get_or_download(url: str = URL): """ download and extract trrosetta data """ import tarfile file = CACHE_PATH / "trrosetta.tar.gz" dir = CACHE_PATH / "trrosetta" dir_temp = CACHE_PATH / "trrosetta_tmp" if dir.is_dir(): print(f"Load cached data from {dir}") return dir if not file.is_file(): print(f"Cache not found, download from {url} to {file}") download_file(url) print(f"Extract data from {file} to {dir}") with tarfile.open(file, "r:gz") as tar: tar.extractall(dir_temp) dir_temp.rename(dir) return dir def pad_sequences(sequences, constant_value=0, dtype=None) -> np.ndarray: batch_size = len(sequences) shape = [batch_size] + np.max([seq.shape for seq in sequences], 0).tolist() if dtype is None: dtype = sequences[0].dtype if isinstance(sequences[0], np.ndarray): array = np.full(shape, constant_value, dtype=dtype) elif isinstance(sequences[0], torch.Tensor): array = torch.full(shape, constant_value, dtype=dtype) for arr, seq in zip(array, sequences): arrslice = tuple(slice(dim) for dim in seq.shape) arr[arrslice] = seq return array class TrRosettaDataset(Dataset): def __init__( self, data_dir: Path, list_path: Path, tokenize: Callable[[str], List[int]], seq_pad_value: int = 20, random_sample_msa: bool = False, max_seq_len: int = 300, max_msa_num: int = 300, overwrite: bool = False, ): self.data_dir = data_dir self.file_list: List[Path] = self.read_file_list(data_dir, list_path) self.tokenize = tokenize self.seq_pad_value = seq_pad_value self.random_sample_msa = random_sample_msa self.max_seq_len = max_seq_len self.max_msa_num = max_msa_num self.overwrite = overwrite def __len__(self) -> int: return len(self.file_list) def read_file_list(self, data_dir: Path, list_path: Path): file_glob = (data_dir / "npz").glob("*.npz") files = set(list_path.read_text().split()) if len(files) == 0: raise ValueError("Passed an empty split file set") file_list = [f for f in file_glob if f.name in files] if len(file_list) != len(files): num_missing = len(files) - len(file_list) raise FileNotFoundError( f"{num_missing} specified split files not found in directory" ) return file_list def has_cache(self, index): if self.overwrite: return False path = (self.data_dir / "cache" / self.file_list[index].stem).with_suffix( ".pkl" ) return path.is_file() def write_cache(self, index, data): path = (self.data_dir / "cache" / self.file_list[index].stem).with_suffix( ".pkl" ) path.parent.mkdir(exist_ok=True, parents=True) with open(path, "wb") as file: pickle.dump(data, file) def read_cache(self, index): path = (self.data_dir / "cache" / self.file_list[index].stem).with_suffix( ".pkl" ) with open(path, "rb") as file: return pickle.load(file) def __getitem__(self, index): if self.has_cache(index): item = self.read_cache(index) else: id = self.file_list[index].stem pdb_path = self.data_dir / "pdb" / f"{id}.pdb" msa_path = self.data_dir / "a3m" / f"{id}.a3m" _, msa = zip(*read_fasta(str(msa_path))) msa = np.array([np.array(list(seq)) for seq in msa]) angles, coords, seq = read_pdb(str(pdb_path)) seq = np.array(list(seq)) coords = coords.reshape((coords.shape[0] // 14, 14, 3)) dist = self.get_bucketed_distance(seq, coords, subset="ca") item = { "id": id, "seq": seq, "msa": msa, "coords": coords, "angles": angles, "dist": dist } self.write_cache(index, item) item["msa"] = self.sample(item["msa"], self.max_msa_num, self.random_sample_msa) item = self.crop(item, self.max_seq_len) return item def calc_cb(self, coord): N = coord[0] CA = coord[1] C = coord[2] b = CA - N c = C - CA a = np.cross(b, c) CB = -0.58273431 * a + 0.56802827 * b - 0.54067466 * c + CA return CB def get_bucketed_distance( self, seq, coords, subset="ca", start=2, bins=DISTOGRAM_BUCKETS-1, step=0.5 ): assert subset in ("ca", "cb") if subset == "ca": coords = coords[:, 1, :] elif subset == "cb": cb_coords = [] for res, coord in zip(seq, coords): if res == "G": cb = self.calc_cb(coord) cb_coords.append(cb) else: cb_coords.append(coord[4, :]) coords = np.array(cb_coords) vcs = coords + np.zeros([coords.shape[0]] + list(coords.shape)) vcs = vcs - np.swapaxes(vcs, 0, 1) distance_map = LA.norm(vcs, axis=2) mask = np.ones(distance_map.shape) - np.eye(distance_map.shape[0]) low_pos = np.where(distance_map < start) high_pos = np.where(distance_map >= start + step * bins) mask[low_pos] = 0 distance_map = (distance_map - start) // step distance_map[high_pos] = bins dist = (distance_map * mask).astype(int) return dist def crop(self, item, max_seq_len: int): seq_len = len(item["seq"]) if seq_len <= max_seq_len or max_seq_len <= 0: return item start = 0 end = start + max_seq_len item["seq"] = item["seq"][start:end] item["msa"] = item["msa"][:, start:end] item["coords"] = item["coords"][start:end] item["angles"] = item["angles"][start:end] item["dist"] = item["dist"][start:end, start:end] return item def sample(self, msa, max_msa_num: int, random: bool): num_msa, seq_len = len(msa), len(msa[0]) if num_msa <= max_msa_num or max_msa_num <= 0: return msa if random: num_sample = max_msa_num - 1 indices = np.random.choice(num_msa - 1, size=num_sample, replace=False) + 1 indices = np.pad(indices, [1, 0], "constant") return msa[indices] else: return msa[:max_msa_num] def collate_fn(self, batch): b = len(batch) batch = {k: [item[k] for item in batch] for k in batch[0]} id = batch["id"] seq = batch["seq"] msa = batch["msa"] coords = batch["coords"] angles = batch["angles"] dist = batch["dist"] lengths = torch.LongTensor([len(x[0]) for x in msa]) depths = torch.LongTensor([len(x) for x in msa]) max_len = lengths.max() max_depth = depths.max() seq = pad_sequences( [torch.LongTensor(self.tokenize(seq_)) for seq_ in seq], self.seq_pad_value, ) msa = pad_sequences( [torch.LongTensor([self.tokenize(seq_) for seq_ in msa_]) for msa_ in msa], self.seq_pad_value, ) coords = pad_sequences([torch.FloatTensor(x) for x in coords], 0.0) angles = pad_sequences([torch.FloatTensor(x) for x in angles], 0.0) dist = pad_sequences([torch.LongTensor(x) for x in dist], -100) mask = repeat(torch.arange(max_len), "l -> b l", b=b) < repeat( lengths, "b -> b l", l=max_len ) msa_seq_mask = repeat( torch.arange(max_len), "l -> b s l", b=b, s=max_depth ) < repeat(lengths, "b -> b s l", s=max_depth, l=max_len) msa_depth_mask = repeat( torch.arange(max_depth), "s -> b s l", b=b, l=max_len ) < repeat(depths, "b -> b s l", s=max_depth, l=max_len) msa_mask = msa_seq_mask & msa_depth_mask return { "id": id, "seq": seq, "msa": msa, "coords": coords, "angles": angles, "mask": mask, "msa_mask": msa_mask, "dist": dist, } class TrRosettaDataModule(LightningDataModule): @staticmethod def add_data_specific_args(parent_parser): parser = ArgumentParser(parents=[parent_parser], add_help=False) parser.add_argument("--data_dir", type=str, default=str(DATA_DIR)) parser.add_argument("--train_batch_size", type=int, default=1) parser.add_argument("--eval_batch_size", type=int, default=1) parser.add_argument("--test_batch_size", type=int, default=1) parser.add_argument("--num_workers", type=int, default=0) parser.add_argument("--train_max_seq_len", type=int, default=256) parser.add_argument("--eval_max_seq_len", type=int, default=256) parser.add_argument("--test_max_seq_len", type=int, default=-1) parser.add_argument("--train_max_msa_num", type=int, default=256) parser.add_argument("--eval_max_msa_num", type=int, default=256) parser.add_argument("--test_max_msa_num", type=int, default=1000) parser.add_argument("--overwrite", dest="overwrite", action="store_true") return parser def __init__( self, data_dir: str = DATA_DIR, train_batch_size: int = 1, eval_batch_size: int = 1, test_batch_size: int = 1, num_workers: int = 0, train_max_seq_len: int = 256, eval_max_seq_len: int = 256, test_max_seq_len: int = -1, train_max_msa_num: int = 32, eval_max_msa_num: int = 32, test_max_msa_num: int = 64, tokenize: Callable[[str], List[int]] = default_tokenize, seq_pad_value: int = 20, overwrite: bool = False, **kwargs, ): super(TrRosettaDataModule, self).__init__() self.data_dir = Path(data_dir).expanduser().resolve() self.train_batch_size = train_batch_size self.eval_batch_size = eval_batch_size self.test_batch_size = test_batch_size self.num_workers = num_workers self.train_max_seq_len = train_max_seq_len self.eval_max_seq_len = eval_max_seq_len self.test_max_seq_len = test_max_seq_len self.train_max_msa_num = train_max_msa_num self.eval_max_msa_num = eval_max_msa_num self.test_max_msa_num = test_max_msa_num self.tokenize = tokenize self.seq_pad_value = seq_pad_value self.overwrite = overwrite get_or_download() def setup(self, stage: Optional[str] = None): self.train = TrRosettaDataset( self.data_dir, self.data_dir / "train_files.txt", self.tokenize, self.seq_pad_value, random_sample_msa=True, max_seq_len=self.train_max_seq_len, max_msa_num=self.train_max_msa_num, overwrite=self.overwrite, ) self.val = TrRosettaDataset( self.data_dir, self.data_dir / "valid_files.txt", self.tokenize, self.seq_pad_value, random_sample_msa=False, max_seq_len=self.eval_max_seq_len, max_msa_num=self.eval_max_msa_num, overwrite=self.overwrite, ) self.test = TrRosettaDataset( self.data_dir, self.data_dir / "valid_files.txt", self.tokenize, self.seq_pad_value, random_sample_msa=False, max_seq_len=self.test_max_seq_len, max_msa_num=self.test_max_msa_num, overwrite=self.overwrite, ) def train_dataloader(self, *args, **kwargs) -> DataLoader: return DataLoader( self.train, batch_size=self.train_batch_size, shuffle=True, collate_fn=self.train.collate_fn, num_workers=self.num_workers, ) def val_dataloader(self, *args, **kwargs) -> Union[DataLoader, List[DataLoader]]: return DataLoader( self.val, batch_size=self.eval_batch_size, shuffle=False, collate_fn=self.val.collate_fn, num_workers=self.num_workers, ) def test_dataloader(self, *args, **kwargs) -> Union[DataLoader, List[DataLoader]]: return DataLoader( self.test, batch_size=self.test_batch_size, shuffle=False, collate_fn=self.test.collate_fn, num_workers=self.num_workers, ) def test(): dm = TrRosettaDataModule(train_batch_size=1, num_workers=4) dm.setup() for batch in dm.train_dataloader(): print("id", batch["id"]) print("seq", batch["seq"].shape, batch["seq"]) print("msa", batch["msa"].shape, batch["msa"][..., :20]) print("msa", batch["msa"].shape, batch["msa"][..., -20:]) print("coords", batch["coords"].shape) print("angles", batch["angles"].shape) print("mask", batch["mask"].shape) print("msa_mask", batch["msa_mask"].shape) print("dist", batch["dist"].shape, batch["dist"]) break if __name__ == "__main__": test()
# will use FastRelax routine to refine structure # science # pyrosetta installation instructs in readme try: import pyrosetta except ModuleNotFoundError: msg = "Unable to find an existing installation of the PyRosetta module. " +\ "Functions involving this module such as the FastRelax pipeline " +\ "will not work." warnings.warn(msg) # no pyRosetta was found ##################### ### ROSETTA STUFF ### ##################### def pdb2rosetta(route): """ Takes pdb file route(s) as input and returns rosetta pose(s). Input: * route: list or string. Output: list of 1 or many according to input """ if isinstance(route, str): return [pyrosetta.io.pose_from_pdb(route)] else: return list(pyrosetta.io.poses_from_files(route)) def rosetta2pdb(pose, route, verbose=True): """ Takes pose(s) as input and saves pdb(s) to disk. Input: * pose: list or string. rosetta poses object(s). * route: list or string. destin filenames to be written. * verbose: bool. warns if lengths dont match and @ every write. Inspo: * https://www.rosettacommons.org/demos/latest/tutorials/input_and_output/input_and_output#controlling-output_common-structure-output-files_pdb-file * https://graylab.jhu.edu/PyRosetta.documentation/pyrosetta.rosetta.core.io.pdb.html#pyrosetta.rosetta.core.io.pdb.dump_pdb """ # convert to list pose = [pose] if isinstance(pose, str) else pose route = [route] if isinstance(route, str) else route # check lengths and warn if necessary if verbose and ( len(pose) != len(route) ): print("Length of pose and route are not the same. Will stop at the minimum.") # convert and save for i,pos in enumerate(pose): pyrosetta.rosetta.core.io.pdb.dump_pdb(pos, route[i]) if verbose: print("Saved structure @ "+route) return def run_fast_relax(config_route, pdb_route=None, pose=None): """ Runs the Fast-Relax pipeline. * config_route: route to json file with config * pose: rosetta pose to run the pipeline on Output: rosetta pose """ # load rosetta pose - if string or list is passed, convert to pose + recall if isinstance(pdb_route, str): pose = pdb2rosetta(pdb_route) return run_fast_relax(config, pose=pose) elif isinstance(pdb_route, list): return [run_fast_relax(config, pdb_route=pdb) for pdb in pdb_route] # load config: config = json.load(config_route) # run fast relax pipeline - examples: # https://colab.research.google.com/github/RosettaCommons/PyRosetta.notebooks/blob/master/notebooks/06.02-Packing-design-and-regional-relax.ipynb#scrollTo=PYr025Rn1Q8i # https://nbviewer.jupyter.org/github/RosettaCommons/PyRosetta.notebooks/blob/master/notebooks/06.03-Design-with-a-resfile-and-relax.ipynb # https://faculty.washington.edu/dimaio/files/demo2.py raise NotImplementedError("Last step. Not implemented yet.")
# following example for saving and setting rng here https://pytorch.org/docs/stable/_modules/torch/utils/checkpoint.html class Deterministic(nn.Module): def __init__(self, net): super().__init__() self.net = net self.cpu_state = None self.cuda_in_fwd = None self.gpu_devices = None self.gpu_states = None def record_rng(self, *args): self.cpu_state = torch.get_rng_state() if torch.cuda._initialized: self.cuda_in_fwd = True self.gpu_devices, self.gpu_states = get_device_states(*args) def forward(self, *args, record_rng = False, set_rng = False, **kwargs): if record_rng: self.record_rng(*args) if not set_rng: return self.net(*args, **kwargs) rng_devices = [] if self.cuda_in_fwd: rng_devices = self.gpu_devices with torch.random.fork_rng(devices=rng_devices, enabled=True): torch.set_rng_state(self.cpu_state) if self.cuda_in_fwd: set_device_states(self.gpu_devices, self.gpu_states) return self.net(*args, **kwargs) # heavily inspired by https://github.com/RobinBruegger/RevTorch/blob/master/revtorch/revtorch.py # once multi-GPU is confirmed working, refactor and send PR back to source class ReversibleBlock(nn.Module): def __init__(self, f, g): super().__init__() self.f = Deterministic(f) self.g = Deterministic(g) def forward(self, x, f_args = {}, g_args = {}): x1, x2 = torch.chunk(x, 2, dim = 1) y1, y2 = None, None with torch.no_grad(): y1 = x1 + self.f(x2, record_rng=self.training, **f_args) y2 = x2 + self.g(y1, record_rng=self.training, **g_args) return torch.cat([y1, y2], dim = 1) def backward_pass(self, y, dy, f_args = {}, g_args = {}): y1, y2 = torch.chunk(y, 2, dim = 1) del y dy1, dy2 = torch.chunk(dy, 2, dim = 1) del dy with torch.enable_grad(): y1.requires_grad = True gy1 = self.g(y1, set_rng=True, **g_args) torch.autograd.backward(gy1, dy2) with torch.no_grad(): x2 = y2 - gy1 del y2, gy1 dx1 = dy1 + y1.grad del dy1 y1.grad = None with torch.enable_grad(): x2.requires_grad = True fx2 = self.f(x2, set_rng=True, **f_args) torch.autograd.backward(fx2, dx1, retain_graph=True) with torch.no_grad(): x1 = y1 - fx2 del y1, fx2 dx2 = dy2 + x2.grad del dy2 x2.grad = None x = torch.cat([x1, x2.detach()], dim = 1) dx = torch.cat([dx1, dx2], dim = 1) return x, dx class IrreversibleBlock(nn.Module): def __init__(self, f, g): super().__init__() self.f = f self.g = g def forward(self, x, f_args, g_args): x1, x2 = torch.chunk(x, 2, dim = 1) y1 = x1 + self.f(x2, **f_args) y2 = x2 + self.g(y1, **g_args) return torch.cat([y1, y2], dim = 1) class _ReversibleFunction(Function): @staticmethod def forward(ctx, x, blocks, kwargs): ctx.kwargs = kwargs for block in blocks: x = block(x, **kwargs) ctx.y = x.detach() ctx.blocks = blocks return x @staticmethod def backward(ctx, dy): y = ctx.y kwargs = ctx.kwargs for block in ctx.blocks[::-1]: y, dy = block.backward_pass(y, dy, **kwargs) return dy, None, None class ReversibleSequence(nn.Module): def __init__(self, blocks, ): super().__init__() self.blocks = nn.ModuleList([ReversibleBlock(f, g) for (f, g) in blocks]) def forward(self, x, arg_route = (True, True), **kwargs): f_args, g_args = map(lambda route: kwargs if route else {}, arg_route) block_kwargs = {'f_args': f_args, 'g_args': g_args} x = torch.cat((x, x), dim = 1) x = _ReversibleFunction.apply(x, self.blocks, block_kwargs) return torch.stack(x.chunk(2, dim = 1)).mean(dim = 0)
# helper functions def exists(val): return val is not None def map_el_ind(arr, ind): return list(map(itemgetter(ind), arr)) def sort_and_return_indices(arr): indices = [ind for ind in range(len(arr))] arr = zip(arr, indices) arr = sorted(arr) return map_el_ind(arr, 0), map_el_ind(arr, 1) # calculates the permutation to bring the input tensor to something attend-able # also calculates the inverse permutation to bring the tensor back to its original shape def calculate_permutations(num_dimensions, emb_dim): total_dimensions = num_dimensions + 2 emb_dim = emb_dim if emb_dim > 0 else (emb_dim + total_dimensions) axial_dims = [ind for ind in range(1, total_dimensions) if ind != emb_dim] permutations = [] for axial_dim in axial_dims: last_two_dims = [axial_dim, emb_dim] dims_rest = set(range(0, total_dimensions)) - set(last_two_dims) permutation = [*dims_rest, *last_two_dims] permutations.append(permutation) return permutations # helper classes class ChanLayerNorm(nn.Module): def __init__(self, dim, eps = 1e-5): super().__init__() self.eps = eps self.g = nn.Parameter(torch.ones(1, dim, 1, 1)) self.b = nn.Parameter(torch.zeros(1, dim, 1, 1)) def forward(self, x): std = torch.var(x, dim = 1, unbiased = False, keepdim = True).sqrt() mean = torch.mean(x, dim = 1, keepdim = True) return (x - mean) / (std + self.eps) * self.g + self.b class PreNorm(nn.Module): def __init__(self, dim, fn): super().__init__() self.fn = fn self.norm = nn.LayerNorm(dim) def forward(self, x): x = self.norm(x) return self.fn(x) class Sequential(nn.Module): def __init__(self, blocks): super().__init__() self.blocks = blocks def forward(self, x): for f, g in self.blocks: x = x + f(x) x = x + g(x) return x class PermuteToFrom(nn.Module): def __init__(self, permutation, fn): super().__init__() self.fn = fn _, inv_permutation = sort_and_return_indices(permutation) self.permutation = permutation self.inv_permutation = inv_permutation def forward(self, x, **kwargs): axial = x.permute(*self.permutation).contiguous() shape = axial.shape *_, t, d = shape # merge all but axial dimension axial = axial.reshape(-1, t, d) # attention axial = self.fn(axial, **kwargs) # restore to original shape and permutation axial = axial.reshape(*shape) axial = axial.permute(*self.inv_permutation).contiguous() return axial # axial pos emb class AxialPositionalEmbedding(nn.Module): def __init__(self, dim, shape, emb_dim_index = 1): super().__init__() parameters = [] total_dimensions = len(shape) + 2 ax_dim_indexes = [i for i in range(1, total_dimensions) if i != emb_dim_index] self.num_axials = len(shape) for i, (axial_dim, axial_dim_index) in enumerate(zip(shape, ax_dim_indexes)): shape = [1] * total_dimensions shape[emb_dim_index] = dim shape[axial_dim_index] = axial_dim parameter = nn.Parameter(torch.randn(*shape)) setattr(self, f'param_{i}', parameter) def forward(self, x): for i in range(self.num_axials): x = x + getattr(self, f'param_{i}') return x # attention class SelfAttention(nn.Module): def __init__(self, dim, heads, dim_heads = None): super().__init__() self.dim_heads = (dim // heads) if dim_heads is None else dim_heads dim_hidden = self.dim_heads * heads self.heads = heads self.to_q = nn.Linear(dim, dim_hidden, bias = False) self.to_kv = nn.Linear(dim, 2 * dim_hidden, bias = False) self.to_out = nn.Linear(dim_hidden, dim) def forward(self, x, kv = None): kv = x if kv is None else kv q, k, v = (self.to_q(x), *self.to_kv(kv).chunk(2, dim=-1)) b, t, d, h, e = *q.shape, self.heads, self.dim_heads merge_heads = lambda x: x.reshape(b, -1, h, e).transpose(1, 2).reshape(b * h, -1, e) q, k, v = map(merge_heads, (q, k, v)) dots = torch.einsum('bie,bje->bij', q, k) * (e ** -0.5) dots = dots.softmax(dim=-1) out = torch.einsum('bij,bje->bie', dots, v) out = out.reshape(b, h, -1, e).transpose(1, 2).reshape(b, -1, d) out = self.to_out(out) return out # axial attention class class AxialAttention(nn.Module): def __init__(self, dim, num_dimensions = 2, heads = 8, dim_heads = None, dim_index = -1, sum_axial_out = True): assert (dim % heads) == 0, 'hidden dimension must be divisible by number of heads' super().__init__() self.dim = dim self.total_dimensions = num_dimensions + 2 self.dim_index = dim_index if dim_index > 0 else (dim_index + self.total_dimensions) attentions = [] for permutation in calculate_permutations(num_dimensions, dim_index): attentions.append(PermuteToFrom(permutation, SelfAttention(dim, heads, dim_heads))) self.axial_attentions = nn.ModuleList(attentions) self.sum_axial_out = sum_axial_out def forward(self, x): assert len(x.shape) == self.total_dimensions, 'input tensor does not have the correct number of dimensions' assert x.shape[self.dim_index] == self.dim, 'input tensor does not have the correct input dimension' if self.sum_axial_out: return sum(map(lambda axial_attn: axial_attn(x), self.axial_attentions)) out = x for axial_attn in self.axial_attentions: out = axial_attn(out) return out # axial image transformer class AxialImageTransformer(nn.Module): def __init__(self, dim, depth, heads = 8, dim_heads = None, dim_index = 1, reversible = True, axial_pos_emb_shape = None): super().__init__() permutations = calculate_permutations(2, dim_index) get_ff = lambda: nn.Sequential( ChanLayerNorm(dim), nn.Conv2d(dim, dim * 4, 3, padding = 1), nn.LeakyReLU(inplace=True), nn.Conv2d(dim * 4, dim, 3, padding = 1) ) self.pos_emb = AxialPositionalEmbedding(dim, axial_pos_emb_shape, dim_index) if exists(axial_pos_emb_shape) else nn.Identity() layers = nn.ModuleList([]) for _ in range(depth): attn_functions = nn.ModuleList([PermuteToFrom(permutation, PreNorm(dim, SelfAttention(dim, heads, dim_heads))) for permutation in permutations]) conv_functions = nn.ModuleList([get_ff(), get_ff()]) layers.append(attn_functions) layers.append(conv_functions) execute_type = ReversibleSequence if reversible else Sequential self.layers = execute_type(layers) def forward(self, x): x = self.pos_emb(x) return self.layers(x)
# constants BITS = 8 # helpers functions def exists(x): return x is not None def default(val, d): if exists(val): return val return d() if callable(d) else d def cycle(dl): while True: for data in dl: yield data def has_int_squareroot(num): return (math.sqrt(num) ** 2) == num def num_to_groups(num, divisor): groups = num // divisor remainder = num % divisor arr = [divisor] * groups if remainder > 0: arr.append(remainder) return arr def convert_image_to(pil_img_type, image): if image.mode != pil_img_type: return image.convert(pil_img_type) return image # small helper modules class Residual(nn.Module): def __init__(self, fn): super().__init__() self.fn = fn def forward(self, x, *args, **kwargs): return self.fn(x, *args, **kwargs) + x def Upsample(dim, dim_out = None): return nn.Sequential( nn.Upsample(scale_factor = 2, mode = 'nearest'), nn.Conv2d(dim, default(dim_out, dim), 3, padding = 1) ) def Downsample(dim, dim_out = None): return nn.Conv2d(dim, default(dim_out, dim), 4, 2, 1) class LayerNorm(nn.Module): def __init__(self, dim): super().__init__() self.g = nn.Parameter(torch.ones(1, dim, 1, 1)) def forward(self, x): eps = 1e-5 if x.dtype == torch.float32 else 1e-3 var = torch.var(x, dim = 1, unbiased = False, keepdim = True) mean = torch.mean(x, dim = 1, keepdim = True) return (x - mean) * (var + eps).rsqrt() * self.g class PreNorm(nn.Module): def __init__(self, dim, fn): super().__init__() self.fn = fn self.norm = LayerNorm(dim) def forward(self, x): x = self.norm(x) return self.fn(x) # positional embeds class LearnedSinusoidalPosEmb(nn.Module): """ following @crowsonkb 's lead with learned sinusoidal pos emb """ """ https://github.com/crowsonkb/v-diffusion-jax/blob/master/diffusion/models/danbooru_128.py#L8 """ def __init__(self, dim): super().__init__() assert (dim % 2) == 0 half_dim = dim // 2 self.weights = nn.Parameter(torch.randn(half_dim)) def forward(self, x): x = rearrange(x, 'b -> b 1') freqs = x * rearrange(self.weights, 'd -> 1 d') * 2 * math.pi fouriered = torch.cat((freqs.sin(), freqs.cos()), dim = -1) fouriered = torch.cat((x, fouriered), dim = -1) return fouriered # building block modules class Block(nn.Module): def __init__(self, dim, dim_out, groups = 8): super().__init__() self.proj = nn.Conv2d(dim, dim_out, 3, padding = 1) self.norm = nn.GroupNorm(groups, dim_out) self.act = nn.SiLU() def forward(self, x, scale_shift = None): x = self.proj(x) x = self.norm(x) if exists(scale_shift): scale, shift = scale_shift x = x * (scale + 1) + shift x = self.act(x) return x class ResnetBlock(nn.Module): def __init__(self, dim, dim_out, *, time_emb_dim = None, groups = 8): super().__init__() self.mlp = nn.Sequential( nn.SiLU(), nn.Linear(time_emb_dim, dim_out * 2) ) if exists(time_emb_dim) else None self.block1 = Block(dim, dim_out, groups = groups) self.block2 = Block(dim_out, dim_out, groups = groups) self.res_conv = nn.Conv2d(dim, dim_out, 1) if dim != dim_out else nn.Identity() def forward(self, x, time_emb = None): scale_shift = None if exists(self.mlp) and exists(time_emb): time_emb = self.mlp(time_emb) time_emb = rearrange(time_emb, 'b c -> b c 1 1') scale_shift = time_emb.chunk(2, dim = 1) h = self.block1(x, scale_shift = scale_shift) h = self.block2(h) return h + self.res_conv(x) class LinearAttention(nn.Module): def __init__(self, dim, heads = 4, dim_head = 32): super().__init__() self.scale = dim_head ** -0.5 self.heads = heads hidden_dim = dim_head * heads self.to_qkv = nn.Conv2d(dim, hidden_dim * 3, 1, bias = False) self.to_out = nn.Sequential( nn.Conv2d(hidden_dim, dim, 1), LayerNorm(dim) ) def forward(self, x): b, c, h, w = x.shape qkv = self.to_qkv(x).chunk(3, dim = 1) q, k, v = map(lambda t: rearrange(t, 'b (h c) x y -> b h c (x y)', h = self.heads), qkv) q = q.softmax(dim = -2) k = k.softmax(dim = -1) q = q * self.scale v = v / (h * w) context = torch.einsum('b h d n, b h e n -> b h d e', k, v) out = torch.einsum('b h d e, b h d n -> b h e n', context, q) out = rearrange(out, 'b h c (x y) -> b (h c) x y', h = self.heads, x = h, y = w) return self.to_out(out) class Attention(nn.Module): def __init__(self, dim, heads = 4, dim_head = 32): super().__init__() self.scale = dim_head ** -0.5 self.heads = heads hidden_dim = dim_head * heads self.to_qkv = nn.Conv2d(dim, hidden_dim * 3, 1, bias = False) self.to_out = nn.Conv2d(hidden_dim, dim, 1) def forward(self, x): b, c, h, w = x.shape qkv = self.to_qkv(x).chunk(3, dim = 1) q, k, v = map(lambda t: rearrange(t, 'b (h c) x y -> b h c (x y)', h = self.heads), qkv) q = q * self.scale sim = einsum('b h d i, b h d j -> b h i j', q, k) attn = sim.softmax(dim = -1) out = einsum('b h i j, b h d j -> b h i d', attn, v) out = rearrange(out, 'b h (x y) d -> b (h d) x y', x = h, y = w) return self.to_out(out) # model class Unet(nn.Module): def __init__( self, dim, init_dim = None, dim_mults=(1, 2, 4, 8), channels = 3, bits = BITS, resnet_block_groups = 8, learned_sinusoidal_dim = 16 ): super().__init__() # determine dimensions channels *= bits self.channels = channels input_channels = channels * 2 init_dim = default(init_dim, dim) self.init_conv = nn.Conv2d(input_channels, init_dim, 7, padding = 3) dims = [init_dim, *map(lambda m: dim * m, dim_mults)] in_out = list(zip(dims[:-1], dims[1:])) block_klass = partial(ResnetBlock, groups = resnet_block_groups) # time embeddings time_dim = dim * 4 sinu_pos_emb = LearnedSinusoidalPosEmb(learned_sinusoidal_dim) fourier_dim = learned_sinusoidal_dim + 1 self.time_mlp = nn.Sequential( sinu_pos_emb, nn.Linear(fourier_dim, time_dim), nn.GELU(), nn.Linear(time_dim, time_dim) ) # layers self.downs = nn.ModuleList([]) self.ups = nn.ModuleList([]) num_resolutions = len(in_out) for ind, (dim_in, dim_out) in enumerate(in_out): is_last = ind >= (num_resolutions - 1) self.downs.append(nn.ModuleList([ block_klass(dim_in, dim_in, time_emb_dim = time_dim), block_klass(dim_in, dim_in, time_emb_dim = time_dim), Residual(PreNorm(dim_in, LinearAttention(dim_in))), Downsample(dim_in, dim_out) if not is_last else nn.Conv2d(dim_in, dim_out, 3, padding = 1) ])) mid_dim = dims[-1] self.mid_block1 = block_klass(mid_dim, mid_dim, time_emb_dim = time_dim) self.mid_attn = Residual(PreNorm(mid_dim, Attention(mid_dim))) self.mid_block2 = block_klass(mid_dim, mid_dim, time_emb_dim = time_dim) for ind, (dim_in, dim_out) in enumerate(reversed(in_out)): is_last = ind == (len(in_out) - 1) self.ups.append(nn.ModuleList([ block_klass(dim_out + dim_in, dim_out, time_emb_dim = time_dim), block_klass(dim_out + dim_in, dim_out, time_emb_dim = time_dim), Residual(PreNorm(dim_out, LinearAttention(dim_out))), Upsample(dim_out, dim_in) if not is_last else nn.Conv2d(dim_out, dim_in, 3, padding = 1) ])) self.final_res_block = block_klass(dim * 2, dim, time_emb_dim = time_dim) self.final_conv = nn.Conv2d(dim, channels, 1) def forward(self, x, time, x_self_cond = None): x_self_cond = default(x_self_cond, lambda: torch.zeros_like(x)) x = torch.cat((x_self_cond, x), dim = 1) x = self.init_conv(x) r = x.clone() t = self.time_mlp(time) h = [] for block1, block2, attn, downsample in self.downs: x = block1(x, t) h.append(x) x = block2(x, t) x = attn(x) h.append(x) x = downsample(x) x = self.mid_block1(x, t) x = self.mid_attn(x) x = self.mid_block2(x, t) for block1, block2, attn, upsample in self.ups: x = torch.cat((x, h.pop()), dim = 1) x = block1(x, t) x = torch.cat((x, h.pop()), dim = 1) x = block2(x, t) x = attn(x) x = upsample(x) x = torch.cat((x, r), dim = 1) x = self.final_res_block(x, t) return self.final_conv(x) # convert to bit representations and back def decimal_to_bits(x, bits = BITS): """ expects image tensor ranging from 0 to 1, outputs bit tensor ranging from -1 to 1 """ device = x.device x = (x * 255).int().clamp(0, 255) mask = 2 ** torch.arange(bits - 1, -1, -1, device = device) mask = rearrange(mask, 'd -> d 1 1') x = rearrange(x, 'b c h w -> b c 1 h w') bits = ((x & mask) != 0).float() bits = rearrange(bits, 'b c d h w -> b (c d) h w') bits = bits * 2 - 1 return bits def bits_to_decimal(x, bits = BITS): """ expects bits from -1 to 1, outputs image tensor from 0 to 1 """ device = x.device x = (x > 0).int() mask = 2 ** torch.arange(bits - 1, -1, -1, device = device, dtype = torch.int32) mask = rearrange(mask, 'd -> d 1 1') x = rearrange(x, 'b (c d) h w -> b c d h w', d = bits) dec = reduce(x * mask, 'b c d h w -> b c h w', 'sum') return (dec / 255).clamp(0., 1.) # bit diffusion class def log(t, eps = 1e-20): return torch.log(t.clamp(min = eps)) def right_pad_dims_to(x, t): padding_dims = x.ndim - t.ndim if padding_dims <= 0: return t return t.view(*t.shape, *((1,) * padding_dims)) def beta_linear_log_snr(t): return -torch.log(expm1(1e-4 + 10 * (t ** 2))) def alpha_cosine_log_snr(t, s: float = 0.008): return -log((torch.cos((t + s) / (1 + s) * math.pi * 0.5) ** -2) - 1, eps = 1e-5) # not sure if this accounts for beta being clipped to 0.999 in discrete version def log_snr_to_alpha_sigma(log_snr): return torch.sqrt(torch.sigmoid(log_snr)), torch.sqrt(torch.sigmoid(-log_snr)) class BitDiffusion(nn.Module): def __init__( self, model, *, image_size, timesteps = 1000, use_ddim = False, noise_schedule = 'cosine', time_difference = 0., bit_scale = 1. ): super().__init__() self.model = model self.channels = self.model.channels self.image_size = image_size if noise_schedule == "linear": self.log_snr = beta_linear_log_snr elif noise_schedule == "cosine": self.log_snr = alpha_cosine_log_snr else: raise ValueError(f'invalid noise schedule {noise_schedule}') self.bit_scale = bit_scale self.timesteps = timesteps self.use_ddim = use_ddim # proposed in the paper, summed to time_next # as a way to fix a deficiency in self-conditioning and lower FID when the number of sampling timesteps is < 400 self.time_difference = time_difference @property def device(self): return next(self.model.parameters()).device def get_sampling_timesteps(self, batch, *, device): times = torch.linspace(1., 0., self.timesteps + 1, device = device) times = repeat(times, 't -> b t', b = batch) times = torch.stack((times[:, :-1], times[:, 1:]), dim = 0) times = times.unbind(dim = -1) return times @torch.no_grad() def ddpm_sample(self, shape, time_difference = None): batch, device = shape[0], self.device time_difference = default(time_difference, self.time_difference) time_pairs = self.get_sampling_timesteps(batch, device = device) img = torch.randn(shape, device=device) x_start = None for time, time_next in tqdm(time_pairs, desc = 'sampling loop time step', total = self.timesteps): # add the time delay time_next = (time_next - self.time_difference).clamp(min = 0.) noise_cond = self.log_snr(time) # get predicted x0 x_start = self.model(img, noise_cond, x_start) # clip x0 x_start.clamp_(-self.bit_scale, self.bit_scale) # get log(snr) log_snr = self.log_snr(time) log_snr_next = self.log_snr(time_next) log_snr, log_snr_next = map(partial(right_pad_dims_to, img), (log_snr, log_snr_next)) # get alpha sigma of time and next time alpha, sigma = log_snr_to_alpha_sigma(log_snr) alpha_next, sigma_next = log_snr_to_alpha_sigma(log_snr_next) # derive posterior mean and variance c = -expm1(log_snr - log_snr_next) mean = alpha_next * (img * (1 - c) / alpha + c * x_start) variance = (sigma_next ** 2) * c log_variance = log(variance) # get noise noise = torch.where( rearrange(time_next > 0, 'b -> b 1 1 1'), torch.randn_like(img), torch.zeros_like(img) ) img = mean + (0.5 * log_variance).exp() * noise return bits_to_decimal(img) @torch.no_grad() def ddim_sample(self, shape, time_difference = None): batch, device = shape[0], self.device time_difference = default(time_difference, self.time_difference) time_pairs = self.get_sampling_timesteps(batch, device = device) img = torch.randn(shape, device = device) x_start = None for times, times_next in tqdm(time_pairs, desc = 'sampling loop time step'): # add the time delay times_next = (times_next - time_difference).clamp(min = 0.) # get times and noise levels log_snr = self.log_snr(times) log_snr_next = self.log_snr(times_next) padded_log_snr, padded_log_snr_next = map(partial(right_pad_dims_to, img), (log_snr, log_snr_next)) alpha, sigma = log_snr_to_alpha_sigma(padded_log_snr) alpha_next, sigma_next = log_snr_to_alpha_sigma(padded_log_snr_next) # predict x0 x_start = self.model(img, log_snr, x_start) # clip x0 x_start.clamp_(-self.bit_scale, self.bit_scale) # get predicted noise pred_noise = (img - alpha * x_start) / sigma.clamp(min = 1e-8) # calculate x next img = x_start * alpha_next + pred_noise * sigma_next return bits_to_decimal(img) @torch.no_grad() def sample(self, batch_size = 16): image_size, channels = self.image_size, self.channels sample_fn = self.ddpm_sample if not self.use_ddim else self.ddim_sample return sample_fn((batch_size, channels, image_size, image_size)) def forward(self, img, *args, **kwargs): batch, c, h, w, device, img_size, = *img.shape, img.device, self.image_size assert h == img_size and w == img_size, f'height and width of image must be {img_size}' # sample random times times = torch.zeros((batch,), device = device).float().uniform_(0, 1.) # convert image to bit representation img = decimal_to_bits(img) * self.bit_scale # noise sample noise = torch.randn_like(img) noise_level = self.log_snr(times) padded_noise_level = right_pad_dims_to(img, noise_level) alpha, sigma = log_snr_to_alpha_sigma(padded_noise_level) noised_img = alpha * img + sigma * noise # if doing self-conditioning, 50% of the time, predict x_start from current set of times # and condition with unet with that # this technique will slow down training by 25%, but seems to lower FID significantly self_cond = None if torch.rand((1)) < 0.5: with torch.no_grad(): self_cond = self.model(noised_img, noise_level).detach_() # predict and take gradient step pred = self.model(noised_img, noise_level, self_cond) return F.mse_loss(pred, img) # dataset classes class Dataset(Dataset): def __init__( self, folder, image_size, exts = ['jpg', 'jpeg', 'png', 'tiff'], augment_horizontal_flip = False, pil_img_type = None ): super().__init__() self.folder = folder self.image_size = image_size self.paths = [p for ext in exts for p in Path(f'{folder}').glob(f'**/*.{ext}')] maybe_convert_fn = partial(convert_image_to, pil_img_type) if exists(pil_img_type) else nn.Identity() self.transform = T.Compose([ T.Lambda(maybe_convert_fn), T.Resize(image_size), T.RandomHorizontalFlip() if augment_horizontal_flip else nn.Identity(), T.CenterCrop(image_size), T.ToTensor() ]) def __len__(self): return len(self.paths) def __getitem__(self, index): path = self.paths[index] img = Image.open(path) return self.transform(img) # trainer class class Trainer(object): def __init__( self, diffusion_model, folder, *, train_batch_size = 16, gradient_accumulate_every = 1, augment_horizontal_flip = True, train_lr = 1e-4, train_num_steps = 100000, ema_update_every = 10, ema_decay = 0.995, adam_betas = (0.9, 0.99), save_and_sample_every = 1000, num_samples = 25, results_folder = './results', amp = False, mixed_precision_type = 'fp16', split_batches = True, pil_img_type = None ): super().__init__() self.accelerator = Accelerator( split_batches = split_batches, mixed_precision = mixed_precision_type if amp else 'no' ) self.model = diffusion_model assert has_int_squareroot(num_samples), 'number of samples must have an integer square root' self.num_samples = num_samples self.save_and_sample_every = save_and_sample_every self.batch_size = train_batch_size self.gradient_accumulate_every = gradient_accumulate_every self.train_num_steps = train_num_steps self.image_size = diffusion_model.image_size # dataset and dataloader self.ds = Dataset(folder, self.image_size, augment_horizontal_flip = augment_horizontal_flip, convert_image_to = pil_img_type) dl = DataLoader(self.ds, batch_size = train_batch_size, shuffle = True, pin_memory = True, num_workers = cpu_count()) dl = self.accelerator.prepare(dl) self.dl = cycle(dl) # optimizer self.opt = Adam(diffusion_model.parameters(), lr = train_lr, betas = adam_betas) # for logging results in a folder periodically if self.accelerator.is_main_process: self.ema = EMA(diffusion_model, beta = ema_decay, update_every = ema_update_every) self.results_folder = Path(results_folder) self.results_folder.mkdir(exist_ok = True) # step counter state self.step = 0 # prepare model, dataloader, optimizer with accelerator self.model, self.opt = self.accelerator.prepare(self.model, self.opt) def save(self, milestone): if not self.accelerator.is_local_main_process: return data = { 'step': self.step, 'model': self.accelerator.get_state_dict(self.model), 'opt': self.opt.state_dict(), 'ema': self.ema.state_dict(), 'scaler': self.accelerator.scaler.state_dict() if exists(self.accelerator.scaler) else None } torch.save(data, str(self.results_folder / f'model-{milestone}.pt')) def load(self, milestone): data = torch.load(str(self.results_folder / f'model-{milestone}.pt')) model = self.accelerator.unwrap_model(self.model) model.load_state_dict(data['model']) self.step = data['step'] self.opt.load_state_dict(data['opt']) self.ema.load_state_dict(data['ema']) if exists(self.accelerator.scaler) and exists(data['scaler']): self.accelerator.scaler.load_state_dict(data['scaler']) def train(self): accelerator = self.accelerator device = accelerator.device with tqdm(initial = self.step, total = self.train_num_steps, disable = not accelerator.is_main_process) as pbar: while self.step < self.train_num_steps: total_loss = 0. for _ in range(self.gradient_accumulate_every): data = next(self.dl).to(device) with self.accelerator.autocast(): loss = self.model(data) loss = loss / self.gradient_accumulate_every total_loss += loss.item() self.accelerator.backward(loss) pbar.set_description(f'loss: {total_loss:.4f}') accelerator.wait_for_everyone() self.opt.step() self.opt.zero_grad() accelerator.wait_for_everyone() if accelerator.is_main_process: self.ema.to(device) self.ema.update() if self.step != 0 and self.step % self.save_and_sample_every == 0: self.ema.ema_model.eval() with torch.no_grad(): milestone = self.step // self.save_and_sample_every batches = num_to_groups(self.num_samples, self.batch_size) all_images_list = list(map(lambda n: self.ema.ema_model.sample(batch_size=n), batches)) all_images = torch.cat(all_images_list, dim = 0) utils.save_image(all_images, str(self.results_folder / f'sample-{milestone}.png'), nrow = int(math.sqrt(self.num_samples))) self.save(milestone) self.step += 1 pbar.update(1) accelerator.print('training complete')
from .adamod import AdaMod
class AdaMod(Optimizer): """Implements AdaMod algorithm with Decoupled Weight Decay (arxiv.org/abs/1711.05101) It has been proposed in `Adaptive and Momental Bounds for Adaptive Learning Rate Methods`_. Arguments: params (iterable): iterable of parameters to optimize or dicts defining parameter groups lr (float, optional): learning rate (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its square (default: (0.9, 0.999)) beta3 (float, optional): smoothing coefficient for adaptive learning rates (default: 0.9999) eps (float, optional): term added to the denominator to improve numerical stability (default: 1e-8) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) """ def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), beta3=0.999, eps=1e-8, weight_decay=0): if not 0.0 <= lr: raise ValueError("Invalid learning rate: {}".format(lr)) if not 0.0 <= eps: raise ValueError("Invalid epsilon value: {}".format(eps)) if not 0.0 <= betas[0] < 1.0: raise ValueError("Invalid beta parameter at index 0: {}".format(betas[0])) if not 0.0 <= betas[1] < 1.0: raise ValueError("Invalid beta parameter at index 1: {}".format(betas[1])) if not 0.0 <= beta3 < 1.0: raise ValueError("Invalid beta3 parameter: {}".format(beta3)) defaults = dict(lr=lr, betas=betas, beta3=beta3, eps=eps, weight_decay=weight_decay) super(AdaMod, self).__init__(params, defaults) def __setstate__(self, state): super(AdaMod, self).__setstate__(state) def step(self, closure=None): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: loss = closure() for group in self.param_groups: for p in group['params']: if p.grad is None: continue grad = p.grad.data if grad.is_sparse: raise RuntimeError( 'AdaMod does not support sparse gradients') state = self.state[p] # State initialization if len(state) == 0: state['step'] = 0 # Exponential moving average of gradient values state['exp_avg'] = torch.zeros_like(p.data) # Exponential moving average of squared gradient values state['exp_avg_sq'] = torch.zeros_like(p.data) # Exponential moving average of actual learning rates state['exp_avg_lr'] = torch.zeros_like(p.data) exp_avg, exp_avg_sq, exp_avg_lr = state['exp_avg'], state['exp_avg_sq'], state['exp_avg_lr'] beta1, beta2 = group['betas'] state['step'] += 1 # Decay the first and second moment running average coefficient exp_avg.mul_(beta1).add_(1 - beta1, grad) exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad) denom = exp_avg_sq.sqrt().add_(group['eps']) bias_correction1 = 1 - beta1 ** state['step'] bias_correction2 = 1 - beta2 ** state['step'] step_size = group['lr'] * math.sqrt(bias_correction2) / bias_correction1 if group['weight_decay'] != 0: p.data.add_(-group['weight_decay'] * group['lr'], p.data) # Applies momental bounds on actual learning rates step_size = torch.full_like(denom, step_size) step_size.div_(denom) exp_avg_lr.mul_(group['beta3']).add_(1 - group['beta3'], step_size) step_size = torch.min(step_size, exp_avg_lr) step_size.mul_(exp_avg) p.data.add_(-step_size) return loss
#%matplotlib notebook LABELS = ['SGD','Adam', 'AdaMod'] def get_folder_path(use_pretrained=True): if use_pretrained: path = 'pretrained' else: path = 'curve' return path def get_curve_data(use_pretrained=True, model='ResNet'): folder_path = get_folder_path(use_pretrained) filenames = [name for name in os.listdir(folder_path) if name.startswith(model.lower())] paths = [os.path.join(folder_path, name) for name in filenames] keys = [name.split('-')[1] for name in filenames] return {key: torch.load(fp) for key, fp in zip(keys, paths)} def plot(use_pretrained=True, model='ResNet', optimizers=None, curve_type='train'): assert model in ['ResNet', 'DenseNet'], 'Invalid model name: {}'.format(model) assert curve_type in ['train', 'test'], 'Invalid curve type: {}'.format(curve_type) assert all(_ in LABELS for _ in optimizers), 'Invalid optimizer' curve_data = get_curve_data(use_pretrained, model=model) plt.figure() plt.title('{} Accuracy for {} on CIFAR-100'.format(curve_type.capitalize(), model)) plt.xlabel('Epoch') plt.ylabel('{} Accuracy %'.format(curve_type.capitalize())) if curve_type == 'train': plt.ylim(80, 101) else: plt.ylim(50, 81) for optim in optimizers: accuracies = np.array(curve_data[optim.lower()]['{}_acc'.format(curve_type)]) plt.plot(accuracies, label=optim) plt.grid(ls='--') plt.legend() plt.show() plt.savefig('cifar100-{}-{}.png'.format(model, curve_type.capitalize())) def main(): # plot(use_pretrained=True, model='ResNet', optimizers=LABELS, curve_type='train') # plot(use_pretrained=True, model='ResNet', optimizers=LABELS, curve_type='test') plot(use_pretrained=True, model='DenseNet', optimizers=LABELS, curve_type='train') plot(use_pretrained=True, model='DenseNet', optimizers=LABELS, curve_type='test') if __name__ == '__main__': main()
"""Train CIFAR100 with PyTorch.""" def get_parser(): parser = argparse.ArgumentParser(description='PyTorch CIFAR100 Training') parser.add_argument('--model', default='resnet', type=str, help='model', choices=['resnet', 'densenet']) parser.add_argument('--optim', default='adamod', type=str, help='optimizer', choices=['sgd', 'adam', 'adamod']) parser.add_argument('--lr', default=0.1, type=float, help='learning rate') parser.add_argument('--beta3', default=0.999, type=float, help=' smoothing coefficient term of AdaMod') parser.add_argument('--momentum', default=0.9, type=float, help='momentum term') parser.add_argument('--beta1', default=0.9, type=float, help='Adam coefficients beta_1') parser.add_argument('--beta2', default=0.999, type=float, help='Adam coefficients beta_2') parser.add_argument('--resume', '-r', action='store_true', help='resume from checkpoint') parser.add_argument('--weight_decay', default=5e-4, type=float, help='weight decay for optimizers') return parser def build_dataset(): print('==> Preparing data..') transform_train = transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize((0.507, 0.487, 0.441), (0.267, 0.256, 0.276)), ]) transform_test = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.507, 0.487, 0.441), (0.267, 0.256, 0.276)), ]) trainset = torchvision.datasets.CIFAR100(root='./data', train=True, download=True, transform=transform_train) train_loader = torch.utils.data.DataLoader(trainset, batch_size=128, shuffle=True, num_workers=2) testset = torchvision.datasets.CIFAR100(root='./data', train=False, download=True, transform=transform_test) test_loader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, num_workers=2) # classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') return train_loader, test_loader def get_ckpt_name(dataset='cifar100', model='resnet', optimizer='adamod', lr=0.1, momentum=0.9, beta1=0.9, beta2=0.999, beta3=0.999): name = { 'sgd': 'lr{}-momentum{}'.format(lr, momentum), 'adam': 'lr{}-betas{}-{}'.format(lr, beta1, beta2), 'adamod': 'lr{}-betas{}-{}-{}'.format(lr, beta1, beta2, beta3), }[optimizer] return '{}-{}-{}'.format(model, optimizer, name) def load_checkpoint(ckpt_name): print('==> Resuming from checkpoint..') path = os.path.join('checkpoint', ckpt_name) assert os.path.isdir('checkpoint'), 'Error: no checkpoint directory found!' assert os.path.exists(path), 'Error: checkpoint {} not found'.format(ckpt_name) return torch.load(ckpt_name) def build_model(args, device, ckpt=None): print('==> Building model..') net = { 'resnet': ResNet34, 'densenet': DenseNet121, }[args.model]() net = net.to(device) if device == 'cuda': net = torch.nn.DataParallel(net) cudnn.benchmark = True if ckpt: net.load_state_dict(ckpt['net']) return net def create_optimizer(args, model_params): if args.optim == 'sgd': return optim.SGD(model_params, args.lr, momentum=args.momentum, weight_decay=args.weight_decay) elif args.optim == 'adam': return optim.AdamW(model_params, args.lr, betas=(args.beta1, args.beta2), weight_decay=args.weight_decay) elif args.optim == 'adamod': return AdaMod(model_params, args.lr, betas=(args.beta1, args.beta2), beta3=args.beta3, weight_decay=args.weight_decay) def train(net, epoch, device, data_loader, optimizer, criterion): print('\nEpoch: %d' % epoch) net.train() train_loss = 0 correct = 0 total = 0 for batch_idx, (inputs, targets) in enumerate(data_loader): inputs, targets = inputs.to(device), targets.to(device) optimizer.zero_grad() outputs = net(inputs) loss = criterion(outputs, targets) loss.backward() optimizer.step() train_loss += loss.item() _, predicted = outputs.max(1) total += targets.size(0) correct += predicted.eq(targets).sum().item() accuracy = 100. * correct / total print('train acc %.3f' % accuracy) return accuracy def test(net, device, data_loader, criterion): net.eval() test_loss = 0 correct = 0 total = 0 with torch.no_grad(): for batch_idx, (inputs, targets) in enumerate(data_loader): inputs, targets = inputs.to(device), targets.to(device) outputs = net(inputs) loss = criterion(outputs, targets) test_loss += loss.item() _, predicted = outputs.max(1) total += targets.size(0) correct += predicted.eq(targets).sum().item() accuracy = 100. * correct / total print(' test acc %.3f' % accuracy) return accuracy def main(): parser = get_parser() args = parser.parse_args() train_loader, test_loader = build_dataset() device = 'cuda' if torch.cuda.is_available() else 'cpu' ckpt_name = get_ckpt_name(model=args.model, optimizer=args.optim, lr=args.lr, momentum=args.momentum, beta1=args.beta1, beta2=args.beta2, beta3=args.beta3) if args.resume: ckpt = load_checkpoint(ckpt_name) best_acc = ckpt['acc'] start_epoch = ckpt['epoch'] else: ckpt = None best_acc = 0 start_epoch = -1 net = build_model(args, device, ckpt=ckpt) criterion = nn.CrossEntropyLoss() optimizer = create_optimizer(args, net.parameters()) scheduler = optim.lr_scheduler.MultiStepLR(optimizer, [150, 225], gamma=0.1, last_epoch=start_epoch) train_accuracies = [] test_accuracies = [] for epoch in range(start_epoch + 1, 300): scheduler.step() train_acc = train(net, epoch, device, train_loader, optimizer, criterion) test_acc = test(net, device, test_loader, criterion) # Save checkpoint. if test_acc > best_acc: print('Saving..') state = { 'net': net.state_dict(), 'acc': test_acc, 'epoch': epoch, } if not os.path.isdir('checkpoint'): os.mkdir('checkpoint') torch.save(state, os.path.join('checkpoint', ckpt_name)) best_acc = test_acc train_accuracies.append(train_acc) test_accuracies.append(test_acc) if not os.path.isdir('curve'): os.mkdir('curve') torch.save({'train_acc': train_accuracies, 'test_acc': test_accuracies}, os.path.join('curve', ckpt_name)) if __name__ == '__main__': main()
""" .. Densely Connected Convolutional Networks: https://arxiv.org/abs/1608.06993 """ class Bottleneck(nn.Module): def __init__(self, in_planes, growth_rate): super(Bottleneck, self).__init__() self.bn1 = nn.BatchNorm2d(in_planes) self.conv1 = nn.Conv2d(in_planes, 4 * growth_rate, kernel_size=1, bias=False) self.bn2 = nn.BatchNorm2d(4 * growth_rate) self.conv2 = nn.Conv2d(4 * growth_rate, growth_rate, kernel_size=3, padding=1, bias=False) def forward(self, x): out = self.conv1(F.relu(self.bn1(x))) out = self.conv2(F.relu(self.bn2(out))) out = torch.cat([out, x], 1) return out class Transition(nn.Module): def __init__(self, in_planes, out_planes): super(Transition, self).__init__() self.bn = nn.BatchNorm2d(in_planes) self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=1, bias=False) def forward(self, x): out = self.conv(F.relu(self.bn(x))) out = F.avg_pool2d(out, 2) return out class DenseNet(nn.Module): def __init__(self, block, nblocks, growth_rate=12, reduction=0.5, num_classes=100): super(DenseNet, self).__init__() self.growth_rate = growth_rate num_planes = 2 * growth_rate self.conv1 = nn.Conv2d(3, num_planes, kernel_size=3, padding=1, bias=False) self.dense1 = self._make_dense_layers(block, num_planes, nblocks[0]) num_planes += nblocks[0] * growth_rate out_planes = int(math.floor(num_planes * reduction)) self.trans1 = Transition(num_planes, out_planes) num_planes = out_planes self.dense2 = self._make_dense_layers(block, num_planes, nblocks[1]) num_planes += nblocks[1] * growth_rate out_planes = int(math.floor(num_planes * reduction)) self.trans2 = Transition(num_planes, out_planes) num_planes = out_planes self.dense3 = self._make_dense_layers(block, num_planes, nblocks[2]) num_planes += nblocks[2] * growth_rate out_planes = int(math.floor(num_planes * reduction)) self.trans3 = Transition(num_planes, out_planes) num_planes = out_planes self.dense4 = self._make_dense_layers(block, num_planes, nblocks[3]) num_planes += nblocks[3] * growth_rate self.bn = nn.BatchNorm2d(num_planes) self.linear = nn.Linear(num_planes, num_classes) def _make_dense_layers(self, block, in_planes, nblock): layers = [] for i in range(nblock): layers.append(block(in_planes, self.growth_rate)) in_planes += self.growth_rate return nn.Sequential(*layers) def forward(self, x): out = self.conv1(x) out = self.trans1(self.dense1(out)) out = self.trans2(self.dense2(out)) out = self.trans3(self.dense3(out)) out = self.dense4(out) out = F.avg_pool2d(F.relu(self.bn(out)), 4) out = out.view(out.size(0), -1) out = self.linear(out) return out def DenseNet121(): return DenseNet(Bottleneck, [6, 12, 24, 16], growth_rate=32) def DenseNet169(): return DenseNet(Bottleneck, [6, 12, 32, 32], growth_rate=32) def DenseNet201(): return DenseNet(Bottleneck, [6, 12, 48, 32], growth_rate=32) def DenseNet161(): return DenseNet(Bottleneck, [6, 12, 36, 24], growth_rate=48) def densenet_cifar(): return DenseNet(Bottleneck, [6, 12, 24, 16], growth_rate=12) def test(): net = densenet_cifar() x = torch.randn(1, 3, 32, 32) y = net(x) print(y) # test()
""" .. Deep Residual Learning for Image Recognition: https://arxiv.org/abs/1512.03385 """ class BasicBlock(nn.Module): expansion = 1 def __init__(self, in_planes, planes, stride=1): super(BasicBlock, self).__init__() self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(planes) self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(planes) self.shortcut = nn.Sequential() if stride != 1 or in_planes != self.expansion * planes: self.shortcut = nn.Sequential( nn.Conv2d(in_planes, self.expansion * planes, kernel_size=1, stride=stride, bias=False), nn.BatchNorm2d(self.expansion * planes) ) def forward(self, x): out = F.relu(self.bn1(self.conv1(x))) out = self.bn2(self.conv2(out)) out += self.shortcut(x) out = F.relu(out) return out class Bottleneck(nn.Module): expansion = 4 def __init__(self, in_planes, planes, stride=1): super(Bottleneck, self).__init__() self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False) self.bn1 = nn.BatchNorm2d(planes) self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(planes) self.conv3 = nn.Conv2d(planes, self.expansion * planes, kernel_size=1, bias=False) self.bn3 = nn.BatchNorm2d(self.expansion * planes) self.shortcut = nn.Sequential() if stride != 1 or in_planes != self.expansion * planes: self.shortcut = nn.Sequential( nn.Conv2d(in_planes, self.expansion * planes, kernel_size=1, stride=stride, bias=False), nn.BatchNorm2d(self.expansion * planes) ) def forward(self, x): out = F.relu(self.bn1(self.conv1(x))) out = F.relu(self.bn2(self.conv2(out))) out = self.bn3(self.conv3(out)) out += self.shortcut(x) out = F.relu(out) return out class ResNet(nn.Module): def __init__(self, block, num_blocks, num_classes=100): super(ResNet, self).__init__() self.in_planes = 64 self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(64) self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1) self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2) self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2) self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2) self.linear = nn.Linear(512 * block.expansion, num_classes) def _make_layer(self, block, planes, num_blocks, stride): strides = [stride] + [1] * (num_blocks - 1) layers = [] for stride in strides: layers.append(block(self.in_planes, planes, stride)) self.in_planes = planes * block.expansion return nn.Sequential(*layers) def forward(self, x): out = F.relu(self.bn1(self.conv1(x))) out = self.layer1(out) out = self.layer2(out) out = self.layer3(out) out = self.layer4(out) out = F.avg_pool2d(out, 4) out = out.view(out.size(0), -1) out = self.linear(out) return out def ResNet18(): return ResNet(BasicBlock, [2, 2, 2, 2]) def ResNet34(): return ResNet(BasicBlock, [3, 4, 6, 3]) def ResNet50(): return ResNet(Bottleneck, [3, 4, 6, 3]) def ResNet101(): return ResNet(Bottleneck, [3, 4, 23, 3]) def ResNet152(): return ResNet(Bottleneck, [3, 8, 36, 3]) def test(): net = ResNet18() y = net(torch.randn(1, 3, 32, 32)) print(y.size()) # test()
# Copyright (c) 2017-present, Facebook, Inc. # All rights reserved. # # This source code is licensed under the license found in the LICENSE file in # the root directory of this source tree. An additional grant of patent rights # can be found in the PATENTS file in the same directory. LR_SCHEDULER_REGISTRY = {} def build_lr_scheduler(args, optimizer): return LR_SCHEDULER_REGISTRY[args.lr_scheduler](args, optimizer) def register_lr_scheduler(name): """Decorator to register a new LR scheduler.""" def register_lr_scheduler_cls(cls): if name in LR_SCHEDULER_REGISTRY: raise ValueError('Cannot register duplicate LR scheduler ({})'.format(name)) if not issubclass(cls, FairseqLRScheduler): raise ValueError('LR Scheduler ({}: {}) must extend FairseqLRScheduler'.format(name, cls.__name__)) LR_SCHEDULER_REGISTRY[name] = cls return cls return register_lr_scheduler_cls # automatically import any Python files in the optim/lr_scheduler/ directory for file in os.listdir(os.path.dirname(__file__)): if file.endswith('.py') and not file.startswith('_'): module = file[:file.find('.py')] importlib.import_module('fairseq.optim.lr_scheduler.' + module)
# Copyright (c) 2017-present, Facebook, Inc. # All rights reserved. # # This source code is licensed under the license found in the LICENSE file in # the root directory of this source tree. An additional grant of patent rights # can be found in the PATENTS file in the same directory. @register_lr_scheduler('cold_start') class ColdStartSchedule(FairseqLRScheduler): """Decay the LR based on the inverse square root of the update number. We also support a warmup phase where we linearly increase the learning rate from some initial learning rate (``--warmup-init-lr``) until the configured learning rate (``--lr``). Thereafter we decay proportional to the number of updates, with a decay factor set to align with the configured learning rate. During warmup:: lrs = torch.linspace(args.warmup_init_lr, args.lr, args.warmup_updates) lr = lrs[update_num] After warmup:: decay_factor = args.lr * sqrt(args.warmup_updates) lr = decay_factor / sqrt(update_num) """ def __init__(self, args, optimizer): super().__init__(args, optimizer) if len(args.lr) > 1: raise ValueError( 'Cannot use a fixed learning rate schedule with inverse_sqrt.' ' Consider --lr-scheduler=fixed instead.' ) warmup_end_lr = args.lr[0] if args.warmup_init_lr < 0: args.warmup_init_lr = warmup_end_lr # linearly warmup for the first args.warmup_updates self.lr_step = (warmup_end_lr - args.warmup_init_lr) / args.warmup_updates # then, decay prop. to the inverse square root of the update number self.decay_factor = warmup_end_lr * args.warmup_updates**0.5 # initial learning rate self.lr = args.warmup_init_lr self.optimizer.set_lr(self.lr) @staticmethod def add_args(parser): """Add arguments to the parser for this LR scheduler.""" # fmt: off parser.add_argument('--warmup-updates', default=4000, type=int, metavar='N', help='warmup the learning rate linearly for the first N updates') parser.add_argument('--warmup-init-lr', default=-1, type=float, metavar='LR', help='initial learning rate during warmup phase; default is args.lr') # fmt: on def step(self, epoch, val_loss=None): """Update the learning rate at the end of the given epoch.""" super().step(epoch, val_loss) # we don't change the learning rate at epoch boundaries return self.optimizer.get_lr() def step_update(self, num_updates): """Update the learning rate after each update.""" if num_updates < self.args.warmup_updates: self.lr = self.args.lr[0] else: self.lr = self.decay_factor * num_updates**-0.5 self.optimizer.set_lr(self.lr) return self.lr
# constants MAX_TOKEN_LENGTH = 256 DATA_DIR = './data' NUM_MEL = 80 TSV_FILE_NAME = 'subset.tsv' # helpers def tsv_to_dict(path): with open(path) as fd: rd = csv.DictReader(fd, delimiter = "\t", quotechar = '"') return [row for row in rd] # script voice_clips = tsv_to_dict(f'{DATA_DIR}/{TSV_FILE_NAME}') for clip in voice_clips: filename = clip['path'] text = clip['sentence'] waveform, sample_rate = torchaudio.load(f"{DATA_DIR}/clips/{filename}", normalization = True) output = torchaudio.transforms.MelSpectrogram(sample_rate, n_mels = NUM_MEL)(waveform)[0] tokenized = torch.tensor([int(byte) for i, byte in enumerate(text.encode('utf-8'))], dtype = torch.uint8) save_path = f"{DATA_DIR}/{filename}.pt" torch.save({ 'audio': output.t(), 'text': tokenized }, save_path)
# data @click.command() @optgroup.group('Model settings') @optgroup.option('--text_vocab', default = 256, type = int) @optgroup.option('--text_dim', default = 512, type = int) @optgroup.option('--text_depth', default = 1, type = int) @optgroup.option('--text_heads', default = 8, type = int) @optgroup.option('--audio_dim', default = 512, type = int) @optgroup.option('--audio_depth', default = 1, type = int) @optgroup.option('--audio_heads', default = 8, type = int) @optgroup.group('Training settings') @optgroup.option('--data_folder', default = './data', type = str) @optgroup.option('--batch_size', default = 16, type = int) @optgroup.option('--epochs', default = 100, type = int) @optgroup.option('--learning_rate', default = 3e-4, type = float) @optgroup.option('--weight_decay', default = 1e-1, type = float) @optgroup.option('--seed', default = 0, type = int) @optgroup.option('--max_norm', default = 0.5, type = float) def train( *, data_folder, batch_size, epochs, learning_rate, weight_decay, seed, max_norm, text_vocab, text_dim, text_depth, text_heads, audio_dim, audio_depth, audio_heads ): # rng rng_key = random.PRNGKey(seed) # data dataset = PairTextSpectrogramDataset(data_folder) dl = DataLoader(dataset, batch_size = batch_size, collate_fn = pair_text_spectrogram_dataset_collate_fn, drop_last = True, shuffle = True) # model model = CLAP( text_vocab = text_vocab, text_dim = text_dim, text_depth = text_depth, text_heads = text_heads, audio_dim = audio_dim, audio_depth = audio_depth, audio_heads = audio_heads ) # optimizer exclude_bias = lambda params: tree_util.tree_map(lambda x: x.ndim != 1, params) optim = chain( clip_by_global_norm(max_norm), scale_by_adam(eps=1e-4), add_decayed_weights(weight_decay, exclude_bias), scale(-learning_rate) ) # init audio, audio_mask, text, text_mask = next(iter(dl)) params = model.init(rng_key, text, audio, text_mask, audio_mask) optim_state = optim.init(params) # loss function, for use with value_and_grad @jit @value_and_grad def loss_fn(params, text, audio, text_mask, audio_mask): return model.apply(params, text, audio, text_mask, audio_mask) # train loop for _ in range(epochs): for audio, audio_mask, text, text_mask in dl: loss, grads = loss_fn(params, text, audio, text_mask, audio_mask) updates, optim_state = optim.update(grads, optim_state, params) params = apply_updates(params, updates) print(f'loss: {loss}') # finished if __name__ == "__main__": train()
# einsum and einops # flax # constants LARGE_NEG_VALUE = -1e10 # config config.enable_omnistaging() # Linen requires enabling omnistaging # helpers def cross_entropy(logits, targets, axis=-1): logprobs = nn.log_softmax(logits, axis=axis) nll = np.take_along_axis(logprobs, np.expand_dims(targets, axis=axis), axis=axis) ce = -np.mean(nll) return ce def fixed_pos_embedding(seq, dim): inv_freq = 1.0 / (10000 ** (np.arange(0, dim, 2) / dim)) sinusoid_inp = np.einsum("i , j -> i j", np.arange(seq), inv_freq) return np.sin(sinusoid_inp), np.cos(sinusoid_inp) def rotate_every_two(x): x1 = x[:, :, ::2] x2 = x[:, :, 1::2] x = np.stack((-x2, x1), axis=-1) return rearrange(x, "... d j -> ... (d j)") def apply_rotary_pos_emb(x, sincos): sin, cos = map(lambda t: repeat(t, "b n -> b (n j)", j=2)[:, None, :], sincos) return (x * cos) + (rotate_every_two(x) * sin) # main class class Attention(nn.Module): dim: int heads: int dim_head: int = 64 causal: bool = False @nn.compact def __call__(self, x, pos_emb, mask): dim_in, h = x.shape[-1], self.heads scale = dim_in ** -0.5 norm = nn.LayerNorm() to_qkv = nn.Dense(features=self.dim_head * h * 3, use_bias=False) to_out = nn.Dense(features=dim_in) x = norm(x) qkv = np.split(to_qkv(x), 3, axis=-1) q, k, v = map(lambda t: rearrange(t, "i (h d) -> i h d", h=h), qkv) q = index_update(q, index[1:], apply_rotary_pos_emb(q[1:], pos_emb)) k = index_update(k, index[1:], apply_rotary_pos_emb(k[1:], pos_emb)) sim = einsum("i h d, j h d -> i j h", q, k) * scale mask = np.pad(mask, (1, 0), constant_values=True) mask = rearrange(mask, "j -> () j ()") if self.causal: i, j = sim.shape[:2] tri_mask = np.ones((i - 1, j - 1), dtype=bool) tri_mask = np.pad(tri_mask, ((1, 0), (1, 0)), constant_values=False) causal_mask = np.triu(tri_mask, j - i + 1) causal_mask = rearrange(causal_mask, "i j -> i j ()") mask = ~causal_mask * mask sim = np.where(mask, sim, LARGE_NEG_VALUE) attn = nn.softmax(sim, axis=-2) out = einsum("i j h, j h d -> i h d", attn, v) out = rearrange(out, "i h d -> i (h d)") return to_out(out) class FeedForward(nn.Module): mult: int = 4 @nn.compact def __call__(self, x): dim_in, mult = x.shape[-1], self.mult norm = nn.LayerNorm() to_intermediate = nn.Dense(features=dim_in * mult) to_out = nn.Dense(features=dim_in) x = norm(x) x = to_intermediate(x) x = nn.gelu(x) x = to_out(x) return x class Transformer(nn.Module): dim: int depth: int heads: int dim_head: int = 64 causal: bool = False cls_init: Callable = nn.initializers.lecun_normal() def setup(self): self.layers = [ ( Attention( dim=self.dim, heads=self.heads, dim_head=self.dim_head, causal=self.causal, ), FeedForward(), ) for _ in range(self.depth) ] @nn.compact def __call__(self, x, mask): n, d, h, dh, dim = *x.shape, self.heads, self.dim_head, self.dim if d != dim: x = nn.Dense(features=dim)(x) cls_token = self.param("cls", self.cls_init, (1, x.shape[-1])) to_norm_out = nn.LayerNorm() sincos = fixed_pos_embedding(n, self.dim_head) x = np.concatenate((cls_token, x), axis=0) for attn, ff in self.layers: x = attn(x, pos_emb=sincos, mask=mask) + x x = ff(x) + x x = to_norm_out(x) return x class CLAP(nn.Module): text_vocab: int text_dim: int text_depth: int text_heads: int audio_dim: int audio_depth: int audio_heads: int temp_init: Callable = nn.initializers.zeros def setup(self): self.audio_encoder = Transformer( dim=self.audio_dim, depth=self.audio_depth, heads=self.audio_heads ) self.text_encoder = Transformer( dim=self.text_dim, depth=self.text_depth, heads=self.text_heads, causal=True ) @nn.compact def __call__(self, text, audio, text_mask, audio_mask, return_loss=True): b, text_vocab, text_dim = text.shape[0], self.text_vocab, self.text_dim to_text_tokens = nn.Embed(num_embeddings=text_vocab, features=text_dim) temp = self.param("temperature", self.temp_init, tuple()) text = to_text_tokens(text) enc_text = vmap(self.text_encoder)(text, mask=text_mask) enc_audio = vmap(self.audio_encoder)(audio, mask=audio_mask) enc_text = enc_text[:, 0] enc_audio = enc_audio[:, 0] enc_text = enc_text / np.linalg.norm(enc_text, axis=-1, keepdims=True) enc_audio = enc_audio / np.linalg.norm(enc_audio, axis=-1, keepdims=True) sim = einsum("i d, j d -> i j", enc_text, enc_audio) * np.exp(temp) if not return_loss: return sim labels = np.arange(b) loss = ( cross_entropy(sim, labels, axis=0) + cross_entropy(sim, labels, axis=1) ) / 2 return loss
class CaptionedAudioMetadataset(IterableDataset): def __init__(self, path_pairs, lazy=False): self.datasets = [ CaptionedAudioDataset(captions_path, spectrograms_path, lazy=lazy) for (captions_path, spectrograms_path) in path_pairs ] def __iter__(self): def roundrobin(datasets): num_active = len(datasets) nexts = cycle(iter(it).__next__ for it in datasets) while num_active: try: for next in nexts: yield next() except StopIteration: # Remove the iterator we just exhausted from the cycle. num_active -= 1 nexts = cycle(islice(nexts, num_active)) iterator = roundrobin(self.datasets) return iterator class CaptionedAudioDataset(IterableDataset): def __init__(self, captions_path, spectrograms_path, lazy=False): self.lazy = lazy if self.lazy: # Warning: The lazy path does not check whether the cpation metadata # links it to the spectrogram. It assumes that the specrogram data, # read from the files from the path in sorted order, loaded in as # tensors, follows the exact same ordering as the LMD-encoded captions. self.captions = lmd.Reader(captions_path).stream_data(get_meta=False) self.spectrograms = SpectrogramLazyDataset(spectrograms_path) else: self.captions = lmd.Reader(captions_path).stream_data(get_meta=True) self.spectrograms = SpectrogramDataset(spectrograms_path) def __iter__(self): if self.lazy: iterator = ( (tokenize(text), spectrogram) for ((text, _), spectrogram) in zip(self.captions, self.spectrograms) ) else: iterator = ( (tokenize(text), self.spectrograms[meta["index"]]) for (text, meta) in self.captions ) return iterator class SpectrogramDataset(Dataset): def __init__(self, path): self.shard_paths = sorted(glob.glob(f"{path}/*.pt")) self.data = ConcatDataset( [SpectrogramDatasetShard(shard_path) for shard_path in self.shard_paths] ) def __len__(self): return len(self.data) def __getitem__(self, idx): return self.data[idx] class SpectrogramLazyDataset(IterableDataset): def __init__(self, path): self.shard_paths = sorted(glob.glob(f"{path}/*.pt")) def __iter__(self): def lazy_shard_loader(): for shard_path in self.shard_paths: self.shard_data = SpectrogramDatasetShard(shard_path) for example in self.shard_data: yield example return lazy_shard_loader() class SpectrogramDatasetShard(Dataset): def __init__(self, path): self.dataset_shard = TensorDataset(torch.load(path)) def __len__(self): # Layout is [examples, frames, channels] return len(self.dataset_shard) def __getitem__(self, idx): return self.dataset_shard[idx] class PairTextSpectrogramDataset(Dataset): def __init__(self, folder, max_audio_len = 2048, max_text_len = 256): self.paths = [path for path in Path(folder).glob('*.pt')] self.max_audio_len = max_audio_len self.max_text_len = max_text_len def __len__(self): return len(self.paths) def __getitem__(self, idx): max_audio_len, max_text_len = self.max_audio_len, self.max_text_len path = self.paths[idx] data = torch.load(path) audio, text = data['audio'], data['text'] audio = audio[:max_audio_len] text = text[:max_text_len] audio_mask = torch.ones(audio.shape[:-1]).bool() text_mask = torch.ones_like(text).bool() return audio, audio_mask, text, text_mask def pair_text_spectrogram_dataset_collate_fn(batch): audios = [el[0] for el in batch] texts = [el[2] for el in batch] max_audio_len = max([audio.shape[0] for audio in audios]) max_text_len = max([text.shape[0] for text in texts]) padded_batch = [] for audio, audio_mask, text, text_mask in batch: audio_len = audio.shape[0] text_len = text.shape[0] audio_pad_len = max_audio_len - audio_len text_pad_len = max_text_len - text_len if audio_pad_len > 0: audio = F.pad(audio, (0, 0, audio_pad_len, 0), value = 0.) audio_mask = F.pad(audio_mask, (audio_pad_len, 0), value = False) if text_pad_len > 0: text = F.pad(text, (text_pad_len, 0), value = 0.) text_mask = F.pad(text_mask, (text_pad_len, 0), value = False) padded_batch.append((audio, audio_mask, text, text_mask)) output = tuple(map(lambda t: torch.stack(t).numpy(), zip(*padded_batch))) return output def tokenize(text, pad_to=256): # Padding token is 0, the null byte tokens = torch.zeros(pad_to, dtype=torch.uint8) # Truncate to context window size on the right if need be for i, byte in enumerate(text.encode("utf-8")): if i < pad_to: tokens[i] = int(byte) else: break return torch.tensor(tokens) def roundrobin(*iterables): "roundrobin('ABC', 'D', 'EF') --> A D E B F C" # Recipe credited to George Sakkis num_active = len(iterables) nexts = cycle(iter(it).__next__ for it in iterables) while num_active: try: for next in nexts: yield next() except StopIteration: # Remove the iterator we just exhausted from the cycle. num_active -= 1 nexts = cycle(islice(nexts, num_active))
# Modified from Google's Vision Transformer repo, whose notice is reproduced below. # # Copyright 2021 Google LLC. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. class MlpBlock(nn.Module): mlp_dim: int @nn.compact def __call__(self, x): y = nn.Dense(self.mlp_dim)(x) y = nn.gelu(y) return nn.Dense(x.shape[-1])(y) class MixerBlock(nn.Module): """Mixer block layer.""" tokens_mlp_dim: int channels_mlp_dim: int @nn.compact def __call__(self, x): y = nn.LayerNorm()(x) y = jnp.swapaxes(y, 0, 1) y = MlpBlock(self.tokens_mlp_dim, name="token_mixing")(y) y = jnp.swapaxes(y, 0, 1) x = x + y y = nn.LayerNorm()(x) return x + MlpBlock(self.channels_mlp_dim, name="channel_mixing")(y) class MlpMixer(nn.Module): """Mixer architecture.""" patches: Any strides: Any num_classes: int num_blocks: int hidden_dim: int tokens_mlp_dim: int channels_mlp_dim: int @nn.compact def __call__(self, inputs): x = nn.Conv( self.hidden_dim, self.patches.size, strides=self.strides.size, name="stem" )(inputs) x = einops.rearrange(x, "h w c -> (h w) c") for _ in range(self.num_blocks): x = MixerBlock(self.tokens_mlp_dim, self.channels_mlp_dim)(x) x = nn.LayerNorm(name="pre_head_layer_norm")(x) x = jnp.mean(x, axis=0) return nn.Dense( self.num_classes, kernel_init=nn.initializers.zeros, name="head" )(x)
p = torch.nn.Parameter(torch.rand(10,10).cuda()) a = torch.rand(10,10).cuda() p1 = p.data.sum().item() adam = bnb.optim.Adam([p]) out = a*p loss = out.sum() loss.backward() adam.step() p2 = p.data.sum().item() assert p1 != p2 print('SUCCESS!') print('Installation was successful!')
torch.set_printoptions( precision=5, sci_mode=False, linewidth=120, edgeitems=20, threshold=10000 ) k = 20 def assert_all_approx_close(a, b, rtol=1e-3, atol=1e-3, count=0): idx = torch.isclose(a, b, rtol, atol) sumval = (idx == 0).sum().item() if sumval > count: print(f"Too many values not close: assert {sumval} < {count}") torch.testing.assert_allclose(a, b, rtol, atol) class FFN(torch.nn.Module): def __init__(self, input_features, hidden_size, bias=True): super().__init__() self.fc1 = torch.nn.Linear(input_features, hidden_size, bias=bias) self.fc2 = torch.nn.Linear(hidden_size, input_features, bias=bias) with torch.no_grad(): torch.nn.init.xavier_uniform_(self.fc1.weight) torch.nn.init.xavier_uniform_(self.fc2.weight) def forward(self, x): x = torch.relu(self.fc1(x)) x = self.fc2(x) return x class Timer: def __init__(self): self.starts = {} self.ends = {} self.agg = {} def tick(self, name="default"): if name not in self.starts: self.starts[name] = torch.cuda.Event(enable_timing=True) self.ends[name] = torch.cuda.Event(enable_timing=True) self.starts[name].record() else: ms = self.tock(name, evict=True, print_ms=False) def tock(self, name="default", evict=True, print_ms=True): if name in self.ends: self.ends[name].record() torch.cuda.synchronize() ms = self.starts[name].elapsed_time(self.ends[name]) if name not in self.agg: self.agg[name] = 0.0 self.agg[name] += ms if evict: self.starts.pop(name) self.ends.pop(name) if print_ms and name in self.agg: print(f"{name} took: {self.agg[name] / 1000.0:.5f}s") return self.agg[name] def reset(self): self.starts = {} self.ends = {} self.agg = {} print("Resetting benchmark data") def setup(): pass def teardown(): pass @pytest.mark.parametrize( "dtype", [torch.float32, torch.float16], ids=["float", "half"] ) def test_estimate_quantiles(dtype): A = torch.rand(1024, 1024, device="cuda") A = A.to(dtype) code = F.estimate_quantiles(A) percs = torch.linspace(1 / 512, 511 / 512, 256, device=A.device) torch.testing.assert_allclose(percs, code, atol=1e-3, rtol=1e-2) A = torch.randn(1024, 1024, device="cuda") A = A.to(dtype) code = F.estimate_quantiles(A) quantiles = torch.quantile(A.float(), percs) diff = torch.abs(code - quantiles) assert (diff > 5e-02).sum().item() == 0 def test_quantile_quantization(): for i in range(100): A1 = torch.randn(1024, 1024, device="cuda") code = F.estimate_quantiles(A1) C = F.quantize_no_absmax(A1, code) A2 = F.dequantize_no_absmax(C, code) diff = torch.abs(A1 - A2).mean().item() assert diff < 0.0075 A1 = torch.rand(1024, 1024, device="cuda") code = F.estimate_quantiles(A1) C = F.quantize_no_absmax(A1, code) A2 = F.dequantize_no_absmax(C, code) diff = torch.abs(A1 - A2).mean().item() torch.testing.assert_allclose(A1, A2, atol=5e-3, rtol=0) assert diff < 0.001 def test_dynamic_quantization(): diffs = [] reldiffs = [] for i in range(100): A1 = torch.randn(1024, 1024, device="cuda") C, S = F.quantize(A1) A2 = F.dequantize(C, S) diff = torch.abs(A1 - A2) reldiff = diff / torch.abs(A1 + 1e-8) diffs.append(diff.mean().item()) reldiffs.append(reldiff.mean().item()) assert diff.mean().item() < 0.0135 # print(sum(diffs)/len(diffs)) # print(sum(reldiffs)/len(reldiffs)) for i in range(100): A1 = torch.rand(1024, 1024, device="cuda") C, S = F.quantize(A1) A2 = F.dequantize(C, S) diff = torch.abs(A1 - A2).mean().item() torch.testing.assert_allclose(A1, A2, atol=1e-2, rtol=0) assert diff < 0.004 def test_dynamic_blockwise_quantization(): #print('') for blocksize in [4096, 2048, 1024, 512]: diffs = [] reldiffs = [] for i in range(100): A1 = torch.randn(1024, 1024, device="cuda") C, S = F.quantize_blockwise(A1, blocksize=blocksize) A2 = F.dequantize_blockwise(C, S, blocksize=blocksize) diff = torch.abs(A1 - A2) reldiff = diff / torch.abs(A1 + 1e-8) diffs.append(diff.mean().item()) reldiffs.append(reldiff.mean().item()) abserr = sum(diffs)/len(diffs) relerr = sum(reldiffs)/len(reldiffs) assert abserr < 0.011 assert relerr < 0.018 #print('randn', blocksize, sum(diffs)/len(diffs)) #print('randn', blocksize, sum(reldiffs)/len(reldiffs)) diffs = [] for i in range(100): A1 = torch.rand(1024, 1024, device="cuda") C, S = F.quantize_blockwise(A1, blocksize=blocksize) A2 = F.dequantize_blockwise(C, S, blocksize=blocksize) diff = torch.abs(A1 - A2) reldiff = diff / torch.abs(A1 + 1e-8) diffs.append(diff.mean().item()) reldiffs.append(reldiff.mean().item()) #torch.testing.assert_allclose(A1, A2, atol=1e-2, rtol=0) abserr = sum(diffs)/len(diffs) relerr = sum(reldiffs)/len(reldiffs) assert abserr < 0.0035 assert relerr < 0.015 #print('rand', blocksize, sum(diffs)/len(diffs)) #print('rand', blocksize, sum(reldiffs)/len(reldiffs)) def test_dynamic_blockwise_stochastic_quantization(): diffs = [] reldiffs = [] rand = torch.rand(1024).cuda() for i in range(100): A1 = torch.randn(1024, 1024, device="cuda") C1, S1 = F.quantize_blockwise(A1, rand=rand) C2, S2 = F.quantize_blockwise(A1) # a maximunm distance of quantized values of 1 torch.testing.assert_allclose(C1, C2, atol=1, rtol=0) fraction_smaller = (C1 < C2).float().sum() / C1.numel() fraction_larger = (C1 > C2).float().sum() / C1.numel() torch.testing.assert_allclose( fraction_larger, fraction_smaller, atol=0.01, rtol=0 ) @pytest.mark.parametrize( "gtype", [torch.float32, torch.float16], ids=["float", "half"] ) def test_percentile_clipping(gtype): gnorm_vec1 = torch.zeros(100, device="cuda") gnorm_vec2 = torch.zeros(100, device="cuda") n = 4 step = 0 percentile = 5 for i in range(k): step += 1 g = torch.randn(n, n, dtype=gtype, device="cuda") gnorm1, clip2, gnorm_scale = F.percentile_clipping( g, gnorm_vec2, step, percentile=percentile ) assert gnorm_scale == 1.0 if gnorm1 < clip2 else clip2 / gnorm1 gnorm2 = torch.norm(g.float()) if step == 1: gnorm_vec1[:] = gnorm2 else: gnorm_vec1[step % 100] = gnorm2 vals, idx = torch.sort(gnorm_vec1) clip1 = vals[percentile] torch.testing.assert_allclose(gnorm_vec1, torch.sqrt(gnorm_vec2)) torch.testing.assert_allclose(clip1, clip2) torch.testing.assert_allclose(gnorm1, gnorm2) def quant(x): max1 = torch.abs(x).max() x = torch.round(x / max1 * 127) return max1, x.to(torch.int8) def dequant(c, maxC): return c.float() * (maxC / 127) def mm_dequant(maxA, maxB, C): return C.float() * (maxA / 127) * (maxB / 127) def quant_multi(x, dim): max1 = torch.amax(torch.abs(x), dim=dim, keepdim=True) max1[max1 == 0] = 1.0 x = torch.round(x / max1 * 127) return max1, x.to(torch.int8) def quant_multi_chunk(x, dim, chunk_size=32): if dim == 1: x_chunked = einops.rearrange(x, "(c a) b -> c a b", c=chunk_size) max1 = torch.amax(torch.abs(x_chunked), dim=dim + 1, keepdim=True) max1 = torch.tile(max1, (1, 1, x.shape[1])) max1 = max1.view(x.shape) elif dim == 0: x_chunked = einops.rearrange(x, "a (b c) -> a b c", c=chunk_size) max1 = torch.amax(torch.abs(x_chunked), dim=dim, keepdim=True) max1 = torch.tile(max1, (x.shape[0], 1, 1)) max1 = max1.view(x.shape) max1[max1 == 0] = 1.0 x = torch.round(x / max1 * 127) return max1, x.to(torch.int8) def quant_minmax(A): minA = A.min() maxA = A.max() def mean(xx): return sum(xx) / float(len(xx)) # dim1 = torch.randint(1,1024*4, size=(4,)).tolist() # dim2 = torch.randint(1,1024*4, size=(4,)).tolist() dim1 = [1024 * 2] dim2 = [1024 * 16] methods = [ ( lambda x, dim: quant(x), lambda x, dim: quant(x), dequant, dequant, mm_dequant, ) ] methods.append((quant_multi, quant_multi, dequant, dequant, mm_dequant)) # methods.append((lambda x: quant_multi_chunk(x, dim=-1), lambda x: quant_multi_chunk(x, dim=0), dequant, dequant, mm_dequant)) method_names = ["linear", "vectorwise"] batched = [False, True] values = list(product(dim1, dim2, methods, batched)) values_names = list(product(dim1, dim2, method_names, batched)) names = [ "dim1_{}_dim2_{}_quant_{}_batched_{}".format(*vals) for vals in values_names ] @pytest.mark.parametrize( "dim1, dim2, quant_methods, batched", values, ids=names ) def test_approx_igemm(dim1, dim2, quant_methods, batched): dim1 = dim1 - (dim1 % 32) dim2 = dim2 - (dim2 % 32) errors = [] relerrors = [] print("") for i in range(5): if batched: A = torch.normal(0, 0.5, size=(32, dim1, dim2 // 32), device="cuda") B = torch.normal(0, 0.5, size=(32, dim2 // 32, dim1), device="cuda") maxA, Ac = quant_methods[0](A, 2) maxB, Bc = quant_methods[1](B, 1) else: A = torch.normal(0, 0.5, size=(dim1, dim2), device="cuda") B = torch.normal(0, 0.5, size=(dim2, dim1), device="cuda") maxA, Ac = quant_methods[0](A, 1) maxB, Bc = quant_methods[1](B, 0) torch.testing.assert_allclose( quant_methods[2](maxA, Ac), A, atol=0.025, rtol=0.05 ) if batched: out2 = torch.bmm(A, B) C = torch.bmm(Ac.float(), Bc.float()) else: out2 = torch.mm(A, B) C = F.igemm(Ac, Bc) out = quant_methods[4](maxA, maxB, C) std = out2.std() out /= std out2 /= std err = torch.abs(out - out2) relerr = err / torch.abs(out2) errors.append(err.mean().item()) relerrors.append(relerr.mean().item()) print(mean(errors)) print(mean(relerrors)) def test_stable_embedding(): layer = bnb.nn.StableEmbedding(1024, 1024) layer.reset_parameters() n = 2 hidden_dim = torch.randint(32, 256, size=(n,)).tolist() batch_dim = torch.randint(16, 256, size=(n,)).tolist() seq_dim = torch.randint(16, 256, size=(n,)).tolist() transpose = [(False, False), (False, True), (True, False), (True, True)] values = list(product(hidden_dim, batch_dim, transpose, seq_dim)) names = [ "hidden_dim_{}_batch_dim_{},transpose_{}_seq_dim_{}".format(*vals) for vals in values ] @pytest.mark.parametrize( "hidden_dim, batch_dim, transpose, seq_dim", values, ids=names ) def test_igemm(hidden_dim, batch_dim, transpose, seq_dim): hidden_dim = hidden_dim - (hidden_dim % 32) batch_dim = batch_dim - (batch_dim % 16) seq_dim = seq_dim - (seq_dim % 16) for i in range(k): shapeA = ( (batch_dim, hidden_dim) if not transpose[0] else (hidden_dim, batch_dim) ) shapeB = ( (32 * random.randint(1, 4), hidden_dim) if transpose[1] else (hidden_dim, 32 * random.randint(1, 4)) ) A = torch.randint(-128, 127, size=shapeA, device="cuda").to(torch.int8) B = torch.randint(-128, 127, size=shapeB, device="cuda").to(torch.int8) if not transpose[0] and not transpose[1]: out2 = torch.matmul(A.float(), B.float()) out = F.igemm(A, B) elif not transpose[0] and transpose[1]: out2 = torch.matmul(A.float(), B.t().float()) out = F.igemm(A, B.t()) elif transpose[0] and not transpose[1]: out2 = torch.matmul(A.t().float(), B.float()) out = F.igemm(A.t(), B) elif transpose[0] and transpose[1]: out2 = torch.matmul(A.t().float(), B.t().float()) out = F.igemm(A.t(), B.t()) torch.testing.assert_allclose(out.float(), out2) for i in range(k): shapeA = (batch_dim, seq_dim, hidden_dim) shapeB = ( (32 * random.randint(1, 4), hidden_dim) if transpose[1] else (hidden_dim, 32 * random.randint(1, 4)) ) A = torch.randint(-128, 127, size=shapeA, device="cuda").to(torch.int8) B = torch.randint(-128, 127, size=shapeB, device="cuda").to(torch.int8) if not transpose[0] and not transpose[1]: out2 = torch.matmul(A.float(), B.float()) out = F.igemm(A, B) elif not transpose[0] and transpose[1]: out2 = torch.matmul(A.float(), B.t().float()) out = F.igemm(A, B.t()) torch.testing.assert_allclose(out.float(), out2) n = 3 seq_dim = torch.randint(32, 512, size=(n,)).tolist() hidden_dim = torch.randint(32, 1024 * 4, size=(n,)).tolist() batch_dim = torch.randint(2, 16, size=(n,)).tolist() values = list(product(seq_dim, hidden_dim, batch_dim)) names = [ "seq_dim{}_hidden_dim{}_batch_dim{}".format(*vals) for vals in values ] @pytest.mark.parametrize("seq_dim, hidden_dim, batch_dim", values, ids=names) def test_dim3_igemm(seq_dim, hidden_dim, batch_dim): seq_dim = seq_dim - (seq_dim % 32) hidden_dim = hidden_dim - (hidden_dim % 32) batch_dim = batch_dim - (batch_dim % 2) for i in range(25): A = torch.randint( -128, 127, size=(batch_dim, seq_dim, hidden_dim), device="cuda" ).to(torch.int8) B = torch.randint( -128, 127, size=(batch_dim, seq_dim, 1024), device="cuda" ).to(torch.int8) out2 = torch.einsum("bsi, bso->io", A.float(), B.float()) iout = torch.empty( A.shape[2], B.shape[2], dtype=torch.int32, device=A.device ) out = F.igemm(A, B, out=iout) torch.testing.assert_allclose(out.float(), out2) n = 2 seq_dim = torch.randint(32, 512, size=(n,)).tolist() hidden_dim = torch.randint(32, 1024 * 4, size=(n,)).tolist() batch_dim = torch.randint(2, 16, size=(n,)).tolist() transpose = [False, True] values = list(product(seq_dim, hidden_dim, batch_dim, transpose)) names = [ "seq_dim={}_hidden_dim={}_batch_dim={}_transpose{}".format(*vals) for vals in values ] @pytest.mark.parametrize( "seq_dim, hidden_dim, batch_dim, transpose", values, ids=names ) def test_minmax_igemm(seq_dim, hidden_dim, batch_dim, transpose): def min_max(x): maxA = torch.amax(x, dim=2, keepdim=True) minA = torch.amin(x, dim=2, keepdim=True) scale = (maxA - minA) / 2.0 return (127 * (x - minA - scale) / scale).to(torch.int8), minA, scale seq_dim = seq_dim - (seq_dim % 16) hidden_dim = hidden_dim - (hidden_dim % 16) batch_dim = batch_dim - (batch_dim % 2) errs = [] relerrs = [] errs2 = [] relerrs2 = [] for i in range(k): A = torch.normal( 0.0, 0.5, size=(batch_dim, seq_dim, hidden_dim), device="cuda" ) if transpose: B = torch.normal(0, 0.5, size=(256, hidden_dim), device="cuda") else: B = torch.normal(0, 0.5, size=(hidden_dim, 256), device="cuda") Ac, minA, scale = min_max(A) if transpose: maxB, Bc = quant_multi(B, dim=(1 if transpose else 0)) out = F.igemm(Ac, Bc.t()) out2 = torch.matmul(A, B.t()) offset = B.t().sum(0) * (minA + scale) out = out.float() out = (out * maxB.t() * scale / (127 * 127)) + offset maxA, Ac = quant_multi(A, dim=2) out3 = F.igemm(Ac, Bc.t()) out3 = mm_dequant(maxA, maxB.t(), out3) else: maxB, Bc = quant_multi(B, dim=0) offset = B.sum(0) * (minA + scale) out = F.igemm(Ac, Bc) out2 = torch.matmul(A, B) out = out.float() out = (out * maxB * scale / (127 * 127)) + offset maxA, Ac = quant_multi(A, dim=2) out3 = F.igemm(Ac, Bc) out3 = mm_dequant(maxA, maxB, out3) std = out2.std() out2 /= std out /= std out3 /= std err = torch.abs(out - out2) relerr = err / (torch.abs(out2) + 1e-7) err2 = torch.abs(out3 - out2) relerr2 = err2 / (torch.abs(out2) + 1e-7) errs.append(err.mean().item()) relerrs.append(relerr.mean().item()) errs2.append(err2.mean().item()) relerrs2.append(relerr2.mean().item()) # print(mean(errs)) # print(mean(relerrs)) # print(mean(errs2)) # print(mean(relerrs2)) assert mean(errs) < 0.015 assert mean(relerrs) < 0.3 n = 2 dim1 = torch.randint(1, 64, size=(n,)).tolist() dim2 = torch.randint(32, 128, size=(n,)).tolist() dim3 = torch.randint(32, 256, size=(n,)).tolist() dim4 = torch.randint(32, 256, size=(n,)).tolist() transpose = [(False, False), (True, False), (False, True), (True, True)] values = list(product(dim1, dim2, dim3, dim4, transpose)) names = [ "dim1_{}_dim2_{}_dim3_{}_dim4_{}_transpose_{}".format(*vals) for vals in values ] @pytest.mark.parametrize("dim1, dim2, dim3, dim4, transpose", values, ids=names) def test_ibmm(dim1, dim2, dim3, dim4, transpose): dim2 = dim2 - (dim2 % 16) dim3 = dim3 - (dim3 % 16) dim4 = dim4 - (dim4 % 16) for i in range(k): shapeA = (dim1, dim3, dim2) if transpose[0] else (dim1, dim2, dim3) shapeB = (dim1, dim4, dim3) if transpose[1] else (dim1, dim3, dim4) A = torch.randint(-128, 127, size=shapeA, device="cuda").to(torch.int8) B = torch.randint(-128, 127, size=shapeB, device="cuda").to(torch.int8) if not transpose[0] and not transpose[1]: out2 = torch.bmm(A.float(), B.float()) out = F.igemm(A, B) elif not transpose[0] and transpose[1]: out2 = torch.bmm(A.float(), B.permute([0, 2, 1]).float()) out = F.igemm(A, B.permute([0, 2, 1])) elif transpose[0] and not transpose[1]: out2 = torch.bmm(A.permute([0, 2, 1]).float(), B.float()) out = F.igemm(A.permute([0, 2, 1]), B) elif transpose[0] and transpose[1]: out2 = torch.bmm( A.permute([0, 2, 1]).float(), B.permute([0, 2, 1]).float() ) out = F.igemm(A.permute([0, 2, 1]), B.permute([0, 2, 1])) torch.testing.assert_allclose(out.float(), out2.float()) n = 1 dim1 = torch.randint(1, 64, size=(n,)).tolist() dim2 = torch.randint(32, 128, size=(n,)).tolist() dim3 = torch.randint(32, 256, size=(n,)).tolist() values = list(product(dim1, dim2, dim3)) names = ["dim1_{}_dim2_{}_dim3_{}".format(*vals) for vals in values] @pytest.mark.parametrize("dim1, dim2, dim3", values, ids=names) def test_vector_quant(dim1, dim2, dim3): dim2 = dim2 - (dim2 % 16) dim3 = dim3 - (dim3 % 16) for i in range(k): A = torch.randn(size=(dim2, dim3), device="cuda") qA, SA = F.vectorwise_quant(A, dim=0) A1 = F.vectorwise_dequant(qA, SA) n = A1.numel() assert_all_approx_close(A1, A, atol=0.01, rtol=0.1, count=int(n*0.002)) n = 2 dim1 = torch.randint(2, 256, size=(n,)).tolist() dim2 = torch.randint(2, 256, size=(n,)).tolist() dim3 = torch.randint(2, 256, size=(n,)).tolist() # dim1, dim2 = (256,), (256,) dtype = [torch.int8, torch.int32] a_order = ["row"] out_order = ["col", "row", "col32"] transpose = [False] dims = [2, 3] values = list(product(dim1, dim2, dim3, dims, dtype, a_order, out_order, transpose)) names = ["dim1_{}_dim2_{}_dim3_{}_dims_{}_dtype_{}_orderA_{}_orderOut_{}_transpose_{}".format(*vals)for vals in values] @pytest.mark.parametrize("dim1, dim2, dim3, dims, dtype, orderA, orderOut, transpose",values,ids=names) def test_nvidia_transform(dim1, dim2, dim3, dims, dtype, orderA, orderOut, transpose): if dims == 3 and out_order != "col32": return if dtype == torch.int32 and out_order != "col32": return func = F.get_transform_func(dtype, orderA, orderOut, transpose) if dims == 2: A = torch.randint(-128, 127, size=(dim1, dim2), device="cuda").to(dtype) elif dims == 3: A = torch.randint(-128, 127, size=(dim1, dim2, dim3), device="cuda").to( dtype ) out, S = F.nvidia_transform(A, to_order=orderOut) if orderOut == "row": torch.testing.assert_allclose(A.flatten(), out.flatten()) elif orderOut == "col": torch.testing.assert_allclose(A.t().flatten(), out.flatten()) elif orderOut == "col32": if dims == 2: n = A.shape[0] * (A.shape[1] + (32 - (A.shape[1] % 32))) elif dims == 3: n = ( A.shape[0] * A.shape[1] * (A.shape[2] + (32 - (A.shape[2] % 32))) ) assert out.numel() == n elif orderOut == "col_turing": # 32 col 8 row tiles n = (A.shape[0] + (8 - A.shape[0] % 8)) * ( A.shape[1] + (32 - (A.shape[1] % 32)) ) assert out.numel() == n total_coltile = (A.shape[1] // 32) + (1 if A.shape[1] % 32 != 0 else 0) for row in range(A.shape[0]): for col in range(A.shape[1]): i = row * A.shape[1] j = col coltile = (col // 32) + (1 if col % 32 != 0 else 0) rowtile = ( (row // 8) + (1 if row % 8 != 0 else 0) ) * total_coltile offset = 32 * 8 * (rowtile + coltile) col2 = col % 32 row2 = (row % 8) * 32 assert A.flatten()[i + j] == A[row, col] # assert A.flatten()[i+j] == out.flatten()[row2+col2] # torch.testing.assert_allclose(A.flatten()[i+j], A[row, col]) # torch.testing.assert_allclose(A.flatten()[i+j], out.flatten()[row2+ col2+block_offset]) if orderOut == "col32": out2, S = F.nvidia_transform( out, from_order=orderOut, to_order="row", state=S ) torch.testing.assert_allclose(A, out2) n = 1 dim1 = torch.randint(1, 256, size=(n,)).tolist() dim2 = torch.randint(32, 512, size=(n,)).tolist() dim3 = torch.randint(32, 1024, size=(n,)).tolist() dim4 = torch.randint(32, 1024, size=(n,)).tolist() # dim1 = [2] # dim2 = [2] # dim3 = [2] # dim4 = [2] dims = (2, 3) ldb = [0] # ldb = list(range(256, 1*1024, 256)) values = list(product(dim1, dim2, dim3, dim4, dims, ldb)) names = [ "dim1_{}_dim2_{}_dim3_{}_dim4_{}_dims_{}_ldb_{}".format(*vals) for vals in values ] @pytest.mark.parametrize("dim1, dim2, dim3, dim4, dims, ldb", values, ids=names) def test_igemmlt_int(dim1, dim2, dim3, dim4, dims, ldb): for i in range(k): if dims == 2: A = torch.randint(-128, 127, size=(dim1, dim3), device="cuda").to( torch.int8 ) elif dims == 3: A = torch.randint( -128, 127, size=(dim1, dim2, dim3), device="cuda" ).to(torch.int8) B = torch.randint(-128, 127, size=(dim4, dim3), device="cuda").to( torch.int8 ) C1 = torch.matmul(A.float(), B.t().float()) A2, SA = F.transform(A, "col32") B2, SB = F.transform(B, "col_turing") C2, SC = F.igemmlt(A2, B2, SA, SB) C3, S = F.nvidia_transform(C2, "row", state=SC) torch.testing.assert_allclose(C1, C3.float()) # transpose B = torch.randint(-128, 127, size=(dim3, dim4), device="cuda").to( torch.int8 ) C1 = torch.matmul(A.float(), B.float()) B2t, SBt = F.transform(B, "col_turing", transpose=True) C2, SC = F.igemmlt(A2, B2t, SA, SBt) C3, S = F.nvidia_transform(C2, "row", state=SC) torch.testing.assert_allclose(C1, C3.float()) dim1 = [32] dim2 = [32] dim3 = [32] dim4 = [32] dims = (2,) # ldb = list(range(256, 1*1024, 256)) values = list(product(dim1, dim2, dim3, dim4, dims)) names = [ "dim1_{}_dim2_{}_dim3_{}_dim4_{}_dims_{}".format(*vals) for vals in values ] @pytest.mark.parametrize("dim1, dim2, dim3, dim4, dims", values, ids=names) def test_igemmlt_half(dim1, dim2, dim3, dim4, dims): formatB = F.get_special_format_str() for i in range(k): if dims == 2: A = torch.normal(0, 0.5, size=(dim1, dim3), device="cuda").half() elif dims == 3: A = torch.normal( 0, 0.5, size=(dim1, dim2, dim3), device="cuda" ).half() B = torch.randn((dim4, dim3), device="cuda").half() torch.nn.init.xavier_uniform_(B) C1 = torch.matmul(A, B.t()) C2 = bnb.matmul(A, B.t()) A = A.view(-1, A.shape[-1]) CA, CAt, statsA, statsAt, coo_tensor = F.double_quant(A) CB, CBt, statsB, statsBt, coo_tensor = F.double_quant(B) C32A, SA = F.transform(CA, "col32") CxB, SB = F.transform(CB, to_order=formatB) out1_32, Sout1_32 = F.igemmlt(C32A, CxB, SA, SB) output = F.mm_dequant(out1_32, Sout1_32, statsAt, statsBt) # print('') # print(output.flatten()[:10]) # print(C1.flatten()[:10]) # print(C2.flatten()[:10]) # torch.testing.assert_allclose(C1.view(-1, C1.shape[-1]), output, atol=0.025, rtol=0.05) # transpose # B = torch.randint(-128, 127, size=(dim3, dim4), device='cuda').to(torch.int8) # C1 = torch.matmul(A.float(), B.float()) # B2t, SBt = F.transform2(B, 'col_turing', transpose=True) # C2, SC = F.igemmlt(A2, B2t, SA, SBt) # C3, S = F.transform(C2, 'row', state=SC) # torch.testing.assert_allclose(C1, C3.float()) batch_size = 2 seqdim = 512 # values = [(batch_size, seqdim, 4*1024, 16*1024),(batch_size, seqdim, 5120, 4*5120),(batch_size, seqdim, 12*1024, 4*12*1024)] values = [ (batch_size, seqdim, 4 * 1024, 3 * 4 * 1024), (batch_size, seqdim, 5120, 3 * 5120), (batch_size, seqdim, 12 * 1024, 4 * 12 * 1024), ] # values = list(product(batch, seq, model, hidden)) names = [ "batch_{}_seq_{}_model_{}_hidden_{}".format(*vals) for vals in values ] @pytest.mark.parametrize("batch, seq, model, hidden", values, ids=names) def test_bench_8bit_training(batch, seq, model, hidden): formatB = F.get_special_format_str() A = torch.randn(batch, seq, model, device="cuda").half() grad = torch.randn(batch, seq, model, device="cuda").half() w1 = torch.randint(-128, 127, size=(hidden, model), device="cuda").half() w2 = torch.randint(-128, 127, size=(model, hidden), device="cuda").half() print("") # torch.cuda.synchronize() ## warmup # for i in range(100): # torch.matmul(A, w1.t()) # torch.cuda.synchronize() dtype = torch.int8 A = A.view(-1, A.shape[-1]).contiguous() grad = grad.view(-1, grad.shape[-1]).contiguous() torch.cuda.synchronize() t0 = time.time() for i in range(k): out1 = torch.matmul(A, w1.t()) # fc1 # out2 = torch.matmul(out1, w2.t())# fc2 # d1 = torch.matmul(grad, w2) # delta1 # d2 = torch.matmul(d1, w1) # delta2 # grad1 = torch.einsum('bo,bh->oh', out1, grad) # grad w2 # grad2 = torch.einsum('bh,bo->ho', A, d2) # grad w1 torch.cuda.synchronize() t16 = time.time() - t0 print(t16) # torch.cuda.empty_cache() # Cw1, Cw1t, statsw1, statsw1t, coo_tensor = F.double_quant(w1) # Cw2, Cw2t, statsw2, statsw2t, coo_tensor = F.double_quant(w2) # CTw1, Sw1 = F.transform2(Cw1, formatB) # CTw2, Sw2 = F.transform2(Cw2, formatB) # CTw2t, Sw2t = F.transform2(Cw2t, formatB, transpose=True) # CTw1t, Sw1t = F.transform2(Cw1t, formatB, transpose=True) # CA, CAt, statsA, statsAt, coo_tensor = F.double_quant(A) # C32A, SA = F.transform2(CA, 'col32') ## fc1 # out1_32, Sout1_32 = F.igemmlt(C32A, CTw1, SA, Sw1, dtype=dtype) ##out1 = F.mm_dequant(out1_32, Sout1_32, statsAt, statsw1t) ## fc2 # Cout1, Cout1t, statsout1, statsout1t, coo_tensor = F.double_quant(out1) # C32out1, Sout1 = F.transform2(Cout1, 'col32') # out2_32, Sout2_32 = F.igemmlt(C32out1, CTw2, Sout1, Sw2, dtype=dtype) ##out2 = F.mm_dequant(out2_32, Sout2_32, statsout1t, statsw2t) ## delta1 # Cgrad, Cgradt, statsgrad, statsgradt, coo_tensor = F.double_quant(grad) # C32grad, Sgrad = F.transform2(Cgrad, 'col32') ##d1_32, Sd1_32 = F.igemmlt(C32grad, CTw2t, Sgrad, Sw2t, dtype=dtype) ##d1 = F.mm_dequant(d1_32, Sd1_32, statsgradt, statsw2) ## delta2 # Cd1, Cd1t, statsd1, statsd1t, coo_tensor = F.double_quant(d1) # C32d1, Sd1 = F.transform2(Cd1, 'col32') ##d2_32, Sd2_32 = F.igemmlt(C32d1, CTw1t, Sd1, Sw1t, dtype=dtype) ##d2 = F.mm_dequant(d2_32, Sd2_32, statsd1t, statsw1) ## grad1 # C32out1t, Sout1t = F.transform2(Cout1t, 'col32', transpose=True) # CTgradt, Sgradt = F.transform2(Cgradt, formatB, transpose=True) ##grad1_32, Sgrad1_32 = F.igemmlt(C32out1t, CTgradt, Sout1t, Sgradt, dtype=dtype) ##grad1 = F.mm_dequant(grad1_32, Sgrad1_32, statsout1, statsgrad) ## grad2 # C32At, SAt = F.transform2(CAt, 'col32', transpose=True) # CTd1t, Sd1t = F.transform2(Cd1t, formatB, transpose=True) ##grad2_32, Sgrad2_32 = F.igemmlt(C32At, CTd1t, SAt, Sd1t, dtype=dtype) ##grad2 = F.mm_dequant(grad2_32, Sgrad2_32, statsA, statsd1) # Cw2, Cw2t, statsw2, statsw2t, coo_tensor = F.double_quant(w2) # Cw1, Cw1t, statsw1, statsw1t, coo_tensor = F.double_quant(w1) # Cw2, Cw2t, statsw2, statsw2t, coo_tensor = F.double_quant(w2) # CTw1, Sw1 = F.transform2(Cw1, formatB) # CTw1t, Sw1t = F.transform2(Cw1t, formatB, transpose=True) # CTw2, Sw2 = F.transform2(Cw2, formatB) # CTw2t, Sw2t = F.transform2(Cw2t, formatB, transpose=True) # torch.cuda.synchronize() # t0 = time.time() # for i in range(k): # #Cw1, Cw1t, statsw1, statsw1t, coo_tensor = F.double_quant(w1) # #CTw1, Sw1 = F.transform2(Cw1, formatB) # #Cw1, Cw1t, statsw1, statsw1t, coo_tensor = F.double_quant(w1) # #CTw1, Sw1 = F.transform2(Cw1, formatB) # #CA, CAt, statsA, statsAt, coo_tensor = F.double_quant(A, threshold=3.5) # CA, CAt, statsA, statsAt, coo_tensor = F.double_quant(A) # #CTw1t, Sw1t = F.transform2(Cw1t, formatB, transpose=True) # #CTw2, Sw2 = F.transform2(Cw2, formatB) # #CTw2t, Sw2t = F.transform2(Cw2t, formatB, transpose=True) # C32A, SA = F.transform2(CA, 'col32') # # fc1 # out1_32, Sout1_32 = F.igemmlt(C32A, CTw1, SA, Sw1, dtype=dtype) # #out1dn = F.mm_dequant(out1_32, Sout1_32, statsA, statsw1) # #print(coo_tensor.nnz) # #out1sp = F.spmm_coo(coo_tensor, w1.t()) # #print(w1.t().shape) # #out1 = out1dn + out1sp # # fc2 # Cout1, Cout1t, statsout1, statsout1t, coo_tensor = F.double_quant(out1) # C32out1, Sout1 = F.transform2(Cout1, 'col32') # out2_32, Sout2_32 = F.igemmlt(C32out1, CTw2, Sout1, Sw2, dtype=dtype) # #out2 = F.mm_dequant(out2_32, Sout2_32, statsout1, statsw2) # # delta1 # Cgrad, Cgradt, statsgrad, statsgradt, coo_tensor = F.double_quant(grad) # C32grad, Sgrad = F.transform2(Cgrad, 'col32') # d1_32, Sd1_32 = F.igemmlt(C32grad, CTw2t, Sgrad, Sw2t, dtype=dtype) # #d1 = F.mm_dequant(d1_32, Sd1_32, statsgrad, statsw2t) # # delta2 # Cd1, Cd1t, statsd1, statsd1t, coo_tensor = F.double_quant(d1) # C32d1, Sd1 = F.transform2(Cd1, 'col32') # d2_32, Sd2_32 = F.igemmlt(C32d1, CTw1t, Sd1, Sw1t, dtype=dtype) # #d2 = F.mm_dequant(d2_32, Sd2_32, statsd1, statsw1t) # # grad1 # #C32out1t, Sout1t = F.transform2(Cout1t, 'col32', transpose=True) # #CTgradt, Sgradt = F.transform2(Cgradt, formatB, transpose=True) # #grad1_32, Sgrad1_32 = F.igemmlt(C32out1t, CTgradt, Sout1t, Sgradt, dtype=dtype) # #grad1 = F.mm_dequant(grad1_32, Sgrad1_32, statsout1t, statsgradt) # ## grad2 # #C32At, SAt = F.transform2(CAt, 'col32', transpose=True) # #CTd1t, Sd1t = F.transform2(Cd1t, formatB, transpose=True) # #grad2_32, Sgrad2_32 = F.igemmlt(C32At, CTd1t, SAt, Sd1t, dtype=dtype) # #grad2 = F.mm_dequant(grad2_32, Sgrad2_32, statsAt, statsd1t) # torch.cuda.synchronize() # t8 = time.time() - t0 # print(t8) n = 2 dim1 = torch.randint(64, 256, size=(n,)).tolist() dim4 = torch.randint(64, 1024, size=(n,)).tolist() #dim1 = [2*1024] #dim4 = [2*1024] #dim1 = [4] #dim4 = [4] dims = (2,) formatB = ["col_turing", "col_ampere"] has_bias = [True, False] values = list(product(dim1, dim4, dims, formatB, has_bias)) names = ["dim1_{}_dim4_{}_dims_{}_formatB_{}_has_bias_{}".format(*vals) for vals in values] @pytest.mark.parametrize("dim1, dim4, dims, formatB, has_bias", values, ids=names) def test_dequant_mm(dim1, dim4, dims, formatB, has_bias): inner = torch.randint(1, 128, size=(1,)).item() bias = None if has_bias: bias = torch.randn(dim4, device='cuda', dtype=torch.float16) formatB = F.get_special_format_str() for i in range(1): A = torch.randn(dim1, inner, device="cuda") B = torch.randn(dim4, inner, device="cuda") C1 = torch.matmul(A.half(), B.t().half()) if has_bias: C1 += bias A1, maxA = F.vectorwise_quant(A, dim=1) B1, maxB = F.vectorwise_quant(B, dim=1) A2, SA = F.nvidia_transform(A1, "col32") B2, SB = F.nvidia_transform(B1, formatB) C2, SC = F.igemmlt(A2, B2, SA, SB) C3, S = F.nvidia_transform(C2, "row", state=SC) C4 = F.vectorwise_mm_dequant(C3.float(), maxA, maxB.t()) if has_bias: C4 += bias # TODO: is something wrong here? If so, the problem goes deeper #n = C1.numel() #p = 0.06 std = C1.std(0).view(1, -1) C1 /= std C4 /= std #assert_all_approx_close(C1, C4, atol=0.02, rtol=0.1, count=int(n*0.06)) #assert (count / n < p), f"error in more than {p} of elements: {count}/{n}={count/n}" C5 = F.mm_dequant(C2, SC, maxA.flatten(), maxB.flatten(), bias=bias) #torch.testing.assert_allclose(C5, C4, atol=0.015, rtol=0.1) n = C5.numel() assert_all_approx_close(C1, C4, atol=0.015, rtol=0.1, count=int(0.01*n)) n = 2 dim1 = [1 * 1024] dim2 = [1 * 1024] # dim1 = torch.randint(1,4*1024, size=(n,)).tolist() # dim2 = torch.randint(1,4*1024, size=(n,)).tolist() dims = (2,) # ldb = list(range(256, 1*1024, 256)) values = list(product(dim1, dim2, dims)) names = ["dim1_{}_dim2_{}_dims_{}".format(*vals) for vals in values] @pytest.mark.parametrize("dim1, dim2, dims", values, ids=names) def test_colrow_absmax(dim1, dim2, dims): for i in range(k): threshold = 3.0 A = torch.randn(dim1, dim2, device="cuda").half() A_truncated = A.clone() A_truncated[torch.abs(A_truncated) >= 3.0] = 0.0 if dims == 2: row_stats1, _ = torch.abs(A.float()).max(1) col_stats1, _ = torch.abs(A.float()).max(0) row_stats1_trunc, _ = torch.abs(A_truncated.float()).max(1) col_stats1_trunc, _ = torch.abs(A_truncated.float()).max(0) else: assert False row_stats2, col_stats2, nnz_block_ptr2 = F.get_colrow_absmax( A, threshold=threshold ) A_blocked = einops.rearrange( torch.abs(A), "(rows row_tiles) (cols block_size)-> rows cols row_tiles block_size", row_tiles=16, block_size=64 * 4, ) nnz_rows1_counts = (torch.abs(A_blocked) >= threshold).sum(3).flatten() nnz_block_ptr1 = torch.zeros( nnz_rows1_counts.shape[0] + 1, dtype=nnz_rows1_counts.dtype, device=nnz_rows1_counts.device, ) nnz_block_ptr1[1:] = nnz_rows1_counts.cumsum(0) torch.testing.assert_allclose(col_stats1_trunc, col_stats2) torch.testing.assert_allclose(row_stats1_trunc, row_stats2) torch.testing.assert_allclose(nnz_block_ptr1, nnz_block_ptr2) row_stats2, col_stats2, nnz_block_ptr2 = F.get_colrow_absmax( A, threshold=0.0 ) torch.testing.assert_allclose(col_stats1, col_stats2) torch.testing.assert_allclose(row_stats1, row_stats2) assert nnz_block_ptr2 is None n = 2 # dim1 = [8*1024] # dim2 = [4*1024] dim1 = torch.randint(1, 4 * 1024, size=(n,)).tolist() dim2 = torch.randint(1, 4 * 1024, size=(n,)).tolist() values = list(product(dim1, dim2)) names = ["dim1_{}_dim2_{}".format(*vals) for vals in values] @pytest.mark.parametrize("dim1, dim2", values, ids=names) def test_double_quant(dim1, dim2): for i in range(k): A = torch.randn(dim1, dim2, device="cuda").half() out_col1, Scol = F.vectorwise_quant(A, dim=0) out_row1, Srow = F.vectorwise_quant(A, dim=1) CA, CAt, statsA, statsAt, coo_tensor = F.double_quant(A) # max difference is 1 due to rounding differences torch.testing.assert_allclose(CA, out_row1, atol=1, rtol=0) torch.testing.assert_allclose(CAt, out_col1, atol=1, rtol=0) n = CAt.numel() num_not_close_rows = ( (torch.isclose(CA, out_row1, atol=1) == 0).sum().item() ) num_not_close_cols = ( (torch.isclose(CAt, out_col1, atol=1) == 0).sum().item() ) # allow for 1:500 error due to rounding differences min_error = 1 / 500 if num_not_close_cols > (min_error * n): print( f"Min error exceeded {num_not_close_cols} elements are different. Error: {num_not_close_cols/n:.4f}" ) assert False if num_not_close_rows > (min_error * n): print( f"Min error exceeded {num_not_close_rows} elements are different. Error: {num_not_close_rows/n:.4f}" ) assert False torch.testing.assert_allclose(Srow.flatten(), statsA) torch.testing.assert_allclose(Scol.flatten(), statsAt) n = 4 dim1 = torch.randint(1, 4 * 1024, size=(n,)).tolist() dim4 = torch.randint(1, 4 * 1024, size=(n,)).tolist() inner = torch.randint(1, 4 * 1024, size=(n,)).tolist() values = list(zip(dim1, dim4, inner)) names = ["dim1_{}_dim4_{}_inner_{}".format(*vals) for vals in values] @pytest.mark.parametrize("dim1, dim4, inner", values, ids=names) def test_integrated_igemmlt(dim1, dim4, inner): for i in range(k): A = torch.randn(dim1, inner, device="cuda").half() B = torch.randn(dim4, inner, device="cuda").half() out1 = torch.matmul(A.half(), B.t().half()) C1a, C1b, stats1a, stats1b, coo_tensor = F.double_quant(A) C2a, C2b, stats2a, stats2b, coo_tensor = F.double_quant(B) A1, maxA = F.vectorwise_quant(A, dim=1) B1, maxB = F.vectorwise_quant(B, dim=1) torch.testing.assert_allclose(maxA.flatten(), stats1a) torch.testing.assert_allclose(maxB.flatten(), stats2a) torch.testing.assert_allclose(C1a, A1, rtol=0, atol=1) torch.testing.assert_allclose(C2a, B1, rtol=0, atol=1) A2, SA = F.nvidia_transform(C1a, "col32") B2, SB = F.nvidia_transform(C2a, "col_turing") outC32, SC = F.igemmlt(A2, B2, SA, SB) out2 = F.mm_dequant(outC32, SC, stats1a, stats2a) A2, SA = F.nvidia_transform(A1, "col32") B2, SB = F.nvidia_transform(B1, "col_turing") C2, SC = F.igemmlt(A2, B2, SA, SB) C3, S = F.nvidia_transform(C2, "row", state=SC) out3 = F.vectorwise_mm_dequant(C3.float(), maxA, maxB.t()) err1 = torch.abs(out1 - out2).mean().item() err2 = torch.abs(out1 - out3).mean().item() assert err2 <= err1 * 1.025 n = 6 dim1 = torch.randint(1, 4 * 1024, size=(n,)).tolist() dim4 = torch.randint(1, 4 * 1024, size=(n,)).tolist() inner = torch.randint(1, 4 * 1024, size=(n,)).tolist() values = list(zip(dim1, dim4, inner)) names = ["dim1_{}_dim4_{}_inner_{}".format(*vals) for vals in values] @pytest.mark.parametrize("dim1, dim4, inner", values, ids=names) @pytest.mark.skip("Row scale has some bugs for ampere") def test_igemmlt_row_scale(dim1, dim4, inner): formatB = F.get_special_format_str() err1, err2, err3 = [], [], [] relerr1, relerr2 = [], [] scale = 1 for i in range(k): A = torch.randn(dim1, inner, device="cuda").half() B = torch.randn(dim4, inner, device="cuda").half() torch.nn.init.xavier_uniform_(B) C1 = torch.matmul(A, B.t()) out1 = torch.matmul(A.half(), B.t().half()) C1a, C1b, stats1a, stats1b, coo_tensor = F.double_quant(A) CB, absmaxB = F.vectorwise_quant(B, quant_type="linear") A2, SA = F.nvidia_transform(C1a, "col32") B2, SB = F.nvidia_transform(CB, formatB) A1, maxA = F.vectorwise_quant(A, dim=1) c = 10.0 * inner * scale row_scale = torch.ones_like(maxA) / c outC32, SC = F.igemmlt( A2, B2, SA, SB, dtype=torch.int8, row_scale=row_scale ) C3, S = F.nvidia_transform(outC32, "row", state=SC) maxval = torch.abs(C3).max() if maxval == 127: scale = 1.5 else: scale = maxval / 120 out3 = C3 * maxA * absmaxB * c / (127 * 127) C4 = torch.matmul(C1a.float(), CB.float().t()) C2a, C2b, stats2a, stats2b, coo_tensor = F.double_quant(B) B2, SB = F.nvidia_transform(C2a, formatB) outC32, SC = F.igemmlt(A2, B2, SA, SB) out2 = F.mm_dequant(outC32, SC, stats1a, stats2a) CA, SA = F.vectorwise_quant(A, dim=1, quant_type="vector") CB, SB = F.vectorwise_quant(B, dim=1, quant_type="linear") C = torch.matmul(CA.float(), CB.t().float()) out4 = C * SA * SB / (127 * 127) # out4 = torch.clip(torch.round(C*SA/c), -127, 127)*c*SB/(127*127) # print('='*80) # print(out1) # print(out2) # print(out3) # print(out1) # print(out2) # print(out3) err1.append(torch.abs(out1 - out2).mean().item()) err2.append(torch.abs(out1 - out3).mean().item()) err3.append(torch.abs(out1 - out4).mean().item()) # assert_all_approx_close(C3.float(), torch.round(C4*row_scale), rtol=0, atol=0, count=10) print("") print(sum(err1) / len(err1)) print(sum(err2) / len(err2)) print(sum(err3) / len(err3)) dim1 = [1024, 2048] inner = [12288 * 4, 4096 * 4] dim4 = [12288, 4096] values = list(zip(dim1, dim4, inner)) names = ["dim1_{}_dim4_{}_inner_{}".format(*vals) for vals in values] @pytest.mark.parametrize("dim1, dim4, inner", values, ids=names) @pytest.mark.skip("Row scale has some bugs for ampere") def test_row_scale_bench(dim1, dim4, inner): err1, err2, err3 = [], [], [] relerr1, relerr2 = [], [] scale = 1 A = torch.randn(dim1, inner, device="cuda").half() B = torch.randn(dim4, inner, device="cuda").half() torch.nn.init.xavier_uniform_(B) # warmpup for i in range(k): C1 = torch.matmul(A, B.t()) torch.cuda.synchronize() t0 = time.time() for i in range(k): C1 = torch.matmul(A, B.t()) torch.cuda.synchronize() print("16", time.time() - t0) C1a, C1b, stats1a, stats1b, coo_tensor = F.double_quant(A) CB, absmaxB = F.vectorwise_quant(B, quant_type="linear") A2, SA = F.nvidia_transform(C1a, "col32") B2, SB = F.nvidia_transform(CB, formatB) A1, maxA = F.vectorwise_quant(A, dim=1) c = 10.0 * inner * scale row_scale = maxA / c torch.cuda.synchronize() t0 = time.time() for i in range(k): outC32, SC = F.igemmlt( A2, B2, SA, SB, dtype=torch.int8, row_scale=row_scale ) torch.cuda.synchronize() print("row-wise", time.time() - t0) C2a, C2b, stats2a, stats2b, coo_tensor = F.double_quant(B) B2, SB = F.nvidia_transform(C2a, formatB) torch.cuda.synchronize() t0 = time.time() for i in range(k): outC32, SC = F.igemmlt(A2, B2, SA, SB) torch.cuda.synchronize() print("vector-wise", time.time() - t0) n = 2 dim1 = torch.randint(2, 1024, size=(n,)).tolist() dim2 = torch.randint(2, 1024, size=(n,)).tolist() # dim1 = [8*1024] # dim2 = [4*1024] dim3 = [0] dtype = [torch.int8] a_order = ["row"] out_order = ["col32", "col_turing", "col_ampere"] transpose = [False, True] dims = [2] values = list( product(dim1, dim2, dim3, dims, dtype, a_order, out_order, transpose) ) names = [ "dim1_{}_dim2_{}_dim3_{}_dims_{}_dtype_{}_orderA_{}_orderOut_{}_{}".format( *vals ) for vals in values ] @pytest.mark.parametrize( "dim1, dim2, dim3, dims, dtype, orderA, orderOut, transpose", values, ids=names, ) def test_transform(dim1, dim2, dim3, dims, dtype, orderA, orderOut, transpose): for i in range(k): if dims == 2: A = torch.randint(10, 99, size=(dim1, dim2), device="cuda").to( dtype ) elif dims == 3: A = torch.randint( 10, 99, size=(dim1, dim2, dim3), device="cuda" ).to(dtype) A.view(-1)[-1] = -1 if transpose: At = A.t().contiguous() out1, S1 = F.nvidia_transform(At, to_order=orderOut) else: out1, S1 = F.nvidia_transform(A, to_order=orderOut) out2, S2 = F.transform(A, to_order=orderOut, transpose=transpose) assert S1[0][0] == S2[0][0] assert S1[0][1] == S2[0][1] # print(out1) # print(out2) torch.testing.assert_allclose(out1, out2) n = 2 # dim1 = torch.randint(2,1024, size=(n,)).tolist() # dim2 = torch.randint(2,1024, size=(n,)).tolist() dim1 = [1] dim2 = [33] dtype = [torch.int8] # a_order = ['col_turing', 'col_ampere'] a_order = ["col_turing"] out_order = ["row"] values = list(product(dim1, dim2, dtype, a_order, out_order)) names = [ "dim1_{}_dim2_{}_dtype_{}_orderA_{}_orderOut_{}".format(*vals) for vals in values ] def test_overflow(): formatB = F.get_special_format_str() print(formatB) for i in range(2): a = torch.arange(5, 15).cuda().to(torch.int8).view(-1, 1) b = torch.arange(5, 15).cuda().to(torch.int8).view(-1, 1) Ca, Sa = F.nvidia_transform(a, "col32") Cb, Sb = F.nvidia_transform(b, formatB) c = F.igemmlt(Ca, Cb, Sa, Sb, dtype=torch.int8) c2 = torch.matmul(a.float(), b.float().t()) n = 2 dim1 = torch.randint(1, 4 * 1024, size=(n,)).tolist() dim2 = torch.randint(1, 4 * 1024, size=(n,)).tolist() # dim1 = [4] # dim2 = [5] values = list(product(dim1, dim2)) names = ["dim1_{}_dim2_{}".format(*vals) for vals in values] @pytest.mark.parametrize("dim1, dim2", values, ids=names) def test_coo_double_quant(dim1, dim2): threshold = 3.00 for i in range(k): A = torch.randn(dim1, dim2, device="cuda").half() idx = torch.abs(A) >= threshold CA2, CAt, statsA, statsAt, coo_tensor = F.double_quant(A) CA, CAt, statsA, statsAt, coo_tensor = F.double_quant( A, threshold=threshold ) if coo_tensor is not None: A1 = A * idx A2 = torch.zeros_like(A) A2[ coo_tensor.rowidx.long(), coo_tensor.colidx.long() ] = coo_tensor.values torch.testing.assert_allclose(A1, A2) A1 = A * (idx == 0) A2 = (CA.float() * statsA.unsqueeze(1) / 127).half() torch.testing.assert_allclose( A * (idx == 0), A2, rtol=0.05, atol=1.5e-2 ) n = 2 dim1 = torch.randint(1, 1 * 1024, size=(n,)).tolist() dim2 = torch.randint(1, 1 * 1024, size=(n,)).tolist() # dim1 = [7] # dim2 = [11] transposed_B = [False, True] values = list(product(dim1, dim2, transposed_B)) names = ["dim1_{}_dim2_{}_transposed_B_{}".format(*vals) for vals in values] @pytest.mark.parametrize("dim1, dim2, transposed_B", values, ids=names) def test_spmm_coo(dim1, dim2, transposed_B): threshold = 1.5 dim3 = torch.randint(32, 128, size=(1,)).item() # dim3 = 17 for i in range(k): A = torch.randn(dim1, dim2).cuda().half() if transposed_B: B = torch.randn(dim3, dim2).cuda().half() else: B = torch.randn(dim2, dim3).cuda().half() idx = torch.abs(A) >= threshold nnz = (idx == 1).sum().item() rows, cols = torch.where(idx) values = A[idx] cooA = F.COOSparseTensor( A.shape[0], A.shape[1], nnz, rows.int(), cols.int(), values ) A2 = A * idx if transposed_B: out2 = F.spmm_coo(cooA, B.t()) out1 = torch.matmul(A2, B.t()) else: out2 = F.spmm_coo(cooA, B) out1 = torch.matmul(A2, B) assert_all_approx_close(out1, out2, rtol=0.01, atol=3.0e-2, count=30) def test_spmm_bench(): batch = 2 model = 1024 * 1 hidden = model * 4 seq = 1024 dim1 = batch * seq dim2 = model dim3 = hidden threshold = 4 A = torch.randn(dim1, dim2, device="cuda").half() B = torch.randn(dim2, dim3, device="cuda").half() for i in range(10): C1 = bnb.matmul(A, B.t()) torch.cuda.synchronize() t0 = time.time() for i in range(k): C1 = bnb.matmul(A, B.t()) torch.cuda.synchronize() t8 = time.time() - t0 idx = torch.abs(A) >= threshold nnz = (idx == 1).sum().item() print(nnz / idx.numel()) rows, cols = torch.where(idx) values = A[idx] cooA = F.COOSparseTensor( A.shape[0], A.shape[1], nnz, rows.int(), cols.int(), values ) for i in range(10): out2 = F.spmm_coo(cooA, B) torch.cuda.synchronize() t0 = time.time() for i in range(k): out2 = F.spmm_coo(cooA, B) torch.cuda.synchronize() tsp = time.time() - t0 print(tsp, t8) print(tsp / t8) n = 2 dim1 = torch.randint(256, 1 * 1024, size=(n,)).tolist() dim2 = torch.randint(256, 1 * 1024, size=(n,)).tolist() values = list(product(dim1, dim2)) names = ["dim1_{}_dim2_{}".format(*vals) for vals in values] @pytest.mark.parametrize("dim1, dim2", values, ids=names) def test_integrated_sparse_decomp(dim1, dim2): threshold = 3.0 formatB = "col_turing" for i in range(k): A = torch.randn(dim1, dim2).cuda().half() w1 = torch.randn(dim1, dim2).cuda().half() out1 = torch.matmul(A, w1.t()) Cw1, Cw1t, statsw1, statsw1t, coo_tensor = F.double_quant(w1) CTw1, Sw1 = F.transform(Cw1, formatB) CA, CAt, statsA, statsAt, coo_tensor = F.double_quant(A) C32A, SA = F.transform(CA, "col32") out1_32, Sout1_32 = F.igemmlt(C32A, CTw1, SA, Sw1) out2 = F.mm_dequant(out1_32, Sout1_32, statsA, statsw1) CA, CAt, statsA, statsAt, coo_tensor = F.double_quant( A, threshold=threshold ) C32A, SA = F.transform(CA, "col32") out1_32, Sout1_32 = F.igemmlt(C32A, CTw1, SA, Sw1) out3 = F.mm_dequant(out1_32, Sout1_32, statsA, statsw1) assert coo_tensor is not None out4 = F.spmm_coo(coo_tensor, w1.t()) out5 = out3 + out4 err1 = torch.abs(out1 - out2).mean().item() err2 = torch.abs(out1 - out5).mean().item() assert err2 < err1 def test_matmuls(): a = torch.randn(256, 512).half().cuda() b = torch.randn(256, 512).half().cuda() c1 = torch.matmul(a, b.t()) c2 = bnb.matmul(a, b) c3 = bnb.matmul_cublas(a, b.t()) err1 = torch.abs(c1 - c2).mean().item() err2 = torch.abs(c1 - c3).mean().item() assert err1 < 0.2 assert err2 < 0.2 print(err1, err2) n = 2 # dim1 = torch.randint(1,1*1024, size=(n,)).tolist() # dim2 = torch.randint(1,4*1024, size=(n,)).tolist() dim1 = [1 * 2048] dim2 = [12288] # dim1 = [32] # dim2 = [32] # dtype = [torch.float16, torch.int8] dtype = [torch.float16] out_function = ["zeros", "ones"] values = list(product(dim1, dim2, dtype, out_function)) names = [ "dim1_{}_dim2_{}_dtype_{}_out_func_{}".format(*vals) for vals in values ] @pytest.mark.parametrize("dim1, dim2, dtype, out_func", values, ids=names) def test_spmm_coo_very_sparse(dim1, dim2, dtype, out_func): out_func = getattr(torch, out_func) threshold = 3.3 # threshold = 2.8 # threshold = 0.0 A = torch.randn(dim1, dim2, device="cuda").half() if dtype == torch.float16: B = torch.randn(dim2, dim2 * 4, device="cuda").half() torch.nn.init.xavier_uniform_(B) else: B = torch.randn(dim2, dim2 * 4, device="cuda").half() torch.nn.init.xavier_uniform_(B) B, SB = F.vectorwise_quant(B, quant_type="linear") # B = torch.randint(-127, 127, size=(dim2, dim2*4), device='cuda').to(torch.int8) print("") idx = torch.abs(A) >= threshold nnz = (idx == 1).sum().item() rows, cols = torch.where(idx) values = A[idx] cooA = F.COOSparseTensor( A.shape[0], A.shape[1], nnz, rows.int(), cols.int(), values ) A2 = A * idx out1 = torch.matmul(A2.half(), B.half()) out = out_func(out1.shape, dtype=torch.float16, device=out1.device) out1 += out.clone() out2 = F.spmm_coo_very_sparse(cooA, B, out=out) # print(B) # print(out1) # print(out2) p = 200 / (2048 * 12288 * 4) n = out1.numel() count = math.ceil(p * n) std = out1.std() out1 /= std out2 /= std assert_all_approx_close( out1, out2.half(), rtol=0.01, atol=3.0e-2, count=count ) # assert_all_approx_close(out1, out2.half(), rtol=0.05, atol=0.01, count=count) idx_col = torch.randint(0, A2.shape[-1], size=(15,)) # torch.testing.assert_allclose(out1, out2.half(), rtol=0.05, atol=0.001) # Bt = torch.randn(dim2*4, dim2, device='cuda').half() # torch.cuda.synchronize() # t0 = time.time() # print(A2.shape, B.shape) # for i in range(100): # #out3 = F.spmm_coo(cooA, Bt.t()) # #out2 = F.spmm_coo(cooA, B) # #out2 = F.spmm_coo_very_sparse(cooA, B) # #out1 = torch.matmul(A, Bt.t()) # torch.cuda.synchronize() # print(time.time() - t0) def test_coo2csr(): threshold = 1 A = torch.randn(128, 128).half().cuda() idx = torch.abs(A) >= threshold nnz = (idx == 1).sum().item() rows, cols = torch.where(idx) values = A[idx] cooA = F.COOSparseTensor( A.shape[0], A.shape[1], nnz, rows.int(), cols.int(), values ) A2 = A * idx csrA = F.coo2csr(cooA) counts = csrA.rowptr[1:] - csrA.rowptr[:-1] assert counts.numel() == A.shape[0] torch.testing.assert_allclose(counts, (A2 != 0).sum(1)) idx = A2 != 0 torch.testing.assert_allclose(A2[idx], csrA.values) def test_coo2csc(): threshold = 1 A = torch.randn(128, 128).half().cuda() idx = torch.abs(A) >= threshold nnz = (idx == 1).sum().item() rows, cols = torch.where(idx) values = A[idx] cooA = F.COOSparseTensor( A.shape[0], A.shape[1], nnz, rows.int(), cols.int(), values ) A2 = A * idx cscA = F.coo2csc(cooA) counts = cscA.colptr[1:] - cscA.colptr[:-1] assert counts.numel() == A.shape[1] torch.testing.assert_allclose(counts, (A2 != 0).sum(0)) # torch uses row-major -> use transpose to transfer to col-major idx = A2.t() != 0 torch.testing.assert_allclose(A2.t()[idx], cscA.values) n = 2 # dim1 = torch.randint(1,1*1024, size=(n,)).tolist() # dim2 = torch.randint(1,4*1024, size=(n,)).tolist() dim1 = [1 * 2048] # dim2 = [12288] dim2 = [2048] # dim1 = [2] # dim2 = [2] dtype = [torch.int8] values = list(product(dim1, dim2, dtype)) names = ["dim1_{}_dim2_{}_dtype_{}".format(*vals) for vals in values] @pytest.mark.parametrize("dim1, dim2, dtype", values, ids=names) def test_spmm_coo_dequant(dim1, dim2, dtype): threshold = 6.0 # threshold = 2.8 # threshold = 0.0 A = torch.randn(dim1, dim2, device="cuda").half() B = torch.empty(dim2, dim2 * 4, device="cuda", dtype=torch.float16) torch.nn.init.xavier_uniform_(B) Bt = B.t().contiguous() CB, CBt, statsB, statsBt, coo_tensor = F.double_quant(B) rowidx = torch.randint(0, A.shape[-1], size=(15,)) A[:, rowidx] = 8.0 idx = torch.abs(A) >= threshold nnz = (idx == 1).sum().item() rows, cols = torch.where(idx) values = A[idx] cooA = F.COOSparseTensor( A.shape[0], A.shape[1], nnz, rows.int(), cols.int(), values ) A2 = A * idx out2 = F.spmm_coo_very_sparse(cooA, CBt, dequant_stats=statsBt) out1 = torch.matmul(A2, B.half()) out3 = F.spmm_coo_very_sparse(cooA, CBt.half()) out3 = out3 * statsBt.half() / 127 values, counts = torch.unique(cooA.rowidx, return_counts=True) offset = counts.cumsum(0).int() max_count, max_idx = torch.sort(counts, descending=True) print(torch.median(max_count.float())) torch.testing.assert_allclose(out2, out3, rtol=0.05, atol=0.001) p = 200 / (2048 * 12288 * 4) n = out1.numel() count = math.ceil(p * n) assert_all_approx_close(out1, out2, rtol=0.01, atol=3.0e-2, count=count) # torch.cuda.synchronize() # t0 = time.time() # for i in range(100): # out2 = F.spmm_coo_very_sparse(cooA, B) # torch.cuda.synchronize() # print('fp16', time.time() - t0) torch.cuda.synchronize() t0 = time.time() for i in range(100): out2 = F.spmm_coo(cooA, B) torch.cuda.synchronize() print("cusparse fp16", time.time() - t0) torch.cuda.synchronize() t0 = time.time() for i in range(100): out2 = F.spmm_coo_very_sparse(cooA, CBt) torch.cuda.synchronize() print("int8", time.time() - t0) torch.cuda.synchronize() t0 = time.time() for i in range(100): out2 = F.spmm_coo_very_sparse(cooA, CBt, dequant_stats=statsBt) torch.cuda.synchronize() print("int8+dequant", time.time() - t0) torch.cuda.synchronize() t0 = time.time() for i in range(100): out2 = torch.matmul(A, B) torch.cuda.synchronize() print("matmul", time.time() - t0) torch.cuda.synchronize() t0 = time.time() for i in range(100): out1 = bnb.matmul(A, Bt) out2 = F.spmm_coo_very_sparse(cooA, CBt, dequant_stats=statsBt) out = out1 + out2 torch.cuda.synchronize() print("sparse+ matmul", time.time() - t0) torch.cuda.synchronize() t0 = time.time() for i in range(100): out1 = bnb.matmul(A, Bt) torch.matmul(A[:, rowidx], Bt.t()[rowidx], out=out1) torch.cuda.synchronize() print("partial matmul", time.time() - t0) torch.cuda.synchronize() t0 = time.time() for i in range(100): out1 = bnb.matmul(A, Bt) torch.cuda.synchronize() print("partial matmul", time.time() - t0) batch_size = 1 seqdim = 1 values = [] values.append((batch_size, seqdim, 768, 4 * 768)) # values.append((batch_size, seqdim, 1024, 4*1024)) # values.append((batch_size, seqdim, 1536, 4*1536)) # values.append((batch_size, seqdim, 2048, 4*2048)) # values.append((batch_size, seqdim, 2560, 4*2560)) # values.append((batch_size, seqdim, 4096, 4*4096)) # values.append((batch_size, seqdim, 5140, 4*5140)) #values.append((batch_size, seqdim, 12288, 4*12288)) names = [ "batch_{}_seq_{}_model_{}_hidden_{}".format(*vals) for vals in values ] @pytest.mark.parametrize("batch, seq, model, hidden", values, ids=names) def test_bench_matmul(batch, seq, model, hidden): iters = 128 formatB = F.get_special_format_str() A = torch.randn(batch, seq, model, device="cuda").half() B = torch.empty(hidden, model, dtype=torch.float16, device="cuda") torch.nn.init.xavier_uniform_(B) linear8bit = bnb.nn.Linear8bitLt(model, hidden, False).cuda().half() linear8bit.eval() outliers = torch.randint(0, model, size=(5,)).cuda() A[:, :, outliers] = 8.0 linearMixedBit = ( bnb.nn.Linear8bitLt(model, hidden, False, threshold=6.0).cuda().half() ) linearMixedBit.eval() # warmup for i in range(iters): torch.matmul(A, B.t()) torch.cuda.synchronize() print("") torch.cuda.synchronize() t0 = time.time() for i in range(iters): torch.matmul(A, B.t()) torch.cuda.synchronize() print( f"pytorch fp16: [{batch},{seq},{model}], [{model},{hidden}]->[{batch},{seq},{hidden}]: {time.time()-t0:.4f}s" ) torch.cuda.synchronize() t0 = time.time() for i in range(iters): bnb.matmul(A, B) torch.cuda.synchronize() print(f"CB -> CxB conversion (each iteration): [{batch},{seq},{model}], [{model},{hidden}]->[{batch},{seq},{hidden}]: {time.time()-t0:.4f}s") torch.cuda.synchronize() t0 = time.time() for i in range(iters): bnb.matmul(A, B, threshold=6.0) torch.cuda.synchronize() print(f"CB -> CxB conversion + threshold: [{batch},{seq},{model}], [{model},{hidden}]->[{batch},{seq},{hidden}]: {time.time()-t0:.4f}s") CA, CAt, SCA, SCAt, coo_tensorA = F.double_quant(A, threshold=0.0) C32A, SA = F.transform(CA, "col32") CB, CBt, SCB, SCBt, coo_tensorB = F.double_quant(B) CxB, SB = F.transform(CB, to_order=formatB) torch.cuda.synchronize() t0 = time.time() for i in range(iters): out32, Sout32 = F.igemmlt(C32A, CxB, SA, SB) torch.cuda.synchronize() print(f"no overhead matmul-lt: [{batch},{seq},{model}], [{model},{hidden}]->[{batch},{seq},{hidden}]: {time.time()-t0:.4f}s") BA, statsB = F.vectorwise_quant(B, dim=1) CxB, SB = F.nvidia_transform(CB, to_order=formatB) torch.cuda.synchronize() t0 = time.time() for i in range(iters): A2 = A.view(-1, A.shape[-1]).contiguous() CA, statsA = F.vectorwise_quant(A2, dim=1) C32A, SA = F.nvidia_transform(CA, "col32") out32, Sout32 = F.igemmlt(C32A, CxB, SA, SB) Cout, Sout = F.nvidia_transform(out32, "row", state=Sout32) F.vectorwise_mm_dequant(Cout, statsA, statsB.t()) torch.cuda.synchronize() #print(f"vector pytorch + nvidia: [{batch},{seq},{model}], [{model},{hidden}]->[{batch},{seq},{hidden}]: {time.time()-t0:.4f}s") BA, statsB = F.vectorwise_quant(B, dim=1, quant_type="linear") CxB, SB = F.nvidia_transform(CB, to_order=formatB) torch.cuda.synchronize() t0 = time.time() for i in range(iters): A2 = A.view(-1, A.shape[-1]).contiguous() CA, statsA = F.vectorwise_quant(A2, dim=1, quant_type="linear") C32A, SA = F.nvidia_transform(CA, "col32") out32, Sout32 = F.igemmlt(C32A, CxB, SA, SB) Cout, Sout = F.nvidia_transform(out32, "row", state=Sout32) out = Cout * statsB * statsA * (1.0 / (127 * 127)) torch.cuda.synchronize() #print(f"linear pytorch + nvidia: [{batch},{seq},{model}], [{model},{hidden}]->[{batch},{seq},{hidden}]: {time.time()-t0:.4f}s") linear8bit(A) torch.cuda.synchronize() t0 = time.time() for i in range(iters): linear8bit(A) torch.cuda.synchronize() print( f"bnb linear8bitlt: [{batch},{seq},{model}], [{model},{hidden}]->[{batch},{seq},{hidden}]: {time.time()-t0:.4f}s" ) linearMixedBit(A) torch.cuda.synchronize() t0 = time.time() for i in range(iters): linearMixedBit(A) torch.cuda.synchronize() print( f"bnb linear8bitlt with threshold: [{batch},{seq},{model}], [{model},{hidden}]->[{batch},{seq},{hidden}]: {time.time()-t0:.4f}s" ) def test_zeropoint(): def quant_zp(x): dtype = x.dtype x = x.float() dyna = x.max() - x.min() if dyna == 0: dyna = 1 qx = 254.0 / dyna minx = x.min() # zpx = torch.round(minx* qx) # zpx = 127 - torch.round(x.max()* qx) zpx = torch.round(x.min() * qx) - 127 x = (qx * x) + zpx return x, qx, zpx batch = 2 seq = 512 model = 1024 hidden = 4 * model A = torch.randn(batch * seq, model, device="cuda").half() * 0.1 B = torch.randn(model, hidden, device="cuda").half() * 0.1 C0 = torch.matmul(A, B) # A, SA = F.vectorwise_quant(A, quant_type='linear') # B, SB = F.vectorwise_quant(B, quant_type='linear') A = A.float() B = B.float() C1 = torch.matmul(A, B) C3 = bnb.matmul(A.half(), B.t().contiguous().half()) zp = 1 # C2 = torch.matmul(A-zp, B) # C2 += B.sum(0).view(1, -1)*zp C2 = torch.matmul(A, B - zp) C2 -= A.sum(1).view(-1, 1) * zp ca, cqa, cza = quant_zp(A) print(ca.min(), ca.max()) print((ca - cza).min(), (ca - cza).max()) zp = 1 scale = 2.0 C5 = torch.matmul((A * scale) - zp, B) C5 += B.sum(0) * zp C5 /= scale CA, qa, zpa = quant_zp(A) C4 = torch.matmul(CA, B) C4 -= B.sum(0) * zpa C4 /= qa zpb = 1 zpa = 1 qa = 2 qb = 2 C6 = torch.matmul((A * qa) + zpa, (B * qb) + zpb) C6 -= (qb * B.sum(0).view(1, -1) * zpa) + (qa * A.sum(1).view(-1, 1) * zpb) C6 -= zpa * zpb * A.shape[1] C6 /= qa * qb CA, qa, zpa = quant_zp(A) CB, qb, zpb = quant_zp(B) C7 = torch.matmul(CA, CB) C7 -= (qb * B.sum(0).view(1, -1) * zpa) + (qa * A.sum(1).view(-1, 1) * zpb) C7 -= zpa * zpb * A.shape[1] C7 /= qa * qb print("") # print(C0.flatten()[:10]) print(C1.flatten()[:10]) print(C2.flatten()[:10]) print(C3.flatten()[:10]) print(C5.flatten()[:10]) print(C6.flatten()[:10]) print(C7.flatten()[:10]) err1 = torch.abs(C1 - C2).mean().item() err2 = torch.abs(C1 - C3).mean().item() err3 = torch.abs(C1 - C4).mean().item() err4 = torch.abs(C1 - C5).mean().item() err5 = torch.abs(C1 - C6).mean().item() err6 = torch.abs(C1 - C7).mean().item() print(err1, err2, err3, err4, err5, err6) def test_extract_outliers(): for i in range(k): shapeA = (4096, 4096 * 4) idx = torch.unique(torch.randint(0, shapeA[1], size=(10,)).int()).cuda() # idx = torch.Tensor([0]).int().cuda() A = torch.randint(-128, 127, size=shapeA, device="cuda").to(torch.int8) outliers1 = A[:, idx.long()] CA, SA = F.transform(A, "col_turing") outliers2 = F.extract_outliers(CA, SA, idx) assert outliers2.shape[0] == shapeA[0] assert outliers2.shape[1] == idx.numel() torch.testing.assert_allclose(outliers1, outliers2) CA, SA = F.transform(A, "col_ampere") outliers2 = F.extract_outliers(CA, SA, idx) assert outliers2.shape[0] == shapeA[0] assert outliers2.shape[1] == idx.numel() torch.testing.assert_allclose(outliers1, outliers2) def test_blockwise_cpu_large(): diffs = [] reldiffs = [] batch = 128 seq = 128 for hidden in [128]:#, 14336]: for blocksize in [4096, 16384]: for i in range(2): A1 = torch.randn(batch, seq, hidden, device='cpu') t0 = time.time() C, S = F.quantize_blockwise(A1, blocksize=blocksize) A2 = F.dequantize_blockwise(C, S, blocksize=blocksize) print(time.time() - t0) diff = torch.abs(A1 - A2) reldiff = diff / torch.abs(A1 + 1e-8) diffs.append(diff.mean().item()) reldiffs.append(reldiff.mean().item()) assert diffs[-1] < 0.011 # print(sum(diffs)/len(diffs)) # print(sum(reldiffs)/len(reldiffs)) def test_fp8_quant(): for e_bits in range(1, 7): p_bits = 7-e_bits code = F.create_fp8_map(True, e_bits, p_bits).cuda() print(e_bits, p_bits) abserr = [] relerr = [] for i in range(100): A1 = torch.randn(1024, 1024, device="cuda") C, SC = F.quantize_blockwise(A1, code=code) A2 = F.dequantize_blockwise(C, SC) diff = torch.abs(A1 - A2) reldiff = diff/torch.abs(A1+1e-8) abserr.append(diff.mean().item()) relerr.append(reldiff.mean().item()) #assert diff < 0.0075 #print(sum(abserr)/len(abserr)) #print(sum(relerr)/len(relerr)) abserr = [] relerr = [] for i in range(100): A1 = torch.rand(1024, 1024, device="cuda") C, SC = F.quantize_blockwise(A1, code=code) A2 = F.dequantize_blockwise(C, SC) diff = torch.abs(A1 - A2) reldiff = diff/torch.abs(A1+1e-8) abserr.append(diff.mean().item()) relerr.append(reldiff.mean().item()) #assert diff < 0.0075 #print(sum(abserr)/len(abserr)) #print(sum(relerr)/len(relerr)) abserr = [] relerr = [] for i in range(100): A1 = torch.randn(1024, 1024, device="cuda") C, SC = F.quantize_blockwise(A1) A2 = F.dequantize_blockwise(C, SC) diff = torch.abs(A1 - A2) reldiff = diff/torch.abs(A1+1e-8) abserr.append(diff.mean().item()) relerr.append(reldiff.mean().item()) #assert diff < 0.0075 #print(3, sum(abserr)/len(abserr)) #print(3, sum(relerr)/len(relerr)) def test_few_bit_quant(): #print('') for bits in range(2, 9): #print('='*30, bits, '='*30) for method in ['linear', 'fp8', 'dynamic', 'quantile']: abserrs = [] relerrs = [] code = None if method == 'linear': code = F.create_linear_map(True, total_bits=bits).cuda() elif method == 'fp8': ebits = math.ceil(bits/2) pbits = bits-ebits-1 code = F.create_fp8_map(True, ebits, pbits, bits).cuda() elif method == 'dynamic': code = F.create_dynamic_map(True, bits-0, bits).cuda() elif method == 'quantile': values = torch.randn(2048, 2048, device='cuda') code = F.create_quantile_map(values, bits).cuda() # for some data types we have no zero # for some data types we have one zero # for some data types we have two zeros assert torch.unique(code).numel() in [2**bits, 2**bits-1], f'bits: {bits}, method: {method}' #print(method, (code==0).sum()) assert code.numel() == 256 for i in range(10): values = torch.randn(1, 32, device='cuda') values /= values.abs().max() #values[values.abs() < 1e-6] += 1e-5 q1 = [] v1 = [] for v in values[0]: idx = torch.abs(v-code).argmin() q1.append(idx.item()) v1.append(code[idx].item()) q1 = torch.Tensor(q1).cuda() v1 = torch.Tensor(v1).cuda() q2, S2 = F.quantize_blockwise(values, code=code) v2 = F.dequantize_blockwise(q2, S2) idx = torch.isclose(q1.int(), q2.int()) err2 = torch.abs(v2-values) abserrs.append(err2.mean().item()) relerrs.append((err2/(1e-10+values).abs()).mean().item()) if idx.sum(): # some weird cases err1 = torch.abs(v1-values).mean() #assert err2.mean() <= err1 else: torch.testing.assert_allclose(q1, q2) #print(method, 'abserr:', sum(abserrs)/len(abserrs), 'relerr:', sum(relerrs)/len(relerrs)) #assert False def test_kbit_quantile_estimation(): for i in range(100): data = torch.randn(1024, 1024, device='cuda') for bits in range(2, 9): p = np.linspace(1.3e-4, 1-1.3e-4, 2**bits) val1 = torch.Tensor(norm.ppf(p)).cuda() val2 = F.estimate_quantiles(data, offset=0, num_quantiles=2**bits) err = torch.abs(val1-val2).mean() assert err < 0.038 for i in range(100): data = torch.randn(1024, 1024, device='cuda') for bits in range(2, 4): total_values = 2**bits-1 p = np.linspace(0, 1, 2*total_values+1) idx = np.arange(1, 2*total_values+1, 2) p = p[idx] offset = 1/(2*total_values) p = np.linspace(offset, 1-offset, total_values) val1 = torch.Tensor(norm.ppf(p)).cuda() val2 = F.estimate_quantiles(data, num_quantiles=2**bits-1) err = torch.abs(val1-val2).mean() assert err < 0.035 def test_bench_dequantization(): a = torch.rand(1024, 1024, device='cuda').half() qa, SA = F.quantize_blockwise(a) max_theoretical_mu = 1024*1024*2/1024**3/672*1000*1000 #print(max_theoretical_mu) torch.cuda.synchronize() t0 = time.time() for i in range(100): F.dequantize_blockwise(qa, SA, blocksize=2048) torch.cuda.synchronize() #print((time.time()-t0)/1e6)
# import apex k = 20 def get_temp_dir(): path = f"/tmp/autoswap/{str(uuid.uuid4())}" os.makedirs(path, exist_ok=True) return path def rm_path(path): shutil.rmtree(path) str2optimizers = {} str2optimizers["adam_pytorch"] = (None, torch.optim.Adam, bnb.optim.Adam) # str2optimizers['adam_apex'] = (None, apex.optimizers.FusedAdam, bnb.optim.Adam) # str2optimizers['momentum_apex'] = (None, lambda pxx: apex.optimizers.FusedSGD(pxx, 0.01, 0.9), bnb.optim.Adam) str2optimizers["lion_pytorch"] = (None, Lion, bnb.optim.Lion) str2optimizers["momentum_pytorch"] = ( None, lambda pxx: torch.optim.SGD(pxx, 0.01, 0.9), bnb.optim.Adam, ) str2optimizers["adam"] = (torch.optim.Adam, bnb.optim.Adam) # str2optimizers['fused_adam'] = (apex.optimizers.FusedAdam, bnb.optim.Adam) str2optimizers["lion"] = (Lion, bnb.optim.Lion) str2optimizers["momentum"] = ( lambda pxx: torch.optim.SGD(pxx, 0.01, 0.9), lambda pxx: bnb.optim.SGD(pxx, 0.01, 0.9, block_wise=False), ) str2optimizers["lars"] = ( lambda pxx: bnb.optim.PytorchLARS(pxx, 0.01, 0.9), lambda pxx: bnb.optim.LARS(pxx, 0.01, 0.9), ) str2optimizers["rmsprop"] = ( lambda pxx: torch.optim.RMSprop(pxx, 0.01, 0.9), lambda pxx: bnb.optim.RMSprop(pxx, 0.01, 0.9, block_wise=False), ) str2optimizers["adam8bit"] = ( torch.optim.Adam, lambda pxx: bnb.optim.Adam8bit(pxx, block_wise=False), ) str2optimizers["lion8bit"] = ( Lion, lambda pxx: bnb.optim.Lion8bit(pxx, block_wise=False), ) str2optimizers["momentum8bit"] = ( lambda pxx: torch.optim.SGD(pxx, 0.01, 0.9), lambda pxx: bnb.optim.SGD8bit(pxx, 0.01, 0.9, block_wise=False), ) str2optimizers["rmsprop8bit"] = ( lambda pxx: torch.optim.RMSprop(pxx, 0.01, 0.9), lambda pxx: bnb.optim.RMSprop8bit(pxx, 0.01, 0.9, block_wise=False), ) str2optimizers["lars8bit"] = ( lambda pxx: bnb.optim.PytorchLARS(pxx, 0.01, 0.9), lambda pxx: bnb.optim.LARS8bit(pxx, 0.01, 0.9), ) str2optimizers["adam8bit_blockwise"] = ( torch.optim.Adam, lambda pxx: bnb.optim.Adam8bit(pxx, block_wise=True), ) str2optimizers["lion8bit_blockwise"] = ( Lion, lambda pxx: bnb.optim.Lion8bit(pxx, block_wise=True), ) str2optimizers["momentum8bit_blockwise"] = ( lambda pxx: torch.optim.SGD(pxx, 0.01, 0.9), lambda pxx: bnb.optim.SGD8bit(pxx, 0.01, 0.9, block_wise=True), ) str2optimizers["rmsprop8bit_blockwise"] = ( lambda pxx: torch.optim.RMSprop(pxx, 0.01, 0.9), lambda pxx: bnb.optim.RMSprop8bit(pxx, 0.01, 0.9, block_wise=True), ) str2statenames = {} str2statenames["adam"] = [("exp_avg", "state1"), ("exp_avg_sq", "state2")] str2statenames["lion"] = [("exp_avg", "state1")] str2statenames["momentum"] = [("momentum_buffer", "state1")] str2statenames["lars"] = [("momentum_buffer", "state1")] str2statenames["lamb"] = [("exp_avg", "state1"), ("exp_avg_sq", "state2")] str2statenames["rmsprop"] = [("square_avg", "state1")] str2statenames["adam8bit"] = [ ("exp_avg", "state1", "qmap1", "max1"), ("exp_avg_sq", "state2", "qmap2", "max2"), ] str2statenames["lion8bit"] = [ ("exp_avg", "state1", "qmap1", "max1") ] str2statenames["lamb8bit"] = [ ("exp_avg", "state1", "qmap1", "max1"), ("exp_avg_sq", "state2", "qmap2", "max2"), ] str2statenames["adam8bit_blockwise"] = [ ("exp_avg", "state1", "qmap1", "absmax1"), ("exp_avg_sq", "state2", "qmap2", "absmax2"), ] str2statenames["lion8bit_blockwise"] = [ ("exp_avg", "state1", "qmap1", "absmax1") ] str2statenames["momentum8bit"] = [ ("momentum_buffer", "state1", "qmap1", "max1") ] str2statenames["momentum8bit_blockwise"] = [ ("momentum_buffer", "state1", "qmap1", "absmax1") ] str2statenames["lars8bit"] = [("momentum_buffer", "state1", "qmap1", "max1")] str2statenames["rmsprop8bit"] = [("square_avg", "state1", "qmap1", "max1")] str2statenames["rmsprop8bit_blockwise"] = [ ("square_avg", "state1", "qmap1", "absmax1") ] dim1 = [1024] dim2 = [32, 1024, 4097, 1] gtype = [torch.float32, torch.float16] optimizer_names = ["adam", "momentum", "rmsprop", "lars", "lion"] values = list(product(dim1, dim2, gtype, optimizer_names)) names = [ "dim1_{}_dim2_{}_gtype_{}_optim_{}".format(*vals) for vals in values ] @pytest.mark.parametrize("dim1, dim2, gtype, optim_name", values, ids=names) def test_optimizer32bit(dim1, dim2, gtype, optim_name): if dim1 == 1 and dim2 == 1: return p1 = torch.randn(dim1, dim2, device="cuda", dtype=gtype) * 0.1 p2 = p1.clone() p1 = p1.float() torch_optimizer = str2optimizers[optim_name][0]([p1]) bnb_optimizer = str2optimizers[optim_name][1]([p2]) if gtype == torch.float32: atol, rtol = 1e-6, 1e-5 else: atol, rtol = 1e-4, 1e-3 for i in range(k): g = torch.randn(dim1, dim2, device="cuda", dtype=gtype) * 0.01 p1.grad = g.clone().float() p2.grad = g.clone() bnb_optimizer.step() torch_optimizer.step() for name1, name2 in str2statenames[optim_name]: torch.testing.assert_allclose( torch_optimizer.state[p1][name1], bnb_optimizer.state[p2][name2], atol=atol, rtol=rtol, ) torch.testing.assert_allclose(p1, p2.float(), atol=atol, rtol=rtol) if i % (k // 5) == 0 and i > 0: path = get_temp_dir() torch.save(bnb_optimizer.state_dict(), join(path, "opt.pt")) del bnb_optimizer bnb_optimizer = None bnb_optimizer = str2optimizers[optim_name][1]([p2]) bnb_optimizer.load_state_dict(torch.load(join(path, "opt.pt"))) rm_path(path) torch.testing.assert_allclose(p1, p2.float(), atol=atol, rtol=rtol) for name1, name2 in str2statenames[optim_name]: torch.testing.assert_allclose( torch_optimizer.state[p1][name1], bnb_optimizer.state[p2][name2], atol=atol, rtol=rtol, ) if gtype == torch.float16: # the adam buffers should also be close because they are 32-bit # but the paramters can diverge because they are 16-bit # the difference grow larger and larger with each update # --> copy the state to keep weights close p1.data = p1.data.half().float() p2.copy_(p1.data) torch.testing.assert_allclose(p1.half(), p2) if optim_name in ["lars", "lamb"]: assert bnb_optimizer.state[p2]["unorm_vec"] > 0.0 dim1 = [1024] dim2 = [32, 1024, 4097] gtype = [torch.float32, torch.float16] values = list(product(dim1, dim2, gtype)) names = ["dim1_{}_dim2_{}_gtype_{}".format(*vals) for vals in values] @pytest.mark.parametrize("dim1, dim2, gtype", values, ids=names) def test_global_config(dim1, dim2, gtype): if dim1 == 1 and dim2 == 1: return p1 = torch.randn(dim1, dim2, device="cpu", dtype=gtype) * 0.1 p2 = torch.randn(dim1, dim2, device="cpu", dtype=gtype) * 0.1 p3 = torch.randn(dim1, dim2, device="cpu", dtype=gtype) * 0.1 mask = torch.rand_like(p2) < 0.1 beta1 = 0.9 beta2 = 0.999 lr = 0.001 eps = 1e-8 bnb.optim.GlobalOptimManager.get_instance().initialize() bnb.optim.GlobalOptimManager.get_instance().override_config( p3, "optim_bits", 8 ) bnb.optim.GlobalOptimManager.get_instance().register_parameters( [p1, p2, p3] ) p1 = p1.cuda() p2 = p2.cuda() p3 = p3.cuda() adam2 = bnb.optim.Adam([p1, p2, p3], lr, (beta1, beta2), eps) if gtype == torch.float32: atol, rtol = 1e-6, 1e-5 else: atol, rtol = 1e-4, 1e-3 for i in range(50): g1 = torch.randn(dim1, dim2, device="cuda", dtype=gtype) * 0.1 + 0.001 g2 = torch.randn(dim1, dim2, device="cuda", dtype=gtype) * 0.1 + 0.001 g3 = torch.randn(dim1, dim2, device="cuda", dtype=gtype) * 0.1 + 0.001 p1.grad = g1 p2.grad = g2 p3.grad = g3 adam2.step() assert adam2.state[p3]["state1"].dtype == torch.uint8 assert adam2.state[p3]["state2"].dtype == torch.uint8 dim1 = [1024] dim2 = [32, 1024, 4097] gtype = [torch.float32, torch.float16] optimizer_names = [ "adam8bit", "lion8bit", "momentum8bit", "rmsprop8bit", "adam8bit_blockwise", "lion8bit_blockwise", "lars8bit", "momentum8bit_blockwise", "rmsprop8bit_blockwise", ] values = list(product(dim1, dim2, gtype, optimizer_names)) names = [ "dim1_{}_dim2_{}_gtype_{}_optim_{}".format(*vals) for vals in values ] @pytest.mark.parametrize("dim1, dim2, gtype, optim_name", values, ids=names) def test_optimizer8bit(dim1, dim2, gtype, optim_name): if dim1 == 1 and dim2 == 1: return p1 = torch.randn(dim1, dim2, device="cuda", dtype=gtype) * 0.1 p2 = p1.clone() p1 = p1.float() blocksize = 2048 torch_optimizer = str2optimizers[optim_name][0]([p1]) bnb_optimizer = str2optimizers[optim_name][1]([p2]) if gtype == torch.float32: atol, rtol = 3e-3, 1e-3 patol, prtol = 1e-5, 1e-3 else: atol, rtol = 3e-3, 1e-3 patol, prtol = 1e-5, 1e-3 errors = [] relerrors = [] for i in range(50): g = torch.randn(dim1, dim2, device="cuda", dtype=gtype) * 0.01 p1.grad = g.clone().float() p2.grad = g.clone() bnb_optimizer.step() torch_optimizer.step() torch.testing.assert_allclose(p1, p2.float(), atol=patol, rtol=prtol) dequant_states = [] for name1, name2, qmap, max_val in str2statenames[optim_name]: # print(bnb_optimizer.state[p2][max_val], name1) if "blockwise" in optim_name: s1 = F.dequantize_blockwise( code=bnb_optimizer.state[p2][qmap], absmax=bnb_optimizer.state[p2][max_val], A=bnb_optimizer.state[p2][name2], blocksize=blocksize, ) else: s1 = F.dequantize( code=bnb_optimizer.state[p2][qmap], absmax=bnb_optimizer.state[p2][max_val], A=bnb_optimizer.state[p2][name2], ) num_not_close = ( torch.isclose( torch_optimizer.state[p1][name1], s1, atol=atol, rtol=rtol ) == 0 ) assert num_not_close.sum().item() < 20 dequant_states.append(s1.clone()) err = torch.abs(p1 - p2) relerr = err / torch.abs(p1) assert err.mean() < 0.0001 assert relerr.mean() < 0.001 errors.append(err.mean().item()) relerrors.append(relerr.mean().item()) if i % 10 == 0 and i > 0: for (name1, name2, qmap, max_val), s in zip( str2statenames[optim_name], dequant_states ): s1cpy = s.clone() raws1cpy = bnb_optimizer.state[p2][name2].clone() qmap1 = bnb_optimizer.state[p2][qmap].clone() path = get_temp_dir() torch.save(bnb_optimizer.state_dict(), join(path, "opt.pt")) del bnb_optimizer bnb_optimizer = None bnb_optimizer = str2optimizers[optim_name][1]([p2]) bnb_optimizer.load_state_dict(torch.load(join(path, "opt.pt"))) rm_path(path) torch.testing.assert_allclose( raws1cpy, bnb_optimizer.state[p2][name2] ) torch.testing.assert_allclose( qmap1, bnb_optimizer.state[p2][qmap] ) if "blockwise" in optim_name: s1 = F.dequantize_blockwise( code=bnb_optimizer.state[p2][qmap], absmax=bnb_optimizer.state[p2][max_val], A=bnb_optimizer.state[p2][name2], blocksize=blocksize, ) else: s1 = F.dequantize( code=bnb_optimizer.state[p2][qmap], absmax=bnb_optimizer.state[p2][max_val], A=bnb_optimizer.state[p2][name2], ) torch.testing.assert_allclose(s1cpy, s1) num_not_close = ( torch.isclose( torch_optimizer.state[p1][name1], s1, atol=atol, rtol=rtol, ) == 0 ) assert num_not_close.sum().item() < 20 torch.testing.assert_allclose( p1, p2.float(), atol=patol, rtol=prtol ) # the parameters diverge quickly. Here we keep them close # together so we can test against the Adam error p1.data = p1.data.to(gtype).float() p2.copy_(p1.data) torch.testing.assert_allclose(p1.to(gtype), p2) for (name1, name2, qmap, max_val), s in zip( str2statenames[optim_name], dequant_states ): torch_optimizer.state[p1][name1].copy_(s.data) # print(sum(errors)/len(errors)) # print(sum(relerrors)/len(relerrors)) dim1 = [1024] dim2 = [32, 1024, 4097] gtype = [torch.float32] optim_bits = [32, 8] values = list(product(dim1, dim2, gtype, optim_bits)) names = [ "dim1_{}_dim2_{}_gtype_{}_optim_bits_{}".format(*vals) for vals in values ] @pytest.mark.parametrize("dim1, dim2, gtype, optim_bits", values, ids=names) def test_adam_percentile_clipping(dim1, dim2, gtype, optim_bits): if dim1 == 1 and dim2 == 1: return p1 = torch.randn(dim1, dim2, device="cpu", dtype=gtype) * 0.1 beta1 = 0.9 beta2 = 0.999 lr = 0.001 eps = 1e-8 p1 = p1.cuda() p2 = p1.clone() adam1 = bnb.optim.Adam([p1], lr, (beta1, beta2), eps, optim_bits=optim_bits) adam2 = bnb.optim.Adam( [p2], lr, (beta1, beta2), eps, optim_bits=optim_bits, percentile_clipping=5, ) gnorm_vec = torch.zeros(100).cuda() step = 0 for i in range(50): step += 1 g1 = torch.randn(dim1, dim2, device="cuda", dtype=gtype) * 0.1 + ( 0.01 * i ) g2 = g1.clone() p2.grad = g2 current_gnorm, clip_val, gnorm_scale = F.percentile_clipping( g1, gnorm_vec, step, 5 ) g1 = (g1.float() * gnorm_scale).to(gtype) p1.grad = g1 adam1.step() adam2.step() # gnorm_scale is not deterministic (warp reductions), as such there can be slight differences in state if optim_bits == 32: torch.testing.assert_allclose(p1, p2) torch.testing.assert_allclose( adam1.state[p1]["state1"], adam2.state[p2]["state1"], atol=5e-5, rtol=1e-4, ) torch.testing.assert_allclose( adam1.state[p1]["state2"], adam2.state[p2]["state2"], atol=5e-5, rtol=1e-4, ) elif optim_bits == 8: torch.testing.assert_allclose(p1, p2, atol=1e-4, rtol=1e-3) torch.testing.assert_allclose( adam1.state[p1]["state1"], adam2.state[p2]["state1"], atol=2, rtol=1e-3, ) torch.testing.assert_allclose( adam1.state[p1]["state2"], adam2.state[p2]["state2"], atol=2, rtol=1e-3, ) adam1.state[p1]["state1"].copy_(adam2.state[p2]["state1"]) adam1.state[p1]["state2"].copy_(adam2.state[p2]["state2"]) if i % 10 == 0 and i > 0: path = get_temp_dir() torch.save(adam2.state_dict(), join(path, "opt.pt")) del adam2 adam2 = None adam2 = bnb.optim.Adam( [p2], lr, (beta1, beta2), eps, optim_bits=optim_bits, percentile_clipping=5, ) adam2.load_state_dict(torch.load(join(path, "opt.pt"))) dim1 = [4096] dim2 = [4096] gtype = [torch.float32, torch.float16] # optimizer_names = ['adam8bit_blockwise', 'adam8bit', 'lamb8bit'] # optimizer_names = ['adam8bit_blockwise', 'adam_apex', 'adam8bit', 'adam', 'adam_pytorch'] # optimizer_names = ['momentum_apex', 'momentum8bit', 'momentum_pytorch'] # optimizer_names = ['lamb_apex', 'lamb8bit'] # optimizer_names = ['lars_apex', 'lars8bit'] optimizer_names = ["adam8bit_blockwise"] values = list(product(dim1, dim2, gtype, optimizer_names)) names = [ "dim1_{}_dim2_{}_gtype_{}_optim_{}".format(*vals) for vals in values ] @pytest.mark.parametrize("dim1, dim2, gtype, optim_name", values, ids=names) def test_benchmark_blockwise(dim1, dim2, gtype, optim_name): if dim1 == 1 and dim2 == 1: return p1 = torch.randn(dim1, dim2, device="cuda", dtype=gtype) * 0.1 bnb_optimizer = str2optimizers[optim_name][1]([p1]) g = torch.randn(dim1, dim2, device="cuda", dtype=gtype) * 0.01 p1.grad = g for i in range(k): if i == k // 5: # 100 iterations for burn-in torch.cuda.synchronize() t0 = time.time() bnb_optimizer.step() torch.cuda.synchronize() s = time.time() - t0 print("") params = (k - k // 5) * dim1 * dim2 print(optim_name, gtype, s / params) # assert s < 3.9
CUDA_RUNTIME_LIB, determine_cuda_runtime_lib_path, evaluate_cuda_setup, extract_candidate_paths, ) """ 'LD_LIBRARY_PATH': ':/mnt/D/titus/local/cuda-11.1/lib64/' 'CONDA_EXE': '/mnt/D/titus/miniconda/bin/conda' 'LESSCLOSE': '/usr/bin/lesspipe %s %s' 'OLDPWD': '/mnt/D/titus/src' 'CONDA_PREFIX': '/mnt/D/titus/miniconda/envs/8-bit' 'SSH_AUTH_SOCK': '/mnt/D/titus/.ssh/ssh-agent.tim-uw.sock' 'CONDA_PREFIX_1': '/mnt/D/titus/miniconda' 'PWD': '/mnt/D/titus/src/8-bit' 'HOME': '/mnt/D/titus' 'CONDA_PYTHON_EXE': '/mnt/D/titus/miniconda/bin/python' 'CUDA_HOME': '/mnt/D/titus/local/cuda-11.1/' 'TMUX': '/tmp/tmux-1007/default,59286,1' 'XDG_DATA_DIRS': '/usr/local/share:/usr/share:/var/lib/snapd/desktop' 'SSH_TTY': '/dev/pts/0' 'MAIL': '/var/mail/titus' 'SHELL': '/bin/bash' 'DBUS_SESSION_BUS_ADDRESS': 'unix:path=/run/user/1007/bus' 'XDG_RUNTIME_DIR': '/run/user/1007' 'PATH': '/mnt/D/titus/miniconda/envs/8-bit/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/mnt/D/titus/local/cuda-11.1/bin' 'LESSOPEN': '| /usr/bin/lesspipe %s' '_': '/mnt/D/titus/miniconda/envs/8-bit/bin/python' # any that include 'CONDA' that are not 'CONDA_PREFIX' # we search for 'CUDA_HOME': '/mnt/D/titus/local/cuda-11.1/' """ class InputAndExpectedOutput(NamedTuple): input: str output: str HAPPY_PATH__LD_LIB_TEST_PATHS: List[InputAndExpectedOutput] = [ ( f"some/other/dir:dir/with/{CUDA_RUNTIME_LIB}", f"dir/with/{CUDA_RUNTIME_LIB}", ), ( f":some/other/dir:dir/with/{CUDA_RUNTIME_LIB}", f"dir/with/{CUDA_RUNTIME_LIB}", ), ( f"some/other/dir:dir/with/{CUDA_RUNTIME_LIB}:", f"dir/with/{CUDA_RUNTIME_LIB}", ), ( f"some/other/dir::dir/with/{CUDA_RUNTIME_LIB}", f"dir/with/{CUDA_RUNTIME_LIB}", ), ( f"dir/with/{CUDA_RUNTIME_LIB}:some/other/dir", f"dir/with/{CUDA_RUNTIME_LIB}", ), ( f"dir/with/{CUDA_RUNTIME_LIB}:other/dir/libcuda.so", f"dir/with/{CUDA_RUNTIME_LIB}", ), ] @pytest.fixture(params=HAPPY_PATH__LD_LIB_TEST_PATHS) def happy_path_path_string(tmpdir, request): for path in extract_candidate_paths(request.param): test_dir.mkdir() if CUDA_RUNTIME_LIB in path: (test_input / CUDA_RUNTIME_LIB).touch() UNHAPPY_PATH__LD_LIB_TEST_PATHS = [ f"a/b/c/{CUDA_RUNTIME_LIB}:d/e/f/{CUDA_RUNTIME_LIB}", f"a/b/c/{CUDA_RUNTIME_LIB}:d/e/f/{CUDA_RUNTIME_LIB}:g/h/j/{CUDA_RUNTIME_LIB}", ] def test_full_system(): ## this only tests the cuda version and not compute capability # if CONDA_PREFIX exists, it has priority before all other env variables # but it does not contain the library directly, so we need to look at the a sub-folder version = "" if "CONDA_PREFIX" in os.environ: ls_output, err = bnb.utils.execute_and_return(f'ls -l {os.environ["CONDA_PREFIX"]}/lib/libcudart.so') major, minor, revision = (ls_output.split(" ")[-1].replace("libcudart.so.", "").split(".")) version = float(f"{major}.{minor}") if version == "" and "LD_LIBRARY_PATH" in os.environ: ld_path = os.environ["LD_LIBRARY_PATH"] paths = ld_path.split(":") version = "" for p in paths: if "cuda" in p: idx = p.rfind("cuda-") version = p[idx + 5 : idx + 5 + 4].replace("/", "") version = float(version) break assert version > 0 binary_name, cudart_path, cuda, cc, cuda_version_string = evaluate_cuda_setup() binary_name = binary_name.replace("libbitsandbytes_cuda", "") assert binary_name.startswith(str(version).replace(".", ""))
# contributed by Alex Borzunov, see: # https://github.com/bigscience-workshop/petals/blob/main/tests/test_linear8bitlt.py @pytest.mark.skipif( not torch.cuda.is_available() or torch.cuda.get_device_capability() < (7, 5), reason="this test requires a turing-generation or newer GPU, see bitsandbytes docs", ) def test_layout_exact_match(): x = (torch.randn(14336 * 3, 14336) * 10).to(torch.int8).cuda() for tile_size, order in ((8, 32), "col_turing"), ((32, 32), "col_ampere"): transform = lambda x: F.transform(x.cuda(), from_order="row", to_order=order)[0].to(x.device) tile_indices = get_inverse_transform_indices(transform, tile_size) cxb = transform(x) torch.cuda.synchronize() restored_x = undo_layout(cxb, tile_indices) torch.cuda.synchronize() assert restored_x.is_contiguous() assert torch.all(torch.eq(restored_x, x)) @pytest.mark.skipif(not torch.cuda.is_available(), reason="this test requires a GPU") def test_linear_no_igemmlt(): linear = torch.nn.Linear(1024, 3072) x = torch.randn(3, 1024, dtype=torch.half) linear_custom = Linear8bitLt( linear.in_features, linear.out_features, linear.bias is not None, has_fp16_weights=False, threshold=6.0, ) linear_custom.state.force_no_igemmlt = True linear_custom.weight = bnb.nn.Int8Params( linear.weight.data.clone(), requires_grad=False, has_fp16_weights=False ).to(linear.weight.dtype) linear_custom.bias = linear.bias linear = linear_custom.cuda() linear = linear.half().cuda() x_ref = x.clone().cuda().requires_grad_(True) x_ours = x.clone().cuda().requires_grad_(True) fx_ref = linear(x_ref).float() grad_proj = torch.randn_like(fx_ref) (fx_ref * grad_proj).mean().backward() fx_ours = linear_custom(x_ours).float() (fx_ours * grad_proj).mean().backward() assert torch.allclose(fx_ref, fx_ours, atol=0.02) assert torch.allclose(x_ref.grad, x_ours.grad, atol=0.01) assert not linear_custom.state.has_fp16_weights assert linear_custom.state.CB is not None assert linear_custom.state.CxB is None
n = 1 k = 25 dim1 = torch.randint(16, 64, size=(n,)).tolist() dim2 = torch.randint(32, 96, size=(n,)).tolist() dim3 = torch.randint(32, 96, size=(n,)).tolist() dim4 = torch.randint(32, 96, size=(n,)).tolist() funcs = [(torch.bmm, bnb.bmm_cublas), (torch.matmul, bnb.matmul_cublas)] str_funcs = ["bmm", "matmul"] req_grad = [(False, False), (True, False), (True, True), (False, True)] req_grad_str = ["FF", "TF", "TT", "FT"] transpose = [(False, False), (False, True), (True, True), (True, False)] str_transpose = ["FF", "FT", "TT", "TF"] dtype = [torch.float32, torch.float16] values = list( product(dim1, dim2, dim3, dim4, funcs, dtype, req_grad, transpose) ) str_values = list( product( dim1, dim2, dim3, dim4, str_funcs, dtype, req_grad_str, str_transpose ) ) names = [ "dim1_{}_dim2_{}_dim3_{}_dim4_{}_func_{}_dtype_{}_requires_grad_{}_transpose_{}".format( *vals ) for vals in str_values ] @pytest.mark.parametrize( "dim1, dim2, dim3, dim4, funcs, dtype, req_grad, transpose", values, ids=names, ) def test_matmul(dim1, dim2, dim3, dim4, funcs, dtype, req_grad, transpose): if not torch.cuda.is_available(): pytest.skip('No GPU found.') if dim2 > 0: dim2 = dim2 - (dim2 % 16) dim3 = dim3 - (dim3 % 16) dim4 = dim4 - (dim4 % 16) for i in range(k): # normal multiply if funcs[0] in [torch.mm, torch.matmul]: dimA = (dim2, dim3) if not transpose[0] else (dim3, dim2) dimB = (dim3, dim4) if not transpose[1] else (dim4, dim3) A = torch.randn(size=dimA, device="cuda", requires_grad=req_grad[0]) B = torch.randn(size=dimB, device="cuda", requires_grad=req_grad[1]) target = torch.randn( size=(dim2, dim4), device="cuda", requires_grad=req_grad[1] ) torch.nn.init.xavier_uniform_(B) if not transpose[0] and not transpose[1]: out_torch = funcs[0](A, B) out_bnb = funcs[1](A, B) elif not transpose[0] and transpose[1]: out_torch = funcs[0](A, B.t()) out_bnb = funcs[1](A, B.t()) elif transpose[0] and not transpose[1]: out_torch = funcs[0](A.t(), B) out_bnb = funcs[1](A.t(), B) elif transpose[0] and transpose[1]: out_torch = funcs[0](A.t(), B.t()) out_bnb = funcs[1](A.t(), B.t()) n = out_bnb.numel() idx = torch.isclose(out_bnb, out_torch, atol=0.01, rtol=0.1) assert (idx == 0).sum().item() < n * 0.0175 idx = torch.isclose(out_bnb, out_torch, atol=0.035, rtol=0.2) assert (idx == 0).sum().item() < n * 0.001 if any(req_grad): out_bnb.data.copy_(out_torch) torch.cuda.synchronize() loss_bnb = torch.nn.functional.mse_loss(out_bnb, target).mean() loss_bnb.backward() gradA1 = A.grad gradB1 = B.grad A.grad = None B.grad = None loss_torch = torch.nn.functional.mse_loss( out_torch, target ).mean() loss_torch.backward() gradA2 = A.grad gradB2 = B.grad A.grad = None B.grad = None if req_grad[0]: torch.testing.assert_allclose( gradA1, gradA2, atol=0.015, rtol=0.1 ) if req_grad[1]: n = gradB1.numel() idx = torch.isclose(gradB1, gradB2, atol=0.06, rtol=0.3) assert (idx == 0).sum().item() < n * 0.1 idx = torch.isclose(gradB1, gradB2, atol=0.10, rtol=0.3) assert (idx == 0).sum().item() < n * 0.02 torch.testing.assert_allclose( gradB1, gradB2, atol=0.18, rtol=0.3 ) # batched matrix multiply if funcs[0] in [torch.bmm, torch.matmul]: A = torch.randn( size=(dim1, dim2, dim3), device="cuda", requires_grad=req_grad[0], ) B = torch.randn( size=(dim1, dim3, dim4), device="cuda", requires_grad=req_grad[1], ) target = torch.randn( size=(dim1, dim2, dim4), device="cuda", requires_grad=req_grad[1], ) torch.nn.init.xavier_uniform_(B) out_torch = funcs[0](A, B) out_bnb = funcs[1](A, B) n = out_bnb.numel() idx = torch.isclose(out_bnb, out_torch, atol=0.01, rtol=0.1) assert (idx == 0).sum().item() < n * 0.01 torch.testing.assert_allclose( out_bnb, out_torch, atol=0.027, rtol=0.2 ) if any(req_grad): out_bnb.data.copy_(out_torch) torch.cuda.synchronize() loss_bnb = torch.nn.functional.mse_loss(out_bnb, target).mean() loss_bnb.backward() gradA1 = A.grad gradB1 = B.grad A.grad = None B.grad = None loss_torch = torch.nn.functional.mse_loss( out_torch, target ).mean() loss_torch.backward() gradA2 = A.grad gradB2 = B.grad A.grad = None B.grad = None if req_grad[0]: torch.testing.assert_allclose( gradA1, gradA2, atol=0.015, rtol=0.1 ) if req_grad[1]: n = gradB1.numel() idx = torch.isclose(gradB1, gradB2, atol=0.06, rtol=0.3) assert (idx == 0).sum().item() < n * 0.1 idx = torch.isclose(gradB1, gradB2, atol=0.10, rtol=0.3) assert (idx == 0).sum().item() < n * 0.02 if funcs[0] in [torch.matmul]: dim1 = dim1 - (dim1 % 16) A = torch.randn( size=(dim1, dim2, dim3), device="cuda", requires_grad=req_grad[0], ) dimB = (dim4, dim3) if transpose[1] else (dim3, dim4) B = torch.randn(size=dimB, device="cuda", requires_grad=req_grad[1]) target = torch.randn( size=(dim1, dim2, dim4), device="cuda", requires_grad=req_grad[1], ) torch.nn.init.xavier_uniform_(B) if transpose[1]: out_torch = funcs[0](A, B.t()) out_bnb = funcs[1](A, B.t()) else: out_torch = funcs[0](A, B) out_bnb = funcs[1](A, B) n = out_bnb.numel() idx = torch.isclose(out_bnb, out_torch, atol=0.01, rtol=0.1) assert (idx == 0).sum().item() < n * 0.0175 idx = torch.isclose(out_bnb, out_torch, atol=0.035, rtol=0.2) assert (idx == 0).sum().item() < n * 0.001 if any(req_grad): out_bnb.data.copy_(out_torch) torch.cuda.synchronize() loss_bnb = torch.nn.functional.mse_loss(out_bnb, target).mean() loss_bnb.backward() gradA1 = A.grad gradB1 = B.grad A.grad = None B.grad = None loss_torch = torch.nn.functional.mse_loss( out_torch, target ).mean() loss_torch.backward() gradA2 = A.grad gradB2 = B.grad A.grad = None B.grad = None if req_grad[0]: torch.testing.assert_allclose( gradA1, gradA2, atol=0.015, rtol=0.1 ) if req_grad[1]: n = gradB1.numel() idx = torch.isclose(gradB1, gradB2, atol=0.06, rtol=0.3) assert (idx == 0).sum().item() < n * 0.1 idx = torch.isclose(gradB1, gradB2, atol=0.10, rtol=0.3) assert (idx == 0).sum().item() < n * 0.02 n = 1 k = 3 dim1 = torch.randint(16, 64, size=(n,)).tolist() dim2 = torch.randint(32, 96, size=(n,)).tolist() dim3 = torch.randint(32, 96, size=(n,)).tolist() dim4 = torch.randint(32, 96, size=(n,)).tolist() dim2.append(0) decomp = [0.0, 6.0] funcs = [(torch.matmul, bnb.matmul)] str_funcs = ["matmul"] req_grad = [(False, False), (True, False), (True, True), (False, True)] req_grad = list(product([True, False], repeat=3)) req_grad_str = [] for c in req_grad: strval = '' for v in c: if v == True: strval += 'T' else: strval += 'F' req_grad_str.append(strval) transpose = [(False, True), (False, False)] str_transpose = ["NT", "NN"] dtype = [torch.float16, torch.bfloat16, torch.float32] has_fp16_weights = [True, False] has_bias = [True, False] values = list( product( dim1, dim2, dim3, dim4, funcs, dtype, req_grad, transpose, decomp, has_fp16_weights, has_bias ) ) str_values = list( product( dim1, dim2, dim3, dim4, str_funcs, dtype, req_grad_str, str_transpose, decomp, has_fp16_weights, has_bias ) ) names = ["dim1_{}_dim2_{}_dim3_{}_dim4_{}_func_{}_dtype_{}_requires_grad_{}_transpose_{}_decomp_{}_has_fp16_weights_{}_has_bias_{}".format(*vals) for vals in str_values] @pytest.mark.parametrize( "dim1, dim2, dim3, dim4, funcs, dtype, req_grad, transpose, decomp, has_fp16_weights, has_bias", values, ids=names, ) def test_matmullt( dim1, dim2, dim3, dim4, funcs, dtype, req_grad, transpose, decomp, has_fp16_weights, has_bias ): if not torch.cuda.is_available(): pytest.skip('No GPU found.') dimA = (dim2, dim3) if not transpose[0] else (dim3, dim2) dimB = (dim3, dim4) if not transpose[1] else (dim4, dim3) outlier_dim = torch.randint(0, dimA[1], size=(dimA[1] // 8,), device="cuda") if has_bias == False: req_grad = list(req_grad) req_grad[2] = False for i in range(k): # normal multiply if funcs[0] in [torch.mm, torch.matmul]: A = torch.randn( size=dimA, device="cuda", requires_grad=req_grad[0], dtype=dtype ) if decomp == 6.0: with torch.no_grad(): A[:, outlier_dim] = 6.0 B = torch.randn( size=dimB, device="cuda", requires_grad=req_grad[1], dtype=dtype ) target = torch.randn( size=(dim2, dim4), device="cuda", requires_grad=req_grad[1], dtype=dtype, ) bias = None bias2 = None if has_bias: bias = torch.randn(dim4, device='cuda', dtype=dtype, requires_grad=req_grad[2]) bias2 = bias.clone() torch.nn.init.xavier_uniform_(B) B2 = B.clone() state = bnb.MatmulLtState() state.threshold = decomp state.has_fp16_weights = has_fp16_weights if not has_fp16_weights: if not transpose[0] and not transpose[1]: B2 = B2.t().contiguous() ( state.CB, CBt, state.SCB, SCBt, coo_tensorB, ) = bnb.functional.double_quant(B2.to(torch.float16)) B2 = state.CB if not transpose[0] and transpose[1]: out_torch = funcs[0](A, B.t()) out_bnb = funcs[1](A, B2, state=state, bias=bias2) elif not transpose[0] and not transpose[1]: out_torch = funcs[0](A, B) out_bnb = funcs[1](A, B2.t(), state=state, bias=bias2) if has_bias: out_torch += bias assert out_bnb.dtype == A.dtype, f"bnb matmullt received {A.dtype} but returned {out_bnb.dtype}" n = out_bnb.numel() err = torch.abs(out_bnb - out_torch).mean().item() # print(f'abs error {err:.4f}') idx = torch.isclose(out_bnb, out_torch, atol=0.01, rtol=0.1) assert (idx == 0).sum().item() <= n * (0.0175 if dtype == torch.float16 else 0.021) idx = torch.isclose(out_bnb, out_torch, atol=0.035, rtol=0.2) assert (idx == 0).sum().item() <= n * 0.001 if has_fp16_weights: if any(req_grad): out_bnb.data.copy_(out_torch) torch.cuda.synchronize() loss_bnb = torch.nn.functional.mse_loss( out_bnb, target ).mean() loss_bnb.backward() gradA1 = A.grad gradB1 = B.grad A.grad = None B.grad = None if has_bias: gradBias1 = bias.grad bias.grad = None loss_torch = torch.nn.functional.mse_loss( out_torch, target ).mean() loss_torch.backward() gradA2 = A.grad gradB2 = B.grad A.grad = None B.grad = None if has_bias: gradBias2 = bias.grad bias.grad = None if req_grad[0]: torch.testing.assert_allclose( gradA1, gradA2, atol=0.015, rtol=0.1 ) if req_grad[1]: n = gradB1.numel() if dim2 > 0: assert torch.abs(gradB1).sum() > 0.0 assert torch.abs(gradB2).sum() > 0.0 else: assert torch.abs(gradB1).sum() == 0.0 assert torch.abs(gradB2).sum() == 0.0 idx = torch.isclose(gradB1, gradB2, atol=0.06, rtol=0.3) assert (idx == 0).sum().item() <= n * 0.1 idx = torch.isclose(gradB1, gradB2, atol=0.10, rtol=0.3) assert (idx == 0).sum().item() <= n * 0.02 torch.testing.assert_allclose( gradB1, gradB2, atol=0.18, rtol=0.3 ) if req_grad[2]: torch.testing.assert_allclose(gradBias1, gradBias2)
class MockArgs: def __init__(self, initial_data): for key in initial_data: setattr(self, key, initial_data[key]) class MLP8bit(torch.nn.Module): def __init__(self, dim1, dim2, has_fp16_weights=True, memory_efficient_backward=False, threshold=0.0): super().__init__() self.fc1 = bnb.nn.Linear8bitLt( dim1, dim2, has_fp16_weights=has_fp16_weights, memory_efficient_backward=memory_efficient_backward, threshold=threshold ) self.fc2 = bnb.nn.Linear8bitLt( dim2, dim1, has_fp16_weights=has_fp16_weights, memory_efficient_backward=memory_efficient_backward, threshold=threshold ) def forward(self, x): x = self.fc1(x) x = self.fc2(x) return x def get_args(): args = MockArgs([]) args.quant_type = "vector" args.use_8bit_training = "full" args.clip_freq = 9999 return args def assert_all_approx_close(a, b, atol=1e-8, rtol=1e-5, count=10): idx = torch.isclose(a, b, rtol, atol) sumval = (idx == 0).sum().item() if sumval > count: print(f"Too many values not close: assert {sumval} < {count}") torch.testing.assert_allclose(a, b, rtol, atol) class LinearFunction(torch.autograd.Function): @staticmethod def get_8bit_linear_trimmed(x, stochastic=False, trim_value=3.0): round_func = ( LinearFunction.round_stoachastic if stochastic else torch.round ) norm = math.sqrt(math.pi) / math.sqrt(2.0) # std = torch.abs(x).mean()*norm std = torch.std(x) max1 = std * trim_value x = x / max1 * 127 x = round_func(x) x[x > 127] = 127 x[x < -127] = -127 x = x / 127 * max1 return x def quant(x, quant_type, dim=1): if quant_type == "linear": max1 = torch.abs(x).max().float() xq = torch.round(x / max1 * 127).to(torch.int8) return xq, max1 elif quant_type == "vector": max1 = torch.amax(torch.abs(x), dim=dim, keepdim=True) xq = torch.round(x / max1 * 127).to(torch.int8) return xq, max1 elif quant_type == "min-max": maxA = torch.amax(x, dim=dim, keepdim=True).float() minA = torch.amin(x, dim=dim, keepdim=True).float() scale = (maxA - minA) / 2.0 xq = torch.round(127 * (x - minA - scale) / scale).to(torch.int8) return xq, (minA.float(), scale.float()) else: return None def dequant(xq, S1, S2, dtype, quant_type): if quant_type == "linear": norm = S1 * S2 / (127 * 127) # double cast needed to prevent overflows return (xq.float() * norm).to(dtype) elif quant_type == "vector": x = xq.float() if len(xq.shape) == 2 and len(S1.shape) == 3: S1 = S1.squeeze(0) if len(xq.shape) == 2 and len(S2.shape) == 3: S2 = S2.squeeze(0) # print(x.shape, S1.shape, S2.shape) if len(S1.shape) == 2: x *= S1.t() / 127 else: x *= S1 / 127 x *= S2 / 127 return x.to(dtype) else: return None def dequant_min_max(xq, A, B, SA, SB, dtype): offset = B.float().t().sum(0) * (SA[0] + SA[1]) x = xq.float() if len(xq.shape) == 2 and len(SB.shape) == 3: SB = SB.squeeze(0) if len(xq.shape) == 2 and len(SA.shape) == 3: SA = SA.squeeze(0) if len(SB.shape) == 2: x *= SB.t() / 127 else: x *= SB / 127 x *= SA[1] / 127 x += offset return x.to(dtype) def get_8bit_linear(x, stochastic=False): round_func = ( LinearFunction.round_stoachastic if stochastic else torch.round ) max1 = torch.abs(x).max() x = x / max1 * 127 x = round_func(x) / 127 * max1 # x = torch.round(x)/128*max1 return x @staticmethod def get_8bit_vector_wise(x, dim, stochastic=False): round_func = ( LinearFunction.round_stoachastic if stochastic else torch.round ) max1 = torch.amax(torch.abs(x), dim=dim, keepdim=True) max1[max1 == 0] = 1.0 x = (x * 127) / max1 x = round_func(x) / 127 * max1 return x @staticmethod def round_stoachastic(x): sign = torch.sign(x) absx = torch.abs(x) decimal = absx - torch.floor(absx) rdm = torch.rand_like(decimal) return sign * (torch.floor(absx) + (rdm < decimal).to(x.dtype)) @staticmethod def fake_8bit_storage(w, exponent_bits): code = bnb.functional.create_dynamic_map(n=exponent_bits).to(w.device) absmax, C = bnb.functional.quantize_blockwise(w.data, code=code) out = bnb.functional.dequantize_blockwise(absmax, C, code) out = out.half() w.copy_(out) return out @staticmethod def fake_8bit_storage_quantile(w, args): code = bnb.functional.estimate_quantiles(w.data, offset=args.offset) # C = bnb.functional.quantize_no_absmax(code, w) # out = bnb.functional.dequantize_no_absmax(code, C, out=w.data) # print(out) # out = out.half() code /= torch.max(torch.abs(code)) absmax, C = bnb.functional.quantize_blockwise(w.data, code=code) out = bnb.functional.dequantize_blockwise(absmax, C, code) out = out.half() w.copy_(out) return out @staticmethod def fake_8bit_storage_stoachstic(w): rand = torch.rand(1024, device=w.device) absmax, C = bnb.functional.quantize_blockwise(w.data, rand=rand) out = bnb.functional.dequantize_blockwise(absmax, C) out = out.half() w.copy_(out) return out @staticmethod def fake_8bit_storage_with_max(w, topk=8): blocked_w = einops.rearrange(w.flatten(), "(h b) -> h b", b=256) max_val, idx = torch.sort(torch.abs(blocked_w), dim=1, descending=True) idx = idx[:, :topk] max_val = max_val[:, :topk] mask = torch.zeros_like(blocked_w) mask.scatter_(dim=1, index=idx, src=torch.ones_like(max_val)) mask = mask.bool() # 1. zero out max values # 2. quantize + dequantize # 3. write back max values # 4. copy matrix back to weight values = blocked_w[mask] blocked_w[mask] = 0 code = bnb.functional.create_dynamic_map() code = code.to(w.device) absmax, C = bnb.functional.quantize_blockwise(blocked_w.data) bnb.functional.dequantize_blockwise(absmax, C, out=blocked_w) blocked_w[mask] = values unblocked_w = blocked_w.flatten().view(w.shape) w.copy_(unblocked_w) return unblocked_w @staticmethod def forward(ctx, x, weight, bias=None, args=None): if args.use_8bit_training != "off": weight8, S1 = LinearFunction.quant(weight, args.quant_type, dim=1) x8, S2 = LinearFunction.quant(x, args.quant_type, dim=2) outputq = bnb.functional.igemm(x8, weight8.t()) output = LinearFunction.dequant( outputq, S1, S2, x.dtype, args.quant_type ) # if torch.rand(1) < 0.01: # output32 = torch.matmul(x, weight.t()) # err = torch.abs(output-output32).float() # relerr = err/(torch.abs(output32).float()+1e-8) # print(f'{err.mean().item():.4f}, {relerr.mean().item():.4f}', args.quant_type, 'forward', proxy) else: # output = torch.matmul(x, weight.t()) output = torch.einsum("bsi,oi->bso", x, weight) ctx.save_for_backward(x, weight, bias) ctx.args = args if bias is not None: output += bias.unsqueeze(0).expand_as(output) return output @staticmethod def backward(ctx, grad_output): x, weight, bias = ctx.saved_tensors args = ctx.args stochastic = False grad_input = grad_weight = grad_bias = None if bias is not None and ctx.needs_input_grad[2]: grad_bias = grad_output.sum(0) # weight and x are already 8bit # -> transform grad_output to 8-bit if args.use_8bit_training == "forward+wgrad": grad_output8, S1 = LinearFunction.quant( grad_output, args.quant_type, dim=[0, 1] ) x8, S2 = LinearFunction.quant(x, args.quant_type, dim=[0, 1]) grad_weight8 = bnb.functional.igemm(grad_output8, x8) grad_weight = LinearFunction.dequant( grad_weight8, S1, S2, grad_output.dtype, args.quant_type ) # grad_weight32 = torch.einsum('bso,bsi->oi', grad_output, x) grad_input = grad_output.matmul(weight) elif args.use_8bit_training == "full": grad_output8, S1 = LinearFunction.quant( grad_output, args.quant_type, dim=[0, 1] ) x8, S2 = LinearFunction.quant(x, args.quant_type, dim=[0, 1]) grad_weight8 = torch.zeros_like(weight, dtype=torch.int32) bnb.functional.igemm(grad_output8, x8, out=grad_weight8) grad_weight = LinearFunction.dequant( grad_weight8, S1, S2, grad_output.dtype, args.quant_type ) grad_output8, S1 = LinearFunction.quant( grad_output, args.quant_type, dim=2 ) weight8, S3 = LinearFunction.quant(weight, args.quant_type, dim=0) grad_input8 = bnb.functional.igemm(grad_output8, weight8) grad_input = LinearFunction.dequant( grad_input8, S1, S3, grad_output.dtype, args.quant_type ) else: grad_input = grad_output.matmul(weight) grad_weight = torch.einsum("bsi,bso->oi", x, grad_output) return grad_input, grad_weight, grad_bias, None class Linear8bit(nn.Module): def __init__(self, input_features, output_features, bias=True, args=None): super().__init__() self.input_features = input_features self.output_features = output_features self.args = args self.weight = nn.Parameter(torch.empty(output_features, input_features)) if bias: self.bias = nn.Parameter(torch.empty(output_features)) else: self.register_parameter("bias", None) torch.nn.init.xavier_uniform_(self.weight) if self.bias is not None: torch.nn.init.zeros_(self.bias) def forward(self, x): self.args.training = self.training return LinearFunction.apply(x, self.weight, self.bias, self.args) threshold = [0.0, 3.0] values = threshold names = [f"threshold_{vals}" for vals in values] @pytest.mark.parametrize("threshold", values, ids=names) def test_linear8bitlt_inference(threshold): l1 = bnb.nn.Linear8bitLt(32, 64, threshold=threshold).cuda().half() assert l1.weight.device.type == "cuda" assert l1.weight.dtype == torch.float16 l1.eval() for i in range(100): b1 = torch.randn(16, 8, 32, device="cuda").half() o1 = l1(b1) if i == 1: assert l1.state.CxB is not None def test_linear8bitlt_accumulated_gradient(): l1 = torch.nn.Sequential( *[bnb.nn.Linear8bitLt(32, 32).cuda().half() for i in range(2)] ) l2 = torch.nn.Sequential( *[torch.nn.Linear(32, 32).cuda().half() for i in range(2)] ) l2[0].weight = torch.nn.Parameter(l1[0].weight.clone()) l2[0].bias = torch.nn.Parameter(l1[0].bias.clone()) l2[1].weight = torch.nn.Parameter(l1[1].weight.clone()) l2[1].bias = torch.nn.Parameter(l1[1].bias.clone()) opt1 = bnb.optim.Adam8bit(l1.parameters(), lr=0.001) opt2 = bnb.optim.Adam8bit(l2.parameters(), lr=0.001) acc_steps = 10 for i in range(10): b1 = torch.randn(16, 8, 32, device="cuda").half() o1 = l1(b1) o2 = l2(b1) loss1 = o1.mean() loss2 = o2.mean() loss1.backward() loss2.backward() if i == 2: assert l1[0].state.CxB is not None assert l1[1].state.CxB is not None if i > 0 and i % acc_steps == 0: opt1.step() opt1.zero_grad(True) opt2.step() opt2.zero_grad(True) assert_all_approx_close( l1[0].weight, l2[0].weight, rtol=1.05, atol=0.01, count=2 ) assert_all_approx_close( l1[1].weight, l2[1].weight, rtol=1.05, atol=0.01, count=2 ) # we do this copy because otherwise we have small divergences over time that add up l1[0].weight.data.copy_(l2[0].weight.data) l1[1].weight.data.copy_(l2[1].weight.data) else: torch.testing.assert_allclose(l1[0].weight.grad, l2[0].weight.grad) torch.testing.assert_allclose(l1[1].weight.grad, l2[1].weight.grad) threshold = [0.0, 2.0] values = threshold names = [f"threshold_{vals}" for vals in values] @pytest.mark.parametrize("threshold", values, ids=names) @pytest.mark.parametrize("memory_efficient_backward", [False]) def test_linear8bitlt_no_fp16_weights(threshold, memory_efficient_backward): l1 = ( bnb.nn.Linear8bitLt( 32, 64, threshold=threshold, has_fp16_weights=False, memory_efficient_backward=memory_efficient_backward ) .cuda() .half() ) assert l1.weight.dtype == torch.int8 l1.eval() for i in range(100): b1 = torch.randn(16, 8, 32, device="cuda").half() o1 = l1(b1) assert o1.dtype == torch.float16 mlp = MLP8bit(32, 64, threshold=threshold, has_fp16_weights=False).cuda() assert mlp.fc1.weight.dtype == torch.int8 assert mlp.fc2.weight.dtype == torch.int8 for i in range(100): b1 = torch.randn(16, 8, 32, device="cuda").half() o1 = mlp(b1) assert o1.dtype == torch.float16 if threshold > 0: assert mlp.fc1.state.idx is not None if threshold > 0: assert mlp.fc2.state.idx is not None mlp = ( MLP8bit(32, 64, threshold=threshold, has_fp16_weights=False) .cuda() .half() ) assert mlp.fc1.weight.dtype == torch.int8 assert mlp.fc2.weight.dtype == torch.int8 for i in range(100): b1 = torch.randn(16, 8, 32, device="cuda").half() o1 = mlp(b1) assert o1.dtype == torch.float16 if threshold > 0: assert mlp.fc1.state.idx is not None if threshold > 0: assert mlp.fc2.state.idx is not None mlp = ( MLP8bit(32, 64, threshold=threshold, has_fp16_weights=False) .half() .cuda() ) for i in range(100): b1 = torch.randn(16, 8, 32, device="cuda").half() o1 = mlp(b1) assert o1.dtype == torch.float16 if threshold > 0: assert mlp.fc1.state.idx is not None if threshold > 0: assert mlp.fc2.state.idx is not None assert mlp.fc1.weight.dtype == torch.int8 assert mlp.fc2.weight.dtype == torch.int8 mlp = ( MLP8bit( 32, 64, threshold=threshold, has_fp16_weights=False, memory_efficient_backward=memory_efficient_backward ) .half() .to("cuda") ) for i in range(100): b1 = torch.randn(16, 8, 32, device="cuda").half() o1 = mlp(b1) assert o1.dtype == torch.float16 if threshold > 0: assert mlp.fc1.state.idx is not None if threshold > 0: assert mlp.fc2.state.idx is not None assert mlp.fc1.weight.dtype == torch.int8 assert mlp.fc2.weight.dtype == torch.int8 assert mlp.fc1.weight.device.type == "cuda" assert mlp.fc2.weight.device.type == "cuda" mlp = MLP8bit( 32, 64, threshold=threshold, has_fp16_weights=False, memory_efficient_backward=memory_efficient_backward ) w1, w2 = mlp.fc1.weight.clone().cuda(), mlp.fc2.weight.clone().cuda() # grab weights before quantization, mlp = mlp.cuda().half() # and this line triggers quantization for i in range(100): b1 = torch.randn(16, 8, 32, device="cuda").half() o1 = mlp(b1) assert o1.dtype == torch.float16 if threshold > 0: assert mlp.fc1.state.idx is not None if threshold > 0: assert mlp.fc2.state.idx is not None assert mlp.fc1.weight.dtype == torch.int8 assert mlp.fc2.weight.dtype == torch.int8 assert mlp.fc1.weight.device.type == "cuda" assert mlp.fc2.weight.device.type == "cuda" if memory_efficient_backward: b1 = torch.randn(16, 8, 32, device="cuda", requires_grad=True, dtype=torch.half) o1 = mlp(b1) assert o1.dtype == torch.float16 assert o1.requires_grad grad_proj = torch.randn_like(o1) mlp.zero_grad() (o1 * grad_proj).sum().backward() grad_ref = grad_proj.flatten(2) @ w2.half() @ w1.half() scale = grad_ref.abs().mean() torch.testing.assert_allclose(b1.grad, grad_ref, rtol=0, atol=0.05 * scale) idx = torch.isclose(b1.grad, grad_ref, atol=0.01 * scale, rtol=0.1) assert (idx == 0).sum().item() <= b1.numel() * 0.005 def test_linear8bitlt_fp32_bias(): # casts model to fp16 -> int8 automatically l1 = bnb.nn.Linear8bitLt(32, 64, has_fp16_weights=False).cuda() assert l1.weight.dtype == torch.int8 assert l1.bias.dtype == torch.float32 for i in range(100): b1 = torch.randn(16, 8, 32, device="cuda").half() # casts bias to fp32 o1 = l1(b1) assert l1.bias.dtype == torch.float16 # casts model to fp16 -> int8 automatically l1 = bnb.nn.Linear8bitLt(32, 64, has_fp16_weights=False, bias=False).cuda() assert l1.weight.dtype == torch.int8 assert l1.bias is None for i in range(100): b1 = torch.randn(16, 8, 32, device="cuda").half() o1 = l1(b1) assert l1.bias is None
setup = CUDASetup.get_instance() if setup.initialized != True: setup.run_cuda_setup() if 'BITSANDBYTES_NOWELCOME' not in os.environ or str(os.environ['BITSANDBYTES_NOWELCOME']) == '0': setup.print_log_stack() lib = setup.lib try: if lib is None and torch.cuda.is_available(): CUDASetup.get_instance().generate_instructions() CUDASetup.get_instance().print_log_stack() raise RuntimeError(''' CUDA Setup failed despite GPU being available. Inspect the CUDA SETUP outputs above to fix your environment! If you cannot find any issues and suspect a bug, please open an issue with detals about your environment: https://github.com/TimDettmers/bitsandbytes/issues''') lib.cadam32bit_g32 lib.get_context.restype = ct.c_void_p lib.get_cusparse.restype = ct.c_void_p COMPILED_WITH_CUDA = True except AttributeError: warn("The installed version of bitsandbytes was compiled without GPU support. " "8-bit optimizers and GPU quantization are unavailable.") COMPILED_WITH_CUDA = False
# 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. MatmulLtState, bmm_cublas, matmul, matmul_cublas, mm_cublas, ) if COMPILED_WITH_CUDA: from .optim import adam __pdoc__ = { "libbitsandbytes": False, "optim.optimizer.Optimizer8bit": False, "optim.optimizer.MockArgs": False, } PACKAGE_GITHUB_URL = "https://github.com/TimDettmers/bitsandbytes"
# 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. # math.prod not compatible with python < 3.8 def prod(iterable): return reduce(operator.mul, iterable, 1) name2qmap = {} if COMPILED_WITH_CUDA: """C FUNCTIONS FOR OPTIMIZERS""" str2optimizer32bit = {} str2optimizer32bit["adam"] = (lib.cadam32bit_g32, lib.cadam32bit_g16) str2optimizer32bit["momentum"] = ( lib.cmomentum32bit_g32, lib.cmomentum32bit_g16, ) str2optimizer32bit["rmsprop"] = ( lib.crmsprop32bit_g32, lib.crmsprop32bit_g16, ) str2optimizer32bit["lion"] = ( lib.clion32bit_g32, lib.clion32bit_g16, ) str2optimizer32bit["adagrad"] = ( lib.cadagrad32bit_g32, lib.cadagrad32bit_g16, ) str2optimizer32bit["lars"] = ( lib.cmomentum32bit_g32, lib.cmomentum32bit_g16, ) str2optimizer32bit["lamb"] = (lib.cadam32bit_g32, lib.cadam32bit_g16) str2optimizer8bit = {} str2optimizer8bit["adam"] = ( lib.cadam_static_8bit_g32, lib.cadam_static_8bit_g16, ) str2optimizer8bit["momentum"] = ( lib.cmomentum_static_8bit_g32, lib.cmomentum_static_8bit_g16, ) str2optimizer8bit["rmsprop"] = ( lib.crmsprop_static_8bit_g32, lib.crmsprop_static_8bit_g16, ) str2optimizer8bit["lion"] = ( lib.clion_static_8bit_g32, lib.clion_static_8bit_g16, ) str2optimizer8bit["lamb"] = ( lib.cadam_static_8bit_g32, lib.cadam_static_8bit_g16, ) str2optimizer8bit["lars"] = ( lib.cmomentum_static_8bit_g32, lib.cmomentum_static_8bit_g16, ) str2optimizer8bit_blockwise = {} str2optimizer8bit_blockwise["adam"] = ( lib.cadam_8bit_blockwise_fp32, lib.cadam_8bit_blockwise_fp16, ) str2optimizer8bit_blockwise["momentum"] = ( lib.cmomentum_8bit_blockwise_fp32, lib.cmomentum_8bit_blockwise_fp16, ) str2optimizer8bit_blockwise["rmsprop"] = ( lib.crmsprop_8bit_blockwise_fp32, lib.crmsprop_8bit_blockwise_fp16, ) str2optimizer8bit_blockwise["lion"] = ( lib.clion_8bit_blockwise_fp32, lib.clion_8bit_blockwise_fp16, ) str2optimizer8bit_blockwise["adagrad"] = ( lib.cadagrad_8bit_blockwise_fp32, lib.cadagrad_8bit_blockwise_fp16, ) class CUBLAS_Context: _instance = None def __init__(self): raise RuntimeError("Call get_instance() instead") def initialize(self): self.context = {} # prev_device = torch.cuda.current_device() # for i in range(torch.cuda.device_count()): # torch.cuda.set_device(torch.device('cuda', i)) # self.context.append(ct.c_void_p(lib.get_context())) # torch.cuda.set_device(prev_device) @classmethod def get_instance(cls): if cls._instance is None: cls._instance = cls.__new__(cls) cls._instance.initialize() return cls._instance def get_context(self, device): if device.index not in self.context: prev_device = torch.cuda.current_device() torch.cuda.set_device(device) self.context[device.index] = ct.c_void_p(lib.get_context()) torch.cuda.set_device(prev_device) return self.context[device.index] class Cusparse_Context: _instance = None def __init__(self): raise RuntimeError("Call get_instance() instead") def initialize(self): self.context = ct.c_void_p(lib.get_cusparse()) @classmethod def get_instance(cls): if cls._instance is None: cls._instance = cls.__new__(cls) cls._instance.initialize() return cls._instance def create_linear_map(signed=True, total_bits=8, add_zero=True): sign = (-1.0 if signed else 0.0) total_values = 2**total_bits if add_zero or total_bits < 8: # add a zero # since we simulate less bits by having zeros in the data type, we # we need to center the quantization around zero and as such lose # a single value total_values = (2**total_bits if not signed else 2**total_bits-1) values = torch.linspace(sign, 1.0, total_values) gap = 256 - values.numel() if gap == 0: return values else: l = values.numel()//2 #return torch.Tensor(values[:l].tolist() + [-1e-6]*((gap//2)-1) + [0]*2 + [1e-6]*((gap//2)-1) + values[l:].tolist()) return torch.Tensor(values[:l].tolist() + [0]*gap + values[l:].tolist()) def create_fp8_map(signed=True, exponent_bits=5, precision_bits=2, total_bits=8): e = exponent_bits p = precision_bits has_sign = 1 if signed else 0 assert e+p == total_bits-has_sign # the exponent is biased to 2^(e-1) -1 == 0 evalues = [] pvalues = [] for i, val in enumerate(range(-((2**(exponent_bits-has_sign))), 2**(exponent_bits-has_sign), 1)): evalues.append(2**val) values = [] lst = list(itertools.product([0, 1], repeat=precision_bits)) #for ev in evalues: bias = 2**(exponent_bits-1)-1 for evalue in range(2**(exponent_bits)): for bit_pattern in lst: value = (1 if evalue != 0 else 0) for i, pval in enumerate(list(bit_pattern)): value += pval*(2**-(i+1)) if evalue == 0: # subnormals value = value*2**-(bias-1) else: # normals value = value*2**-(evalue-bias-2) values.append(value) if signed: values.append(-value) assert len(values) == 2**total_bits values.sort() if total_bits < 8: gap = 256 - len(values) for i in range(gap): values.append(0) values.sort() code = torch.Tensor(values) code /= code.max() return code def create_dynamic_map(signed=True, max_exponent_bits=7, total_bits=8): """ Creates the dynamic quantiztion map. The dynamic data type is made up of a dynamic exponent and fraction. As the exponent increase from 0 to -7 the number of bits available for the fraction shrinks. This is a generalization of the dynamic type where a certain number of the bits and be reserved for the linear quantization region (the fraction). n determines the maximum number of exponent bits. For more details see (8-Bit Approximations for Parallelism in Deep Learning)[https://arxiv.org/abs/1511.04561] """ data = [] # these are additional items that come from the case # where all the exponent bits are zero and no # indicator bit is present non_sign_bits = total_bits - (1 if signed else 0) additional_items = 2 ** (non_sign_bits - max_exponent_bits) - 1 if not signed: additional_items = 2 * additional_items for i in range(max_exponent_bits): fraction_items = int((2 ** (i + non_sign_bits - max_exponent_bits) + 1 if signed else 2 ** (i + non_sign_bits - max_exponent_bits + 1) + 1)) boundaries = torch.linspace(0.1, 1, fraction_items) means = (boundaries[:-1] + boundaries[1:]) / 2.0 data += ((10 ** (-(max_exponent_bits - 1) + i)) * means).tolist() if signed: data += (-(10 ** (-(max_exponent_bits - 1) + i)) * means).tolist() if additional_items > 0: boundaries = torch.linspace(0.1, 1, additional_items + 1) means = (boundaries[:-1] + boundaries[1:]) / 2.0 data += ((10 ** (-(max_exponent_bits - 1) + i)) * means).tolist() if signed: data += (-(10 ** (-(max_exponent_bits - 1) + i)) * means).tolist() data.append(0) data.append(1.0) gap = 256 - len(data) for i in range(gap): data.append(0) data.sort() return Tensor(data) def create_quantile_map(A, total_bits=8): q = estimate_quantiles(A, num_quantiles=2**total_bits-1) q = q.tolist() q.append(0) gap = 256 - len(q) for i in range(gap): q.append(0) q.sort() q = Tensor(q) q = q/q.abs().max() return q def get_special_format_str(): if not torch.cuda.is_available(): return 'col_turing' major, _minor = torch.cuda.get_device_capability() if major <= 7: return "col_turing" if major == 8: return "col_ampere" return "col_turing" def is_on_gpu(tensors): on_gpu = True for t in tensors: if t is None: continue # NULL pointers are fine on_gpu &= t.device.type == 'cuda' return on_gpu def get_ptr(A: Tensor) -> ct.c_void_p: """ Get the ctypes pointer from a PyTorch Tensor. Parameters ---------- A : torch.tensor The PyTorch tensor. Returns ------- ctypes.c_void_p """ if A is None: return None else: return ct.c_void_p(A.data.data_ptr()) def pre_call(device): prev_device = torch.cuda.current_device() torch.cuda.set_device(device) return prev_device def post_call(prev_device): torch.cuda.set_device(prev_device) def get_transform_func(dtype, orderA, orderOut, transpose=False): name = f'ctransform_{(8 if dtype == torch.int8 else 32)}_{orderA}_to_{orderOut}_{"t" if transpose else "n"}' if not hasattr(lib, name): print(name) raise ValueError( f"Transform function not supported: {orderA} to {orderOut} for data type {dtype} and transpose={transpose}" ) else: return getattr(lib, name) def get_transform_buffer( shape, dtype, device, to_order, from_order="row", transpose=False ): # init_func = torch.empty init_func = torch.zeros dims = len(shape) if dims == 2: rows = shape[0] elif dims == 3: rows = shape[0] * shape[1] cols = shape[-1] state = (shape, to_order) if transpose: # swap dims tmp = rows rows = cols cols = tmp state = (shape[::-1], to_order) if to_order == "row" or to_order == "col": return init_func(shape, dtype=dtype, device=device), state elif to_order == "col32": # blocks of 32 columns (padded) cols = 32 * ((cols + 31) // 32) return init_func((rows, cols), dtype=dtype, device=device), state elif to_order == "col_turing": # blocks of 32 columns and 8 rows cols = 32 * ((cols + 31) // 32) rows = 8 * ((rows + 7) // 8) return init_func((rows, cols), dtype=dtype, device=device), state elif to_order == "col_ampere": # blocks of 32 columns and 32 rows cols = 32 * ((cols + 31) // 32) rows = 32 * ((rows + 31) // 32) return init_func((rows, cols), dtype=dtype, device=device), state else: raise NotImplementedError(f"To_order not supported: {to_order}") def nvidia_transform( A, to_order, from_order="row", out=None, transpose=False, state=None, ld=None, ): if state is None: state = (A.shape, from_order) else: from_order = state[1] if out is None: out, new_state = get_transform_buffer( state[0], A.dtype, A.device, to_order, state[1] ) else: new_state = (state[1], to_order) func = get_transform_func(A.dtype, from_order, to_order, transpose) shape = state[0] if len(shape) == 2: dim1 = ct.c_int32(shape[0]) dim2 = ct.c_int32(shape[1]) elif ld is not None: n = prod(shape) dim1 = prod([shape[i] for i in ld]) dim2 = ct.c_int32(n // dim1) dim1 = ct.c_int32(dim1) else: dim1 = ct.c_int32(shape[0] * shape[1]) dim2 = ct.c_int32(shape[2]) ptr = CUBLAS_Context.get_instance().get_context(A.device) func(ptr, get_ptr(A), get_ptr(out), dim1, dim2) return out, new_state def estimate_quantiles(A: Tensor, out: Tensor = None, offset: float = 1 / 512, num_quantiles=256) -> Tensor: ''' Estimates 256 equidistant quantiles on the input tensor eCDF. Uses SRAM-Quantiles algorithm to quickly estimate 256 equidistant quantiles via the eCDF of the input tensor `A`. This is a fast but approximate algorithm and the extreme quantiles close to 0 and 1 have high variance / large estimation errors. These large errors can be avoided by using the offset variable which trims the distribution. The default offset value of 1/512 ensures minimum entropy encoding -- it trims 1/512 = 0.2% from each side of the distrivution. An offset value of 0.01 to 0.02 usually has a much lower error but is not a minimum entropy encoding. Given an offset of 0.02 equidistance points in the range [0.02, 0.98] are used for the quantiles. Parameters ---------- A : torch.Tensor The input tensor. Any shape. out : torch.Tensor Tensor with the 256 estimated quantiles. offset : float The offset for the first and last quantile from 0 and 1. Default: 1/(2*num_quantiles) num_quantiles : int The number of equally spaced quantiles. Returns ------- torch.Tensor: The 256 quantiles in float32 datatype. ''' if A.numel() < 256: raise NotImplementedError(f'Quantile estimation needs at least 256 values in the Tensor, but Tensor had only {A.numel()} values.') if num_quantiles > 256: raise NotImplementedError(f"Currently only a maximum of 256 equally spaced quantiles are supported, but the argument num_quantiles={num_quantiles}") if num_quantiles < 256 and offset == 1/(512): # override default arguments offset = 1/(2*num_quantiles) if out is None: out = torch.zeros((256,), dtype=torch.float32, device=A.device) is_on_gpu([A, out]) device = pre_call(A.device) if A.dtype == torch.float32: lib.cestimate_quantiles_fp32(get_ptr(A), get_ptr(out), ct.c_float(offset), ct.c_int(A.numel())) elif A.dtype == torch.float16: lib.cestimate_quantiles_fp16(get_ptr(A), get_ptr(out), ct.c_float(offset), ct.c_int(A.numel())) else: raise NotImplementedError(f"Not supported data type {A.dtype}") post_call(device) if num_quantiles < 256: step = round(256/num_quantiles) idx = torch.linspace(0, 255, num_quantiles).long().to(A.device) out = out[idx] return out def quantize_blockwise(A: Tensor, code: Tensor = None, absmax: Tensor = None, rand=None, out: Tensor = None, blocksize=4096) -> Tensor: """ Quantize tensor A in blocks of size 4096 values. Quantizes tensor A by dividing it into blocks of 4096 values. Then the absolute maximum value within these blocks is calculated for the non-linear quantization. Parameters ---------- A : torch.Tensor The input tensor. code : torch.Tensor The quantization map. absmax : torch.Tensor The absmax values. rand : torch.Tensor The tensor for stochastic rounding. out : torch.Tensor The output tensor (8-bit). Returns ------- torch.Tensor: The 8-bit tensor. tuple(torch.Tensor, torch.Tensor): The quantization state to undo the quantization. """ if code is None: if "dynamic" not in name2qmap: name2qmap["dynamic"] = create_dynamic_map().to(A.device) code = name2qmap["dynamic"] if absmax is None: n = A.numel() blocks = n // blocksize blocks += 1 if n % blocksize > 0 else 0 absmax = torch.zeros((blocks,), device=A.device) if out is None: out = torch.zeros_like(A, dtype=torch.uint8) if A.device.type != 'cpu': assert blocksize in [4096, 2048, 1024, 512, 256, 128, 64] cblocksize = ct.c_int32(blocksize) prev_device = pre_call(A.device) code = code.to(A.device) if rand is not None: is_on_gpu([code, A, out, absmax, rand]) assert blocksize==4096 assert rand.numel() >= 1024 rand_offset = random.randint(0, 1023) if A.dtype == torch.float32: lib.cquantize_blockwise_stochastic_fp32(get_ptr(code), get_ptr(A),get_ptr(absmax), get_ptr(out), get_ptr(rand), ct.c_int32(rand_offset), ct.c_int(A.numel())) elif A.dtype == torch.float16: lib.cquantize_blockwise_stochastic_fp16(get_ptr(code), get_ptr(A),get_ptr(absmax), get_ptr(out), get_ptr(rand), ct.c_int32(rand_offset), ct.c_int(A.numel())) else: raise ValueError(f"Blockwise quantization only supports 16/32-bit floats, but got {A.dtype}") else: is_on_gpu([code, A, out, absmax]) if A.dtype == torch.float32: lib.cquantize_blockwise_fp32(get_ptr(code), get_ptr(A), get_ptr(absmax), get_ptr(out), cblocksize, ct.c_int(A.numel())) elif A.dtype == torch.float16: lib.cquantize_blockwise_fp16(get_ptr(code), get_ptr(A), get_ptr(absmax), get_ptr(out), cblocksize, ct.c_int(A.numel())) else: raise ValueError(f"Blockwise quantization only supports 16/32-bit floats, but got {A.dtype}") post_call(A.device) else: # cpu code = code.cpu() assert rand is None lib.cquantize_blockwise_cpu_fp32(get_ptr(code), get_ptr(A), get_ptr(absmax), get_ptr(out), ct.c_longlong(blocksize), ct.c_longlong(A.numel())) return out, (absmax, code) def dequantize_blockwise( A: Tensor, quant_state: Tuple[Tensor, Tensor] = None, absmax: Tensor = None, code: Tensor = None, out: Tensor = None, blocksize: int = 4096, ) -> Tensor: """ Dequantizes blockwise quantized values. Dequantizes the tensor A with maximum absolute values absmax in blocks of size 4096. Parameters ---------- A : torch.Tensor The input 8-bit tensor. quant_state : tuple(torch.Tensor, torch.Tensor) Tuple of code and absmax values. absmax : torch.Tensor The absmax values. code : torch.Tensor The quantization map. out : torch.Tensor Dequantized output tensor (default: float32) Returns ------- torch.Tensor: Dequantized tensor (default: float32) """ assert quant_state is not None or absmax is not None if code is None and quant_state is None: if "dynamic" not in name2qmap: name2qmap["dynamic"] = create_dynamic_map().to(A.device) code = name2qmap["dynamic"] if out is None: out = torch.zeros_like(A, dtype=torch.float32) if quant_state is None: quant_state = (absmax, code) else: absmax, code = quant_state if A.device.type != 'cpu': device = pre_call(A.device) code = code.to(A.device) if blocksize not in [2048, 4096, 1024, 512, 256, 128, 64]: raise ValueError(f"The blockwise of {blocksize} is not supported. Supported values: [2048, 4096, 1024, 512, 256, 128, 64]") is_on_gpu([A, out]) if out.dtype == torch.float32: lib.cdequantize_blockwise_fp32(get_ptr(code), get_ptr(A), get_ptr(absmax), get_ptr(out), ct.c_int(blocksize), ct.c_int(A.numel())) elif out.dtype == torch.float16: lib.cdequantize_blockwise_fp16(get_ptr(code), get_ptr(A), get_ptr(absmax), get_ptr(out), ct.c_int(blocksize), ct.c_int(A.numel())) else: raise ValueError(f"Blockwise quantization only supports 16/32-bit floats, but got {A.dtype}") post_call(A.device) else: code = code.cpu() lib.cdequantize_blockwise_cpu_fp32(get_ptr(quant_state[1]), get_ptr(A), get_ptr(quant_state[0]), get_ptr(out), ct.c_longlong(blocksize), ct.c_longlong(A.numel())) return out def quantize(A: Tensor, code: Tensor = None, out: Tensor = None) -> Tensor: if code is None: if "dynamic" not in name2qmap: name2qmap["dynamic"] = create_dynamic_map().to(A.device) code = name2qmap["dynamic"] code = code.to(A.device) absmax = torch.abs(A).max() inp = A / absmax out = quantize_no_absmax(inp, code, out) return out, (absmax, code) def dequantize( A: Tensor, quant_state: Tuple[Tensor, Tensor] = None, absmax: Tensor = None, code: Tensor = None, out: Tensor = None, ) -> Tensor: assert quant_state is not None or absmax is not None if code is None and quant_state is None: if "dynamic" not in name2qmap: name2qmap["dynamic"] = create_dynamic_map().to(A.device) code = name2qmap["dynamic"] code = code.to(A.device) if quant_state is None: quant_state = (absmax, code) out = dequantize_no_absmax(A, quant_state[1], out) return out * quant_state[0] def quantize_no_absmax(A: Tensor, code: Tensor, out: Tensor = None) -> Tensor: ''' Quantizes input tensor to 8-bit. Quantizes the 32-bit input tensor `A` to the 8-bit output tensor `out` using the quantization map `code`. Parameters ---------- A : torch.Tensor The input tensor. code : torch.Tensor The quantization map. out : torch.Tensor, optional The output tensor. Needs to be of type byte. Returns ------- torch.Tensor: Quantized 8-bit tensor. ''' if out is None: out = torch.zeros_like(A, dtype=torch.uint8) is_on_gpu([A, out]) lib.cquantize(get_ptr(code), get_ptr(A), get_ptr(out), ct.c_int(A.numel())) return out def dequantize_no_absmax(A: Tensor, code: Tensor, out: Tensor = None) -> Tensor: ''' Dequantizes the 8-bit tensor to 32-bit. Dequantizes the 8-bit tensor `A` to the 32-bit tensor `out` via the quantization map `code`. Parameters ---------- A : torch.Tensor The 8-bit input tensor. code : torch.Tensor The quantization map. out : torch.Tensor The 32-bit output tensor. Returns ------- torch.Tensor: 32-bit output tensor. ''' if out is None: out = torch.zeros_like(A, dtype=torch.float32) is_on_gpu([code, A, out]) lib.cdequantize(get_ptr(code), get_ptr(A), get_ptr(out), ct.c_int(A.numel())) return out def optimizer_update_32bit( optimizer_name: str, g: Tensor, p: Tensor, state1: Tensor, beta1: float, eps: float, step: int, lr: float, state2: Tensor = None, beta2: float = 0.0, weight_decay: float = 0.0, gnorm_scale: float = 1.0, unorm_vec: Tensor = None, max_unorm: float = 0.0, skip_zeros=False, ) -> None: """ Performs an inplace optimizer update with one or two optimizer states. Universal optimizer update for 32-bit state and 32/16-bit gradients/weights. Parameters ---------- optimizer_name : str The name of the optimizer: {adam}. g : torch.Tensor Gradient tensor. p : torch.Tensor Parameter tensor. state1 : torch.Tensor Optimizer state 1. beta1 : float Optimizer beta1. eps : float Optimizer epsilon. weight_decay : float Weight decay. step : int Current optimizer step. lr : float The learning rate. state2 : torch.Tensor Optimizer state 2. beta2 : float Optimizer beta2. gnorm_scale : float The factor to rescale the gradient to the max clip value. unorm_vec : torch.Tensor The tensor for the update norm. max_unorm : float The maximum update norm relative to the weight norm. skip_zeros : bool Whether to skip zero-valued gradients or not (default: False). """ param_norm = 0.0 if max_unorm > 0.0: param_norm = torch.norm(p.data.float()) if optimizer_name not in str2optimizer32bit: raise NotImplementedError( f'Optimizer not implemented: {optimizer_name}. Choices: {",".join(str2optimizer32bit.keys())}' ) if g.dtype == torch.float32 and state1.dtype == torch.float32: str2optimizer32bit[optimizer_name][0]( get_ptr(g), get_ptr(p), get_ptr(state1), get_ptr(state2), get_ptr(unorm_vec), ct.c_float(max_unorm), ct.c_float(param_norm), ct.c_float(beta1), ct.c_float(beta2), ct.c_float(eps), ct.c_float(weight_decay), ct.c_int32(step), ct.c_float(lr), ct.c_float(gnorm_scale), ct.c_bool(skip_zeros), ct.c_int32(g.numel()), ) elif g.dtype == torch.float16 and state1.dtype == torch.float32: str2optimizer32bit[optimizer_name][1]( get_ptr(g), get_ptr(p), get_ptr(state1), get_ptr(state2), get_ptr(unorm_vec), ct.c_float(max_unorm), ct.c_float(param_norm), ct.c_float(beta1), ct.c_float(beta2), ct.c_float(eps), ct.c_float(weight_decay), ct.c_int32(step), ct.c_float(lr), ct.c_float(gnorm_scale), ct.c_bool(skip_zeros), ct.c_int32(g.numel()), ) else: raise ValueError( f"Gradient+optimizer bit data type combination not supported: grad {g.dtype}, optimizer {state1.dtype}" ) def optimizer_update_8bit( optimizer_name: str, g: Tensor, p: Tensor, state1: Tensor, state2: Tensor, beta1: float, beta2: float, eps: float, step: int, lr: float, qmap1: Tensor, qmap2: Tensor, max1: Tensor, max2: Tensor, new_max1: Tensor, new_max2: Tensor, weight_decay: float = 0.0, gnorm_scale: float = 1.0, unorm_vec: Tensor = None, max_unorm: float = 0.0, ) -> None: """ Performs an inplace Adam update. Universal Adam update for 32/8-bit state and 32/16-bit gradients/weights. Uses AdamW formulation if weight decay > 0.0. Parameters ---------- optimizer_name : str The name of the optimizer. Choices {adam, momentum} g : torch.Tensor Gradient tensor. p : torch.Tensor Parameter tensor. state1 : torch.Tensor Adam state 1. state2 : torch.Tensor Adam state 2. beta1 : float Adam beta1. beta2 : float Adam beta2. eps : float Adam epsilon. weight_decay : float Weight decay. step : int Current optimizer step. lr : float The learning rate. qmap1 : torch.Tensor Quantization map for first Adam state. qmap2 : torch.Tensor Quantization map for second Adam state. max1 : torch.Tensor Max value for first Adam state update. max2 : torch.Tensor Max value for second Adam state update. new_max1 : torch.Tensor Max value for the next Adam update of the first state. new_max2 : torch.Tensor Max value for the next Adam update of the second state. gnorm_scale : float The factor to rescale the gradient to the max clip value. unorm_vec : torch.Tensor The tensor for the update norm. max_unorm : float The maximum update norm relative to the weight norm. """ param_norm = 0.0 if max_unorm > 0.0: param_norm = torch.norm(p.data.float()) if g.dtype == torch.float32 and state1.dtype == torch.uint8: str2optimizer8bit[optimizer_name][0]( get_ptr(p), get_ptr(g), get_ptr(state1), get_ptr(state2), get_ptr(unorm_vec), ct.c_float(max_unorm), ct.c_float(param_norm), ct.c_float(beta1), ct.c_float(beta2), ct.c_float(eps), ct.c_int32(step), ct.c_float(lr), get_ptr(qmap1), get_ptr(qmap2), get_ptr(max1), get_ptr(max2), get_ptr(new_max1), get_ptr(new_max2), ct.c_float(weight_decay), ct.c_float(gnorm_scale), ct.c_int32(g.numel()), ) elif g.dtype == torch.float16 and state1.dtype == torch.uint8: str2optimizer8bit[optimizer_name][1]( get_ptr(p), get_ptr(g), get_ptr(state1), get_ptr(state2), get_ptr(unorm_vec), ct.c_float(max_unorm), ct.c_float(param_norm), ct.c_float(beta1), ct.c_float(beta2), ct.c_float(eps), ct.c_int32(step), ct.c_float(lr), get_ptr(qmap1), get_ptr(qmap2), get_ptr(max1), get_ptr(max2), get_ptr(new_max1), get_ptr(new_max2), ct.c_float(weight_decay), ct.c_float(gnorm_scale), ct.c_int32(g.numel()), ) else: raise ValueError( f"Gradient+optimizer bit data type combination not supported: grad {g.dtype}, optimizer {state1.dtype}" ) def optimizer_update_8bit_blockwise( optimizer_name: str, g: Tensor, p: Tensor, state1: Tensor, state2: Tensor, beta1: float, beta2: float, eps: float, step: int, lr: float, qmap1: Tensor, qmap2: Tensor, absmax1: Tensor, absmax2: Tensor, weight_decay: float = 0.0, gnorm_scale: float = 1.0, skip_zeros=False, ) -> None: if g.dtype == torch.float32 and state1.dtype == torch.uint8: str2optimizer8bit_blockwise[optimizer_name][0]( get_ptr(p), get_ptr(g), get_ptr(state1), get_ptr(state2), ct.c_float(beta1), ct.c_float(beta2), ct.c_float(eps), ct.c_int32(step), ct.c_float(lr), get_ptr(qmap1), get_ptr(qmap2), get_ptr(absmax1), get_ptr(absmax2), ct.c_float(weight_decay), ct.c_float(gnorm_scale), ct.c_bool(skip_zeros), ct.c_int32(g.numel()), ) elif g.dtype == torch.float16 and state1.dtype == torch.uint8: str2optimizer8bit_blockwise[optimizer_name][1]( get_ptr(p), get_ptr(g), get_ptr(state1), get_ptr(state2), ct.c_float(beta1), ct.c_float(beta2), ct.c_float(eps), ct.c_int32(step), ct.c_float(lr), get_ptr(qmap1), get_ptr(qmap2), get_ptr(absmax1), get_ptr(absmax2), ct.c_float(weight_decay), ct.c_float(gnorm_scale), ct.c_bool(skip_zeros), ct.c_int32(g.numel()), ) else: raise ValueError( f"Gradient+optimizer bit data type combination not supported: grad {g.dtype}, optimizer {state1.dtype}" ) def percentile_clipping( grad: Tensor, gnorm_vec: Tensor, step: int, percentile: int = 5 ): """Applies percentile clipping grad: torch.Tensor The gradient tensor. gnorm_vec: torch.Tensor Vector of gradient norms. 100 elements expected. step: int The current optimiation steps (number of past gradient norms). """ is_on_gpu([grad, gnorm_vec]) if grad.dtype == torch.float32: lib.cpercentile_clipping_g32( get_ptr(grad), get_ptr(gnorm_vec), ct.c_int32(step), ct.c_int32(grad.numel()), ) elif grad.dtype == torch.float16: lib.cpercentile_clipping_g16( get_ptr(grad), get_ptr(gnorm_vec), ct.c_int32(step), ct.c_int32(grad.numel()), ) else: raise ValueError(f"Gradient type {grad.dtype} not supported!") current_gnorm = torch.sqrt(gnorm_vec[step % 100]) vals, idx = torch.sort(gnorm_vec) clip_value = torch.sqrt(vals[percentile]) gnorm_scale = 1.0 if current_gnorm > clip_value: gnorm_scale = clip_value / current_gnorm return current_gnorm, clip_value, gnorm_scale def histogram_scatter_add_2d( histogram: Tensor, index1: Tensor, index2: Tensor, source: Tensor ): assert len(histogram.shape) == 2 assert histogram.dtype == torch.float32 assert source.dtype == torch.float32 assert index1.dtype == torch.int32 assert index2.dtype == torch.int32 assert histogram.device.type == "cuda" assert index1.device.type == "cuda" assert index2.device.type == "cuda" assert source.device.type == "cuda" maxdim1 = ct.c_int32(histogram.shape[0]) n = ct.c_int32(index1.numel()) is_on_gpu([histogram, index1, index2, source]) lib.chistogram_scatter_add_2d(get_ptr(histogram), get_ptr(index1), get_ptr(index2), get_ptr(source), maxdim1, n) def check_matmul(A, B, out, transposed_A, transposed_B, expected_type=torch.int8): if not torch.cuda.is_initialized(): torch.cuda.init() if A.dtype != expected_type or B.dtype != expected_type: raise TypeError( f"Expected torch.int8 input tensors A and B, but got {A.dtype} and {B.dtype}" ) sA = A.shape sB = B.shape tA = transposed_A tB = transposed_B correct = True if len(sA) == 2 and len(sB) == 2: if not tA and not tB and A.shape[1] != B.shape[0]: correct = False elif tA and not tB and A.shape[0] != B.shape[0]: correct = False elif tA and tB and A.shape[0] != B.shape[1]: correct = False elif not tA and tB and A.shape[1] != B.shape[1]: correct = False elif len(sA) == 3 and len(sB) == 2: if not tA and not tB and A.shape[2] != B.shape[0]: correct = False elif tA and not tB and A.shape[1] != B.shape[0]: correct = False elif tA and tB and A.shape[1] != B.shape[1]: correct = False elif not tA and tB and A.shape[2] != B.shape[1]: correct = False elif len(sA) == 3 and len(sB) == 3: if not tA and not tB and A.shape[2] != B.shape[1]: correct = False elif tA and not tB and A.shape[1] != B.shape[1]: correct = False elif tA and tB and A.shape[1] != B.shape[2]: correct = False elif not tA and tB and A.shape[2] != B.shape[2]: correct = False if out is not None: sout = out.shape # special case common in backprop if not correct and len(sA) == 3 and len(sB) == 3: if ( sout[0] == sA[2] and sout[1] == sB[2] and sA[0] == sB[0] and sA[1] == sB[1] ): correct = True else: if len(sA) == 2 and len(sB) == 2: if not tA and not tB: sout = (sA[0], sB[1]) elif tA and tB: sout = (sA[1], sB[0]) elif tA and not tB: sout = (sA[1], sB[1]) elif not tA and tB: sout = (sA[0], sB[0]) elif len(sA) == 3 and len(sB) == 2: if not tA and not tB: sout = (sA[0], sA[1], sB[1]) elif tA and tB: sout = (sA[0], sA[2], sB[0]) elif tA and not tB: sout = (sA[0], sA[2], sB[1]) elif not tA and tB: sout = (sA[0], sA[1], sB[0]) elif len(sA) == 3 and len(sB) == 3: if not tA and not tB: sout = (sA[0], sA[1], sB[2]) elif tA and tB: sout = (sA[0], sA[2], sB[1]) elif tA and not tB: sout = (sA[0], sA[2], sB[2]) elif not tA and tB: sout = (sA[0], sA[1], sB[1]) if not correct: raise ValueError( f"Tensor dimensions incorrect for matrix mulitiplication: A x B: {sA} x {sB} with transpose for A x B: {tA} x {tB}." ) return sout def igemm( A: Tensor, B: Tensor, out: Tensor = None, transposed_A=False, transposed_B=False, ): sout = check_matmul(A, B, out, transposed_A, transposed_B) if out is None: out = torch.zeros(size=sout, dtype=torch.int32, device=A.device) if len(A.shape) == 3 and len(B.shape) == 3: if A.shape[0] == B.shape[0] and A.shape[2] == B.shape[1]: return batched_igemm(A, B, out) sA = A.shape sB = B.shape if transposed_A and len(sA) == 2: sA = (sA[1], sA[0]) elif transposed_A and len(sA) == 3: sA = (sA[0], sA[2], sA[0]) if transposed_B and len(sB) == 2: sB = (sB[1], sB[0]) elif transposed_B and len(sB) == 3: sB = (sB[0], sB[2], sB[0]) # this is a mess: cuBLAS expect column major, but PyTorch is row major. # So to perform the matrix multiplication, we have to treat A, B, and C matrices # (transpose of row major is column major) # This means we compute B^T A^T = C^T and we explicitly switch the dimensions of each of these # matrices in the input arguments for cuBLAS # column major: A @ B = C: [m, k] @ [k, n] = [m, n] # row major: B^T @ A^T = C^T: [m, k] @ [k, n] = [m, n] # column major with row major layout: B^T @ A^T = C^T: [k, m] @ [n, k] = [n, m] if len(sB) == 2: if B.stride()[0] == B.shape[1]: transposed_B = False elif B.stride()[1] == B.shape[0]: transposed_B = True if len(A.shape) == 2: if A.stride()[0] == A.shape[1]: transposed_A = False elif A.stride()[1] == A.shape[0]: transposed_A = True else: if A.stride()[1] == A.shape[2]: transposed_A = False elif A.stride()[2] == A.shape[1]: transposed_A = True if len(sA) == 2: n = sA[0] ldb = A.stride()[1 if transposed_A else 0] elif len(sA) == 3 and len(sB) == 2: n = sA[0] * sA[1] ldb = sA[2] m = sB[1] k = sB[0] lda = B.stride()[(1 if transposed_B else 0)] ldc = sB[1] elif len(sB) == 3: # special case assert len(sA) == 3 if not (sA[0] == sB[0] and sA[1] == sB[1]): raise ValueError( f"Only bsi,bso->io supported for tensor contractions, but dims for A x B were: {sA} x {sB}" ) transposed_A = True transposed_B = False m = sB[2] n = sA[2] k = sB[0] * sB[1] lda = m ldb = sA[2] ldc = m ptr = CUBLAS_Context.get_instance().get_context(A.device) # B^T @ A^T = C^T # [km, nk -> mn] is_on_gpu([B, A, out]) lib.cigemm(ptr, ct.c_bool(transposed_B), ct.c_bool(transposed_A), ct.c_int32(m), ct.c_int32(n), ct.c_int32(k), get_ptr(B), get_ptr(A), get_ptr(out), ct.c_int32(lda), ct.c_int32(ldb), ct.c_int32(ldc)) return out def batched_igemm( A: Tensor, B: Tensor, out: Tensor = None, transposed_A=False, transposed_B=False, ): if not len(A.shape) == 3 or not len(B.shape) == 3: raise ValueError( f"Expected 3-dimensional tensors for bmm, but got shapes A and B: {A.shape} and {B.shape}" ) sout = check_matmul(A, B, out, transposed_A, transposed_B) if out is None: out = torch.zeros(size=sout, dtype=torch.int32, device=A.device) if B.is_contiguous(): lda = B.stride()[1] transposed_A = False else: s = B.stride() if s[0] != B.shape[0]: B = B.contiguous() lda = B.stride()[1] elif s[2] == B.shape[1]: transposed_A = True lda = B.stride()[2] else: if s[2] == 1: B = B.contiguous() lda = B.stride()[1] elif s[1] == 1: B = B.contiguous() lda = B.stride()[1] else: B = B.contiguous() lda = B.stride()[1] if A.is_contiguous(): ldb = A.stride()[1] transposed_B = False else: s = A.stride() if s[0] != A.shape[0]: A = A.contiguous() ldb = A.stride()[1] transposed_B = False elif s[2] == A.shape[1]: ldb = A.stride()[2] transposed_B = True else: A = A.contiguous() ldb = A.stride()[1] transposed_B = False # this is a mess: cuBLAS expect column major, but PyTorch is row major. # So to perform the matrix multiplication, we have to treat A, B, and C matrices # (transpose of row major is column major) # This means we compute B^T A^T = C^T and we explicitly switch the dimensions of each of these # matrices in the input arguments for cuBLAS # column major: A @ B = C: [batch, m, k] @ [batch, k, n] = [batch, m, n] # row major: B^T @ A^T = C^T: [batch, m, k] @ [batch, k, n] = [batch, m, n] # column major with row major layout: B^T @ A^T = C^T: [batch, k, m] @ [batch, n, k] = [batch, n, m] num_batch = A.shape[0] n = A.shape[1] m = B.shape[2] k = B.shape[1] ldc = m strideA = B.shape[1] * B.shape[2] strideB = A.shape[1] * A.shape[2] strideC = A.shape[1] * B.shape[2] ptr = CUBLAS_Context.get_instance().get_context(A.device) is_on_gpu([B, A, out]) lib.cbatched_igemm(ptr, ct.c_bool(transposed_B), ct.c_bool(transposed_A), ct.c_int32(m), ct.c_int32(n), ct.c_int32(k), get_ptr(B), get_ptr(A), get_ptr(out), ct.c_int32(lda), ct.c_int32(ldb), ct.c_int32(ldc), ct.c_long(strideA), ct.c_long(strideB), ct.c_long(strideC), ct.c_uint32(num_batch)) return out def igemmlt(A, B, SA, SB, out=None, Sout=None, dtype=torch.int32): shapeA = SA[0] shapeB = SB[0] dimsA = len(shapeA) dimsB = len(shapeB) assert dimsB == 2, 'Only two dimensional matrices are supported for argument B' if dimsA == 2: m = shapeA[0] elif dimsA == 3: m = shapeA[0] * shapeA[1] rows = n = shapeB[0] assert prod(list(shapeA)) > 0, f'Input tensor dimensions need to be > 0: {shapeA}' # if the tensor is empty, return a transformed empty tensor with the right dimensions if shapeA[0] == 0 and dimsA == 2: return torch.empty((0, shapeB[0]), device=A.device, dtype=torch.float16) elif shapeA[1] == 0 and dimsA == 3: return torch.empty(tuple(shapeA[:2] + [shapeB[0]]), device=A.device, dtype=torch.float16) if dimsA == 2 and out is None: out, Sout = get_transform_buffer( (shapeA[0], shapeB[0]), dtype, A.device, "col32", "row" ) elif dimsA == 3 and out is None: out, Sout = get_transform_buffer( (shapeA[0], shapeA[1], shapeB[0]), dtype, A.device, "col32", "row" ) assert dimsB != 3, "len(B.shape)==3 not supported" assert A.device.type == "cuda" assert B.device.type == "cuda" assert A.dtype == torch.int8 assert B.dtype == torch.int8 assert out.dtype == dtype assert SA[1] == "col32" assert SB[1] in ["col_turing", "col_ampere"] assert Sout[1] == "col32" assert ( shapeA[-1] == shapeB[-1] ), f"Matmullt only supports A @ B^T. Inner matrix dimensions do not match: A @ B = {shapeA} @ {shapeB}" formatB = SB[1] prev_device = A.device torch.cuda.set_device(A.device) ptr = CUBLAS_Context.get_instance().get_context(A.device) ptrA = get_ptr(A) ptrB = get_ptr(B) ptrC = get_ptr(out) k = shapeA[-1] lda = ct.c_int32(m * 32) if formatB == "col_turing": # turing: tiles with rows filled up to multiple of 8 rows by 32 columns # n = rows ldb = ct.c_int32(((rows + 7) // 8) * 8 * 32) else: # ampere: tiles with rows filled up to multiple of 32 rows by 32 columns # n = rows ldb = ct.c_int32(((rows + 31) // 32) * 32 * 32) ldc = ct.c_int32(m * 32) m = ct.c_int32(m) n = ct.c_int32(n) k = ct.c_int32(k) has_error = 0 ptrRowScale = get_ptr(None) is_on_gpu([A, B, out]) if formatB == 'col_turing': if dtype == torch.int32: has_error = lib.cigemmlt_turing_32( ptr, m, n, k, ptrA, ptrB, ptrC, ptrRowScale, lda, ldb, ldc ) else: has_error = lib.cigemmlt_turing_8( ptr, m, n, k, ptrA, ptrB, ptrC, ptrRowScale, lda, ldb, ldc ) elif formatB == "col_ampere": if dtype == torch.int32: has_error = lib.cigemmlt_ampere_32( ptr, m, n, k, ptrA, ptrB, ptrC, ptrRowScale, lda, ldb, ldc ) else: has_error = lib.cigemmlt_ampere_8( ptr, m, n, k, ptrA, ptrB, ptrC, ptrRowScale, lda, ldb, ldc ) if has_error == 1: print(f'A: {shapeA}, B: {shapeB}, C: {Sout[0]}; (lda, ldb, ldc): {(lda, ldb, ldc)}; (m, n, k): {(m, n, k)}') raise Exception('cublasLt ran into an error!') torch.cuda.set_device(prev_device) return out, Sout def mm_dequant( A, quant_state, row_stats, col_stats, out=None, new_row_stats=None, new_col_stats=None, bias=None ): assert A.dtype == torch.int32 if bias is not None: assert bias.dtype == torch.float16 out_shape = quant_state[0] if len(out_shape) == 3: out_shape = (out_shape[0] * out_shape[1], out_shape[2]) if out is None: out = torch.empty(out_shape, dtype=torch.float16, device=A.device) if new_row_stats is None: new_row_stats = torch.empty( out_shape[0], dtype=torch.float32, device=A.device ) if new_col_stats is None: new_col_stats = torch.empty( out_shape[1], dtype=torch.float32, device=A.device ) assert ( new_row_stats.shape[0] == row_stats.shape[0] ), f"{new_row_stats.shape} vs {row_stats.shape}" assert ( new_col_stats.shape[0] == col_stats.shape[0] ), f"{new_col_stats.shape} vs {col_stats.shape}" prev_device = pre_call(A.device) ptrA = get_ptr(A) ptrOut = get_ptr(out) ptrRowStats = get_ptr(row_stats) ptrColStats = get_ptr(col_stats) ptrNewRowStats = get_ptr(new_row_stats) ptrNewColStats = get_ptr(new_col_stats) ptrBias = get_ptr(bias) numRows = ct.c_int32(out_shape[0]) numCols = ct.c_int32(out_shape[1]) is_on_gpu([A, row_stats, col_stats, out, new_row_stats, new_col_stats, bias]) lib.cdequant_mm_int32_fp16(ptrA, ptrRowStats, ptrColStats, ptrOut, ptrNewRowStats, ptrNewColStats, ptrBias, numRows, numCols) post_call(prev_device) return out def get_colrow_absmax( A, row_stats=None, col_stats=None, nnz_block_ptr=None, threshold=0.0 ): assert A.dtype == torch.float16 device = A.device cols = A.shape[-1] if len(A.shape) == 3: rows = A.shape[0] * A.shape[1] else: rows = A.shape[0] col_tiles = (cols + 255) // 256 tiled_rows = ((rows + 15) // 16) * 16 if row_stats is None: row_stats = torch.empty( (rows,), dtype=torch.float32, device=device ).fill_(-50000.0) if col_stats is None: col_stats = torch.empty( (cols,), dtype=torch.float32, device=device ).fill_(-50000.0) if nnz_block_ptr is None and threshold > 0.0: nnz_block_ptr = torch.zeros( ((tiled_rows * col_tiles) + 1,), dtype=torch.int32, device=device ) ptrA = get_ptr(A) ptrRowStats = get_ptr(row_stats) ptrColStats = get_ptr(col_stats) ptrNnzrows = get_ptr(nnz_block_ptr) rows = ct.c_int32(rows) cols = ct.c_int32(cols) prev_device = pre_call(A.device) is_on_gpu([A, row_stats, col_stats, nnz_block_ptr]) lib.cget_col_row_stats(ptrA, ptrRowStats, ptrColStats, ptrNnzrows, ct.c_float(threshold), rows, cols) post_call(prev_device) if threshold > 0.0: nnz_block_ptr.cumsum_(0) return row_stats, col_stats, nnz_block_ptr class COOSparseTensor: def __init__(self, rows, cols, nnz, rowidx, colidx, values): assert rowidx.dtype == torch.int32 assert colidx.dtype == torch.int32 assert values.dtype == torch.float16 assert values.numel() == nnz assert rowidx.numel() == nnz assert colidx.numel() == nnz self.rows = rows self.cols = cols self.nnz = nnz self.rowidx = rowidx self.colidx = colidx self.values = values class CSRSparseTensor: def __init__(self, rows, cols, nnz, rowptr, colidx, values): assert rowptr.dtype == torch.int32 assert colidx.dtype == torch.int32 assert values.dtype == torch.float16 assert values.numel() == nnz assert colidx.numel() == nnz assert rowptr.numel() == rows + 1 self.rows = rows self.cols = cols self.nnz = nnz self.rowptr = rowptr self.colidx = colidx self.values = values class CSCSparseTensor: def __init__(self, rows, cols, nnz, colptr, rowidx, values): assert colptr.dtype == torch.int32 assert rowidx.dtype == torch.int32 assert values.dtype == torch.float16 assert values.numel() == nnz assert rowidx.numel() == nnz assert colptr.numel() == cols + 1 self.rows = rows self.cols = cols self.nnz = nnz self.colptr = colptr self.rowidx = rowidx self.values = values def coo2csr(cooA): values, counts = torch.unique(cooA.rowidx, return_counts=True) values.add_(1) rowptr = torch.zeros( (cooA.rows + 1,), dtype=torch.int32, device=cooA.rowidx.device ) rowptr.scatter_(index=values.long(), src=counts.int(), dim=0) rowptr.cumsum_(0) return CSRSparseTensor( cooA.rows, cooA.cols, cooA.nnz, rowptr, cooA.colidx, cooA.values ) def coo2csc(cooA): val, col2rowidx = torch.sort(cooA.colidx) rowidx = cooA.rowidx[col2rowidx] values = cooA.values[col2rowidx] colvalues, counts = torch.unique(val, return_counts=True) colvalues.add_(1) colptr = torch.zeros( (cooA.cols + 1,), dtype=torch.int32, device=cooA.colidx.device ) colptr.scatter_(index=colvalues.long(), src=counts.int(), dim=0) colptr.cumsum_(0) return CSCSparseTensor( cooA.rows, cooA.cols, cooA.nnz, colptr, rowidx, values ) def coo_zeros(rows, cols, nnz, device, dtype=torch.half): rowidx = torch.zeros((nnz,), dtype=torch.int32, device=device) colidx = torch.zeros((nnz,), dtype=torch.int32, device=device) values = torch.zeros((nnz,), dtype=dtype, device=device) return COOSparseTensor(rows, cols, nnz, rowidx, colidx, values) def double_quant( A, col_stats=None, row_stats=None, out_col=None, out_row=None, threshold=0.0 ): device = A.device assert A.dtype == torch.half assert device.type == "cuda" prev_device = pre_call(A.device) cols = A.shape[-1] if len(A.shape) == 3: rows = A.shape[0] * A.shape[1] else: rows = A.shape[0] if row_stats is None or col_stats is None: row_stats, col_stats, nnz_row_ptr = get_colrow_absmax( A, threshold=threshold ) if out_col is None: out_col = torch.zeros(A.shape, device=device, dtype=torch.int8) if out_row is None: out_row = torch.zeros(A.shape, device=device, dtype=torch.int8) coo_tensor = None ptrA = get_ptr(A) ptrColStats = get_ptr(col_stats) ptrRowStats = get_ptr(row_stats) ptrOutCol = get_ptr(out_col) ptrOutRow = get_ptr(out_row) is_on_gpu([A, col_stats, row_stats, out_col, out_row]) if threshold > 0.0: nnz = nnz_row_ptr[-1].item() if nnz > 0: coo_tensor = coo_zeros( A.shape[0], A.shape[1], nnz_row_ptr[-1].item(), device ) ptrRowIdx = get_ptr(coo_tensor.rowidx) ptrColIdx = get_ptr(coo_tensor.colidx) ptrVal = get_ptr(coo_tensor.values) ptrRowPtr = get_ptr(nnz_row_ptr) lib.cdouble_rowcol_quant( ptrA, ptrRowStats, ptrColStats, ptrOutCol, ptrOutRow, ptrRowIdx, ptrColIdx, ptrVal, ptrRowPtr, ct.c_float(threshold), ct.c_int32(rows), ct.c_int32(cols), ) val, idx = torch.sort(coo_tensor.rowidx) coo_tensor.rowidx = val coo_tensor.colidx = coo_tensor.colidx[idx] coo_tensor.values = coo_tensor.values[idx] else: lib.cdouble_rowcol_quant( ptrA, ptrRowStats, ptrColStats, ptrOutCol, ptrOutRow, None, None, None, None, ct.c_float(0.0), ct.c_int32(rows), ct.c_int32(cols), ) else: lib.cdouble_rowcol_quant( ptrA, ptrRowStats, ptrColStats, ptrOutCol, ptrOutRow, None, None, None, None, ct.c_float(threshold), ct.c_int32(rows), ct.c_int32(cols), ) post_call(prev_device) return out_row, out_col, row_stats, col_stats, coo_tensor def transform(A, to_order, from_order='row', out=None, transpose=False, state=None, ld=None): prev_device = pre_call(A.device) if state is None: state = (A.shape, from_order) else: from_order = state[1] if out is None: out, new_state = get_transform_buffer(state[0], A.dtype, A.device, to_order, state[1], transpose) else: new_state = (state[0], to_order) # (shape, order) shape = state[0] if len(shape) == 2: dim1 = ct.c_int32(shape[0]) dim2 = ct.c_int32(shape[1]) else: dim1 = ct.c_int32(shape[0] * shape[1]) dim2 = ct.c_int32(shape[2]) is_on_gpu([A, out]) if to_order == 'col32': if transpose: lib.ctransform_row2col32T(get_ptr(A), get_ptr(out), dim1, dim2) else: lib.ctransform_row2col32(get_ptr(A), get_ptr(out), dim1, dim2) elif to_order == "col_turing": if transpose: lib.ctransform_row2turingT(get_ptr(A), get_ptr(out), dim1, dim2) else: lib.ctransform_row2turing(get_ptr(A), get_ptr(out), dim1, dim2) elif to_order == "col_ampere": if transpose: lib.ctransform_row2ampereT(get_ptr(A), get_ptr(out), dim1, dim2) else: lib.ctransform_row2ampere(get_ptr(A), get_ptr(out), dim1, dim2) elif to_order == "row": if from_order == "col_turing": lib.ctransform_turing2row(get_ptr(A), get_ptr(out), dim1, dim2) elif from_order == "col_ampere": lib.ctransform_ampere2row(get_ptr(A), get_ptr(out), dim1, dim2) else: raise NotImplementedError(f'Transform function not implemented: From {from_order} to {to_order}') post_call(prev_device) return out, new_state def spmm_coo(cooA, B, out=None): if out is None: out = torch.empty( (cooA.rows, B.shape[1]), device=B.device, dtype=B.dtype ) nnz = cooA.nnz assert cooA.rowidx.numel() == nnz assert cooA.colidx.numel() == nnz assert cooA.values.numel() == nnz assert cooA.cols == B.shape[0] transposed_B = False if B.is_contiguous() else True ldb = B.stride()[(1 if transposed_B else 0)] ldc = B.shape[1] ptr = Cusparse_Context.get_instance().context ptrRowidx = get_ptr(cooA.rowidx) ptrColidx = get_ptr(cooA.colidx) ptrValues = get_ptr(cooA.values) ptrB = get_ptr(B) ptrC = get_ptr(out) cnnz = ct.c_int32(cooA.nnz) crowsA = ct.c_int32(cooA.rows) ccolsA = ct.c_int32(cooA.cols) ccolsB = ct.c_int32(B.shape[1]) cldb = ct.c_int32(ldb) cldc = ct.c_int32(ldc) is_on_gpu([cooA.rowidx, cooA.colidx, cooA.values, B, out]) lib.cspmm_coo(ptr, ptrRowidx, ptrColidx, ptrValues, cnnz, crowsA, ccolsA, ccolsB, cldb, ptrB, cldc, ptrC, ct.c_bool(transposed_B)) return out def spmm_coo_very_sparse(cooA, B, dequant_stats=None, out=None): if out is None: out = torch.zeros( (cooA.rows, B.shape[1]), device=B.device, dtype=cooA.values.dtype ) nnz = cooA.nnz assert cooA.rowidx.numel() == nnz assert cooA.colidx.numel() == nnz assert cooA.values.numel() == nnz assert cooA.cols == B.shape[0], f"{cooA.cols} vs {B.shape}" transposed_B = False if B.is_contiguous() else True ldb = B.stride()[(1 if transposed_B else 0)] ldc = B.shape[1] values, counts = torch.unique(cooA.rowidx, return_counts=True) offset = counts.cumsum(0).int() max_count, max_idx = torch.sort(counts, descending=True) max_idx = max_idx.int() max_count = max_count.int() assert ( max_count[0] <= 32 ), f"Current max count per row is 8 but found {max_count[0]}." assert B.dtype in [torch.float16, torch.int8] ptrOffset = get_ptr(offset) ptrMaxCount = get_ptr(max_count) ptrMaxIdx = get_ptr(max_idx) ptrRowidx = get_ptr(cooA.rowidx) ptrColidx = get_ptr(cooA.colidx) ptrValues = get_ptr(cooA.values) ptrB = get_ptr(B) ptrC = get_ptr(out) ptrDequantStats = get_ptr(dequant_stats) cnnz_rows = ct.c_int32(counts.numel()) cnnz = ct.c_int32(cooA.nnz) crowsA = ct.c_int32(cooA.rows) ccolsA = ct.c_int32(cooA.cols) crowsB = ct.c_int32(B.shape[1]) ccolsB = ct.c_int32(B.shape[1]) cldb = ct.c_int32(ldb) cldc = ct.c_int32(ldc) # print(cooA.rowidx[:64]) # print(cooA.colidx[:64].sort()[0]) is_on_gpu([cooA.rowidx, cooA.colidx, cooA.values, B, out, dequant_stats]) if B.dtype == torch.float16: lib.cspmm_coo_very_sparse_naive_fp16( ptrMaxCount, ptrMaxIdx, ptrOffset, ptrRowidx, ptrColidx, ptrValues, ptrB, ptrC, ptrDequantStats, cnnz_rows, cnnz, crowsA, crowsB, ccolsB, ) elif B.dtype == torch.int8: lib.cspmm_coo_very_sparse_naive_int8( ptrMaxCount, ptrMaxIdx, ptrOffset, ptrRowidx, ptrColidx, ptrValues, ptrB, ptrC, ptrDequantStats, cnnz_rows, cnnz, crowsA, crowsB, ccolsB, ) # else: assertion error return out C = 127.0 def vectorwise_quant(x, dim=1, quant_type="vector"): if quant_type == "linear": max1 = torch.abs(x).max().float() xq = torch.round(x / max1 * 127).to(torch.int8) return xq, max1 elif quant_type in ["vector", "row"]: max1 = torch.amax(torch.abs(x), dim=dim, keepdim=True) xq = torch.round(x * (C / max1)).to(torch.int8) return xq, max1 elif quant_type == "zeropoint": dtype = x.dtype x = x.float() dyna = x.max() - x.min() if dyna == 0: dyna = 1 qx = 255.0 / dyna minx = x.min() zpx = torch.round(minx * qx) x = torch.round(qx * x - zpx) + zpx return x, qx elif quant_type in ["vector-zeropoint", "row-zeropoint"]: dtype = x.dtype x = x.float() dyna = torch.amax(x, dim=dim, keepdim=True) - torch.amin( x, dim=dim, keepdim=True ) dyna[dyna == 0] = 1 qx = 255.0 / dyna minx = torch.amin(x, dim=dim, keepdim=True) zpx = torch.round(minx * qx) x = torch.round(qx * x - zpx) + zpx return x, qx elif quant_type == "truncated-vector": with torch.no_grad(): absx = torch.abs(x) max1 = torch.amax(absx, dim=dim, keepdim=True) max1 = max1 * 0.7 idx = absx > max1.expand_as(absx) sign = torch.sign(x[idx]) x[idx] = max1.expand_as(absx)[idx] * sign xq = torch.round(x / max1 * C).to(torch.int8) return xq, max1 else: return None def vectorwise_dequant(xq, max1, quant_type="vector"): if quant_type == "vector": x = (xq / C * max1).to(torch.float32) return x else: return None def vectorwise_mm_dequant(xq, S1, S2, dtype=torch.half, quant_type="vector"): if quant_type == "linear": norm = S1 * S2 / (C * C) # double cast needed to prevent overflows return (xq.float() * norm).to(dtype) elif quant_type == "zeropoint": norm = 1.0 / (S1 * S2) return (xq.float() * norm).to(dtype) elif quant_type == "row-zeropoint": norm = 1.0 / (S1 * S2) x = xq.float() if len(S1.shape) == 3 and len(x.shape) == 2: S1 = S1.squeeze(0) if len(S2.shape) == 3 and len(x.shape) == 2: S2 = S2.squeeze(0) if len(S1.shape) == 2: x *= norm else: x *= norm return x.to(dtype) elif quant_type == "vector-zeropoint": x = xq.float() if len(S1.shape) == 3 and len(x.shape) == 2: S1 = S1.squeeze(0) if len(S2.shape) == 3 and len(x.shape) == 2: S2 = S2.squeeze(0) if len(S1.shape) == 2: x *= 1.0 / S1 else: x *= 1.0 / S1 x *= 1.0 / S2.t() return x.to(dtype) elif quant_type == "row": x = xq.float() if len(S1.shape) == 3 and len(x.shape) == 2: S1 = S1.squeeze(0) if len(S2.shape) == 3 and len(x.shape) == 2: S2 = S2.squeeze(0) if len(S1.shape) == 2: x *= S1 * S2 / (C * C) else: x *= S1 * S2 / (C * C) return x.to(dtype) elif quant_type in ["truncated-vector", "vector"]: x = xq.float() if len(S1.shape) == 3 and len(x.shape) == 2: S1 = S1.squeeze(0) if len(S2.shape) == 3 and len(x.shape) == 2: S2 = S2.squeeze(0) if len(S1.shape) == 2: x *= S1 / C else: x *= S1 / C x *= S2 / C return x.to(dtype) else: return None def dequant_min_max(xq, A, B, SA, SB, dtype=torch.half): offset = B.float().t().sum(0) * (SA[0] + SA[1]) x = xq.float() if len(xq.shape) == 2 and len(SB.shape) == 3: SB = SB.squeeze(0) if len(SB.shape) == 2: x *= SB.t() / 127 else: x *= SB / 127 x *= SA[1] / 127 x += offset return x.to(dtype) def extract_outliers(A, SA, idx): shapeA = SA[0] formatA = SA[1] assert formatA in ["col_turing", "col_ampere"] assert A.device.type == "cuda" out = torch.zeros( (shapeA[0], idx.numel()), dtype=torch.int8, device=A.device ) idx_size = ct.c_int32(idx.numel()) rows = ct.c_int32(shapeA[0]) cols = ct.c_int32(shapeA[1]) ptrA = get_ptr(A) ptrIdx = get_ptr(idx) ptrOut = get_ptr(out) prev_device = pre_call(A.device) if formatA == 'col_turing': lib.cextractOutliers_turing(ptrA, ptrIdx, ptrOut, idx_size, rows, cols) elif formatA == "col_ampere": lib.cextractOutliers_ampere(ptrA, ptrIdx, ptrOut, idx_size, rows, cols) post_call(prev_device) return out
def execute_and_return(command_string: str) -> Tuple[str, str]: def _decode(subprocess_err_out_tuple): return tuple( to_decode.decode("UTF-8").strip() for to_decode in subprocess_err_out_tuple ) def execute_and_return_decoded_std_streams(command_string): return _decode( subprocess.Popen( shlex.split(command_string), stdout=subprocess.PIPE, stderr=subprocess.PIPE, ).communicate() ) std_out, std_err = execute_and_return_decoded_std_streams(command_string) return std_out, std_err
HEADER_WIDTH = 60 def print_header( txt: str, width: int = HEADER_WIDTH, filler: str = "+" ) -> None: txt = f" {txt} " if txt else "" print(txt.center(width, filler)) def print_debug_info() -> None: print( "\nAbove we output some debug information. Please provide this info when " f"creating an issue via {PACKAGE_GITHUB_URL}/issues/new/choose ...\n" ) print_header("") print_header("DEBUG INFORMATION") print_header("") print() print_header("POTENTIALLY LIBRARY-PATH-LIKE ENV VARS") for k, v in os.environ.items(): if "/" in v and not to_be_ignored(k, v): print(f"'{k}': '{v}'") print_header("") print( "\nWARNING: Please be sure to sanitize sensible info from any such env vars!\n" ) print_header("OTHER") print(f"{COMPILED_WITH_CUDA = }") cuda = get_cuda_lib_handle() print(f"COMPUTE_CAPABILITIES_PER_GPU = {get_compute_capabilities(cuda)}") print_header("") print_header("DEBUG INFO END") print_header("") print( """ Running a quick check that: + library is importable + CUDA function is callable """ ) try: from bitsandbytes.optim import Adam p = torch.nn.Parameter(torch.rand(10, 10).cuda()) a = torch.rand(10, 10).cuda() p1 = p.data.sum().item() adam = Adam([p]) out = a * p loss = out.sum() loss.backward() adam.step() p2 = p.data.sum().item() assert p1 != p2 print("SUCCESS!") print("Installation was successful!") sys.exit(0) except ImportError: print() warn( f"WARNING: {__package__} is currently running as CPU-only!\n" "Therefore, 8-bit optimizers and GPU quantization are unavailable.\n\n" f"If you think that this is so erroneously,\nplease report an issue!" ) print_debug_info() sys.exit(0) except Exception as e: print(e) print_debug_info() sys.exit(1)
# 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.
# 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. T = TypeVar("T", bound="torch.nn.Module") class StableEmbedding(torch.nn.Embedding): def __init__( self, num_embeddings: int, embedding_dim: int, padding_idx: Optional[int] = None, max_norm: Optional[float] = None, norm_type: float = 2.0, scale_grad_by_freq: bool = False, sparse: bool = False, _weight: Optional[Tensor] = None, device=None, dtype=None, ) -> None: super().__init__( num_embeddings, embedding_dim, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse, _weight, device, dtype, ) self.norm = torch.nn.LayerNorm(embedding_dim, device=device) GlobalOptimManager.get_instance().register_module_override( self, "weight", {"optim_bits": 32} ) def reset_parameters(self) -> None: torch.nn.init.xavier_uniform_(self.weight) self._fill_padding_idx_with_zero() """ !!! This is a redefinition of _fill_padding_idx_with_zero in torch.nn.Embedding to make the Layer compatible with Pytorch < 1.9. This means that if this changes in future PyTorch releases this need to change too which is cumbersome. However, with this we can ensure compatibility with previous PyTorch releases. """ def _fill_padding_idx_with_zero(self) -> None: if self.padding_idx is not None: with torch.no_grad(): self.weight[self.padding_idx].fill_(0) def forward(self, input: Tensor) -> Tensor: emb = F.embedding( input, self.weight, self.padding_idx, self.max_norm, self.norm_type, self.scale_grad_by_freq, self.sparse, ) # always apply layer norm in full precision emb = emb.to(torch.get_default_dtype()) return self.norm(emb).to(self.weight.dtype) class Embedding(torch.nn.Embedding): def __init__( self, num_embeddings: int, embedding_dim: int, padding_idx: Optional[int] = None, max_norm: Optional[float] = None, norm_type: float = 2.0, scale_grad_by_freq: bool = False, sparse: bool = False, _weight: Optional[Tensor] = None, ) -> None: super().__init__( num_embeddings, embedding_dim, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse, _weight, ) GlobalOptimManager.get_instance().register_module_override( self, "weight", {"optim_bits": 32} ) def reset_parameters(self) -> None: torch.nn.init.xavier_uniform_(self.weight) self._fill_padding_idx_with_zero() """ !!! This is a redefinition of _fill_padding_idx_with_zero in torch.nn.Embedding to make the Layer compatible with Pytorch < 1.9. This means that if this changes in future PyTorch releases this need to change too which is cumbersome. However, with this we can ensure compatibility with previous PyTorch releases. """ def _fill_padding_idx_with_zero(self) -> None: if self.padding_idx is not None: with torch.no_grad(): self.weight[self.padding_idx].fill_(0) def forward(self, input: Tensor) -> Tensor: emb = F.embedding( input, self.weight, self.padding_idx, self.max_norm, self.norm_type, self.scale_grad_by_freq, self.sparse, ) return emb class Int8Params(torch.nn.Parameter): def __new__( cls, data=None, requires_grad=True, has_fp16_weights=False, CB=None, SCB=None, ): cls.has_fp16_weights = has_fp16_weights cls.CB = None cls.SCB = None if data is None: data = torch.empty(0) return torch.Tensor._make_subclass(cls, data, requires_grad) def cuda(self, device): if self.has_fp16_weights: return super().cuda(device) else: # we store the 8-bit rows-major weight # we convert this weight to the turning/ampere weight during the first inference pass B = self.data.contiguous().half().cuda(device) CB, CBt, SCB, SCBt, coo_tensorB = bnb.functional.double_quant(B) del CBt del SCBt self.data = CB setattr(self, "CB", CB) setattr(self, "SCB", SCB) return self @overload def to( self: T, device: Optional[Union[int, device]] = ..., dtype: Optional[Union[dtype, str]] = ..., non_blocking: bool = ..., ) -> T: ... @overload def to(self: T, dtype: Union[dtype, str], non_blocking: bool = ...) -> T: ... @overload def to(self: T, tensor: Tensor, non_blocking: bool = ...) -> T: ... def to(self, *args, **kwargs): device, dtype, non_blocking, convert_to_format = torch._C._nn._parse_to( *args, **kwargs ) if ( device is not None and device.type == "cuda" and self.data.device.type == "cpu" ): return self.cuda(device) else: new_param = Int8Params( super().to( device=device, dtype=dtype, non_blocking=non_blocking ), requires_grad=self.requires_grad, has_fp16_weights=self.has_fp16_weights, ) new_param.CB = self.CB new_param.SCB = self.SCB return new_param class Linear8bitLt(nn.Linear): def __init__(self, input_features, output_features, bias=True, has_fp16_weights=True, memory_efficient_backward=False, threshold=0.0, index=None): super().__init__(input_features, output_features, bias) assert not memory_efficient_backward, "memory_efficient_backward is no longer required and the argument is deprecated in 0.37.0 and will be removed in 0.39.0" self.state = bnb.MatmulLtState() self.index = index self.state.threshold = threshold self.state.has_fp16_weights = has_fp16_weights self.state.memory_efficient_backward = memory_efficient_backward if threshold > 0.0 and not has_fp16_weights: self.state.use_pool = True self.weight = Int8Params(self.weight.data, has_fp16_weights=has_fp16_weights, requires_grad=has_fp16_weights) def init_8bit_state(self): self.state.CB = self.weight.CB self.state.SCB = self.weight.SCB self.weight.CB = None self.weight.SCB = None def forward(self, x: torch.Tensor): self.state.is_training = self.training if self.weight.CB is not None: self.init_8bit_state() # weights are cast automatically as Int8Params, but the bias has to be cast manually if self.bias is not None and self.bias.dtype != x.dtype: self.bias.data = self.bias.data.to(x.dtype) out = bnb.matmul(x, self.weight, bias=self.bias, state=self.state) if not self.state.has_fp16_weights: if self.state.CB is not None and self.state.CxB is not None: # we converted 8-bit row major to turing/ampere format in the first inference pass # we no longer need the row-major weight del self.state.CB self.weight.data = self.state.CxB return out
# math.prod not compatible with python < 3.8 def prod(iterable): return reduce(operator.mul, iterable, 1) tensor = torch.Tensor # The inverse transformation for the colTuring and colAmpere format were contributed by Alex Borzunov: # https://github.com/bigscience-workshop/petals/blob/main/src/petals/utils/linear8bitlt_patch.py """ This class pools outlier dimensions across layers. This is particularly important for small models where outlier features are less systematic and occur with low frequency. """ class GlobalOutlierPooler: _instance = None def __init__(self): raise RuntimeError("Call get_instance() instead") def initialize(self): self.outliers = set() self.model_dim = None @classmethod def get_instance(cls): if cls._instance is None: cls._instance = cls.__new__(cls) cls._instance.initialize() return cls._instance def add_outliers(self, outlier_idx, feature_dim): if self.model_dim is None: self.model_dim = feature_dim if feature_dim != self.model_dim: return # we do not encode outliers for the 2nd FFN layer self.outliers.update(outlier_idx.tolist()) def get_current_outlier_idx(self): return torch.Tensor(list(self.outliers)).to(torch.int64) def get_inverse_transform_indices(transform_tile: callable, tile_size: Tuple[int, int]): """ Compute a permutation of indices that invert the specified (tiled) matrix transformation :param transform_tile: a function that applies forward transform to a tensor of shape [dim1, dim2] :param tile_size: higher-level tile dimensions, i.e. (8, 32) for Turing and (32, 32) for Ampere :note: we assume that tile_transform applies to a cpu-based int8 tensor of shape tile_size :example: transform_tile function for the turing layout (bitsandbytes.functional as F) :returns: indices """ d1, d2 = tile_size assert 0 < d1 * d2 < 2**64 tile_indices = torch.arange(d1 * d2, dtype=torch.int64).view(d1, d2) # encode each position in tile as a tuple of <= 8 unique bytes permuted_tile_indices = torch.zeros_like(tile_indices) for i in range(8): # select i-th byte, apply transformation and trace where each index ended up ith_dim_indices = torch.div(tile_indices, 256**i, rounding_mode="trunc") % 256 sample_tile_i = (ith_dim_indices - 128).to(torch.int8).contiguous() assert torch.all(sample_tile_i.int() + 128 == ith_dim_indices), "int overflow" permuted_tile_i = transform_tile(sample_tile_i) ith_permuted_indices = permuted_tile_i.to(tile_indices.dtype) + 128 permuted_tile_indices += ith_permuted_indices * (256**i) if d1 * d2 < 256**i: break # if all indices fit in i bytes, stop early return permuted_tile_indices def undo_layout(permuted_tensor: torch.Tensor, tile_indices: torch.LongTensor) -> torch.Tensor: """ Undo a tiled permutation such as turing or ampere layout :param permuted_tensor: torch tensor in a permuted layout :param tile_indices: reverse transformation indices, from get_inverse_transform_indices :return: contiguous row-major tensor """ (rows, cols), (tile_rows, tile_cols) = permuted_tensor.shape, tile_indices.shape assert rows % tile_rows == cols % tile_cols == 0, "tensor must contain a whole number of tiles" tensor = permuted_tensor.reshape(-1, tile_indices.numel()).t() outputs = torch.empty_like(tensor) # note: not using .index_copy because it was slower on cuda outputs[tile_indices.flatten()] = tensor outputs = outputs.reshape(tile_rows, tile_cols, cols // tile_cols, rows // tile_rows) outputs = outputs.permute(3, 0, 2, 1) # (rows // tile_rows, tile_rows), (cols // tile_cols, tile_cols) return outputs.reshape(rows, cols).contiguous() class MatMul8bit(torch.autograd.Function): @staticmethod def forward(ctx, A, B, out=None, quant_type="vector", precision=None): if precision is None: precision = [8, 8, 8] if precision[0] != 8: with torch.no_grad(): output = torch.matmul(A, B) else: if len(B.shape) == 2: dim = 0 else: dim = 1 qA, SA = F.vectorwise_quant(A, dim=-1, quant_type=quant_type) qB, SB = F.vectorwise_quant(B, dim=dim, quant_type=quant_type) iout = F.igemm(qA, qB) output = F.vectorwise_mm_dequant(iout, SA, SB, A.dtype, quant_type) if A.requires_grad or B.requires_grad: ctx.save_for_backward(A, B) ctx.quant_type = quant_type ctx.precision = precision return output @staticmethod def backward(ctx, grad_output): A, B = ctx.saved_tensors quant_type = ctx.quant_type precision = ctx.precision grad_A = grad_B = None if B.requires_grad: if len(A.shape) == 3: dims = [0, 1] # bsi -> ibs permute_dim = [0, 2, 1] else: dims = [0] # bs -> sb permute_dim = [1, 0] if precision[1] != 8: with torch.no_grad(): grad_B = torch.matmul(A.permute(permute_dim), grad_output) else: if len(B.shape) == 2 and len(A.shape) == 3: grad_output = grad_output.contiguous() if not grad_output.is_contiguous(): grad_output.contiguous() qgrad_output, S1 = F.vectorwise_quant( grad_output.view(-1, grad_output.shape[2]), dim=0, quant_type=quant_type, ) if not A.is_contiguous(): A = A.contiguous() qA, S2 = F.vectorwise_quant( A.view(-1, A.shape[2]), dim=0, quant_type=quant_type ) igrad_B = F.igemm(qA.t(), qgrad_output) grad_B = F.vectorwise_mm_dequant( igrad_B, S2.t(), S1, grad_output.dtype, quant_type ) else: qgrad_output, S1 = F.vectorwise_quant( grad_output, dim=dims, quant_type=quant_type ) qA, S2 = F.vectorwise_quant( A, dim=dims, quant_type=quant_type ) igrad_B = F.igemm(qA.permute(permute_dim), qgrad_output) grad_B = F.vectorwise_mm_dequant( igrad_B, S2.permute(permute_dim), S1, grad_output.dtype, quant_type, ) if A.requires_grad: if len(grad_output.shape) == 3: dims = [2] else: dims = [1] if len(B.shape) == 3: # bio -> boi permute_dim = [0, 2, 1] dim_B = dims else: # io -> oi permute_dim = [1, 0] dim_B = [1] if precision[2] != 8: with torch.no_grad(): grad_A = torch.matmul(grad_output, B.permute(permute_dim)) else: qgrad_output, S1 = F.vectorwise_quant( grad_output, dim=dims, quant_type=quant_type ) qB, S3 = F.vectorwise_quant(B, dim=dim_B, quant_type=quant_type) igrad_A = F.igemm(qgrad_output, qB.permute(permute_dim)) grad_A = F.vectorwise_mm_dequant( igrad_A, S1, S3.permute(permute_dim), grad_output.dtype, quant_type, ) return grad_A, grad_B, None, None, None mm_cublas = MatMul8bit.apply bmm_cublas = MatMul8bit.apply matmul_cublas = MatMul8bit.apply @dataclass class MatmulLtState: tile_indices: Optional[torch.Tensor] = None force_no_igemmlt: bool = False CB = None CxB = None SB = None SCB = None CxBt = None SBt = None CBt = None subB = None outlier_pool = None has_accumulated_gradients = False threshold = 0.0 idx = None is_training = True has_fp16_weights = True memory_efficient_backward = False use_pool = False formatB = F.get_special_format_str() def reset_grads(self): self.CB = None self.CxB = None self.SB = None self.SCB = None self.CxBt = None self.SBt = None self.CBt = None def get_tile_size(self): assert self.formatB in ( "col_turing", "col_ampere", ), f"please find this assert and manually enter tile size for {self.formatB}" return (8, 32) if self.formatB == "col_turing" else (32, 32) class MatMul8bitLt(torch.autograd.Function): # forward is the same, but we added the fallback for pre-turing GPUs # backward is mostly the same, but adds one extra clause (see "elif state.CxB is not None") @staticmethod def forward(ctx, A, B, out=None, bias=None, state=MatmulLtState): using_igemmlt = torch.cuda.get_device_capability(device=A.device) >= (7, 5) and not state.force_no_igemmlt # default of pytorch behavior if inputs are empty ctx.is_empty = False if prod(A.shape) == 0: ctx.is_empty = True ctx.A = A ctx.B = B ctx.bias = bias if A.shape[-1] == B.shape[0]: return torch.empty(A.shape[:-1] + B.shape[1:], dtype=A.dtype, device=A.device) else: return torch.empty(A.shape[:-1] + B.shape[:1], dtype=A.dtype, device=A.device) # 1. Quantize A # 2. Quantize B # 3. Matmul # 4. Mixed-precision decomposition matmul # 5. Save state formatB = state.formatB input_shape = A.shape if state.outlier_pool is None: state.outlier_pool = GlobalOutlierPooler.get_instance() # Cast A to fp16 if A.dtype != torch.float16: warnings.warn(f"MatMul8bitLt: inputs will be cast from {A.dtype} to float16 during quantization") # 1. Quantize A if len(A.shape) == 3: A = A.view(-1, A.shape[-1]).contiguous() CA, CAt, SCA, SCAt, coo_tensorA = F.double_quant(A.to(torch.float16), threshold=state.threshold) if state.threshold > 0.0 and coo_tensorA is not None: if state.has_fp16_weights: idx = torch.unique(coo_tensorA.colidx).long() CA[:, idx] = 0 CAt[:, idx] = 0 subA = A[:, idx] state.subB = B[:, idx].t().contiguous() state.idx = idx else: if state.CxB is None and using_igemmlt: # B in in 8-bit row-major, we can transform it back to 16-bit to extract outlier dimensions # we also need to convert it to the turing/ampere format state.CxB, state.SB = F.transform(state.CB, to_order=formatB) else: if not state.has_fp16_weights and state.CxB is None and using_igemmlt: state.CxB, state.SB = F.transform(state.CB, to_order=formatB) subA = None # 2. Quantize B if state.has_fp16_weights: has_grad = True if (getattr(B, "grad", None) is not None) else False is_transposed = not B.is_contiguous() and B.shape[0] == B.stride(1) if is_transposed: B = B.contiguous() if (state.is_training and not has_grad) or state.CxB is None: state.reset_grads() ( CB, state.CBt, state.SCB, state.SCBt, coo_tensorB, ) = F.double_quant(B.to(torch.float16)) if using_igemmlt: state.CxB, state.SB = F.transform(CB, to_order=formatB) else: state.CB = CB else: has_grad = False if coo_tensorA is not None and not state.has_fp16_weights: # extract outliers outlier_idx = torch.unique(coo_tensorA.colidx) state.idx = outlier_idx # state.outlier_pool.add_outliers(outlier_idx, A.shape[-1]) # if state.use_pool and state.outlier_pool.model_dim == A.shape[-1]: # # do not use pool for 2nd FFN layer # state.idx = state.outlier_pool.get_current_outlier_idx().to(A.device) # else: # state.idx = outlier_idx if state.CxB is not None: outliers = F.extract_outliers(state.CxB, state.SB, state.idx.int()) else: outliers = state.CB[:, state.idx.long()].clone() state.subB = (outliers * state.SCB.view(-1, 1) / 127.0).t().contiguous().to(A.dtype) CA[:, state.idx.long()] = 0 CAt[:, state.idx.long()] = 0 subA = A[:, state.idx.long()] shapeB = state.SB[0] if state.SB else B.shape if len(input_shape) == 3: output_shape = (input_shape[0], input_shape[1], shapeB[0]) else: output_shape = (input_shape[0], shapeB[0]) # 3. Matmul if using_igemmlt: C32A, SA = F.transform(CA, "col32") out32, Sout32 = F.igemmlt(C32A, state.CxB, SA, state.SB) if bias is None or bias.dtype == torch.float16: # we apply the fused bias here output = F.mm_dequant(out32, Sout32, SCA, state.SCB, bias=bias) output = output.to(A.dtype) else: # apply bias separately output = F.mm_dequant(out32, Sout32, SCA, state.SCB, bias=None) output = output.to(A.dtype).add_(bias) else: A_wo_outliers = A.clone() if state.idx is not None: A_wo_outliers[:, state.idx.long()] = 0 output = torch.nn.functional.linear(A_wo_outliers, state.CB.to(A.dtype)) output = output.mul_(state.SCB.unsqueeze(0).mul(1.0 / 127.0)) if bias is not None: output = output.add_(bias) # 4. Mixed-precision decomposition matmul if coo_tensorA is not None and subA is not None: output += torch.matmul(subA, state.subB) # 5. Save state ctx.state = state ctx.formatB = formatB ctx.grad_shape = input_shape ctx.dtype_A, ctx.dtype_B, ctx.dtype_bias = A.dtype, B.dtype, None if bias is None else bias.dtype if any(ctx.needs_input_grad[:2]): ctx.tensors = (CAt, subA) ctx.tensor_states = (SCAt, state.idx) else: ctx.tensors = [None, None] ctx.tensor_states = (None, None) ctx.save_for_backward(None, None) clone_func = torch.clone if len(output_shape) == 3 else lambda x: x return clone_func(output.view(output_shape)) @staticmethod def backward(ctx, grad_output): if ctx.is_empty: bias_grad = None if ctx.bias is None else torch.zeros_like(ctx.bias) return torch.zeros_like(ctx.A), torch.zeros_like(ctx.B), None, bias_grad, None req_gradA, req_gradB, _, req_gradBias, _ = ctx.needs_input_grad CAt, subA = ctx.tensors SCAt, idx = ctx.tensor_states formatB = ctx.formatB state = ctx.state grad_A = grad_B = grad_bias = None if req_gradBias: # compute grad_bias first before changing grad_output dtype grad_bias = grad_output.sum(0, dtype=ctx.dtype_bias) # Cast grad_output to fp16 if len(grad_output.shape) == 3: grad_output = grad_output.reshape(-1, grad_output.shape[-1]).contiguous() Cgrad, Cgradt, SCgrad, SCgradt, coo_tensor = F.double_quant(grad_output.to(torch.float16)) if req_gradB: CxAt, SAt = F.transform(CAt, formatB, transpose=True) C32grad, Sgrad = F.transform(Cgradt, "col32", transpose=True) gradB32, SgradB32 = F.igemmlt(C32grad, CxAt, Sgrad, SAt) grad_B = F.mm_dequant(gradB32, SgradB32, SCgradt, SCAt) if state.threshold > 0.0 and subA is not None: grad_B[:, idx] += torch.matmul(grad_output.t(), subA) if req_gradA: if state.CBt is not None: C32grad, Sgrad = F.transform(Cgrad, "col32") if state.CxBt is None: state.CxBt, state.SBt = F.transform(state.CBt, to_order=formatB, transpose=True) gradA32, SgradA32 = F.igemmlt(C32grad, state.CxBt, Sgrad, state.SBt) grad_A = F.mm_dequant(gradA32, SgradA32, SCgrad, state.SCBt).view(ctx.grad_shape).to(ctx.dtype_A) elif state.CB is not None: CB = state.CB.to(ctx.dtype_A, copy=True).mul_(state.SCB.unsqueeze(1).mul(1.0 / 127.0)) grad_A = torch.matmul(grad_output, CB).view(ctx.grad_shape).to(ctx.dtype_A) elif state.CxB is not None: if state.tile_indices is None: order, tile_size = state.formatB, state.get_tile_size() transform = lambda x: F.transform(x.cuda(), from_order="row", to_order=order)[0].to(x.device) with torch.no_grad(): state.tile_indices = get_inverse_transform_indices(transform, tile_size).to(state.CxB.device) CB = ( undo_layout(state.CxB, state.tile_indices) .to(ctx.dtype_A) .mul_(state.SCB.unsqueeze(1).mul(1.0 / 127.0)) ) grad_A = torch.matmul(grad_output, CB).view(ctx.grad_shape).to(ctx.dtype_A) else: raise Exception("State must contain either CBt or CB or CxB matrix for backward") return grad_A, grad_B, None, grad_bias, None def matmul( A: tensor, B: tensor, out: tensor = None, state: MatmulLtState = None, threshold=0.0, bias=None ): state = state or MatmulLtState() if threshold > 0.0: state.threshold = threshold return MatMul8bitLt.apply(A, B, out, bias, state)
def to_be_ignored(env_var: str, value: str) -> bool: ignorable = { "PWD", # PWD: this is how the shell keeps track of the current working dir "OLDPWD", "SSH_AUTH_SOCK", # SSH stuff, therefore unrelated "SSH_TTY", "HOME", # Linux shell default "TMUX", # Terminal Multiplexer "XDG_DATA_DIRS", # XDG: Desktop environment stuff "XDG_RUNTIME_DIR", "MAIL", # something related to emails "SHELL", # binary for currently invoked shell "DBUS_SESSION_BUS_ADDRESS", # hardware related "PATH", # this is for finding binaries, not libraries "LESSOPEN", # related to the `less` command "LESSCLOSE", "_", # current Python interpreter } return env_var in ignorable def might_contain_a_path(candidate: str) -> bool: return "/" in candidate def is_active_conda_env(env_var: str) -> bool: return "CONDA_PREFIX" == env_var def is_other_conda_env_var(env_var: str) -> bool: return "CONDA" in env_var def is_relevant_candidate_env_var(env_var: str, value: str) -> bool: return is_active_conda_env(env_var) or ( might_contain_a_path(value) and not is_other_conda_env_var(env_var) and not to_be_ignored(env_var, value) ) def get_potentially_lib_path_containing_env_vars() -> Dict[str, str]: return { env_var: value for env_var, value in os.environ.items() if is_relevant_candidate_env_var(env_var, value) }
""" extract factors the build is dependent on: [X] compute capability [ ] TODO: Q - What if we have multiple GPUs of different makes? - CUDA version - Software: - CPU-only: only CPU quantization functions (no optimizer, no matrix multipl) - CuBLAS-LT: full-build 8-bit optimizer - no CuBLAS-LT: no 8-bit matrix multiplication (`nomatmul`) evaluation: - if paths faulty, return meaningful error - else: - determine CUDA version - determine capabilities - based on that set the default path """ CUDA_RUNTIME_LIB: str = "libcudart.so" class CUDASetup: _instance = None def __init__(self): raise RuntimeError("Call get_instance() instead") def generate_instructions(self): if self.cuda is None: self.add_log_entry('CUDA SETUP: Problem: The main issue seems to be that the main CUDA library was not detected.') self.add_log_entry('CUDA SETUP: Solution 1): Your paths are probably not up-to-date. You can update them via: sudo ldconfig.') self.add_log_entry('CUDA SETUP: Solution 2): If you do not have sudo rights, you can do the following:') self.add_log_entry('CUDA SETUP: Solution 2a): Find the cuda library via: find / -name libcuda.so 2>/dev/null') self.add_log_entry('CUDA SETUP: Solution 2b): Once the library is found add it to the LD_LIBRARY_PATH: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:FOUND_PATH_FROM_2a') self.add_log_entry('CUDA SETUP: Solution 2c): For a permanent solution add the export from 2b into your .bashrc file, located at ~/.bashrc') return if self.cudart_path is None: self.add_log_entry('CUDA SETUP: Problem: The main issue seems to be that the main CUDA runtime library was not detected.') self.add_log_entry('CUDA SETUP: Solution 1: To solve the issue the libcudart.so location needs to be added to the LD_LIBRARY_PATH variable') self.add_log_entry('CUDA SETUP: Solution 1a): Find the cuda runtime library via: find / -name libcudart.so 2>/dev/null') self.add_log_entry('CUDA SETUP: Solution 1b): Once the library is found add it to the LD_LIBRARY_PATH: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:FOUND_PATH_FROM_1a') self.add_log_entry('CUDA SETUP: Solution 1c): For a permanent solution add the export from 1b into your .bashrc file, located at ~/.bashrc') self.add_log_entry('CUDA SETUP: Solution 2: If no library was found in step 1a) you need to install CUDA.') self.add_log_entry('CUDA SETUP: Solution 2a): Download CUDA install script: wget https://github.com/TimDettmers/bitsandbytes/blob/main/cuda_install.sh') self.add_log_entry('CUDA SETUP: Solution 2b): Install desired CUDA version to desired location. The syntax is bash cuda_install.sh CUDA_VERSION PATH_TO_INSTALL_INTO.') self.add_log_entry('CUDA SETUP: Solution 2b): For example, "bash cuda_install.sh 113 ~/local/" will download CUDA 11.3 and install into the folder ~/local') return make_cmd = f'CUDA_VERSION={self.cuda_version_string}' if len(self.cuda_version_string) < 3: make_cmd += ' make cuda92' elif self.cuda_version_string == '110': make_cmd += ' make cuda110' elif self.cuda_version_string[:2] == '11' and int(self.cuda_version_string[2]) > 0: make_cmd += ' make cuda11x' elif self.cuda_version_string == '100': self.add_log_entry('CUDA SETUP: CUDA 10.0 not supported. Please use a different CUDA version.') self.add_log_entry('CUDA SETUP: Before you try again running bitsandbytes, make sure old CUDA 10.0 versions are uninstalled and removed from $LD_LIBRARY_PATH variables.') return has_cublaslt = is_cublasLt_compatible(self.cc) if not has_cublaslt: make_cmd += '_nomatmul' self.add_log_entry('CUDA SETUP: Something unexpected happened. Please compile from source:') self.add_log_entry('git clone [email protected]:TimDettmers/bitsandbytes.git') self.add_log_entry('cd bitsandbytes') self.add_log_entry(make_cmd) self.add_log_entry('python setup.py install') def initialize(self): if not getattr(self, 'initialized', False): self.has_printed = False self.lib = None self.initialized = False def run_cuda_setup(self): self.initialized = True self.cuda_setup_log = [] binary_name, cudart_path, cuda, cc, cuda_version_string = evaluate_cuda_setup() self.cudart_path = cudart_path self.cuda = cuda self.cc = cc self.cuda_version_string = cuda_version_string package_dir = Path(__file__).parent.parent binary_path = package_dir / binary_name try: if not binary_path.exists(): self.add_log_entry(f"CUDA SETUP: Required library version not found: {binary_name}. Maybe you need to compile it from source?") legacy_binary_name = "libbitsandbytes_cpu.so" self.add_log_entry(f"CUDA SETUP: Defaulting to {legacy_binary_name}...") binary_path = package_dir / legacy_binary_name if not binary_path.exists() or torch.cuda.is_available(): self.add_log_entry('') self.add_log_entry('='*48 + 'ERROR' + '='*37) self.add_log_entry('CUDA SETUP: CUDA detection failed! Possible reasons:') self.add_log_entry('1. CUDA driver not installed') self.add_log_entry('2. CUDA not installed') self.add_log_entry('3. You have multiple conflicting CUDA libraries') self.add_log_entry('4. Required library not pre-compiled for this bitsandbytes release!') self.add_log_entry('CUDA SETUP: If you compiled from source, try again with `make CUDA_VERSION=DETECTED_CUDA_VERSION` for example, `make CUDA_VERSION=113`.') self.add_log_entry('CUDA SETUP: The CUDA version for the compile might depend on your conda install. Inspect CUDA version via `conda list | grep cuda`.') self.add_log_entry('='*80) self.add_log_entry('') self.generate_instructions() self.print_log_stack() raise Exception('CUDA SETUP: Setup Failed!') self.lib = ct.cdll.LoadLibrary(binary_path) else: self.add_log_entry(f"CUDA SETUP: Loading binary {binary_path}...") self.lib = ct.cdll.LoadLibrary(binary_path) except Exception as ex: self.add_log_entry(str(ex)) self.print_log_stack() def add_log_entry(self, msg, is_warning=False): self.cuda_setup_log.append((msg, is_warning)) def print_log_stack(self): for msg, is_warning in self.cuda_setup_log: if is_warning: warn(msg) else: print(msg) @classmethod def get_instance(cls): if cls._instance is None: cls._instance = cls.__new__(cls) cls._instance.initialize() return cls._instance def is_cublasLt_compatible(cc): has_cublaslt = False if cc is not None: cc_major, cc_minor = cc.split('.') if int(cc_major) < 7 or (int(cc_major) == 7 and int(cc_minor) < 5): cuda_setup.add_log_entry("WARNING: Compute capability < 7.5 detected! Only slow 8-bit matmul is supported for your GPU!", is_warning=True) else: has_cublaslt = True return has_cublaslt def extract_candidate_paths(paths_list_candidate: str) -> Set[Path]: return {Path(ld_path) for ld_path in paths_list_candidate.split(":") if ld_path} def remove_non_existent_dirs(candidate_paths: Set[Path]) -> Set[Path]: existent_directories: Set[Path] = set() for path in candidate_paths: try: if path.exists(): existent_directories.add(path) except OSError as exc: if exc.errno != errno.ENAMETOOLONG: raise exc non_existent_directories: Set[Path] = candidate_paths - existent_directories if non_existent_directories: CUDASetup.get_instance().add_log_entry("WARNING: The following directories listed in your path were found to " f"be non-existent: {non_existent_directories}", is_warning=True) return existent_directories def get_cuda_runtime_lib_paths(candidate_paths: Set[Path]) -> Set[Path]: return { path / CUDA_RUNTIME_LIB for path in candidate_paths if (path / CUDA_RUNTIME_LIB).is_file() } def resolve_paths_list(paths_list_candidate: str) -> Set[Path]: """ Searches a given environmental var for the CUDA runtime library, i.e. `libcudart.so`. """ return remove_non_existent_dirs(extract_candidate_paths(paths_list_candidate)) def find_cuda_lib_in(paths_list_candidate: str) -> Set[Path]: return get_cuda_runtime_lib_paths( resolve_paths_list(paths_list_candidate) ) def warn_in_case_of_duplicates(results_paths: Set[Path]) -> None: if len(results_paths) > 1: warning_msg = ( f"Found duplicate {CUDA_RUNTIME_LIB} files: {results_paths}.. " "We'll flip a coin and try one of these, in order to fail forward.\n" "Either way, this might cause trouble in the future:\n" "If you get `CUDA error: invalid device function` errors, the above " "might be the cause and the solution is to make sure only one " f"{CUDA_RUNTIME_LIB} in the paths that we search based on your env.") CUDASetup.get_instance().add_log_entry(warning_msg, is_warning=True) def determine_cuda_runtime_lib_path() -> Union[Path, None]: """ Searches for a cuda installations, in the following order of priority: 1. active conda env 2. LD_LIBRARY_PATH 3. any other env vars, while ignoring those that - are known to be unrelated (see `bnb.cuda_setup.env_vars.to_be_ignored`) - don't contain the path separator `/` If multiple libraries are found in part 3, we optimistically try one, while giving a warning message. """ candidate_env_vars = get_potentially_lib_path_containing_env_vars() if "CONDA_PREFIX" in candidate_env_vars: conda_libs_path = Path(candidate_env_vars["CONDA_PREFIX"]) / "lib" conda_cuda_libs = find_cuda_lib_in(str(conda_libs_path)) warn_in_case_of_duplicates(conda_cuda_libs) if conda_cuda_libs: return next(iter(conda_cuda_libs)) CUDASetup.get_instance().add_log_entry(f'{candidate_env_vars["CONDA_PREFIX"]} did not contain ' f'{CUDA_RUNTIME_LIB} as expected! Searching further paths...', is_warning=True) if "LD_LIBRARY_PATH" in candidate_env_vars: lib_ld_cuda_libs = find_cuda_lib_in(candidate_env_vars["LD_LIBRARY_PATH"]) if lib_ld_cuda_libs: return next(iter(lib_ld_cuda_libs)) warn_in_case_of_duplicates(lib_ld_cuda_libs) CUDASetup.get_instance().add_log_entry(f'{candidate_env_vars["LD_LIBRARY_PATH"]} did not contain ' f'{CUDA_RUNTIME_LIB} as expected! Searching further paths...', is_warning=True) remaining_candidate_env_vars = { env_var: value for env_var, value in candidate_env_vars.items() if env_var not in {"CONDA_PREFIX", "LD_LIBRARY_PATH"} } cuda_runtime_libs = set() for env_var, value in remaining_candidate_env_vars.items(): cuda_runtime_libs.update(find_cuda_lib_in(value)) if len(cuda_runtime_libs) == 0: CUDASetup.get_instance().add_log_entry('CUDA_SETUP: WARNING! libcudart.so not found in any environmental path. Searching /usr/local/cuda/lib64...') cuda_runtime_libs.update(find_cuda_lib_in('/usr/local/cuda/lib64')) warn_in_case_of_duplicates(cuda_runtime_libs) return next(iter(cuda_runtime_libs)) if cuda_runtime_libs else None def check_cuda_result(cuda, result_val): # 3. Check for CUDA errors if result_val != 0: error_str = ct.c_char_p() cuda.cuGetErrorString(result_val, ct.byref(error_str)) if error_str.value is not None: CUDASetup.get_instance().add_log_entry(f"CUDA exception! Error code: {error_str.value.decode()}") else: CUDASetup.get_instance().add_log_entry(f"Unknown CUDA exception! Please check your CUDA install. It might also be that your GPU is too old.") # https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART____VERSION.html#group__CUDART____VERSION def get_cuda_version(cuda, cudart_path): if cuda is None: return None try: cudart = ct.CDLL(cudart_path) except OSError: CUDASetup.get_instance().add_log_entry(f'ERROR: libcudart.so could not be read from path: {cudart_path}!') return None version = ct.c_int() try: check_cuda_result(cuda, cudart.cudaRuntimeGetVersion(ct.byref(version))) except AttributeError as e: CUDASetup.get_instance().add_log_entry(f'ERROR: {str(e)}') CUDASetup.get_instance().add_log_entry(f'CUDA SETUP: libcudart.so path is {cudart_path}') CUDASetup.get_instance().add_log_entry(f'CUDA SETUP: Is seems that your cuda installation is not in your path. See https://github.com/TimDettmers/bitsandbytes/issues/85 for more information.') version = int(version.value) major = version//1000 minor = (version-(major*1000))//10 if major < 11: CUDASetup.get_instance().add_log_entry('CUDA SETUP: CUDA version lower than 11 are currently not supported for LLM.int8(). You will be only to use 8-bit optimizers and quantization routines!!') return f'{major}{minor}' def get_cuda_lib_handle(): # 1. find libcuda.so library (GPU driver) (/usr/lib) try: cuda = ct.CDLL("libcuda.so") except OSError: CUDASetup.get_instance().add_log_entry('CUDA SETUP: WARNING! libcuda.so not found! Do you have a CUDA driver installed? If you are on a cluster, make sure you are on a CUDA machine!') return None check_cuda_result(cuda, cuda.cuInit(0)) return cuda def get_compute_capabilities(cuda): """ 1. find libcuda.so library (GPU driver) (/usr/lib) init_device -> init variables -> call function by reference 2. call extern C function to determine CC (https://docs.nvidia.com/cuda/cuda-driver-api/group__CUDA__DEVICE__DEPRECATED.html) 3. Check for CUDA errors https://stackoverflow.com/questions/14038589/what-is-the-canonical-way-to-check-for-errors-using-the-cuda-runtime-api # bits taken from https://gist.github.com/f0k/63a664160d016a491b2cbea15913d549 """ nGpus = ct.c_int() cc_major = ct.c_int() cc_minor = ct.c_int() device = ct.c_int() check_cuda_result(cuda, cuda.cuDeviceGetCount(ct.byref(nGpus))) ccs = [] for i in range(nGpus.value): check_cuda_result(cuda, cuda.cuDeviceGet(ct.byref(device), i)) ref_major = ct.byref(cc_major) ref_minor = ct.byref(cc_minor) # 2. call extern C function to determine CC check_cuda_result(cuda, cuda.cuDeviceComputeCapability(ref_major, ref_minor, device)) ccs.append(f"{cc_major.value}.{cc_minor.value}") return ccs # def get_compute_capability()-> Union[List[str, ...], None]: # FIXME: error def get_compute_capability(cuda): """ Extracts the highest compute capbility from all available GPUs, as compute capabilities are downwards compatible. If no GPUs are detected, it returns None. """ if cuda is None: return None # TODO: handle different compute capabilities; for now, take the max ccs = get_compute_capabilities(cuda) if ccs: return ccs[-1] def evaluate_cuda_setup(): if 'BITSANDBYTES_NOWELCOME' not in os.environ or str(os.environ['BITSANDBYTES_NOWELCOME']) == '0': print('') print('='*35 + 'BUG REPORT' + '='*35) print('Welcome to bitsandbytes. For bug reports, please submit your error trace to: https://github.com/TimDettmers/bitsandbytes/issues') print('='*80) if not torch.cuda.is_available(): return 'libsbitsandbytes_cpu.so', None, None, None, None cuda_setup = CUDASetup.get_instance() cudart_path = determine_cuda_runtime_lib_path() cuda = get_cuda_lib_handle() cc = get_compute_capability(cuda) cuda_version_string = get_cuda_version(cuda, cudart_path) failure = False if cudart_path is None: failure = True cuda_setup.add_log_entry("WARNING: No libcudart.so found! Install CUDA or the cudatoolkit package (anaconda)!", is_warning=True) else: cuda_setup.add_log_entry(f"CUDA SETUP: CUDA runtime path found: {cudart_path}") if cc == '' or cc is None: failure = True cuda_setup.add_log_entry("WARNING: No GPU detected! Check your CUDA paths. Proceeding to load CPU-only library...", is_warning=True) else: cuda_setup.add_log_entry(f"CUDA SETUP: Highest compute capability among GPUs detected: {cc}") if cuda is None: failure = True else: cuda_setup.add_log_entry(f'CUDA SETUP: Detected CUDA version {cuda_version_string}') # 7.5 is the minimum CC vor cublaslt has_cublaslt = is_cublasLt_compatible(cc) # TODO: # (1) CUDA missing cases (no CUDA installed by CUDA driver (nvidia-smi accessible) # (2) Multiple CUDA versions installed # we use ls -l instead of nvcc to determine the cuda version # since most installations will have the libcudart.so installed, but not the compiler if failure: binary_name = "libbitsandbytes_cpu.so" elif has_cublaslt: binary_name = f"libbitsandbytes_cuda{cuda_version_string}.so" else: "if not has_cublaslt (CC < 7.5), then we have to choose _nocublaslt.so" binary_name = f"libbitsandbytes_cuda{cuda_version_string}_nocublaslt.so" return binary_name, cudart_path, cuda, cc, cuda_version_string
# 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. class RMSprop(Optimizer1State): def __init__( self, params, lr=1e-2, alpha=0.99, eps=1e-8, weight_decay=0, momentum=0, centered=False, optim_bits=32, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, ): if alpha == 0: raise NotImplementedError( "RMSprop with alpha==0.0 is not supported!" ) if centered: raise NotImplementedError("Centered RMSprop is not supported!") super().__init__( "rmsprop", params, lr, (alpha, momentum), eps, weight_decay, optim_bits, args, min_8bit_size, percentile_clipping, block_wise, ) class RMSprop8bit(Optimizer1State): def __init__( self, params, lr=1e-2, alpha=0.99, eps=1e-8, weight_decay=0, momentum=0, centered=False, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, ): if alpha == 0: raise NotImplementedError( "RMSprop with alpha==0.0 is not supported!" ) if centered: raise NotImplementedError("Centered RMSprop is not supported!") super().__init__( "rmsprop", params, lr, (alpha, momentum), eps, weight_decay, 8, args, min_8bit_size, percentile_clipping, block_wise, ) class RMSprop32bit(Optimizer1State): def __init__( self, params, lr=1e-2, alpha=0.99, eps=1e-8, weight_decay=0, momentum=0, centered=False, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, ): if alpha == 0: raise NotImplementedError( "RMSprop with alpha==0.0 is not supported!" ) if centered: raise NotImplementedError("Centered RMSprop is not supported!") super().__init__( "rmsprop", params, lr, (alpha, momentum), eps, weight_decay, 32, args, min_8bit_size, percentile_clipping, block_wise, )
# 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. class Lion(Optimizer1State): def __init__( self, params, lr=1e-4, betas=(0.9, 0.99), weight_decay=0, optim_bits=32, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, ): super().__init__( "lion", params, lr, betas, 0., weight_decay, optim_bits, args, min_8bit_size, percentile_clipping, block_wise, ) class Lion8bit(Optimizer1State): def __init__( self, params, lr=1e-4, betas=(0.9, 0.99), weight_decay=0, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, ): super().__init__( "lion", params, lr, betas, 0., weight_decay, 8, args, min_8bit_size, percentile_clipping, block_wise, ) class Lion32bit(Optimizer1State): def __init__( self, params, lr=1e-4, betas=(0.9, 0.99), weight_decay=0, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, ): super().__init__( "lion", params, lr, betas, 0., weight_decay, 32, args, min_8bit_size, percentile_clipping, block_wise, )
# 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. class LAMB(Optimizer2State): def __init__( self, params, lr=1e-3, bias_correction=True, betas=(0.9, 0.999), eps=1e-8, weight_decay=0, amsgrad=False, adam_w_mode=True, optim_bits=32, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=False, max_unorm=1.0, ): super().__init__( "lamb", params, lr, betas, eps, weight_decay, optim_bits, args, min_8bit_size, percentile_clipping, block_wise, max_unorm=1.0, ) class LAMB8bit(Optimizer2State): def __init__( self, params, lr=1e-3, bias_correction=True, betas=(0.9, 0.999), eps=1e-8, weight_decay=0, amsgrad=False, adam_w_mode=True, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=False, max_unorm=1.0, ): super().__init__( "lamb", params, lr, betas, eps, weight_decay, 8, args, min_8bit_size, percentile_clipping, block_wise, max_unorm=1.0, ) class LAMB32bit(Optimizer2State): def __init__( self, params, lr=1e-3, bias_correction=True, betas=(0.9, 0.999), eps=1e-8, weight_decay=0, amsgrad=False, adam_w_mode=True, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=False, max_unorm=1.0, ): super().__init__( "lamb", params, lr, betas, eps, weight_decay, 32, args, min_8bit_size, percentile_clipping, block_wise, max_unorm=1.0, )
# 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. class SGD(Optimizer1State): def __init__( self, params, lr, momentum=0, dampening=0, weight_decay=0, nesterov=False, optim_bits=32, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, ): if momentum == 0: raise NotImplementedError("SGD without momentum is not supported!") super().__init__( "momentum", params, lr, (momentum, dampening), 0.0, weight_decay, optim_bits, args, min_8bit_size, percentile_clipping, block_wise, ) class SGD8bit(Optimizer1State): def __init__( self, params, lr, momentum=0, dampening=0, weight_decay=0, nesterov=False, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, ): if momentum == 0: raise NotImplementedError("SGD without momentum is not supported!") super().__init__( "momentum", params, lr, (momentum, dampening), 0.0, weight_decay, 8, args, min_8bit_size, percentile_clipping, block_wise, ) class SGD32bit(Optimizer1State): def __init__( self, params, lr, momentum=0, dampening=0, weight_decay=0, nesterov=False, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, ): if momentum == 0: raise NotImplementedError("SGD without momentum is not supported!") super().__init__( "momentum", params, lr, (momentum, dampening), 0.0, weight_decay, 32, args, min_8bit_size, percentile_clipping, block_wise, )
# 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. class LARS(Optimizer1State): def __init__( self, params, lr, momentum=0, dampening=0, weight_decay=0, nesterov=False, optim_bits=32, args=None, min_8bit_size=4096, percentile_clipping=100, max_unorm=0.02, ): if momentum == 0: raise NotImplementedError( "LARS without momentum is not supported!" ) super().__init__( "lars", params, lr, (momentum, dampening), 0.0, weight_decay, optim_bits, args, min_8bit_size, percentile_clipping, max_unorm=max_unorm, block_wise=False, ) class LARS8bit(Optimizer1State): def __init__( self, params, lr, momentum=0, dampening=0, weight_decay=0, nesterov=False, args=None, min_8bit_size=4096, percentile_clipping=100, max_unorm=0.02, ): if momentum == 0: raise NotImplementedError( "LARS without momentum is not supported!" ) super().__init__( "lars", params, lr, (momentum, dampening), 0.0, weight_decay, 8, args, min_8bit_size, percentile_clipping, max_unorm=max_unorm, block_wise=False, ) class LARS32bit(Optimizer1State): def __init__( self, params, lr, momentum=0, dampening=0, weight_decay=0, nesterov=False, args=None, min_8bit_size=4096, percentile_clipping=100, max_unorm=0.02, ): if momentum == 0: raise NotImplementedError( "LARS without momentum is not supported!" ) super().__init__( "lars", params, lr, (momentum, dampening), 0.0, weight_decay, 32, args, min_8bit_size, percentile_clipping, max_unorm=max_unorm, block_wise=False, ) class PytorchLARS(Optimizer): def __init__( self, params, lr=0.01, momentum=0, dampening=0, weight_decay=0, nesterov=False, max_unorm=0.02, ): if lr < 0.0: raise ValueError(f"Invalid learning rate: {lr}") if momentum < 0.0: raise ValueError(f"Invalid momentum value: {momentum}") if weight_decay < 0.0: raise ValueError( f"Invalid weight_decay value: {weight_decay}" ) defaults = dict( lr=lr, momentum=momentum, dampening=dampening, weight_decay=weight_decay, nesterov=nesterov, max_unorm=max_unorm, ) if nesterov and (momentum <= 0 or dampening != 0): raise ValueError( "Nesterov momentum requires a momentum and zero dampening" ) super().__init__(params, defaults) def __setstate__(self, state): super().__setstate__(state) for group in self.param_groups: group.setdefault("nesterov", False) @torch.no_grad() def step(self, closure=None): """Performs a single optimization step. Args: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: with torch.enable_grad(): loss = closure() for group in self.param_groups: params_with_grad = [] d_p_list = [] momentum_buffer_list = [] weight_decay = group["weight_decay"] momentum = group["momentum"] dampening = group["dampening"] nesterov = group["nesterov"] max_unorm = group["max_unorm"] lr = group["lr"] for p in group["params"]: if p.grad is None: continue state = self.state[p] d_p = p.grad if weight_decay != 0: d_p = d_p.add(p, alpha=weight_decay) if momentum != 0: buf = state.get("momentum_buffer", None) if buf is None: buf = torch.clone(d_p).detach() state["momentum_buffer"] = buf else: buf.mul_(momentum).add_(d_p, alpha=1 - dampening) if nesterov: update = d_p + buf * momentum else: update = buf update_scale = 1.0 if max_unorm > 0.0: assert p.dtype == torch.float32 pnorm = torch.norm(p.detach()) unorm = torch.norm(update) if unorm > max_unorm * pnorm: update_scale = max_unorm * pnorm / unorm p.add_(update, alpha=-lr * update_scale) return loss
# 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.
# 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. class Adagrad(Optimizer1State): def __init__( self, params, lr=1e-2, lr_decay=0, weight_decay=0, initial_accumulator_value=0, eps=1e-10, optim_bits=32, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, ): if not 0.0 <= lr: raise ValueError(f"Invalid learning rate: {lr}") if not 0.0 <= weight_decay: raise ValueError( f"Invalid weight_decay value: {weight_decay}" ) if not 0.0 <= eps: raise ValueError(f"Invalid epsilon value: {eps}") if initial_accumulator_value != 0.0: raise ValueError("Initial accumulator value != 0.0 not supported!") if lr_decay != 0.0: raise ValueError("Lr Decay != 0.0 not supported!") super().__init__( "adagrad", params, lr, (0.0, 0.0), eps, weight_decay, optim_bits, args, min_8bit_size, percentile_clipping, block_wise, ) class Adagrad8bit(Optimizer1State): def __init__( self, params, lr=1e-2, lr_decay=0, weight_decay=0, initial_accumulator_value=0, eps=1e-10, optim_bits=8, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, ): if not 0.0 <= lr: raise ValueError(f"Invalid learning rate: {lr}") if not 0.0 <= weight_decay: raise ValueError( f"Invalid weight_decay value: {weight_decay}" ) if not 0.0 <= eps: raise ValueError(f"Invalid epsilon value: {eps}") if initial_accumulator_value != 0.0: raise ValueError("Initial accumulator value != 0.0 not supported!") if lr_decay != 0.0: raise ValueError("Lr Decay != 0.0 not supported!") assert block_wise super().__init__( "adagrad", params, lr, (0.0, 0.0), eps, weight_decay, 8, args, min_8bit_size, percentile_clipping, block_wise, ) class Adagrad32bit(Optimizer1State): def __init__( self, params, lr=1e-2, lr_decay=0, weight_decay=0, initial_accumulator_value=0, eps=1e-10, optim_bits=32, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, ): if not 0.0 <= lr: raise ValueError(f"Invalid learning rate: {lr}") if not 0.0 <= weight_decay: raise ValueError( f"Invalid weight_decay value: {weight_decay}" ) if not 0.0 <= eps: raise ValueError(f"Invalid epsilon value: {eps}") if initial_accumulator_value != 0.0: raise ValueError("Initial accumulator value != 0.0 not supported!") if lr_decay != 0.0: raise ValueError("Lr Decay != 0.0 not supported!") super().__init__( "adagrad", params, lr, (0.0, 0.0), eps, weight_decay, 32, args, min_8bit_size, percentile_clipping, block_wise, )
# 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. class AdamW(Optimizer2State): def __init__( self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=1e-2, amsgrad=False, optim_bits=32, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, ): super().__init__( "adam", params, lr, betas, eps, weight_decay, optim_bits, args, min_8bit_size, percentile_clipping, block_wise, ) class AdamW8bit(Optimizer2State): def __init__( self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=1e-2, amsgrad=False, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, ): super().__init__( "adam", params, lr, betas, eps, weight_decay, 8, args, min_8bit_size, percentile_clipping, block_wise, ) class AdamW32bit(Optimizer2State): def __init__( self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=1e-2, amsgrad=False, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, ): super().__init__( "adam", params, lr, betas, eps, weight_decay, 32, args, min_8bit_size, percentile_clipping, block_wise, )
# 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. class Adam(Optimizer2State): def __init__( self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0, amsgrad=False, optim_bits=32, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, ): super().__init__( "adam", params, lr, betas, eps, weight_decay, optim_bits, args, min_8bit_size, percentile_clipping, block_wise, ) class Adam8bit(Optimizer2State): def __init__( self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0, amsgrad=False, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, ): super().__init__( "adam", params, lr, betas, eps, weight_decay, 8, args, min_8bit_size, percentile_clipping, block_wise, ) class Adam32bit(Optimizer2State): def __init__( self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0, amsgrad=False, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, ): super().__init__( "adam", params, lr, betas, eps, weight_decay, 32, args, min_8bit_size, percentile_clipping, block_wise, ) class AnalysisAdam(torch.optim.Optimizer): """Adam that performs 8-bit vs 32-bit error analysis. This implementation is modified from torch.optim.Adam based on: `Fixed Weight Decay Regularization in Adam` (see https://arxiv.org/abs/1711.05101) It has been proposed in `Adam: A Method for Stochastic Optimization`_. Arguments: params (iterable): iterable of parameters to optimize or dicts defining parameter groups lr (float, optional): learning rate (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its square (default: (0.9, 0.999)) eps (float, optional): term added to the denominator to improve numerical stability (default: 1e-8) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) amsgrad (boolean, optional): whether to use the AMSGrad variant of this algorithm from the paper `On the Convergence of Adam and Beyond`_ .. _Adam: A Method for Stochastic Optimization: https://arxiv.org/abs/1412.6980 .. _On the Convergence of Adam and Beyond: https://openreview.net/forum?id=ryQu7f-RZ """ def __init__( self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0, amsgrad=False, bnb_analysis="dynamic-blockwise", savedir=None, ): defaults = dict( lr=lr, betas=betas, eps=eps, weight_decay=weight_decay, amsgrad=amsgrad, ) super().__init__(params, defaults) self.analysis = bnb_analysis self.savedir = savedir @property def supports_memory_efficient_fp16(self): return True @property def supports_flat_params(self): return True def step(self, closure=None): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: loss = closure() for group in self.param_groups: for p_id, p in enumerate(group["params"]): if p.grad is None: continue grad = p.grad.data if grad.dtype in {torch.float16, torch.bfloat16}: grad = grad.float() if grad.is_sparse: raise RuntimeError( "Adam does not support sparse gradients, please consider SparseAdam instead" ) amsgrad = group.get("amsgrad", False) assert not amsgrad p_data_fp32 = p.data if p.data.dtype in {torch.float16, torch.bfloat16}: p_data_fp32 = p_data_fp32.float() state = self.state[p] # State initialization if len(state) == 0: state["step"] = 0 # Exponential moving average of gradient values state["exp_avg"] = torch.zeros_like(p_data_fp32) # Exponential moving average of squared gradient values state["exp_avg_sq"] = torch.zeros_like(p_data_fp32) state["abserrors"] = torch.zeros( (256, 256), device=p_data_fp32.device ) state["relerrors"] = torch.zeros( (256, 256), device=p_data_fp32.device ) state["counts"] = torch.zeros( (256, 256), device=p_data_fp32.device ) if amsgrad: # Maintains max of all exp. moving avg. of sq. grad. values state["max_exp_avg_sq"] = torch.zeros_like(p_data_fp32) else: state["exp_avg"] = state["exp_avg"].to(p_data_fp32) state["exp_avg_sq"] = state["exp_avg_sq"].to(p_data_fp32) if amsgrad: state["max_exp_avg_sq"] = state["max_exp_avg_sq"].to( p_data_fp32 ) state["step"] += 1 beta1, beta2 = group["betas"] bias_correction1 = 1 - beta1 ** state["step"] bias_correction2 = 1 - beta2 ** state["step"] step_size = ( group["lr"] * math.sqrt(bias_correction2) / bias_correction1 ) e = state["abserrors"] rele = state["relerrors"] counts = state["counts"] if group["weight_decay"] != 0: p_data_fp32.add_( p_data_fp32, alpha=-group["weight_decay"] * group["lr"] ) exp_avg, exp_avg_sq = state["exp_avg"], state["exp_avg_sq"] if amsgrad: max_exp_avg_sq = state["max_exp_avg_sq"] # Decay the first and second moment running average coefficient exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1) exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2) denom = exp_avg_sq.sqrt().add_(group["eps"]) update_fp32 = exp_avg / denom if ( p_data_fp32.numel() <= 8192 or p_data_fp32.numel() > 50000 * 1000 ): # embedding layer or too small p_data_fp32 += -step_size * update_fp32 else: if self.analysis == "dynamic-blockwise": code1 = F.create_dynamic_map(signed=True).to(p.device) code2 = F.create_dynamic_map(signed=False).to(p.device) C1, S1 = F.quantize_blockwise(exp_avg, code=code1) state1 = F.dequantize_blockwise(C1, S1) C2, S2 = F.quantize_blockwise(exp_avg_sq, code=code2) state2 = F.dequantize_blockwise(C2, S2) elif self.analysis == "dynamic": code1 = F.create_dynamic_map(signed=True).to(p.device) code2 = F.create_dynamic_map(signed=False).to(p.device) C1, S1 = F.quantize(exp_avg, code=code1) state1 = F.dequantize(C1, S1) C2, S2 = F.quantize(exp_avg_sq, code=code2) state2 = F.dequantize(C2, S2) elif self.analysis == "linear": code1 = F.create_linear_map(signed=True).to(p.device) code2 = F.create_linear_map(signed=False).to(p.device) C1, S1 = F.quantize(exp_avg, code=code1) state1 = F.dequantize(C1, S1) C2, S2 = F.quantize(exp_avg_sq, code=code2) state2 = F.dequantize(C2, S2) elif self.analysis == "quantile": code1 = F.estimate_quantiles(exp_avg) code2 = F.estimate_quantiles(exp_avg_sq) C1 = F.quantize_no_absmax(exp_avg, code=code1) state1 = F.dequantize_no_absmax(C1, code1) C2 = F.quantize_no_absmax(exp_avg_sq, code=code2) state2 = F.dequantize_no_absmax(C2, code2) elif self.analysis == "my-quantization-routine": pass # 1. get code # 2. quantize # 3. dequantize # Error will be calculated automatically! else: raise ValueError( f"Invalid analysis value: {self.analysis}!" ) denom = state2.sqrt().add_(group["eps"]) update_8bit = state1 / denom abserr = torch.abs(update_8bit - update_fp32) relerr = abserr / torch.abs(update_fp32 + 1e-6) C1, C2 = C1.int(), C2.int() F.histogram_scatter_add_2d(e, C1.int(), C2.int(), abserr) F.histogram_scatter_add_2d(rele, C1.int(), C2.int(), relerr) F.histogram_scatter_add_2d( counts, C1.int(), C2.int(), torch.ones_like(abserr) ) p_data_fp32 += -step_size * update_fp32 if not dist.is_initialized() or dist.get_rank() == 0: if self.savedir != "" and state["step"] % 100 == 0: if not os.path.exists(self.savedir): os.makedirs(self.savedir) shapestr = "_".join( [str(dim) for dim in p_data_fp32.shape] ) pathe = os.path.join( self.savedir, f"{p_id}_{shapestr}_abserr.pkl" ) pathrele = os.path.join( self.savedir, f"{p_id}_{shapestr}_relerr.pkl" ) pathcounts = os.path.join( self.savedir, f"{p_id}_{shapestr}_counts.pkl" ) torch.save(e, pathe) torch.save(rele, pathrele) torch.save(counts, pathcounts) if p.data.dtype in {torch.float16, torch.bfloat16}: p.data.copy_(p_data_fp32) return loss
# 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. class MockArgs: def __init__(self, initial_data): for key in initial_data: setattr(self, key, initial_data[key]) class GlobalOptimManager: _instance = None def __init__(self): raise RuntimeError("Call get_instance() instead") def initialize(self): self.pid2config = {} self.index2config = {} self.optimizer = None self.uses_config_override = False self.module_weight_config_triple = [] @classmethod def get_instance(cls): if cls._instance is None: cls._instance = cls.__new__(cls) cls._instance.initialize() return cls._instance def register_parameters(self, params): param_groups = list(params) if not isinstance(param_groups[0], dict): param_groups = [{"params": param_groups}] for group_index, group in enumerate(param_groups): for p_index, p in enumerate(group["params"]): if id(p) in self.pid2config: self.index2config[(group_index, p_index)] = self.pid2config[ id(p) ] def override_config( self, parameters, key=None, value=None, key_value_dict=None ): """ Overrides initial optimizer config for specific parameters. The key-values of the optimizer config for the input parameters are overridden This can be both, optimizer parameters like "betas", or "lr" or it can be 8-bit specific parameters like "optim_bits", "percentile_clipping". Parameters ---------- parameters : torch.Tensor or list(torch.Tensors) The input parameters. key : str The hyperparamter to override. value : object The value for the hyperparamters. key_value_dict : dict A dictionary with multiple key-values to override. """ self.uses_config_override = True if isinstance(parameters, torch.nn.Parameter): parameters = [parameters] if isinstance(parameters, torch.Tensor): parameters = [parameters] if key is not None and value is not None: assert key_value_dict is None key_value_dict = {key: value} if key_value_dict is not None: for p in parameters: if id(p) in self.pid2config: self.pid2config[id(p)].update(key_value_dict) else: self.pid2config[id(p)] = key_value_dict def register_module_override(self, module, param_name, config): self.module_weight_config_triple.append((module, param_name, config)) class Optimizer8bit(torch.optim.Optimizer): def __init__(self, params, defaults, optim_bits=32): super().__init__(params, defaults) self.initialized = False self.name2qmap = {} self.mng = GlobalOptimManager.get_instance() self.non_castable_tensor_keys = { "qmap1", "qmap2", "max1", "max2", "new_max1", "new_max2", "state1", "state2", "gnorm_vec", "absmax1", "absmax2", "unorm_vec", } if optim_bits == 8: self.fill_qmap() def fill_qmap(self): self.name2qmap["dynamic"] = F.create_dynamic_map(signed=True) self.name2qmap["udynamic"] = F.create_dynamic_map(signed=False) def __setstate__(self, state): super().__setstate__(state) def load_state_dict(self, state_dict): r"""Loads the optimizer state. Args: state_dict (dict): optimizer state. Should be an object returned from a call to :meth:`state_dict`. """ # deepcopy, to be consistent with module API state_dict = deepcopy(state_dict) # Validate the state_dict groups = self.param_groups saved_groups = state_dict["param_groups"] if len(groups) != len(saved_groups): raise ValueError( "loaded state dict has a different number of " "parameter groups" ) param_lens = (len(g["params"]) for g in groups) saved_lens = (len(g["params"]) for g in saved_groups) if any(p_len != s_len for p_len, s_len in zip(param_lens, saved_lens)): raise ValueError( "loaded state dict contains a parameter group " "that doesn't match the size of optimizer's group" ) # Update the state id_map = { old_id: p for old_id, p in zip( chain.from_iterable(g["params"] for g in saved_groups), chain.from_iterable(g["params"] for g in groups), ) } def cast(param, value): r"""Make a deep copy of value, casting all tensors to device of param.""" if isinstance(value, torch.Tensor): # Floating-point types are a bit special here. They are the only ones # that are assumed to always match the type of params. if param.is_floating_point() and value.dtype != torch.uint8: value = value.to(param.dtype) return value elif isinstance(value, dict): for k, v in value.items(): if k in self.non_castable_tensor_keys: value[k] = v.to(param.device) else: value[k] = cast(param, v) return value elif isinstance(value, container_abcs.Iterable): return type(value)(cast(param, v) for v in value) else: return value # Copy state assigned to params (and cast tensors to appropriate types). # State that is not assigned to params is copied as is (needed for # backward compatibility). state = defaultdict(dict) for k, v in state_dict["state"].items(): if k in id_map: param = id_map[k] state[param] = cast(param, v) else: state[k] = v # Update parameter groups, setting their 'params' value def update_group(group, new_group): new_group["params"] = group["params"] return new_group param_groups = [ update_group(g, ng) for g, ng in zip(groups, saved_groups) ] self.__setstate__({"state": state, "param_groups": param_groups}) def to_gpu(self): for gindex, group in enumerate(self.param_groups): for pindex, p in enumerate(group["params"]): if p in self.state: values = self.state[p] for k, v in values.items(): if isinstance(v, torch.Tensor): self.state[p][k] = v.to(p.device) def check_overrides(self): for module, attr, config in self.mng.module_weight_config_triple: pmodule = getattr(module, attr) assert pmodule is not None assert isinstance(pmodule, torch.Tensor) or isinstance( pmodule, torch.Parameter ) found = False for gindex, group in enumerate(self.param_groups): if found: break for pindex, p in enumerate(group["params"]): if found: break if id(p) == id(pmodule): # found the matching parameter # init override self.mng.pid2config[id(p)] = config self.mng.index2config[ (gindex, pindex) ] = self.mng.pid2config[id(p)] found = True @torch.no_grad() def step(self, closure=None): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: with torch.enable_grad(): loss = closure() overflows = [] if not self.initialized: self.check_overrides() self.to_gpu() # needed for fairseq pure fp16 training self.initialized = True for gindex, group in enumerate(self.param_groups): for pindex, p in enumerate(group["params"]): if p.grad is None: continue state = self.state[p] if len(state) == 0: self.init_state(group, p, gindex, pindex) self.update_step(group, p, gindex, pindex) return loss def get_config(self, gindex, pindex, group): config = {} config["betas"] = group["betas"] config["eps"] = group["eps"] config["weight_decay"] = group["weight_decay"] config["lr"] = group["lr"] config["optim_bits"] = self.args.optim_bits config["min_8bit_size"] = self.args.min_8bit_size config["percentile_clipping"] = self.args.percentile_clipping config["block_wise"] = self.args.block_wise config["max_unorm"] = self.args.max_unorm config["skip_zeros"] = self.args.skip_zeros if (gindex, pindex) in self.mng.index2config: config.update(self.mng.index2config[(gindex, pindex)]) return config def init_state(self, group, p, gindex, pindex): raise NotImplementedError("init_state method needs to be overridden") def update_step(self, group, p, gindex, pindex): raise NotImplementedError( "The update_step method needs to be overridden" ) class Optimizer2State(Optimizer8bit): def __init__( self, optimizer_name, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0.0, optim_bits=32, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, max_unorm=0.0, skip_zeros=False, ): if not 0.0 <= lr: raise ValueError(f"Invalid learning rate: {lr}") if not 0.0 <= eps: raise ValueError(f"Invalid epsilon value: {eps}") if isinstance(betas, str): # format: '(beta1, beta2)' betas = betas.replace("(", "").replace(")", "").strip().split(",") betas = [float(b) for b in betas] for i in range(len(betas)): if not 0.0 <= betas[i] < 1.0: raise ValueError( f"Invalid beta parameter at index {i}: {betas[i]}" ) if not 0.0 <= weight_decay: raise ValueError( f"Invalid weight_decay value: {weight_decay}" ) defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay) super().__init__(params, defaults, optim_bits) if args is None: args = {} args["optim_bits"] = optim_bits args["percentile_clipping"] = 100 args["min_8bit_size"] = min_8bit_size args["percentile_clipping"] = percentile_clipping args["block_wise"] = block_wise args["max_unorm"] = max_unorm args["skip_zeros"] = skip_zeros self.args = MockArgs(args) else: self.args = args self.optimizer_name = optimizer_name @torch.no_grad() def init_state(self, group, p, gindex, pindex): config = self.get_config(gindex, pindex, group) if config["optim_bits"] == 32: dtype = torch.float32 elif config["optim_bits"] == 8: dtype = torch.uint8 else: raise NotImplementedError( f'Amount of optimizer bits not supported: {config["optim_bits"]}' ) if p.numel() < config["min_8bit_size"]: dtype = torch.float32 state = self.state[p] state["step"] = 0 if dtype == torch.float32 or ( dtype == torch.uint8 and p.numel() < 4096 ): state["state1"] = torch.zeros_like( p, memory_format=torch.preserve_format, dtype=torch.float32, device=p.device, ) state["state2"] = torch.zeros_like( p, memory_format=torch.preserve_format, dtype=torch.float32, device=p.device, ) elif dtype == torch.uint8: if state["step"] == 0: if "dynamic" not in self.name2qmap: self.fill_qmap() self.name2qmap["dynamic"] = self.name2qmap["dynamic"].to( p.device ) self.name2qmap["udynamic"] = self.name2qmap["udynamic"].to( p.device ) state["state1"] = torch.zeros_like( p, memory_format=torch.preserve_format, dtype=torch.uint8, device=p.device, ) state["qmap1"] = self.name2qmap["dynamic"] state["state2"] = torch.zeros_like( p, memory_format=torch.preserve_format, dtype=torch.uint8, device=p.device, ) state["qmap2"] = self.name2qmap["udynamic"] if config["block_wise"]: n = p.numel() blocks = n // 2048 blocks += 1 if n % 2048 > 0 else 0 state["absmax1"] = torch.zeros( (blocks,), dtype=torch.float32, device=p.device ) state["absmax2"] = torch.zeros( (blocks,), dtype=torch.float32, device=p.device ) else: state["max1"] = torch.zeros( (1,), dtype=torch.float32, device=p.device ) state["new_max1"] = torch.zeros( (1,), dtype=torch.float32, device=p.device ) state["max2"] = torch.zeros( (1,), dtype=torch.float32, device=p.device ) state["new_max2"] = torch.zeros( (1,), dtype=torch.float32, device=p.device ) if config["percentile_clipping"] < 100: state["gnorm_vec"] = torch.zeros((100,), device=p.device) if config["max_unorm"] > 0.0: state["unorm_vec"] = torch.zeros((1,), device=p.device) @torch.no_grad() def update_step(self, group, p, gindex, pindex): state = self.state[p] grad = p.grad config = self.get_config(gindex, pindex, group) state["step"] += 1 step = state["step"] if config["percentile_clipping"] < 100: current_gnorm, clip_value, gnorm_scale = F.percentile_clipping( grad, state["gnorm_vec"], step, config["percentile_clipping"] ) else: gnorm_scale = 1.0 if state["state1"].dtype == torch.float: F.optimizer_update_32bit( self.optimizer_name, grad, p, state["state1"], config["betas"][0], config["eps"], step, config["lr"], state["state2"], config["betas"][1], config["weight_decay"], gnorm_scale, state["unorm_vec"] if config["max_unorm"] > 0.0 else None, max_unorm=config["max_unorm"], skip_zeros=config["skip_zeros"], ) elif state["state1"].dtype == torch.uint8 and not config["block_wise"]: F.optimizer_update_8bit( self.optimizer_name, grad, p, state["state1"], state["state2"], config["betas"][0], config["betas"][1], config["eps"], step, config["lr"], state["qmap1"], state["qmap2"], state["max1"], state["max2"], state["new_max1"], state["new_max2"], config["weight_decay"], gnorm_scale=gnorm_scale, unorm_vec=state["unorm_vec"] if config["max_unorm"] > 0.0 else None, max_unorm=config["max_unorm"], ) # swap maxes state["max1"], state["new_max1"] = state["new_max1"], state["max1"] state["max2"], state["new_max2"] = state["new_max2"], state["max2"] elif state["state1"].dtype == torch.uint8 and config["block_wise"]: F.optimizer_update_8bit_blockwise( self.optimizer_name, grad, p, state["state1"], state["state2"], config["betas"][0], config["betas"][1], config["eps"], step, config["lr"], state["qmap1"], state["qmap2"], state["absmax1"], state["absmax2"], config["weight_decay"], gnorm_scale=gnorm_scale, skip_zeros=config["skip_zeros"], ) class Optimizer1State(Optimizer8bit): def __init__( self, optimizer_name, params, lr=1e-3, betas=(0.9, 0.0), eps=1e-8, weight_decay=0.0, optim_bits=32, args=None, min_8bit_size=4096, percentile_clipping=100, block_wise=True, max_unorm=0.0, skip_zeros=False, ): if not 0.0 <= lr: raise ValueError(f"Invalid learning rate: {lr}") if not 0.0 <= eps: raise ValueError(f"Invalid epsilon value: {eps}") for i in range(len(betas)): if not 0.0 <= betas[i] < 1.0: raise ValueError( f"Invalid beta parameter at index {i}: {betas[i]}" ) if not 0.0 <= weight_decay: raise ValueError( f"Invalid weight_decay value: {weight_decay}" ) defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay) super().__init__(params, defaults, optim_bits) if args is None: args = {} args["optim_bits"] = optim_bits args["percentile_clipping"] = 100 args["min_8bit_size"] = min_8bit_size args["percentile_clipping"] = percentile_clipping args["block_wise"] = block_wise args["max_unorm"] = max_unorm args["skip_zeros"] = skip_zeros self.args = MockArgs(args) else: self.args = args self.optimizer_name = optimizer_name @torch.no_grad() def init_state(self, group, p, gindex, pindex): config = self.get_config(gindex, pindex, group) if config["optim_bits"] == 32: dtype = torch.float32 elif config["optim_bits"] == 8: dtype = torch.uint8 else: raise NotImplementedError( f'Amount of optimizer bits not supported: {config["optim_bits"]}' ) if p.numel() < config["min_8bit_size"]: dtype = torch.float32 state = self.state[p] state["step"] = 0 if dtype == torch.float32 or ( dtype == torch.uint8 and p.numel() < 4096 ): state["state1"] = torch.zeros_like( p, memory_format=torch.preserve_format, dtype=torch.float32, device=p.device, ) elif dtype == torch.uint8: if state["step"] == 0: if "dynamic" not in self.name2qmap: self.fill_qmap() self.name2qmap["dynamic"] = self.name2qmap["dynamic"].to( p.device ) state["state1"] = torch.zeros_like( p, memory_format=torch.preserve_format, dtype=torch.uint8, device=p.device, ) state["qmap1"] = self.name2qmap["dynamic"] if config["block_wise"]: n = p.numel() blocks = n // 2048 blocks += 1 if n % 2048 > 0 else 0 state["absmax1"] = torch.zeros( (blocks,), dtype=torch.float32, device=p.device ) else: state["max1"] = torch.zeros( (1,), dtype=torch.float32, device=p.device ) state["new_max1"] = torch.zeros( (1,), dtype=torch.float32, device=p.device ) if config["percentile_clipping"] < 100: state["gnorm_vec"] = torch.zeros((100,), device=p.device) if config["max_unorm"] > 0.0: state["unorm_vec"] = torch.zeros((1,), device=p.device) @torch.no_grad() def update_step(self, group, p, gindex, pindex): state = self.state[p] grad = p.grad config = self.get_config(gindex, pindex, group) state["step"] += 1 step = state["step"] if config["percentile_clipping"] < 100: current_gnorm, clip_value, gnorm_scale = F.percentile_clipping( grad, state["gnorm_vec"], step, config["percentile_clipping"] ) else: gnorm_scale = 1.0 if state["state1"].dtype == torch.float: F.optimizer_update_32bit( self.optimizer_name, grad, p, state["state1"], config["betas"][0], config["eps"], step, config["lr"], None, 0.0, config["weight_decay"], gnorm_scale, state["unorm_vec"] if config["max_unorm"] > 0.0 else None, max_unorm=config["max_unorm"], skip_zeros=config["skip_zeros"], ) elif state["state1"].dtype == torch.uint8 and not config["block_wise"]: F.optimizer_update_8bit( self.optimizer_name, grad, p, state["state1"], None, config["betas"][0], config["betas"][1], config["eps"], step, config["lr"], state["qmap1"], None, state["max1"], None, state["new_max1"], None, config["weight_decay"], gnorm_scale, state["unorm_vec"] if config["max_unorm"] > 0.0 else None, max_unorm=config["max_unorm"], ) state["max1"], state["new_max1"] = state["new_max1"], state["max1"] elif state["state1"].dtype == torch.uint8 and config["block_wise"]: F.optimizer_update_8bit_blockwise( self.optimizer_name, grad, p, state["state1"], None, config["betas"][0], config["betas"][1], config["eps"], step, config["lr"], state["qmap1"], None, state["absmax1"], None, config["weight_decay"], gnorm_scale=gnorm_scale, skip_zeros=config["skip_zeros"], )
# helper functions def exists(val): return val is not None # freezing of neural networks (teacher needs to be frozen) def set_module_requires_grad_(module, requires_grad): for param in module.parameters(): param.requires_grad = requires_grad def freeze_all_layers_(module): set_module_requires_grad_(module, False) def unfreeze_all_layers_(module): set_module_requires_grad_(module, True) # in the paper # the network attention gates the exteroception, and then sums it to the belief state # todo: make sure the padding is on the right side def sum_with_zeropad(x, y): x_dim, y_dim = x.shape[-1], y.shape[-1] if x_dim == y_dim: return x + y if x_dim < y_dim: x = F.pad(x, (y_dim - x_dim, 0)) if y_dim < x_dim: y = F.pad(y, (x_dim - y_dim, 0)) return x + y # add basic MLP class MLP(nn.Module): def __init__( self, dims, activation = nn.LeakyReLU, final_activation = False ): super().__init__() assert isinstance(dims, (list, tuple)) assert len(dims) > 2, 'must have at least 3 dimensions (input, *hiddens, output)' dim_pairs = list(zip(dims[:-1], dims[1:])) *dim_pairs, dim_out_pair = dim_pairs layers = [] for dim_in, dim_out in dim_pairs: layers.extend([ nn.Linear(dim_in, dim_out), activation() ]) layers.append(nn.Linear(*dim_out_pair)) if final_activation: layers.append(activation()) self.net = nn.Sequential(*layers) def forward(self, x): if isinstance(x, (tuple, list)): x = torch.cat(x, dim = -1) return self.net(x) class Student(nn.Module): def __init__( self, num_actions, proprio_dim = 133, extero_dim = 52, # in paper, height samples was marked as 208, but wasn't sure if that was per leg, or (4 legs x 52) = 208 latent_extero_dim = 24, extero_encoder_hidden = (80, 60), belief_state_encoder_hiddens = (64, 64), extero_gate_encoder_hiddens = (64, 64), belief_state_dim = 120, # should be equal to teacher's extero_dim + privileged_dim (part of the GRU's responsibility is to maintain a hidden state that forms an opinion on the privileged information) gru_num_layers = 2, gru_hidden_size = 50, mlp_hidden = (256, 160, 128), num_legs = 4, privileged_dim = 50, privileged_decoder_hiddens = (64, 64), extero_decoder_hiddens = (64, 64), ): super().__init__() assert belief_state_dim > (num_legs * latent_extero_dim) self.num_legs = num_legs self.proprio_dim = proprio_dim self.extero_dim = extero_dim # encoding of exteroception self.extero_encoder = MLP((extero_dim, *extero_encoder_hidden, latent_extero_dim)) # GRU related parameters gru_input_dim = (latent_extero_dim * num_legs) + proprio_dim gru_input_dims = (gru_input_dim, *((gru_hidden_size,) * (gru_num_layers - 1))) self.gru_cells = nn.ModuleList([GRUCell(input_dim, gru_hidden_size) for input_dim in gru_input_dims]) self.gru_hidden_size = gru_hidden_size # belief state encoding self.belief_state_encoder = MLP((gru_hidden_size, *belief_state_encoder_hiddens, belief_state_dim)) # attention gating of exteroception self.to_latent_extero_attn_gate = MLP((gru_hidden_size, *extero_gate_encoder_hiddens, latent_extero_dim * num_legs)) # belief state decoder self.privileged_decoder = MLP((gru_hidden_size, *privileged_decoder_hiddens, privileged_dim)) self.extero_decoder = MLP((gru_hidden_size, *extero_decoder_hiddens, extero_dim * num_legs)) self.to_extero_attn_gate = MLP((gru_hidden_size, *extero_gate_encoder_hiddens, extero_dim * num_legs)) # final MLP to action logits self.to_logits = MLP(( belief_state_dim + proprio_dim, *mlp_hidden )) self.to_action_head = nn.Sequential( nn.LeakyReLU(), nn.Linear(mlp_hidden[-1], num_actions) ) def get_gru_hiddens(self): device = next(self.parameters()).device return torch.zeros((len(self.gru_cells), self.gru_hidden_size)) def forward( self, proprio, extero, hiddens = None, return_estimated_info = False, # for returning estimated privileged info + exterceptive info, for reconstruction loss return_action_categorical_dist = False ): check_shape(proprio, 'b d', d = self.proprio_dim) check_shape(extero, 'b n d', n = self.num_legs, d = self.extero_dim) latent_extero = self.extero_encoder(extero) latent_extero = rearrange(latent_extero, 'b ... -> b (...)') # RNN if not exists(hiddens): prev_hiddens = (None,) * len(self.gru_cells) else: prev_hiddens = hiddens.unbind(dim = -2) gru_input = torch.cat((proprio, latent_extero), dim = -1) next_hiddens = [] for gru_cell, prev_hidden in zip(self.gru_cells, prev_hiddens): gru_input = gru_cell(gru_input, prev_hidden) next_hiddens.append(gru_input) gru_output = gru_input next_hiddens = torch.stack(next_hiddens, dim = -2) # attention gating of exteroception latent_extero_attn_gate = self.to_latent_extero_attn_gate(gru_output) gated_latent_extero = latent_extero * latent_extero_attn_gate.sigmoid() # belief state and add gated exteroception belief_state = self.belief_state_encoder(gru_output) belief_state = sum_with_zeropad(belief_state, gated_latent_extero) # to action logits belief_state_with_proprio = torch.cat(( proprio, belief_state, ), dim = 1) logits = self.to_logits(belief_state_with_proprio) pi_logits = self.to_action_head(logits) return_action = Categorical(pi_logits.softmax(dim = -1)) if return_action_categorical_dist else pi_logits if not return_estimated_info: return return_action, next_hiddens # belief state decoding # for reconstructing privileged and exteroception information from hidden belief states recon_privileged = self.privileged_decoder(gru_output) recon_extero = self.extero_decoder(gru_output) extero_attn_gate = self.to_extero_attn_gate(gru_output) gated_extero = rearrange(extero, 'b ... -> b (...)') * extero_attn_gate.sigmoid() recon_extero = recon_extero + gated_extero recon_extero = rearrange(recon_extero, 'b (n d) -> b n d', n = self.num_legs) # whether to return raw policy logits or action probs wrapped with Categorical return return_action, next_hiddens, (recon_privileged, recon_extero) class Teacher(nn.Module): def __init__( self, num_actions, proprio_dim = 133, extero_dim = 52, # in paper, height samples was marked as 208, but wasn't sure if that was per leg, or (4 legs x 52) = 208 latent_extero_dim = 24, extero_encoder_hidden = (80, 60), privileged_dim = 50, latent_privileged_dim = 24, privileged_encoder_hidden = (64, 32), mlp_hidden = (256, 160, 128), num_legs = 4 ): super().__init__() self.num_legs = num_legs self.proprio_dim = proprio_dim self.extero_dim = extero_dim self.privileged_dim = privileged_dim self.extero_encoder = MLP((extero_dim, *extero_encoder_hidden, latent_extero_dim)) self.privileged_encoder = MLP((privileged_dim, *privileged_encoder_hidden, latent_privileged_dim)) self.to_logits = MLP(( latent_extero_dim * num_legs + latent_privileged_dim + proprio_dim, *mlp_hidden )) self.to_action_head = nn.Sequential( nn.LeakyReLU(), nn.Linear(mlp_hidden[-1], num_actions) ) self.to_value_head = nn.Sequential( nn.LeakyReLU(), nn.Linear(mlp_hidden[-1], 1), Rearrange('... 1 -> ...') ) def forward( self, proprio, extero, privileged, return_value_head = False, return_action_categorical_dist = False ): check_shape(proprio, 'b d', d = self.proprio_dim) check_shape(extero, 'b n d', n = self.num_legs, d = self.extero_dim) check_shape(privileged, 'b d', d = self.privileged_dim) latent_extero = self.extero_encoder(extero) latent_extero = rearrange(latent_extero, 'b ... -> b (...)') latent_privileged = self.privileged_encoder(privileged) latent = torch.cat(( proprio, latent_extero, latent_privileged, ), dim = -1) logits = self.to_logits(latent) pi_logits = self.to_action_head(logits) if not return_value_head: return pi_logits value_logits = self.to_value_head(logits) return_action = Categorical(pi_logits.softmax(dim = -1)) if return_action_categorical_dist else pi_logits return return_action, value_logits # manages both teacher and student under one module class Anymal(nn.Module): def __init__( self, num_actions, proprio_dim = 133, extero_dim = 52, privileged_dim = 50, num_legs = 4, latent_extero_dim = 24, latent_privileged_dim = 24, teacher_extero_encoder_hidden = (80, 60), teacher_privileged_encoder_hidden = (64, 32), student_extero_gate_encoder_hiddens = (64, 64), student_belief_state_encoder_hiddens = (64, 64), student_belief_state_dim = 120, student_gru_num_layers = 2, student_gru_hidden_size = 50, student_privileged_decoder_hiddens = (64, 64), student_extero_decoder_hiddens = (64, 64), student_extero_encoder_hidden = (80, 60), mlp_hidden = (256, 160, 128), recon_loss_weight = 0.5 ): super().__init__() self.proprio_dim = proprio_dim self.num_legs = num_legs self.extero_dim = extero_dim self.student = Student( num_actions = num_actions, proprio_dim = proprio_dim, extero_dim = extero_dim, latent_extero_dim = latent_extero_dim, extero_encoder_hidden = student_extero_encoder_hidden, belief_state_encoder_hiddens = student_belief_state_encoder_hiddens, extero_gate_encoder_hiddens = student_extero_gate_encoder_hiddens, belief_state_dim = student_belief_state_dim, gru_num_layers = student_gru_num_layers, gru_hidden_size = student_gru_hidden_size, mlp_hidden = mlp_hidden, num_legs = num_legs, privileged_dim = privileged_dim, privileged_decoder_hiddens = student_privileged_decoder_hiddens, extero_decoder_hiddens = student_extero_decoder_hiddens, ) self.teacher = Teacher( num_actions = num_actions, proprio_dim = proprio_dim, extero_dim = extero_dim, latent_extero_dim = latent_extero_dim, extero_encoder_hidden = teacher_extero_encoder_hidden, privileged_dim = privileged_dim, latent_privileged_dim = latent_privileged_dim, privileged_encoder_hidden = teacher_privileged_encoder_hidden, mlp_hidden = mlp_hidden, num_legs = num_legs ) self.recon_loss_weight = recon_loss_weight def get_observation_running_stats(self): return RunningStats(self.proprio_dim), RunningStats((self.num_legs, self.extero_dim)) def init_student_with_teacher(self): self.student.extero_encoder.load_state_dict(self.teacher.extero_encoder.state_dict()) self.student.to_logits.load_state_dict(self.teacher.to_logits.state_dict()) self.student.to_action_head.load_state_dict(self.teacher.to_action_head.state_dict()) def forward_teacher(self, *args, return_value_head = False, **kwargs): return self.teacher(*args, return_value_head = return_value_head, **kwargs) def forward_student(self, *args, **kwargs): return self.student(*args, **kwargs) # main forward for training the student with teacher as guide def forward( self, proprio, extero, privileged, teacher_states = None, hiddens = None, noise_strength = 0.1 ): self.teacher.eval() freeze_all_layers_(self.teacher) with torch.no_grad(): teacher_proprio, teacher_extero = teacher_states if exists(teacher_states) else (proprio, extero) teacher_action_logits = self.forward_teacher(teacher_proprio, teacher_extero, privileged) noised_extero = extero + torch.rand_like(extero) * noise_strength student_action_logits, hiddens, recons = self.student(proprio, noised_extero, hiddens = hiddens, return_estimated_info = True) # calculate reconstruction loss of privileged and denoised exteroception (recon_privileged, recon_extero) = recons recon_loss = F.mse_loss(recon_privileged, privileged) + F.mse_loss(recon_extero, extero) # calculate behavior loss, which is also squared distance? behavior_loss = F.mse_loss(teacher_action_logits, student_action_logits) # why not kl div on action probs? loss = behavior_loss + recon_loss * self.recon_loss_weight return loss, hiddens
class ExperienceDataset(Dataset): def __init__(self, data): super().__init__() self.data = data def __len__(self): return len(self.data[0]) def __getitem__(self, ind): return tuple(map(lambda t: t[ind], self.data)) def create_dataloader(data, batch_size): ds = ExperienceDataset(data) return DataLoader(ds, batch_size = batch_size, drop_last = True) class StudentTrainer(nn.Module): def __init__( self, *, anymal, env, epochs = 2, lr = 5e-4, max_timesteps = 10000, update_timesteps = 5000, minibatch_size = 16, truncate_tpbtt = 10 ): super().__init__() self.env = env self.anymal = anymal self.optimizer = Adam(anymal.student.parameters(), lr = lr) self.epochs = epochs self.max_timesteps = max_timesteps self.update_timesteps = update_timesteps self.minibatch_size = minibatch_size self.truncate_tpbtt = truncate_tpbtt self.running_proprio, self.running_extero = anymal.get_observation_running_stats() def learn_from_memories( self, memories, next_states, noise_strength = 0. ): device = next(self.parameters()).device # retrieve and prepare data from memory for training states = [] teacher_states = [] hiddens = [] dones = [] for (state, teacher_state, hidden, done) in memories: states.append(state) teacher_states.append(teacher_state) hiddens.append(hidden) dones.append(torch.Tensor([done])) states = tuple(zip(*states)) teacher_states = tuple(zip(*teacher_states)) # convert values to torch tensors to_torch_tensor = lambda t: torch.stack(t).to(device).detach() states = map(to_torch_tensor, states) teacher_states = map(to_torch_tensor, teacher_states) hiddens = to_torch_tensor(hiddens) dones = to_torch_tensor(dones) # prepare dataloader for policy phase training dl = create_dataloader([*states, *teacher_states, hiddens, dones], self.minibatch_size) current_hiddens = self.anymal.student.get_gru_hiddens() current_hiddens = rearrange(current_hiddens, 'l d -> 1 l d') for _ in range(self.epochs): for ind, (proprio, extero, privileged, teacher_proprio, teacher_extero, episode_hiddens, done) in enumerate(dl): straight_through_hiddens = current_hiddens - current_hiddens.detach() + episode_hiddens loss, current_hiddens = self.anymal( proprio, extero, privileged, teacher_states = (teacher_proprio, teacher_extero), hiddens = straight_through_hiddens, noise_strength = noise_strength ) loss.backward(retain_graph = True) tbptt_limit = not ((ind + 1) % self.truncate_tpbtt) if tbptt_limit: # how far back in time should the gradients go for recurrence self.optimizer.step() self.optimizer.zero_grad() current_hiddens = current_hiddens.detach() # detacher hiddens depending on whether it is a new episode or not # todo: restructure dataloader to load one episode per batch rows maybe_detached_hiddens = [] for current_hidden, done in zip(current_hiddens.unbind(dim = 0), dones.unbind(dim = 0)): maybe_detached_hiddens.append(current_hidden.detached() if done else current_hidden) current_hiddens = torch.stack(maybe_detached_hiddens) def forward( self, noise_strength = 0. ): device = next(self.parameters()).device time = 0 done = False states = self.env.reset() memories = deque([]) hidden = self.anymal.student.get_gru_hiddens() hidden = rearrange(hidden, 'l d -> 1 l d') self.running_proprio.clear() self.running_extero.clear() for timestep in range(self.max_timesteps): time += 1 states = list(map(lambda t: t.to(device), states)) anymal_states = list(map(lambda t: rearrange(t, '... -> 1 ...'), states)) # teacher needs to have normalized observations (proprio, extero, privileged) = states self.running_proprio.push(proprio) self.running_extero.push(extero) teacher_states = ( self.running_proprio.norm(proprio), self.running_extero.norm(extero) ) teacher_anymal_states = list(map(lambda t: rearrange(t, '... -> 1 ...'), teacher_states)) # add states to memories memories.append(( states, teacher_states, rearrange(hidden, '1 ... -> ...'), done )) dist, hidden = self.anymal.forward_student( *anymal_states[:-1], hiddens = hidden, return_action_categorical_dist = True ) action = dist.sample() action_log_prob = dist.log_prob(action) action = action.item() next_states, _, done, _ = self.env.step(action) states = next_states if time % self.update_timesteps == 0: self.learn_from_memories(memories, next_states, noise_strength = noise_strength) memories.clear() if done: break
# they use basic PPO for training the teacher with privileged information # then they used noisy student training, using the trained "oracle" teacher as guide # ppo data Memory = namedtuple('Memory', ['state', 'action', 'action_log_prob', 'reward', 'done', 'value']) class ExperienceDataset(Dataset): def __init__(self, data): super().__init__() self.data = data def __len__(self): return len(self.data[0]) def __getitem__(self, ind): return tuple(map(lambda t: t[ind], self.data)) def create_shuffled_dataloader(data, batch_size): ds = ExperienceDataset(data) return DataLoader(ds, batch_size = batch_size, shuffle = True) # ppo helper functions def normalize(t, eps = 1e-5): return (t - t.mean()) / (t.std() + eps) def clipped_value_loss(values, rewards, old_values, clip): value_clipped = old_values + (values - old_values).clamp(-clip, clip) value_loss_1 = (value_clipped.flatten() - rewards) ** 2 value_loss_2 = (values.flatten() - rewards) ** 2 return torch.mean(torch.max(value_loss_1, value_loss_2)) # mock environment class MockEnv(object): def __init__( self, proprio_dim, extero_dim, privileged_dim, num_legs = 4 ): self.proprio_dim = proprio_dim self.extero_dim = extero_dim self.privileged_dim = privileged_dim self.num_legs = num_legs def rand_state(self): return ( torch.randn((self.proprio_dim,)), torch.randn((self.num_legs, self.extero_dim,)), torch.randn((self.privileged_dim,)) ) def reset(self): return self.rand_state() def step(self, action): reward = torch.randn((1,)) done = torch.tensor([False]) return self.rand_state(), reward, done, None # main ppo class class PPO(nn.Module): def __init__( self, *, env, anymal, epochs = 2, lr = 5e-4, betas = (0.9, 0.999), eps_clip = 0.2, beta_s = 0.005, value_clip = 0.4, max_timesteps = 10000, update_timesteps = 5000, lam = 0.95, gamma = 0.99, minibatch_size = 8300 ): super().__init__() assert isinstance(anymal, Anymal) self.env = env self.anymal = anymal self.minibatch_size = minibatch_size self.optimizer = Adam(anymal.teacher.parameters(), lr = lr, betas = betas) self.epochs = epochs self.max_timesteps = max_timesteps self.update_timesteps = update_timesteps self.beta_s = beta_s self.eps_clip = eps_clip self.value_clip = value_clip self.lam = lam self.gamma = gamma # in paper, they said observations fed to teacher were normalized # by running mean self.running_proprio, self.running_extero = anymal.get_observation_running_stats() def learn_from_memories( self, memories, next_states ): device = next(self.parameters()).device # retrieve and prepare data from memory for training states = [] actions = [] old_log_probs = [] rewards = [] masks = [] values = [] for mem in memories: states.append(mem.state) actions.append(torch.tensor(mem.action)) old_log_probs.append(mem.action_log_prob) rewards.append(mem.reward) masks.append(1 - float(mem.done)) values.append(mem.value) states = tuple(zip(*states)) # calculate generalized advantage estimate next_states = map(lambda t: t.to(device), next_states) next_states = map(lambda t: rearrange(t, '... -> 1 ...'), next_states) _, next_value = self.anymal.forward_teacher(*next_states, return_value_head = True) next_value = next_value.detach() values = values + [next_value] returns = [] gae = 0 for i in reversed(range(len(rewards))): delta = rewards[i] + self.gamma * values[i + 1] * masks[i] - values[i] gae = delta + self.gamma * self.lam * masks[i] * gae returns.insert(0, gae + values[i]) # convert values to torch tensors to_torch_tensor = lambda t: torch.stack(t).to(device).detach() states = map(to_torch_tensor, states) actions = to_torch_tensor(actions) old_log_probs = to_torch_tensor(old_log_probs) old_values = to_torch_tensor(values[:-1]) old_values = rearrange(old_values, '... 1 -> ...') rewards = torch.tensor(returns).float().to(device) # prepare dataloader for policy phase training dl = create_shuffled_dataloader([*states, actions, old_log_probs, rewards, old_values], self.minibatch_size) # policy phase training, similar to original PPO for _ in range(self.epochs): for proprio, extero, privileged, actions, old_log_probs, rewards, old_values in dl: dist, values = self.anymal.forward_teacher( proprio, extero, privileged, return_value_head = True, return_action_categorical_dist = True ) action_log_probs = dist.log_prob(actions) entropy = dist.entropy() ratios = (action_log_probs - old_log_probs).exp() advantages = normalize(rewards - old_values.detach()) surr1 = ratios * advantages surr2 = ratios.clamp(1 - self.eps_clip, 1 + self.eps_clip) * advantages policy_loss = - torch.min(surr1, surr2) - self.beta_s * entropy value_loss = clipped_value_loss(values, rewards, old_values, self.value_clip) (policy_loss.mean() + value_loss.mean()).backward() self.optimizer.step() self.optimizer.zero_grad() # does one episodes worth of learning def forward(self): device = next(self.parameters()).device unfreeze_all_layers_(self.anymal) time = 0 states = self.env.reset() # states assumed to be (proprioception, exteroception, privileged information) memories = deque([]) self.running_proprio.clear() self.running_extero.clear() for timestep in range(self.max_timesteps): time += 1 states = list(map(lambda t: t.to(device), states)) proprio, extero, privileged = states # update running means for observations, for teacher self.running_proprio.push(proprio) self.running_extero.push(extero) # normalize observation states for teacher (proprio and extero) states = ( self.running_proprio.norm(proprio), self.running_extero.norm(extero), privileged ) anymal_states = list(map(lambda t: rearrange(t, '... -> 1 ...'), states)) dist, values = self.anymal.forward_teacher( *anymal_states, return_value_head = True, return_action_categorical_dist = True ) action = dist.sample() action_log_prob = dist.log_prob(action) action = action.item() next_states, reward, done, _ = self.env.step(action) memory = Memory(states, action, action_log_prob, reward, done, values) memories.append(memory) states = next_states if time % self.update_timesteps == 0: self.learn_from_memories(memories, next_states) memories.clear() if done: break print('trained for 1 episode')
class RunningStats(nn.Module): def __init__(self, shape, eps = 1e-5): super().__init__() shape = shape if isinstance(shape, tuple) else (shape,) self.shape = shape self.eps = eps self.n = 0 self.register_buffer('old_mean', torch.zeros(shape), persistent = False) self.register_buffer('new_mean', torch.zeros(shape), persistent = False) self.register_buffer('old_std', torch.zeros(shape), persistent = False) self.register_buffer('new_std', torch.zeros(shape), persistent = False) def clear(self): self.n = 0 def push(self, x): self.n += 1 if self.n == 1: self.old_mean.copy_(x.data) self.new_mean.copy_(x.data) self.old_std.zero_() self.new_std.zero_() return self.new_mean.copy_(self.old_mean + (x - self.old_mean) / self.n) self.new_std.copy_(self.old_std + (x - self.old_mean) * (x - self.new_mean)) self.old_mean.copy_(self.new_mean) self.old_std.copy_(self.new_std) def mean(self): return self.new_mean if self.n else torch.zeros_like(self.new_mean) def variance(self): return (self.new_std / (self.n - 1)) if self.n > 1 else torch.zeros_like(self.new_std) def rstd(self): return torch.rsqrt(self.variance() + self.eps) def norm(self, x): return (x - self.mean()) * self.rstd()
# translated from tensorflow code # https://gist.github.com/aravindsrinivas/56359b79f0ce4449bcb04ab4b56a57a2 # positional embedding helpers def pair(x): return (x, x) if not isinstance(x, tuple) else x def expand_dim(t, dim, k): t = t.unsqueeze(dim = dim) expand_shape = [-1] * len(t.shape) expand_shape[dim] = k return t.expand(*expand_shape) def rel_to_abs(x): b, h, l, _, device, dtype = *x.shape, x.device, x.dtype dd = {'device': device, 'dtype': dtype} col_pad = torch.zeros((b, h, l, 1), **dd) x = torch.cat((x, col_pad), dim = 3) flat_x = rearrange(x, 'b h l c -> b h (l c)') flat_pad = torch.zeros((b, h, l - 1), **dd) flat_x_padded = torch.cat((flat_x, flat_pad), dim = 2) final_x = flat_x_padded.reshape(b, h, l + 1, 2 * l - 1) final_x = final_x[:, :, :l, (l-1):] return final_x def relative_logits_1d(q, rel_k): b, heads, h, w, dim = q.shape logits = einsum('b h x y d, r d -> b h x y r', q, rel_k) logits = rearrange(logits, 'b h x y r -> b (h x) y r') logits = rel_to_abs(logits) logits = logits.reshape(b, heads, h, w, w) logits = expand_dim(logits, dim = 3, k = h) return logits # positional embeddings class AbsPosEmb(nn.Module): def __init__( self, fmap_size, dim_head ): super().__init__() height, width = pair(fmap_size) scale = dim_head ** -0.5 self.height = nn.Parameter(torch.randn(height, dim_head) * scale) self.width = nn.Parameter(torch.randn(width, dim_head) * scale) def forward(self, q): emb = rearrange(self.height, 'h d -> h () d') + rearrange(self.width, 'w d -> () w d') emb = rearrange(emb, ' h w d -> (h w) d') logits = einsum('b h i d, j d -> b h i j', q, emb) return logits class RelPosEmb(nn.Module): def __init__( self, fmap_size, dim_head ): super().__init__() height, width = pair(fmap_size) scale = dim_head ** -0.5 self.fmap_size = fmap_size self.rel_height = nn.Parameter(torch.randn(height * 2 - 1, dim_head) * scale) self.rel_width = nn.Parameter(torch.randn(width * 2 - 1, dim_head) * scale) def forward(self, q): h, w = self.fmap_size q = rearrange(q, 'b h (x y) d -> b h x y d', x = h, y = w) rel_logits_w = relative_logits_1d(q, self.rel_width) rel_logits_w = rearrange(rel_logits_w, 'b h x i y j-> b h (x y) (i j)') q = rearrange(q, 'b h x y d -> b h y x d') rel_logits_h = relative_logits_1d(q, self.rel_height) rel_logits_h = rearrange(rel_logits_h, 'b h x i y j -> b h (y x) (j i)') return rel_logits_w + rel_logits_h # classes class Attention(nn.Module): def __init__( self, *, dim, fmap_size, heads = 4, dim_head = 128, rel_pos_emb = False ): super().__init__() self.heads = heads self.scale = dim_head ** -0.5 inner_dim = heads * dim_head self.to_qkv = nn.Conv2d(dim, inner_dim * 3, 1, bias = False) rel_pos_class = AbsPosEmb if not rel_pos_emb else RelPosEmb self.pos_emb = rel_pos_class(fmap_size, dim_head) def forward(self, fmap): heads, b, c, h, w = self.heads, *fmap.shape q, k, v = self.to_qkv(fmap).chunk(3, dim = 1) q, k, v = map(lambda t: rearrange(t, 'b (h d) x y -> b h (x y) d', h = heads), (q, k, v)) q = q * self.scale sim = einsum('b h i d, b h j d -> b h i j', q, k) sim = sim + self.pos_emb(q) attn = sim.softmax(dim = -1) out = einsum('b h i j, b h j d -> b h i d', attn, v) out = rearrange(out, 'b h (x y) d -> b (h d) x y', x = h, y = w) return out class BottleBlock(nn.Module): def __init__( self, *, dim, fmap_size, dim_out, proj_factor, downsample, heads = 4, dim_head = 128, rel_pos_emb = False, activation = nn.ReLU() ): super().__init__() # shortcut if dim != dim_out or downsample: kernel_size, stride, padding = (3, 2, 1) if downsample else (1, 1, 0) self.shortcut = nn.Sequential( nn.Conv2d(dim, dim_out, kernel_size, stride = stride, padding = padding, bias = False), nn.BatchNorm2d(dim_out), activation ) else: self.shortcut = nn.Identity() # contraction and expansion attn_dim_in = dim_out // proj_factor attn_dim_out = heads * dim_head self.net = nn.Sequential( nn.Conv2d(dim, attn_dim_in, 1, bias = False), nn.BatchNorm2d(attn_dim_in), activation, Attention( dim = attn_dim_in, fmap_size = fmap_size, heads = heads, dim_head = dim_head, rel_pos_emb = rel_pos_emb ), nn.AvgPool2d((2, 2)) if downsample else nn.Identity(), nn.BatchNorm2d(attn_dim_out), activation, nn.Conv2d(attn_dim_out, dim_out, 1, bias = False), nn.BatchNorm2d(dim_out) ) # init last batch norm gamma to zero nn.init.zeros_(self.net[-1].weight) # final activation self.activation = activation def forward(self, x): shortcut = self.shortcut(x) x = self.net(x) x = x + shortcut return self.activation(x) # main bottle stack class BottleStack(nn.Module): def __init__( self, *, dim, fmap_size, dim_out = 2048, proj_factor = 4, num_layers = 3, heads = 4, dim_head = 128, downsample = True, rel_pos_emb = False, activation = nn.ReLU() ): super().__init__() fmap_size = pair(fmap_size) self.dim = dim self.fmap_size = fmap_size layers = [] for i in range(num_layers): is_first = i == 0 dim = (dim if is_first else dim_out) layer_downsample = is_first and downsample fmap_divisor = (2 if downsample and not is_first else 1) layer_fmap_size = tuple(map(lambda t: t // fmap_divisor, fmap_size)) layers.append(BottleBlock( dim = dim, fmap_size = layer_fmap_size, dim_out = dim_out, proj_factor = proj_factor, heads = heads, dim_head = dim_head, downsample = layer_downsample, rel_pos_emb = rel_pos_emb, activation = activation )) self.net = nn.Sequential(*layers) def forward(self, x): _, c, h, w = x.shape assert c == self.dim, f'channels of feature map {c} must match channels given at init {self.dim}' assert h == self.fmap_size[0] and w == self.fmap_size[1], f'height and width ({h} {w}) of feature map must match the fmap_size given at init {self.fmap_size}' return self.net(x)
# constants NUM_BATCHES = int(1e5) BATCH_SIZE = 4 GRADIENT_ACCUMULATE_EVERY = 4 LEARNING_RATE = 1e-4 VALIDATE_EVERY = 100 PRIME_LENGTH = 128 GENERATE_EVERY = 250 GENERATE_LENGTH = 2048 SEQ_LEN = 2048 # helpers def cycle(loader): while True: for data in loader: yield data def decode_token(token): return str(chr(max(32, token))) def decode_tokens(tokens): return "".join(list(map(decode_token, tokens))) # accelerator accelerator = Accelerator() device = accelerator.device acc_print = accelerator.print # instantiate palm model = BlockRecurrentTransformer( num_tokens = 256, dim = 512, depth = 6, dim_head = 64, heads = 8, max_seq_len = 1024, block_width = 512, num_state_vectors = 512, recurrent_layers = (4,), use_flash_attn = True ) train_wrapper = RecurrentTrainerWrapper( model, xl_memories_dropout = 0.1, state_dropout = 0.1, ) model.to(device) # prepare enwik8 data with gzip.open("./data/enwik8.gz") as file: data = np.frombuffer(file.read(int(95e6)), dtype=np.uint8).copy() np_train, np_valid = np.split(data, [int(90e6)]) data_train, data_val = torch.from_numpy(np_train), torch.from_numpy(np_valid) class TextSamplerDataset(Dataset): def __init__(self, data, seq_len): super().__init__() self.data = data self.seq_len = seq_len def __getitem__(self, index): rand_start = torch.randint(0, self.data.size(0) - self.seq_len, (1,)) full_seq = self.data[rand_start : rand_start + self.seq_len + 1].long() return full_seq.to(device) def __len__(self): return self.data.size(0) // self.seq_len train_dataset = TextSamplerDataset(data_train, SEQ_LEN) val_dataset = TextSamplerDataset(data_val, SEQ_LEN) train_loader = cycle(DataLoader(train_dataset, batch_size=BATCH_SIZE)) val_loader = cycle(DataLoader(val_dataset, batch_size=BATCH_SIZE)) # optimizer optim = Adam(model.parameters(), lr = LEARNING_RATE) model, optim, train_loader, val_loader = accelerator.prepare( model, optim, train_loader, val_loader ) # training for i in tqdm.tqdm(range(NUM_BATCHES), mininterval=10.0, desc="training"): model.train() for _ in range(GRADIENT_ACCUMULATE_EVERY): loss = train_wrapper(next(train_loader)) accelerator.backward(loss / GRADIENT_ACCUMULATE_EVERY) acc_print(f"training loss: {loss.item()}") accelerator.clip_grad_norm_(model.parameters(), 0.5) optim.step() optim.zero_grad() if i % VALIDATE_EVERY == 0: model.eval() with torch.no_grad(): loss = train_wrapper(next(val_loader)) acc_print(f"validation loss: {loss.item()}") if i % GENERATE_EVERY == 0: model.eval() inp = random.choice(val_dataset)[:PRIME_LENGTH] prime = decode_tokens(inp) acc_print(f"%s \n\n %s", (prime, "*" * 100)) sample = train_wrapper.generate(inp[None, ...], length = GENERATE_LENGTH) output_str = decode_tokens(sample[0]) acc_print(output_str, "\n")
if version.parse(torch.__version__) >= version.parse('2.0.0'): from einops._torch_specific import allow_ops_in_compiled_graph allow_ops_in_compiled_graph()
# helpers def exists(val): return val is not None def default(val, d): return val if exists(val) else d def is_empty(t: torch.Tensor): return t.numel() == 0 def cast_tuple(t, length = 1): return t if isinstance(t, tuple) else ((t,) * length) def all_unique(arr): return len(arr) == len(set(arr)) def eval_decorator(fn): def inner(self, *args, **kwargs): was_training = self.training self.eval() out = fn(self, *args, **kwargs) self.train(was_training) return out return inner def once(fn): called = False @wraps(fn) def inner(x): nonlocal called if called: return called = True return fn(x) return inner print_once = once(print) def compact(arr): return [*filter(exists, arr)] def and_reduce(arr: List[torch.Tensor]): if len(arr) == 0: return None head, *rest = arr for t in rest: head = head & t return head def safe_cat(*args, dim = 1): args = compact(args) if len(args) == 0: return None return torch.cat(args, dim = dim) def divisible_by(numer, denom): return (numer % denom) == 0 def l2norm(t): return F.normalize(t, dim = -1) def pack_one(t, pattern): return pack([t], pattern) def unpack_one(t, ps, pattern): return unpack(t, ps, pattern)[0] def pad_at_dim(t, pad, dim = -1, value = 0.): dims_from_right = (- dim - 1) if dim < 0 else (t.ndim - dim - 1) zeros = ((0, 0) * dims_from_right) return F.pad(t, (*zeros, *pad), value = value) # bias-less layernorm class LayerNorm(nn.Module): def __init__(self, dim): super().__init__() self.gamma = nn.Parameter(torch.ones(dim)) self.register_buffer("beta", torch.zeros(dim)) def forward(self, x): return F.layer_norm(x, x.shape[-1:], self.gamma, self.beta) # sampling helpers def log(t, eps = 1e-20): return torch.log(t.clamp(min = eps)) def gumbel_noise(t): noise = torch.zeros_like(t).uniform_(0, 1) return -log(-log(noise)) def gumbel_sample(t, temperature = 1., dim = -1): return ((t / max(temperature, 1e-10)) + gumbel_noise(t)).argmax(dim = dim) def top_k(logits, thres = 0.9): k = math.ceil((1 - thres) * logits.shape[-1]) val, ind = torch.topk(logits, k) probs = torch.full_like(logits, float('-inf')) probs.scatter_(1, ind, val) return probs # rotary positional embedding w/ xpos # https://arxiv.org/abs/2104.09864 # https://arxiv.org/abs/2212.10554v1 class RotaryEmbedding(nn.Module): def __init__( self, dim, width, scale_base = 512, theta = 10000 ): super().__init__() self.width = width inv_freq = 1.0 / (theta ** (torch.arange(0, dim, 2).float() / dim)) self.register_buffer("inv_freq", inv_freq, persistent = False) self.scale_base = scale_base scale = (torch.arange(0, dim, 2) + 0.4 * dim) / (1.4 * dim) self.register_buffer('scale', scale, persistent = False) self.register_buffer('cached_freqs', None, persistent = False) self.register_buffer('cached_scales', None, persistent = False) @property def device(self): return next(self.buffers()).device def forward(self): device, seq_len = self.device, self.width if exists(self.cached_freqs): cached_seq_len = self.cached_freqs.shape[-2] if cached_seq_len >= seq_len: return self.cached_freqs[:seq_len], self.cached_scales[:seq_len] t = torch.arange(seq_len, device = device).type_as(self.inv_freq) freqs = torch.einsum('i , j -> i j', t, self.inv_freq) freqs = torch.cat((freqs, freqs), dim = -1) power = (t - (seq_len // 2)) / self.scale_base scale = self.scale ** rearrange(power, 'n -> n 1') scale = torch.cat((scale, scale), dim = -1) self.register_buffer('cached_freqs', freqs, persistent = False) self.register_buffer('cached_scales', scale, persistent = False) return freqs, scale def rotate_half(x): x1, x2 = x.chunk(2, dim=-1) return torch.cat((-x2, x1), dim=-1) def apply_rotary_pos_emb(t, pos, scale = 1.): scale = default(scale, 1.) seq_len = t.shape[-2] assert pos.shape[-2] >= seq_len pos = pos[-seq_len:] if isinstance(scale, torch.Tensor): assert scale.shape[-2] >= seq_len scale = scale[-seq_len:] return (t * pos.cos() * scale) + (rotate_half(t) * pos.sin() * scale) # memory management class MemoryManager(nn.Module): def __init__( self, dim, *, layers = 1, mem_lengths = 512, compress_factors = 1 ): super().__init__() mem_lengths = cast_tuple(mem_lengths) compress_factors = cast_tuple(compress_factors) assert all([mem_length > 0 for mem_length in mem_lengths]) assert len(mem_lengths) == len(compress_factors) assert layers >= 1 self.mem_lengths = mem_lengths self.compress_factors = compress_factors self.layers = nn.ModuleList([]) for _ in range(layers): compress_fns = nn.ModuleList([]) for compress_factor in compress_factors: compress_fn = nn.Identity() if compress_factor > 1: compress_fn = nn.Sequential( Rearrange('b n d -> b d n'), nn.Conv1d( dim * 2, dim * 2, compress_factor, stride = compress_factor, groups = 2 ), Rearrange('b d n -> b n d'), ) compress_fns.append(compress_fn) self.layers.append(compress_fns) def forward( self, past_memories: List[torch.Tensor], new_memories: List[torch.Tensor] ): next_memories = [] for past_memory, new_memory, compress_fns in zip_longest(past_memories, new_memories, self.layers): # edge case if neither memories exist if not (exists(past_memory) or exists(new_memory)): next_memories.append(None) continue next_memory = None for mem_length, compress_factor, compress_fn in zip(self.mem_lengths, self.compress_factors, compress_fns): # first get the memories for the given compression factor "current_memory" current_memory = None if exists(past_memory): past_memory, current_memory = past_memory[..., :-mem_length, :], past_memory[..., -mem_length:, :] # compress the new memories coming in, based on the compression factors set at init if (not is_empty(new_memory)) and compress_factor > 1: # make sure memory length is divisible by compression factor new_mem_length = new_memory.shape[-2] curtailed_length = (new_mem_length // compress_factor) * compress_factor curtailed_slice = slice(-curtailed_length, None) if curtailed_length > 0 else slice(0, 0) new_memory = new_memory[..., curtailed_slice, :] # compress the memory pushed to the next stage if new_memory.shape[-2] > 0: new_memory = rearrange(new_memory, 'm b n d -> b n (m d)') new_memory = compress_fn(new_memory) new_memory = rearrange(new_memory, 'b n (m d) -> m b n d', m = 2) # fifo memory queue # add the new memory on the right current_memory = safe_cat(current_memory, new_memory, dim = -2) # "new" memory is new with respect to the next compressed segment new_memory, current_memory = current_memory[..., :-mem_length, :], current_memory[..., -mem_length:, :] # concat the new memory to the left into the past next_memory = safe_cat(current_memory, next_memory, dim = -2) next_memories.append(next_memory) return next_memories # maybe flash attention, if using pytorch 2.0 # constants Config = namedtuple('EfficientAttentionConfig', ['enable_flash', 'enable_math', 'enable_mem_efficient']) # state container class StateContainer(nn.Module): def __init__( self, dim, *, num_state_vectors, dim_head = 64, heads = 8, qk_rmsnorm = False, qk_rmsnorm_scale = 8, use_flash_attn = False ): super().__init__() assert num_state_vectors > 0 self.heads = heads inner_dim = dim_head * heads self.state_norm = LayerNorm(dim) self.q_to_state = nn.Linear(dim, inner_dim, bias = False) self.q_from_state = nn.Linear(dim, inner_dim, bias = False) self.state_to_q = nn.Linear(dim, inner_dim, bias = False) self.state_to_kv = nn.Linear(dim, dim_head * 2, bias = False) self.init_state = nn.Parameter(torch.randn(num_state_vectors, dim)) self.state_pos_ids = nn.Parameter(torch.randn(num_state_vectors, dim)) self.to_state_out = nn.Linear(inner_dim * 2, dim, bias = False) self.to_state_cross_attn = Attention(dim_head, qk_rmsnorm = qk_rmsnorm, qk_rmsnorm_scale = qk_rmsnorm_scale, use_flash_attn = use_flash_attn) self.state_self_attn = Attention(dim_head, qk_rmsnorm = qk_rmsnorm, qk_rmsnorm_scale = qk_rmsnorm_scale, use_flash_attn = use_flash_attn) self.from_state_cross_attn = Attention(dim_head, qk_rmsnorm = qk_rmsnorm, qk_rmsnorm_scale = qk_rmsnorm_scale, use_flash_attn = use_flash_attn) # gating related parameters - using the fixed simple config self.state_out_to_gate = nn.Linear(dim, dim) self.learned_ema_beta = nn.Parameter(torch.randn(dim)) # since each read should be followed by a write, just store cache in the container self.cache = None self.next_read_state = None def set_next_read_state( self, states ): if not exists(states): states = self.init_state self.next_read_state = (states,) def read(self, x): assert exists(self.next_read_state), 'states to be read must be set with .set_next_read_state' states, = self.next_read_state self.next_read_state = None # pre norm state for attention normed_states = self.state_norm(states) # add the positional ids, as stated in the paper critical for it to work normed_states = normed_states + self.state_pos_ids # get queries for cross attention, which they do not share, although they share key / values. another intriguing detail q_to_state = self.q_to_state(x) q_to_state = rearrange(q_to_state, '... n (h d) -> ... h n d', h = self.heads) # self attention qkv for states state_k, state_v = self.state_to_kv(normed_states).chunk(2, dim = -1) # cross attend to the past states key values to_state_out = self.to_state_cross_attn(q_to_state, state_k, state_v) to_state_out = rearrange(to_state_out, 'b h n d -> b n (h d)') # cache for next write self.cache = (states, normed_states, state_k, state_v) return to_state_out def write( self, *, memories ): assert exists(self.cache) k, v = memories batch = k.shape[0] # get cached values from the previous read states, normed_states, state_k, state_v = self.cache self.cache = None # derive queries q_from_state = self.q_from_state(normed_states) q_from_state = rearrange(q_from_state, '... n (h d) -> ... h n d', h = self.heads) state_q = self.state_to_q(normed_states) state_q_einsum = 'n (h d)' if state_q.ndim == 2 else 'b n (h d)' state_q = repeat(state_q, f'{state_q_einsum} -> b h n d', h = self.heads, b = batch) # states must also undergo self attention if q_from_state.ndim == 3: q_from_state = repeat(q_from_state, '... -> b ...', b = batch) state_out = self.state_self_attn(state_q, state_k, state_v) from_state_out = self.from_state_cross_attn(q_from_state, k, v) state_out = torch.cat((state_out, from_state_out), dim = -1) state_out = rearrange(state_out, 'b h n d -> b n (h d)') state_out = self.to_state_out(state_out) # use the best performing configuration # fixed simple gate - nothing more than a learned EMA with some resemblance to highway networks z = self.state_out_to_gate(state_out) learned_ema_decay = self.learned_ema_beta.sigmoid() # set new state with the learned EMA gating return learned_ema_decay * z + (1 - learned_ema_decay) * states def forward(self, x): raise NotImplementedError # main class class Attend(nn.Module): def __init__( self, causal = False, use_flash_attn = False ): super().__init__() self.causal = causal self.register_buffer("mask", None, persistent=False) self.use_flash_attn = use_flash_attn assert not (use_flash_attn and version.parse(torch.__version__) < version.parse('2.0.0')), 'in order to use flash attention, you must be using pytorch 2.0 or above' # determine efficient attention configs for cuda and cpu self.cpu_config = Config(True, True, True) self.cuda_config = None if not torch.cuda.is_available() or not use_flash_attn: return device_properties = torch.cuda.get_device_properties(torch.device('cuda')) if device_properties.major == 8 and device_properties.minor == 0: print_once('A100 GPU detected, using flash attention if input tensor is on cuda') self.cuda_config = Config(True, False, False) else: print_once('Non-A100 GPU detected, using math or mem efficient attention if input tensor is on cuda') self.cuda_config = Config(False, True, True) def get_mask(self, n, device): if exists(self.mask) and self.mask.shape[-1] >= n: return self.mask[:n, :n] mask = torch.ones((n, n), device=device, dtype=torch.bool).triu(1) self.register_buffer("mask", mask, persistent=False) return mask def flash_attn(self, q, k, v, mask = None): _, heads, q_len, _, k_len, is_cuda = *q.shape, k.shape[-2], q.is_cuda # Recommended for multi-query single-key-value attention by Tri Dao # kv shape torch.Size([1, 512, 64]) -> torch.Size([1, 8, 512, 64]) if k.ndim == 3: k = repeat(k, 'b ... -> b h ...', h = q.shape[1]) if v.ndim == 3: v = repeat(v, 'b ... -> b h ...', h = q.shape[1]) # Check if mask exists and expand to compatible shape # The mask is B L, so it would have to be expanded to B H N L masks = [] if self.causal: i, j = q_len, k_len causal_mask = torch.ones((i, j), dtype = torch.bool, device = q.device).triu(j - i + 1) masks.append(~causal_mask) if exists(mask): if mask.ndim != 2: mask = repeat(mask, 'w ... -> (b w) ...', b = q.shape[0] // mask.shape[0]) masks.append(mask) attn_mask = and_reduce(masks) # Check if there is a compatible device for flash attention config = self.cuda_config if is_cuda else self.cpu_config # pytorch 2.0 flash attn: q, k, v, mask, dropout, causal, softmax_scale with torch.backends.cuda.sdp_kernel(**config._asdict()): out = F.scaled_dot_product_attention( q, k, v, attn_mask = attn_mask ) return out def forward(self, q, k, v, mask = None, use_flash_attn = None): use_flash_attn = default(use_flash_attn, self.use_flash_attn) b, n, device = q.shape[0], q.shape[-2], q.device q, ps = pack_one(q, '* h n d') k, _ = pack_one(k, '* n d') v, _ = pack_one(v, '* n d') if use_flash_attn: out = self.flash_attn(q, k, v, mask = mask) return unpack_one(out, ps, '* h n d') scale = q.shape[-1] ** -0.5 k_einsum = 'b j d' if k.ndim == 3 else 'b h j d' v_einsum = 'b j d' if v.ndim == 3 else 'b h j d' # similarity sim = einsum(f"b h i d, {k_einsum} -> b h i j", q, k) * scale # key padding mask if exists(mask): if mask.ndim != 2: mask = repeat(mask, 'w ... -> (b w) ...', b = b) sim = sim.masked_fill(~mask, -torch.finfo(sim.dtype).max) # causal mask if self.causal: i, j = sim.shape[-2:] causal_mask = torch.ones((i, j), dtype = torch.bool, device = q.device).triu(j - i + 1) sim = sim.masked_fill(causal_mask, -torch.finfo(sim.dtype).max) # attention attn = sim.softmax(dim=-1) # aggregate values out = einsum(f"b h i j, {v_einsum} -> b h i d", attn, v) return unpack_one(out, ps, '* h n d') # geglu feedforward class GEGLU(nn.Module): def forward(self, x): x, gate = x.chunk(2, dim = -1) return F.gelu(gate) * x def FeedForward(dim, mult = 4): inner_dim = int(dim * mult * 2 / 3) return nn.Sequential( LayerNorm(dim), nn.Linear(dim, inner_dim * 2, bias = False), GEGLU(), nn.Linear(inner_dim, dim, bias = False) ) # attention class Attention(nn.Module): def __init__( self, dim_head, causal = False, qk_rmsnorm = False, qk_rmsnorm_scale = 8, use_flash_attn = False ): super().__init__() self.causal = causal self.qk_rmsnorm = qk_rmsnorm self.qk_rmsnorm_scale = qk_rmsnorm_scale self.attend = Attend(causal = causal, use_flash_attn = use_flash_attn) if qk_rmsnorm: self.q_scale = nn.Parameter(torch.ones(dim_head)) self.k_scale = nn.Parameter(torch.ones(dim_head)) def forward( self, q, k, v, mask = None, rotary_pos_emb = None, xpos_scale = None ): scale = q.shape[-1] ** -0.5 if self.qk_rmsnorm: q, k = map(l2norm, (q, k)) scale = self.qk_rmsnorm_scale if self.qk_rmsnorm: q = q * self.q_scale k = k * self.k_scale # rotary positional embedding with xpos for length extrapolation if exists(rotary_pos_emb): q = apply_rotary_pos_emb(q, rotary_pos_emb, xpos_scale) k = apply_rotary_pos_emb(k, rotary_pos_emb, xpos_scale ** -1) # attention out = self.attend(q, k, v, mask = mask) return out class AttentionBlock(nn.Module): def __init__( self, dim, block_width, dim_head = 64, heads = 8, qk_rmsnorm = False, qk_rmsnorm_scale = 8, use_flash_attn = False, num_state_vectors = 0, num_external_state_reads = 0, state_read_before_write = True # this will be defaulted to on as in the paper, but will be turned off in the case the researcher wants to test out reading the state at a lower layer ): super().__init__() inner_dim = dim_head * heads self.heads = heads self.norm = LayerNorm(dim) self.to_q = nn.Linear(dim, inner_dim, bias = False) self.to_kv = nn.Linear(dim, dim_head * 2, bias = False) self.attn = Attention(dim_head, qk_rmsnorm = qk_rmsnorm, qk_rmsnorm_scale = qk_rmsnorm_scale, use_flash_attn = use_flash_attn) self.block_width = block_width self.is_recurrent_layer = num_state_vectors > 0 # decide how many states this attention layer is going to read from num_state_reads = int(self.is_recurrent_layer and state_read_before_write) + num_external_state_reads self.to_out = nn.Linear(inner_dim * (1 + num_state_reads), dim, bias = False) if not self.is_recurrent_layer: return self.state_read_before_write = state_read_before_write self.state_container = StateContainer( dim, dim_head = dim_head, heads = heads, num_state_vectors = num_state_vectors, qk_rmsnorm = qk_rmsnorm, qk_rmsnorm_scale = qk_rmsnorm_scale, use_flash_attn = use_flash_attn ) @property def device(self): return next(self.parameters()).device def forward( self, x, rotary_pos_emb = None, xpos_scale = None, attn_mask = None, xl_memories: Optional[torch.Tensor] = None, read_from_state_containers: List[StateContainer] = [] ): batch, seq_len, _, width, device = *x.shape, self.block_width, self.device # pre normalization x = self.norm(x) # queries, keys, values and split out heads q, k, v = (self.to_q(x), *self.to_kv(x).chunk(2, dim = -1)) split_head = partial(rearrange, pattern = 'b n (h d) -> b h n d', h = self.heads) q = split_head(q) # save the last key / values as memories for recurrence memories = torch.stack((k, v)) mem_len = 0 if exists(xl_memories): # if past memories are passed in, concat as the first bucket mem_len = xl_memories.shape[-2] past_k, past_v = xl_memories k = torch.cat((past_k, k), dim = 1) v = torch.cat((past_v, v), dim = 1) # handle cropping of attention mask and positional embeddings if exists(attn_mask): attn_mask = attn_mask[:seq_len, :seq_len] attn_mask = F.pad(attn_mask, (mem_len, 0), value = True) # attention, but of course out = self.attn( q, k, v, rotary_pos_emb = rotary_pos_emb, xpos_scale = xpos_scale, mask = attn_mask ) # merge heads out = rearrange(out, 'b h n d -> b n (h d)') # early return if not a recurrent layer if not self.is_recurrent_layer and len(read_from_state_containers) == 0: return self.to_out(out), memories, None # whether to read from own state container, default to on, but may pass in more if self.is_recurrent_layer and self.state_read_before_write: read_from_state_containers = [self.state_container, *read_from_state_containers] for read_state_container in read_from_state_containers: # read from the states ... to_state_out = read_state_container.read(x) # and concat it to the output of self-attention out = torch.cat((out, to_state_out), dim = -1) new_states = None if self.is_recurrent_layer: # then write to the states as well if need be new_states = self.state_container.write(memories = memories) return self.to_out(out), memories, new_states # classes @beartype class BlockRecurrentTransformer(nn.Module): def __init__( self, *, num_tokens, dim, depth, dim_head = 64, heads = 8, all_layers_qk_rmsnorm = False, ff_mult = 4, max_seq_len = 1024, block_width = 512, recurrent_layers: Optional[Tuple[int, ...]] = None, read_recurrent_layers: Optional[Tuple[int, ...]] = None, num_state_vectors = None, ignore_index = -100, use_flash_attn = False, use_compressed_mem = False, compressed_mem_factor = 4 ): super().__init__() num_state_vectors = default(num_state_vectors, block_width) # set recurrent layers recurrent_layers = default(recurrent_layers, (depth // 2,)) # default to one recurent layer at middle of the network assert all([0 < layer <= depth for layer in recurrent_layers]), f'recurrent layers must range from 1 to the depth {depth}' assert all_unique(recurrent_layers), 'recurrent layers must be all unique. no duplicate layers' self.recurrent_layers = recurrent_layers # set read recurrent layers read_recurrent_layers = default(read_recurrent_layers, recurrent_layers) assert all([read_layer <= write_layer for read_layer, write_layer in zip(read_recurrent_layers, recurrent_layers)]), 'the recurrent read layer must be always less than or equal to the write layer' assert all([0 < layer <= depth for layer in read_recurrent_layers]) assert len(read_recurrent_layers) == len(recurrent_layers) self.read_recurrent_layers = read_recurrent_layers # token embedding self.token_emb = nn.Embedding(num_tokens, dim) self.rotary_pos_emb = RotaryEmbedding(dim = dim_head, width = (2 if not use_compressed_mem else 3) * block_width) self.layers = nn.ModuleList([]) self.write_to_read_map = {write_layer: read_layer for write_layer, read_layer in zip(recurrent_layers, read_recurrent_layers)} self.read_state_router = defaultdict(list) for layer in range(1, depth + 1): is_recurrent_layer = layer in self.recurrent_layers layer_num_state_vectors = num_state_vectors if is_recurrent_layer else 0 num_external_state_reads = sum([int(layer == read_layer) for read_layer in read_recurrent_layers]) # only layers with xl memories # or has recurrence in horizontal direction # use qk rmsnorm (in paper, they use cosine sim attention, but i think qk rmsnorm is more proven given Vit 22B paper) # one can also override to use all qk rmsnorm by setting all_layers_qk_rmsnorm = True qk_rmsnorm = all_layers_qk_rmsnorm or is_recurrent_layer attn_block = AttentionBlock( dim, block_width = block_width, dim_head = dim_head, heads = heads, qk_rmsnorm = qk_rmsnorm, num_state_vectors = layer_num_state_vectors, use_flash_attn = use_flash_attn, num_external_state_reads = num_external_state_reads, state_read_before_write = False, ) ff_block = FeedForward(dim, mult = ff_mult) if is_recurrent_layer: read_layer = self.write_to_read_map[layer] self.read_state_router[read_layer].append(attn_block.state_container) self.layers.append(nn.ModuleList([ attn_block, ff_block ])) # (compressed) memory management self.mem_manager = MemoryManager( dim = dim_head, layers = depth, mem_lengths = block_width if not use_compressed_mem else (block_width, block_width // 2), compress_factors = 1 if not use_compressed_mem else (1, compressed_mem_factor) ) # to logits self.to_logits = nn.Sequential( LayerNorm(dim), nn.Linear(dim, num_tokens, bias = False) ) self.max_seq_len = max_seq_len self.block_width = block_width assert divisible_by(max_seq_len, block_width) self.ignore_index = ignore_index self.register_buffer('cached_causal_attn_mask', None, persistent = False) @property def device(self): return next(self.parameters()).device def get_causal_attn_mask(self, width): if exists(self.cached_causal_attn_mask): cached_mask = self.cached_causal_attn_mask cached_width = cached_mask.shape[-2] padding = (width - cached_width) // 2 j_slice = Ellipsis if padding == 0 else slice(padding, -padding) return cached_mask[:cached_width, j_slice] device = self.device causal_mask = torch.ones((width, width), device = device, dtype = torch.bool).triu(1) return ~causal_mask @torch.no_grad() @eval_decorator def generate( self, prime, length = None, xl_memories: List[torch.Tensor] = [], states: List[torch.Tensor] = [], temperature = 1., filter_thres = 0.9, return_memories_and_states = False ): length = default(length, self.max_seq_len + 1) start_len = prime.shape[-1] assert start_len < self.max_seq_len assert length <= (self.max_seq_len + 1) assert start_len < length output = prime memories = [] for ind in range(length - start_len): logits, next_memories, next_states = self.forward( output, xl_memories = xl_memories, states = states ) logits = logits[:, -1] filtered_logits = top_k(logits, thres = filter_thres) sampled = gumbel_sample(filtered_logits, temperature = temperature) sampled = rearrange(sampled, 'b -> b 1') output = torch.cat((output, sampled), dim = -1) if divisible_by(output.shape[-1] - 1, self.max_seq_len): # on the sampling of the last token in the current window, set new memories and states memories = next_memories states = next_states output = output[:, start_len:] if return_memories_and_states: return output, memories, states return output def forward( self, x, return_loss = False, xl_memories: List[torch.Tensor] = [], states: List[torch.Tensor] = [], return_memories_and_states = None # can force to either return memory + state or not. by default will only return when number of tokens == max_seq_len ): device = x.device if return_loss: x, labels = x[:, :-1], x[:, 1:] # get sequence length i and j for dynamic pos bias assert x.shape[-1] <= self.max_seq_len w = self.block_width # token embedding x = self.token_emb(x) # dynamic pos bias attn_mask = self.get_causal_attn_mask(w) rotary_pos_emb, xpos_scale = self.rotary_pos_emb() # only return memories and state if at the full block width, but can be overridden return_memories_and_states = default(return_memories_and_states, self.max_seq_len == x.shape[-2]) # ready output tensor, to be concatted to block by block batch, _, dim = x.shape out = torch.empty(batch, 0, dim, dtype = x.dtype, device = self.device) # split input into blocks of width w input_blocks = x.split(w, dim = -2) # process each block at a time for input_block in input_blocks: input_block_length = input_block.shape[-2] # ready xl memories and states iter_xl_memories = iter(xl_memories) iter_states = iter(states) next_xl_memories = [] next_states = [] # set the states on the appropriate state containers for attn, _ in self.layers: if not attn.is_recurrent_layer: continue attn.state_container.set_next_read_state(next(iter_states, None)) # go through layers for ind, (attn, ff) in enumerate(self.layers): # determine if the layer requires transformer xl memories layer = ind + 1 # whether to pass in xl memories attn_kwargs = dict( rotary_pos_emb = rotary_pos_emb, xpos_scale = xpos_scale, attn_mask = attn_mask, xl_memories = next(iter_xl_memories, None), read_from_state_containers = self.read_state_router[layer] ) # attention layer residual = input_block attn_branch_out, layer_xl_memories, layer_next_states = attn(input_block, **attn_kwargs) if exists(layer_xl_memories): next_xl_memories.append(layer_xl_memories) if exists(layer_next_states): next_states.append(layer_next_states) input_block = attn_branch_out + residual # feedforward layer input_block = ff(input_block) + input_block # concat to output out = torch.cat((out, input_block), dim = -2) # set new xl memories and states states = next_states if input_block_length == w: xl_memories = self.mem_manager(xl_memories, next_xl_memories) # project to logits logits = self.to_logits(out) # detach the states and memories returned_next_states = list(map(torch.detach, states)) if return_memories_and_states else None returned_next_xl_memories = list(map(torch.detach, xl_memories)) if return_memories_and_states else None # whether to return logits if not return_loss: return logits, returned_next_xl_memories, returned_next_states # cross entropy loss logits = rearrange(logits, 'b n c -> b c n') loss = F.cross_entropy(logits, labels, ignore_index = self.ignore_index) return loss, returned_next_xl_memories, returned_next_states # recurrent trainer wrapper @beartype class RecurrentTrainerWrapper(nn.Module): def __init__( self, transformer: BlockRecurrentTransformer, xl_memories_dropout = 0., state_dropout = 0. ): super().__init__() self.transformer = transformer self.seq_len = transformer.max_seq_len self.xl_memories_dropout = xl_memories_dropout self.state_dropout = state_dropout @eval_decorator @torch.no_grad() def generate( self, prime, length, **kwargs ): seq_len = self.seq_len start_len = prime.shape[-1] assert start_len < length output = prime current_len = start_len memories = [] states = [] # determine lengths has_remainder = not divisible_by(length, seq_len) remainder_amount = length % seq_len total_segments = math.ceil(length / seq_len) if not has_remainder: lengths = (*((seq_len + 1,) * (total_segments - 1)), seq_len) elif remainder_amount == 1: lengths = (seq_len + 1,) * (total_segments - 1) else: lengths = (*((seq_len + 1,) * (total_segments - 1)), remainder_amount) # loop through lengths for next_length in lengths: segment_output, memories, states = self.transformer.generate( output[:, -current_len:], length = next_length, xl_memories = memories, states = states, return_memories_and_states = True, **kwargs ) output = torch.cat((output, segment_output), dim = -1) current_len = 1 return output[:, start_len:] def forward( self, x, return_memories_and_states = False ): total_seq_len, seq_len = x.shape[1], self.seq_len assert divisible_by(total_seq_len - 1, seq_len), f'length of sequence ({total_seq_len}) must be equal to a multiple of {seq_len} + 1 (one extra token) during training' segments = total_seq_len // seq_len total_loss = 0. memories = [] states = [] for ind in range(segments): start = ind * seq_len end = start + seq_len + 1 if self.training and random() < self.xl_memories_dropout: memories.clear() if self.training and random() < self.state_dropout: states.clear() loss, memories, states = self.transformer( x[:, start:end], xl_memories = memories, states = states, return_loss = True ) total_loss = total_loss + (loss / segments) if return_memories_and_states: return total_loss, memories, states return total_loss
def exists(val): return val is not None class Adan(Optimizer): def __init__( self, params, lr = 1e-3, betas = (0.02, 0.08, 0.01), eps = 1e-8, weight_decay = 0, restart_cond: callable = None ): assert len(betas) == 3 defaults = dict( lr = lr, betas = betas, eps = eps, weight_decay = weight_decay, restart_cond = restart_cond ) super().__init__(params, defaults) def step(self, closure = None): loss = None if exists(closure): loss = closure() for group in self.param_groups: lr = group['lr'] beta1, beta2, beta3 = group['betas'] weight_decay = group['weight_decay'] eps = group['eps'] restart_cond = group['restart_cond'] for p in group['params']: if not exists(p.grad): continue data, grad = p.data, p.grad.data assert not grad.is_sparse state = self.state[p] if len(state) == 0: state['step'] = 0 state['prev_grad'] = torch.zeros_like(grad) state['m'] = torch.zeros_like(grad) state['v'] = torch.zeros_like(grad) state['n'] = torch.zeros_like(grad) step, m, v, n, prev_grad = state['step'], state['m'], state['v'], state['n'], state['prev_grad'] if step > 0: prev_grad = state['prev_grad'] # main algorithm m.mul_(1 - beta1).add_(grad, alpha = beta1) grad_diff = grad - prev_grad v.mul_(1 - beta2).add_(grad_diff, alpha = beta2) next_n = (grad + (1 - beta2) * grad_diff) ** 2 n.mul_(1 - beta3).add_(next_n, alpha = beta3) # bias correction terms step += 1 correct_m, correct_v, correct_n = map(lambda n: 1 / (1 - (1 - n) ** step), (beta1, beta2, beta3)) # gradient step def grad_step_(data, m, v, n): weighted_step_size = lr / (n * correct_n).sqrt().add_(eps) denom = 1 + weight_decay * lr data.addcmul_(weighted_step_size, (m * correct_m + (1 - beta2) * v * correct_v), value = -1.).div_(denom) grad_step_(data, m, v, n) # restart condition if exists(restart_cond) and restart_cond(state): m.data.copy_(grad) v.zero_() n.data.copy_(grad ** 2) grad_step_(data, m, v, n) # set new incremented step prev_grad.copy_(grad) state['step'] = step return loss
def exists(val): return val is not None def default(val, d): return val if exists(val) else d def stable_softmax(t, dim = -1): t = t - t.amax(dim = dim, keepdim = True) return t.softmax(dim = dim) # bidirectional cross attention - have two sequences attend to each other with 1 attention step class BidirectionalCrossAttention(nn.Module): def __init__( self, *, dim, heads = 8, dim_head = 64, context_dim = None, dropout = 0., talking_heads = False, prenorm = False, ): super().__init__() context_dim = default(context_dim, dim) self.norm = nn.LayerNorm(dim) if prenorm else nn.Identity() self.context_norm = nn.LayerNorm(context_dim) if prenorm else nn.Identity() self.heads = heads self.scale = dim_head ** -0.5 inner_dim = dim_head * heads self.dropout = nn.Dropout(dropout) self.context_dropout = nn.Dropout(dropout) self.to_qk = nn.Linear(dim, inner_dim, bias = False) self.context_to_qk = nn.Linear(context_dim, inner_dim, bias = False) self.to_v = nn.Linear(dim, inner_dim, bias = False) self.context_to_v = nn.Linear(context_dim, inner_dim, bias = False) self.to_out = nn.Linear(inner_dim, dim) self.context_to_out = nn.Linear(inner_dim, context_dim) self.talking_heads = nn.Conv2d(heads, heads, 1, bias = False) if talking_heads else nn.Identity() self.context_talking_heads = nn.Conv2d(heads, heads, 1, bias = False) if talking_heads else nn.Identity() def forward( self, x, context, mask = None, context_mask = None, return_attn = False, rel_pos_bias = None ): b, i, j, h, device = x.shape[0], x.shape[-2], context.shape[-2], self.heads, x.device x = self.norm(x) context = self.context_norm(context) # get shared query/keys and values for sequence and context qk, v = self.to_qk(x), self.to_v(x) context_qk, context_v = self.context_to_qk(context), self.context_to_v(context) # split out head qk, context_qk, v, context_v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = h), (qk, context_qk, v, context_v)) # get similarities sim = einsum('b h i d, b h j d -> b h i j', qk, context_qk) * self.scale # relative positional bias, if supplied if exists(rel_pos_bias): sim = sim + rel_pos_bias # mask if exists(mask) or exists(context_mask): mask = default(mask, torch.ones((b, i), device = device, dtype = torch.bool)) context_mask = default(context_mask, torch.ones((b, j), device = device, dtype = torch.bool)) attn_mask = rearrange(mask, 'b i -> b 1 i 1') * rearrange(context_mask, 'b j -> b 1 1 j') sim = sim.masked_fill(~attn_mask, -torch.finfo(sim.dtype).max) # get attention along both sequence length and context length dimensions # shared similarity matrix attn = stable_softmax(sim, dim = -1) context_attn = stable_softmax(sim, dim = -2) # dropouts attn = self.dropout(attn) context_attn = self.context_dropout(context_attn) # talking heads attn = self.talking_heads(attn) context_attn = self.context_talking_heads(context_attn) # src sequence aggregates values from context, context aggregates values from src sequence out = einsum('b h i j, b h j d -> b h i d', attn, context_v) context_out = einsum('b h j i, b h j d -> b h i d', context_attn, v) # merge heads and combine out out, context_out = map(lambda t: rearrange(t, 'b h n d -> b n (h d)'), (out, context_out)) out = self.to_out(out) context_out = self.context_to_out(context_out) if return_attn: return out, context_out, attn, context_attn return out, context_out
# helper functions def default(val, def_val): return def_val if val is None else val def flatten(t): return t.reshape(t.shape[0], -1) def singleton(cache_key): def inner_fn(fn): @wraps(fn) def wrapper(self, *args, **kwargs): instance = getattr(self, cache_key) if instance is not None: return instance instance = fn(self, *args, **kwargs) setattr(self, cache_key, instance) return instance return wrapper return inner_fn def get_module_device(module): return next(module.parameters()).device def set_requires_grad(model, val): for p in model.parameters(): p.requires_grad = val # loss fn def loss_fn(x, y): x = F.normalize(x, dim=-1, p=2) y = F.normalize(y, dim=-1, p=2) return 2 - 2 * (x * y).sum(dim=-1) # augmentation utils class RandomApply(nn.Module): def __init__(self, fn, p): super().__init__() self.fn = fn self.p = p def forward(self, x): if random.random() > self.p: return x return self.fn(x) # exponential moving average class EMA(): def __init__(self, beta): super().__init__() self.beta = beta def update_average(self, old, new): if old is None: return new return old * self.beta + (1 - self.beta) * new def update_moving_average(ema_updater, ma_model, current_model): for current_params, ma_params in zip(current_model.parameters(), ma_model.parameters()): old_weight, up_weight = ma_params.data, current_params.data ma_params.data = ema_updater.update_average(old_weight, up_weight) # MLP class for projector and predictor def MLP(dim, projection_size, hidden_size=4096): return nn.Sequential( nn.Linear(dim, hidden_size), nn.BatchNorm1d(hidden_size), nn.ReLU(inplace=True), nn.Linear(hidden_size, projection_size) ) def SimSiamMLP(dim, projection_size, hidden_size=4096): return nn.Sequential( nn.Linear(dim, hidden_size, bias=False), nn.BatchNorm1d(hidden_size), nn.ReLU(inplace=True), nn.Linear(hidden_size, hidden_size, bias=False), nn.BatchNorm1d(hidden_size), nn.ReLU(inplace=True), nn.Linear(hidden_size, projection_size, bias=False), nn.BatchNorm1d(projection_size, affine=False) ) # a wrapper class for the base neural network # will manage the interception of the hidden layer output # and pipe it into the projecter and predictor nets class NetWrapper(nn.Module): def __init__(self, net, projection_size, projection_hidden_size, layer = -2, use_simsiam_mlp = False): super().__init__() self.net = net self.layer = layer self.projector = None self.projection_size = projection_size self.projection_hidden_size = projection_hidden_size self.use_simsiam_mlp = use_simsiam_mlp self.hidden = {} self.hook_registered = False def _find_layer(self): if type(self.layer) == str: modules = dict([*self.net.named_modules()]) return modules.get(self.layer, None) elif type(self.layer) == int: children = [*self.net.children()] return children[self.layer] return None def _hook(self, _, input, output): device = input[0].device self.hidden[device] = flatten(output) def _register_hook(self): layer = self._find_layer() assert layer is not None, f'hidden layer ({self.layer}) not found' handle = layer.register_forward_hook(self._hook) self.hook_registered = True @singleton('projector') def _get_projector(self, hidden): _, dim = hidden.shape create_mlp_fn = MLP if not self.use_simsiam_mlp else SimSiamMLP projector = create_mlp_fn(dim, self.projection_size, self.projection_hidden_size) return projector.to(hidden) def get_representation(self, x): if self.layer == -1: return self.net(x) if not self.hook_registered: self._register_hook() self.hidden.clear() _ = self.net(x) hidden = self.hidden[x.device] self.hidden.clear() assert hidden is not None, f'hidden layer {self.layer} never emitted an output' return hidden def forward(self, x, return_projection = True): representation = self.get_representation(x) if not return_projection: return representation projector = self._get_projector(representation) projection = projector(representation) return projection, representation # main class class BYOL(nn.Module): def __init__( self, net, image_size, hidden_layer = -2, projection_size = 256, projection_hidden_size = 4096, augment_fn = None, augment_fn2 = None, moving_average_decay = 0.99, use_momentum = True ): super().__init__() self.net = net # default SimCLR augmentation DEFAULT_AUG = torch.nn.Sequential( RandomApply( T.ColorJitter(0.8, 0.8, 0.8, 0.2), p = 0.3 ), T.RandomGrayscale(p=0.2), T.RandomHorizontalFlip(), RandomApply( T.GaussianBlur((3, 3), (1.0, 2.0)), p = 0.2 ), T.RandomResizedCrop((image_size, image_size)), T.Normalize( mean=torch.tensor([0.485, 0.456, 0.406]), std=torch.tensor([0.229, 0.224, 0.225])), ) self.augment1 = default(augment_fn, DEFAULT_AUG) self.augment2 = default(augment_fn2, self.augment1) self.online_encoder = NetWrapper(net, projection_size, projection_hidden_size, layer=hidden_layer, use_simsiam_mlp=not use_momentum) self.use_momentum = use_momentum self.target_encoder = None self.target_ema_updater = EMA(moving_average_decay) self.online_predictor = MLP(projection_size, projection_size, projection_hidden_size) # get device of network and make wrapper same device device = get_module_device(net) self.to(device) # send a mock image tensor to instantiate singleton parameters self.forward(torch.randn(2, 3, image_size, image_size, device=device)) @singleton('target_encoder') def _get_target_encoder(self): target_encoder = copy.deepcopy(self.online_encoder) set_requires_grad(target_encoder, False) return target_encoder def reset_moving_average(self): del self.target_encoder self.target_encoder = None def update_moving_average(self): assert self.use_momentum, 'you do not need to update the moving average, since you have turned off momentum for the target encoder' assert self.target_encoder is not None, 'target encoder has not been created yet' update_moving_average(self.target_ema_updater, self.target_encoder, self.online_encoder) def forward( self, x, return_embedding = False, return_projection = True ): assert not (self.training and x.shape[0] == 1), 'you must have greater than 1 sample when training, due to the batchnorm in the projection layer' if return_embedding: return self.online_encoder(x, return_projection = return_projection) image_one, image_two = self.augment1(x), self.augment2(x) online_proj_one, _ = self.online_encoder(image_one) online_proj_two, _ = self.online_encoder(image_two) online_pred_one = self.online_predictor(online_proj_one) online_pred_two = self.online_predictor(online_proj_two) with torch.no_grad(): target_encoder = self._get_target_encoder() if self.use_momentum else self.online_encoder target_proj_one, _ = target_encoder(image_one) target_proj_two, _ = target_encoder(image_two) target_proj_one.detach_() target_proj_two.detach_() loss_one = loss_fn(online_pred_one, target_proj_two.detach()) loss_two = loss_fn(online_pred_two, target_proj_one.detach()) loss = loss_one + loss_two return loss.mean()