


# Copyright (c) 2019 Uber Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import csv
import json
import math
import time

import numpy as np
import torch
import torch.optim as optim
import torch.utils.data as data
from nltk.tokenize.treebank import TreebankWordDetokenizer
from pplm_classification_head import ClassificationHead
from torch import nn
from torchtext import data as torchtext_data
from torchtext import datasets
from tqdm import tqdm, trange

from transformers import GPTaLMHeadModel, GPTaTokenizer


torch.manual_seed(0)
np.random.seed(0)
lowerCAmelCase					 =	1E-10
lowerCAmelCase					 =	'''This is incredible! I love it, this is the best chicken I have ever had.'''
lowerCAmelCase					 =	1_0_0





class    A					(     nn.Module				):


							def __init__(self   ,       lowerCAmelCase   ,       lowerCAmelCase="gpt2-medium"   ,       lowerCAmelCase=False   ,       lowerCAmelCase="cpu"      ):
									super().__init__()
									__lowercase=    GPTaTokenizer.from_pretrained(lowerCAmelCase      )
									__lowercase=    GPTaLMHeadModel.from_pretrained(lowerCAmelCase      )
									__lowercase=    self.encoder.transformer.config.hidden_size
									__lowercase=    ClassificationHead(class_size=lowerCAmelCase   ,       embed_size=self.embed_size      )
									__lowercase=    cached_mode
									__lowercase=    device


							def     _A (self      ):
									return self.classifier_head


							def     _A (self      ):
									for param in self.encoder.parameters():
											__lowercase=    False
									self.classifier_head.train()


							def     _A (self   ,       lowerCAmelCase      ):
									__lowercase=    x.ne(0      ).unsqueeze(2      ).repeat(1   ,       1   ,       self.embed_size      ).float().to(self.device      ).detach()
									__lowercase=    self.encoder.transformer(lowerCAmelCase      )['last_hidden_state']
									__lowercase=    hidden * mask
									__lowercase=    torch.sum(lowerCAmelCase   ,       dim=1      ) / (torch.sum(lowerCAmelCase   ,       dim=1      ).detach() + EPSILON)
									return avg_hidden







							def     _A (self   ,       lowerCAmelCase      ):
									if self.cached_mode:
											__lowercase=    x.to(self.device      )
									else:
											__lowercase=    self.avg_representation(x.to(self.device      )      )

									__lowercase=    self.classifier_head(lowerCAmelCase      )
									__lowercase=    nn.functional.log_softmax(lowerCAmelCase   ,       dim=-1      )

									return probs





class    A					(     data.Dataset				):


							def __init__(self   ,       lowerCAmelCase   ,       lowerCAmelCase      ):
									__lowercase=    X
									__lowercase=    y


							def __len__(self      ):
									return len(self.X      )







							def __getitem__(self   ,       lowerCAmelCase      ):
									__lowercase=    {}
									__lowercase=    self.X[index]
									__lowercase=    self.y[index]
									return data







def  _lowerCamelCase(			lowercase__			)		->			Dict:







		def pad_sequences(lowercase__			):
				__lowercase=    [len(lowercase__			) for seq in sequences]

				__lowercase=    torch.zeros(len(lowercase__			)     ,				max(lowercase__			)			).long()  # padding value = 0

				for i, seq in enumerate(lowercase__			):
						__lowercase=    lengths[i]
						__lowercase=    seq[:end]

				return padded_sequences, lengths

		__lowercase=    {}
		for key in data[0].keys():
				__lowercase=    [d[key] for d in data]

		__lowercase,							__lowercase=    pad_sequences(item_info['X']			)
		__lowercase=    torch.tensor(item_info['y']     ,				dtype=torch.long			)

		return x_batch, y_batch







def  _lowerCamelCase(			lowercase__			)		->			Dict:
		__lowercase=    {}
		for key in data[0].keys():
				__lowercase=    [d[key] for d in data]

		__lowercase=    torch.cat(item_info['X']     ,				0			)
		__lowercase=    torch.tensor(item_info['y']     ,				dtype=torch.long			)

		return x_batch, y_batch







def  _lowerCamelCase(			lowercase__     ,				lowercase__     ,				lowercase__     ,				lowercase__=0     ,				lowercase__=1_0     ,				lowercase__="cpu"			)		->			int:
		__lowercase=    0
		discriminator.train_custom()
		for batch_idx, (input_t, target_t) in enumerate(lowercase__			):
				__lowercase,							__lowercase=    input_t.to(lowercase__			), target_t.to(lowercase__			)

				optimizer.zero_grad()

				__lowercase=    discriminator(lowercase__			)
				__lowercase=    nn.functional.nll_loss(lowercase__     ,				lowercase__			)
				loss.backward(retain_graph=lowercase__			)
				optimizer.step()

				samples_so_far += len(lowercase__			)

				if batch_idx % log_interval == 0:
						print(
						    'Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
						        epoch + 1     ,				lowercase__     ,				len(data_loader.dataset			)     ,				1_0_0 * samples_so_far / len(data_loader.dataset			)     ,				loss.item()     ,				)			)







def  _lowerCamelCase(			lowercase__     ,				lowercase__     ,				lowercase__="cpu"			)		->			Dict:
		discriminator.eval()
		__lowercase=    0
		__lowercase=    0
		with torch.no_grad():
				for input_t, target_t in data_loader:
						__lowercase,							__lowercase=    input_t.to(lowercase__			), target_t.to(lowercase__			)
						__lowercase=    discriminator(lowercase__			)
						# sum up batch loss
						test_loss += nn.functional.nll_loss(lowercase__     ,				lowercase__     ,				reduction='sum'			).item()
						# get the index of the max log-probability
						__lowercase=    output_t.argmax(dim=1     ,				keepdim=lowercase__			)
						correct += pred_t.eq(target_t.view_as(lowercase__			)			).sum().item()

		test_loss /= len(data_loader.dataset			)

		print(
		    'Performance on test set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)'.format(
		        lowercase__     ,				lowercase__     ,				len(data_loader.dataset			)     ,				100.0 * correct / len(data_loader.dataset			)			)			)







def  _lowerCamelCase(			lowercase__     ,				lowercase__     ,				lowercase__     ,				lowercase__=False     ,				lowercase__="cpu"			)		->			List[str]:
		__lowercase=    model.tokenizer.encode(lowercase__			)
		__lowercase=    torch.tensor([input_t]     ,				dtype=torch.long     ,				device=lowercase__			)
		if cached:
				__lowercase=    model.avg_representation(lowercase__			)

		__lowercase=    model(lowercase__			).data.cpu().numpy().flatten().tolist()
		print('Input sentence:'     ,				lowercase__			)
		print(
		    'Predictions:'     ,				', '.join('{}: {:.4f}'.format(lowercase__     ,				math.exp(lowercase__			)			) for c, log_prob in zip(lowercase__     ,				lowercase__			)			)     ,				)







def  _lowerCamelCase(			lowercase__     ,				lowercase__     ,				lowercase__     ,				lowercase__=False     ,				lowercase__="cpu"			)		->			Union[str, Any]:
		__lowercase=    torch.utils.data.DataLoader(dataset=lowercase__     ,				batch_size=lowercase__     ,				collate_fn=lowercase__			)

		__lowercase=    []
		__lowercase=    []
		for batch_idx, (x, y) in enumerate(tqdm(lowercase__     ,				ascii=lowercase__			)			):
				with torch.no_grad():
						__lowercase=    x.to(lowercase__			)
						__lowercase=    discriminator.avg_representation(lowercase__			).cpu().detach()
						__lowercase=    torch.unbind(avg_rep.unsqueeze(1			)			)
						xs += avg_rep_list
						ys += y.cpu().numpy().tolist()

		__lowercase=    torch.utils.data.DataLoader(
		    dataset=Dataset(lowercase__     ,				lowercase__			)     ,				batch_size=lowercase__     ,				shuffle=lowercase__     ,				collate_fn=lowercase__			)

		return data_loader







def  _lowerCamelCase(			lowercase__     ,				lowercase__=None     ,				lowercase__="gpt2-medium"     ,				lowercase__=1_0     ,				lowercase__=6_4     ,				lowercase__=1_0     ,				lowercase__=False     ,				lowercase__=False     ,				lowercase__=False     ,				)		->			Dict:
		__lowercase=    'cuda' if torch.cuda.is_available() and not no_cuda else 'cpu'

		print('Preprocessing {} dataset...'.format(lowercase__			)			)
		__lowercase=    time.time()

		if dataset == "SST":
				__lowercase=    ['positive', 'negative', 'very positive', 'very negative', 'neutral']
				__lowercase=    {c: i for i, c in enumerate(lowercase__			)}

				__lowercase=    Discriminator(
				    class_size=len(lowercase__			)     ,				pretrained_model=lowercase__     ,				cached_mode=lowercase__     ,				device=lowercase__			).to(lowercase__			)

				__lowercase=    torchtext_data.Field()
				__lowercase=    torchtext_data.Field(sequential=lowercase__			)
				__lowercase,							__lowercase,							__lowercase=    datasets.SST.splits(
				    lowercase__     ,				lowercase__     ,				fine_grained=lowercase__     ,				train_subtrees=lowercase__     ,				)

				__lowercase=    []
				__lowercase=    []
				for i in trange(len(lowercase__			)     ,				ascii=lowercase__			):
						__lowercase=    TreebankWordDetokenizer().detokenize(vars(train_data[i]			)['text']			)
						__lowercase=    discriminator.tokenizer.encode(lowercase__			)
						__lowercase=    torch.tensor([5_0_2_5_6] + seq     ,				device=lowercase__     ,				dtype=torch.long			)
						x.append(lowercase__			)
						y.append(classaidx[vars(train_data[i]			)['label']]			)
				__lowercase=    Dataset(lowercase__     ,				lowercase__			)

				__lowercase=    []
				__lowercase=    []
				for i in trange(len(lowercase__			)     ,				ascii=lowercase__			):
						__lowercase=    TreebankWordDetokenizer().detokenize(vars(test_data[i]			)['text']			)
						__lowercase=    discriminator.tokenizer.encode(lowercase__			)
						__lowercase=    torch.tensor([5_0_2_5_6] + seq     ,				device=lowercase__     ,				dtype=torch.long			)
						test_x.append(lowercase__			)
						test_y.append(classaidx[vars(test_data[i]			)['label']]			)
				__lowercase=    Dataset(lowercase__     ,				lowercase__			)

				__lowercase=    {
				    'class_size': len(lowercase__			),
				    'embed_size': discriminator.embed_size,
				    'pretrained_model': pretrained_model,
				    'class_vocab': classaidx,
				    'default_class': 2,
				}

		elif dataset == "clickbait":
				__lowercase=    ['non_clickbait', 'clickbait']
				__lowercase=    {c: i for i, c in enumerate(lowercase__			)}

				__lowercase=    Discriminator(
				    class_size=len(lowercase__			)     ,				pretrained_model=lowercase__     ,				cached_mode=lowercase__     ,				device=lowercase__			).to(lowercase__			)

				with open('datasets/clickbait/clickbait_train_prefix.txt'			) as f:
						__lowercase=    []
						for i, line in enumerate(lowercase__			):
								try:
										data.append(eval(lowercase__			)			)
								except Exception:
										print('Error evaluating line {}: {}'.format(lowercase__     ,				lowercase__			)			)
										continue
				__lowercase=    []
				__lowercase=    []
				with open('datasets/clickbait/clickbait_train_prefix.txt'			) as f:
						for i, line in enumerate(tqdm(lowercase__     ,				ascii=lowercase__			)			):
								try:
										__lowercase=    eval(lowercase__			)
										__lowercase=    discriminator.tokenizer.encode(d['text']			)

										if len(lowercase__			) < max_length_seq:
												__lowercase=    torch.tensor([5_0_2_5_6] + seq     ,				device=lowercase__     ,				dtype=torch.long			)
										else:
												print('Line {} is longer than maximum length {}'.format(lowercase__     ,				lowercase__			)			)
												continue
										x.append(lowercase__			)
										y.append(d['label']			)
								except Exception:
										print('Error evaluating / tokenizing line {}, skipping it'.format(lowercase__			)			)
										pass

				__lowercase=    Dataset(lowercase__     ,				lowercase__			)
				__lowercase=    int(0.9 * len(lowercase__			)			)
				__lowercase=    len(lowercase__			) - train_size
				__lowercase,							__lowercase=    torch.utils.data.random_split(lowercase__     ,				[train_size, test_size]			)

				__lowercase=    {
				    'class_size': len(lowercase__			),
				    'embed_size': discriminator.embed_size,
				    'pretrained_model': pretrained_model,
				    'class_vocab': classaidx,
				    'default_class': 1,
				}

		elif dataset == "toxic":
				__lowercase=    ['non_toxic', 'toxic']
				__lowercase=    {c: i for i, c in enumerate(lowercase__			)}

				__lowercase=    Discriminator(
				    class_size=len(lowercase__			)     ,				pretrained_model=lowercase__     ,				cached_mode=lowercase__     ,				device=lowercase__			).to(lowercase__			)

				__lowercase=    []
				__lowercase=    []
				with open('datasets/toxic/toxic_train.txt'			) as f:
						for i, line in enumerate(tqdm(lowercase__     ,				ascii=lowercase__			)			):
								try:
										__lowercase=    eval(lowercase__			)
										__lowercase=    discriminator.tokenizer.encode(d['text']			)

										if len(lowercase__			) < max_length_seq:
												__lowercase=    torch.tensor([5_0_2_5_6] + seq     ,				device=lowercase__     ,				dtype=torch.long			)
										else:
												print('Line {} is longer than maximum length {}'.format(lowercase__     ,				lowercase__			)			)
												continue
										x.append(lowercase__			)
										y.append(int(np.sum(d['label']			) > 0			)			)
								except Exception:
										print('Error evaluating / tokenizing line {}, skipping it'.format(lowercase__			)			)
										pass

				__lowercase=    Dataset(lowercase__     ,				lowercase__			)
				__lowercase=    int(0.9 * len(lowercase__			)			)
				__lowercase=    len(lowercase__			) - train_size
				__lowercase,							__lowercase=    torch.utils.data.random_split(lowercase__     ,				[train_size, test_size]			)

				__lowercase=    {
				    'class_size': len(lowercase__			),
				    'embed_size': discriminator.embed_size,
				    'pretrained_model': pretrained_model,
				    'class_vocab': classaidx,
				    'default_class': 0,
				}

		else:  # if dataset == "generic":
				# This assumes the input dataset is a TSV with the following structure:
				# class \t text

				if dataset_fp is None:
						raise ValueError('When generic dataset is selected, dataset_fp needs to be specified aswell.'			)

				__lowercase=    set()
				with open(lowercase__			) as f:
						__lowercase=    csv.reader(lowercase__     ,				delimiter='\t'			)
						for row in tqdm(lowercase__     ,				ascii=lowercase__			):
								if row:
										classes.add(row[0]			)

				__lowercase=    sorted(lowercase__			)
				__lowercase=    {c: i for i, c in enumerate(lowercase__			)}

				__lowercase=    Discriminator(
				    class_size=len(lowercase__			)     ,				pretrained_model=lowercase__     ,				cached_mode=lowercase__     ,				device=lowercase__			).to(lowercase__			)

				__lowercase=    []
				__lowercase=    []
				with open(lowercase__			) as f:
						__lowercase=    csv.reader(lowercase__     ,				delimiter='\t'			)
						for i, row in enumerate(tqdm(lowercase__     ,				ascii=lowercase__			)			):
								if row:
										__lowercase=    row[0]
										__lowercase=    row[1]

										try:
												__lowercase=    discriminator.tokenizer.encode(lowercase__			)
												if len(lowercase__			) < max_length_seq:
														__lowercase=    torch.tensor([5_0_2_5_6] + seq     ,				device=lowercase__     ,				dtype=torch.long			)

												else:
														print('Line {} is longer than maximum length {}'.format(lowercase__     ,				lowercase__			)			)
														continue

												x.append(lowercase__			)
												y.append(classaidx[label]			)

										except Exception:
												print('Error tokenizing line {}, skipping it'.format(lowercase__			)			)
												pass

				__lowercase=    Dataset(lowercase__     ,				lowercase__			)
				__lowercase=    int(0.9 * len(lowercase__			)			)
				__lowercase=    len(lowercase__			) - train_size
				__lowercase,							__lowercase=    torch.utils.data.random_split(lowercase__     ,				[train_size, test_size]			)

				__lowercase=    {
				    'class_size': len(lowercase__			),
				    'embed_size': discriminator.embed_size,
				    'pretrained_model': pretrained_model,
				    'class_vocab': classaidx,
				    'default_class': 0,
				}

		__lowercase=    time.time()
		print('Preprocessed {} data points'.format(len(lowercase__			) + len(lowercase__			)			)			)
		print('Data preprocessing took: {:.3f}s'.format(end - start			)			)

		if cached:
				print('Building representation cache...'			)

				__lowercase=    time.time()

				__lowercase=    get_cached_data_loader(lowercase__     ,				lowercase__     ,				lowercase__     ,				shuffle=lowercase__     ,				device=lowercase__			)

				__lowercase=    get_cached_data_loader(lowercase__     ,				lowercase__     ,				lowercase__     ,				device=lowercase__			)

				__lowercase=    time.time()
				print('Building representation cache took: {:.3f}s'.format(end - start			)			)

		else:
				__lowercase=    torch.utils.data.DataLoader(
				    dataset=lowercase__     ,				batch_size=lowercase__     ,				shuffle=lowercase__     ,				collate_fn=lowercase__			)
				__lowercase=    torch.utils.data.DataLoader(dataset=lowercase__     ,				batch_size=lowercase__     ,				collate_fn=lowercase__			)

		if save_model:
				with open('{}_classifier_head_meta.json'.format(lowercase__			)     ,				'w'			) as meta_file:
						json.dump(lowercase__     ,				lowercase__			)

		__lowercase=    optim.Adam(discriminator.parameters()     ,				lr=0.0001			)

		for epoch in range(lowercase__			):
				__lowercase=    time.time()
				print('\nEpoch'     ,				epoch + 1			)

				train_epoch(
				    discriminator=lowercase__     ,				data_loader=lowercase__     ,				optimizer=lowercase__     ,				epoch=lowercase__     ,				log_interval=lowercase__     ,				device=lowercase__     ,				)
				evaluate_performance(data_loader=lowercase__     ,				discriminator=lowercase__     ,				device=lowercase__			)

				__lowercase=    time.time()
				print('Epoch took: {:.3f}s'.format(end - start			)			)

				print('\nExample prediction'			)
				predict(lowercase__     ,				lowercase__     ,				lowercase__     ,				cached=lowercase__     ,				device=lowercase__			)

				if save_model:
						# torch.save(discriminator.state_dict(),
						#           "{}_discriminator_{}.pt".format(
						#               args.dataset, epoch + 1
						#               ))
						torch.save(
						    discriminator.get_classifier().state_dict()     ,				'{}_classifier_head_epoch_{}.pt'.format(lowercase__     ,				epoch + 1			)     ,				)


if __name__ == "__main__":
	lowerCAmelCase					 =	argparse.ArgumentParser(description='''Train a discriminator on top of GPT-2 representations''')
	parser.add_argument(
	    '''--dataset''',
	    type=str,
	    default='''SST''',
	    choices=('''SST''', '''clickbait''', '''toxic''', '''generic'''),
	    help=(
	        '''dataset to train the discriminator on.'''
	        '''In case of generic, the dataset is expected'''
	        '''to be a TSBV file with structure: class \\t text'''
	    ),
	)
	parser.add_argument(
	    '''--dataset_fp''',
	    type=str,
	    default='''''',
	    help='''File path of the dataset to use. Needed only in case of generic datadset''',
	)
	parser.add_argument(
	    '''--pretrained_model''', type=str, default='''gpt2-medium''', help='''Pretrained model to use as encoder'''
	)
	parser.add_argument('''--epochs''', type=int, default=1_0, metavar='''N''', help='''Number of training epochs''')
	parser.add_argument(
	    '''--batch_size''', type=int, default=6_4, metavar='''N''', help='''input batch size for training (default: 64)'''
	)
	parser.add_argument(
	    '''--log_interval''',
	    type=int,
	    default=1_0,
	    metavar='''N''',
	    help='''how many batches to wait before logging training status''',
	)
	parser.add_argument('''--save_model''', action='''store_true''', help='''whether to save the model''')
	parser.add_argument('''--cached''', action='''store_true''', help='''whether to cache the input representations''')
	parser.add_argument('''--no_cuda''', action='''store_true''', help='''use to turn off cuda''')
	lowerCAmelCase					 =	parser.parse_args()

	train_discriminator(**(vars(args)))










from dataclasses import dataclass
from enum import Enum
from typing import List, Optional, Union

import numpy as np
import PIL
from PIL import Image

from ...utils import BaseOutput, is_torch_available, is_transformers_available





@dataclass
class    A					(     A_				):
							UpperCamelCase_     :							Optional[int] ={
							    '''sld_warmup_steps''': 15,
							    '''sld_guidance_scale''': 20,
							    '''sld_threshold''': 0.0,
							    '''sld_momentum_scale''': 0.0,
							    '''sld_mom_beta''': 0.0,
							}
							UpperCamelCase_     :							Tuple ={
							    '''sld_warmup_steps''': 10,
							    '''sld_guidance_scale''': 1_000,
							    '''sld_threshold''': 0.01,
							    '''sld_momentum_scale''': 0.3,
							    '''sld_mom_beta''': 0.4,
							}
							UpperCamelCase_     :							Union[str, Any] ={
							    '''sld_warmup_steps''': 7,
							    '''sld_guidance_scale''': 2_000,
							    '''sld_threshold''': 0.025,
							    '''sld_momentum_scale''': 0.5,
							    '''sld_mom_beta''': 0.7,
							}
							UpperCamelCase_     :							List[Any] ={
							    '''sld_warmup_steps''': 0,
							    '''sld_guidance_scale''': 5_000,
							    '''sld_threshold''': 1.0,
							    '''sld_momentum_scale''': 0.5,
							    '''sld_mom_beta''': 0.7,
							}





@dataclass
class    A					(     A_				):
							UpperCamelCase_     :							Union[List[PIL.Image.Image], np.ndarray]
							UpperCamelCase_     :							Optional[List[bool]]
							UpperCamelCase_     :							Optional[Union[List[PIL.Image.Image], np.ndarray]]
							UpperCamelCase_     :							Optional[str]


if is_transformers_available() and is_torch_available():
	from .pipeline_stable_diffusion_safe import StableDiffusionPipelineSafe
	from .safety_checker import SafeStableDiffusionSafetyChecker






