def test_gpt3(): gpt3 = models.get_model('gpt3').create_from_arg_string("engine=ada") (ll_dog, ig_dog), (ll_cat, ig_cat), (_, ll_max_0), (_, ll_max_1), (_, ll_max_2), *vals = gpt3.loglikelihood([ ('The quick brown fox jumps over the lazy', ' dog'), ('The quick brown fox jumps over the lazy', ' cat'), ('The quick brown fox jumps over the lazy', ', lazy dog'), ('The quick brown fox jumps over the lazy', ', lazy fox'), ('The quick brown fox jumps over the lazy', ', lazy fox and they both fall to the ground'), ("""A mult""", """ilayer perceptron (MLP) is a class of feedforward artificial neural network (ANN)"""), ("""The term MLP is used ambiguously, sometimes loosely to any feedforward ANN, sometimes strictly to refer to networks composed of multiple layers of perceptrons""", """ (with threshold activation); see § Terminology"""), ("""Multilayer perceptrons are sometimes coll""", """oquially referred to as "vanilla" neural networks, especially when they have a single hidden layer.[1]"""), ("""An MLP consists of at least three layers of nodes: an input layer, a hidden layer and an output layer. Except for the input nodes, each node is a neuron that uses a nonlinear""", """ activation function."""), ("""MLP utilizes a supervised""", """ learning technique called backpropagation for training.[2][3] Its multiple layers and non-linear activation distinguish MLP from a linear perceptron. It can distinguish data that is not linearly separable.[4]"""), ("""Recent work has demonstrated substantial gains on many NLP tasks and benchmarks by pre-training on a large corpus of text followed by fine-tuning on a specific task. While typically task-agnostic""", """ in architecture, this method still requires task-specific fine-tuning datasets of thousands or tens of thousands of examples. By contrast, humans can generally perform a new language task from only a few examples or from simple instructions - something which current NLP systems still largely struggle to do. Here we show that scaling up language models greatly improves task-agnostic, few-shot performance, sometimes even reaching competitiveness with prior state-of-the-art fine-tuning approaches. """), ("""Specifically, we train GPT-3, an autoregressive language model with 175""", """ billion parameters, 10x more than any previous non-sparse language model, and test its performance in the few-shot setting. For all tasks, GPT-3 is applied without any gradient updates or fine-tuning, with tasks and few-shot demonstrations specified purely via text interaction with the model. GPT-3 achieves strong performance on many NLP datasets, including translation, question-answering, and cloze tasks, as well as several tasks that require on-the-fly reasoning or domain adaptation, such as unscrambling words, using a novel word in a sentence, or performing 3-digit arithmetic. At the same time, we also identify some datasets where GPT-3's few-shot learning still struggles, as well as some datasets where GPT-3 faces methodological issues related to training on large web corpora. Finally, we find that GPT-3 can generate samples of news articles which human evaluators have difficulty distinguishing from articles written by humans. We discuss broader societal impacts of this finding and of GPT-3 in general."""), ("""A mult""", """ilayer perceptron (MLP) is a class of feedforward artificial neural network (ANN)"""), ("""Hello""", """ World"""), ]) assert ll_dog > ll_cat assert not ig_cat assert ig_dog assert not ll_max_0 assert not ll_max_1 assert not ll_max_2 # test empty context gpt3.loglikelihood([('', 'test')]) gen, = gpt3.greedy_until([ ('The quick brown fox jumps over the lazy', ['.', '\n']) ]) assert gen == ' dog' print([x[0] for x in vals]) targets = [-34.85833048, -47.114367866, -45.43520782100001, -5.289627985, -133.96879783896998, -321.30299892039994, -658.0542459504098, -34.85833048, -7.5162964] for (pred, _), tgt in zip(vals, targets): assert pred == pytest.approx(tgt, rel=1e-3)
def test_evaluator(taskname, Task): task_dict = tasks.get_task_dict([taskname]) lm = models.get_model('dummy')() def ll_fn(reqs): for ctx, cont in reqs: # space convention assert ctx[-1] != ' ' assert cont[0] == ' ' or ctx[-1] == '\n' res = [] random.seed(42) for _ in reqs: res.append((-random.random(), False)) return res lm.loglikelihood = ll_fn evaluator.evaluate(lm, task_dict, False, 0, 10)
def test_gpt2_perplexity(): gpt2 = models.get_model('gpt2').create_from_arg_string("device=cpu") test_string = "We study empirical scaling laws for language model performance on the cross-entropy loss." perplexity = gpt2.loglikelihood_rolling([(test_string, )])[0] tgt = sum([ -4.9599953, -8.069298, -8.308624, -10.178513, -8.906924, -1.9318912, -7.745445, -7.146077, -5.2072, -3.5882986, -1.9957212, -8.044922, -0.20841774, -5.1096807, -0.099879116, -8.888423, -4.6180487 ]) assert perplexity == pytest.approx(tgt, rel=1e-3) # Hack: modify gpt2 to have shorter context length to induce rolling windows gpt2.max_length = 5 perplexity = gpt2.loglikelihood_rolling([(test_string, )])[0] tgt = sum([ -4.96001, -8.069275, -8.308612, -10.178482, -8.90691, -4.037338, -8.09261, -11.662385, -10.206891, -4.425003, -2.2563353, -7.909143, -1.9304147, -7.3610134, -2.3120654, -7.3229, -2.1643813 ]) assert perplexity == pytest.approx(tgt, rel=1e-3)
def cross_validation(args, training_docs, task, shuffled_train_indices=None, b_idx=None): lm = models.get_model(args.model).create_from_arg_string(args.model_args) results = [] if shuffled_train_indices is None: shuffled_train_indices = [*range(len(training_docs))] random.shuffle(shuffled_train_indices) for i in range(0, len(shuffled_train_indices), args.num_fewshot): train_indices = shuffled_train_indices[i:i + args.num_fewshot] train_subset = [] val_subset = [] for i in range(len(shuffled_train_indices)): if i in train_indices: train_subset.append(training_docs[i]) else: val_subset.append(training_docs[i]) print(len(val_subset)) accuracy = task.evaluate(val_subset, lm, args.provide_description, args.num_fewshot, train_doc=train_subset) result = {} result['indices'] = train_indices result['accuracy'] = accuracy results.append(result) with open( args.output_path + '/' + args.task_names[0] + "_" + str(b_idx) + str(i), "w") as f: dumped = json.dumps(result) f.write(dumped) return results
def main(): args = parse_args() random.seed(args.seed) np.random.seed(args.seed) lm = models.get_model(args.model).create_from_arg_string(args.model_args) if args.cache: lm = base.CachingLM(lm, 'lm_cache/' + args.model + '_' + args.model_args.replace('=', '-').replace(',', '_') + '.db') if args.tasks == "all_tasks": task_names = tasks.ALL_TASKS else: task_names = args.tasks.split(",") task_dict = tasks.get_task_dict(task_names) results = evaluator.evaluate(lm, task_dict, args.provide_description, args.num_fewshot, args.limit) dumped = json.dumps(results, indent=2) print(dumped) if args.output_path: with open(args.output_path, "w") as f: f.write(dumped)
def main(args): lm = models.get_model(args.model).create_from_arg_string(args.model_args) if args.tasks == "all_tasks": task_names = tasks.ALL_TASKS else: task_names = args.tasks.split(",") task_dict = tasks.get_task_dict(task_names) results = {} for task_name, task in task_dict.items(): if not task.has_validation_docs(): continue result = task.evaluate( docs=task.validation_docs(), lm=lm, provide_description=args.provide_description, num_fewshot=args.num_fewshot, ) results[task_name] = result dumped = json.dumps(results, indent=2) print(dumped) if args.output_path: with open(args.output_path + task_names[0] + '.jsonl', "w") as f: f.write(dumped)
def main(): args = parse_args() random.seed(args.seed) np.random.seed(args.seed) lm = models.get_model(args.model).create_from_arg_string(args.model_args) if args.tasks == "all_tasks": task_names = tasks.ALL_TASKS else: task_names = args.tasks.split(",") task_dict = tasks.get_task_dict(task_names) # TODO: fall back to test docs task_dict_items = [(name, task) for name, task in task_dict.items() if task.has_validation_docs()] results = collections.defaultdict(dict) requests = collections.defaultdict(list) requests_origin = collections.defaultdict(list) # if we ever run into issues where the eval tasks don't fit in memory and we can't afford a machine with bigger memory, # we can always modify this plumbing to support that, but i didn't want to include it just yet because overengineering is bad # (or we could make it write the requests to disk and then read them back out again - probably using an sqlite db because of all the moving parts we have # TODO: we need unit tests & sanity checks or something to ensure that the return of `validation_docs` is stable docs = {} # get lists of each type of requeste for task_name, task in task_dict_items: for doc_id, doc in enumerate( itertools.islice(task.validation_docs(), 0, args.limit)): docs[(task_name, doc_id)] = doc ctx = task.fewshot_context( doc=doc, provide_description=args.provide_description, num_fewshot=args.num_fewshot, ) reqs = task.construct_requests(doc, ctx) for i, req in enumerate(reqs): requests[req.type].append(req) # i: index in requests for a single task instance # doc_id: unique id that we can get back to a doc using `docs` requests_origin[req.type].append((i, task_name, doc, doc_id)) # all responses for each (task, doc) process_res_queue = collections.defaultdict(list) # execute each type of request for reqtype, reqs in requests.items(): # TODO: right now, this code runs multiple seperate LM requests for multiple Requests differing # only in index. We could implement some kind of caching, but that would be more of a bandaid # solution. we could also implement some kind of autogrouping here; they should end up next to each other. resps = getattr(lm, reqtype)([req.args for req in reqs]) resps = [ x if req.index is None else x[req.index] for x, req in zip(resps, reqs) ] for resp, (i, task_name, doc, doc_id) in zip(resps, requests_origin[reqtype]): process_res_queue[(task_name, doc_id)].append((i, resp)) vals = collections.defaultdict(list) # unpack results and sort back in order and return control to Task for (task_name, doc_id), requests in process_res_queue.items(): requests.sort(key=lambda x: x[0]) requests = [x[1] for x in requests] task = task_dict[task_name] doc = docs[(task_name, doc_id)] metrics = task.process_results(doc, requests) for metric, value in metrics.items(): vals[(task_name, metric)].append(value) # aggregate results for (task_name, metric), items in vals.items(): task = task_dict[task_name] results[task_name][metric] = task.aggregation()[metric](items) dumped = json.dumps(results, indent=2) print(dumped) if args.output_path: with open(args.output_path, "w") as f: f.write(dumped)
def main(): args = parse_args() random.seed(args.seed) np.random.seed(args.seed) lm = models.get_model(args.model).create_from_arg_string( args.model_args, { 'batch_size': args.batch_size, 'device': args.device }) if args.limit: print( "WARNING: --limit SHOULD ONLY BE USED FOR TESTING. REAL METRICS SHOULD NOT BE COMPUTED USING LIMIT." ) if not args.no_cache: lm = base.CachingLM( lm, 'lm_cache/' + args.model + '_' + args.model_args.replace( '=', '-').replace(',', '_').replace('/', '-') + '.db') if args.tasks == "all_tasks": task_names = tasks.ALL_TASKS else: task_names = args.tasks.split(",") task_dict = tasks.get_task_dict(task_names) results = evaluator.evaluate(lm, task_dict, args.provide_description, args.num_fewshot, args.limit) dumped = json.dumps(results, indent=2) print(dumped) if args.output_path: with open(args.output_path, "w") as f: f.write(dumped) # MAKE TABLE from pytablewriter import MarkdownTableWriter, LatexTableWriter md_writer = MarkdownTableWriter() latex_writer = LatexTableWriter() md_writer.headers = ["Task", "Version", "Metric", "Value", "", "Stderr"] latex_writer.headers = ["Task", "Version", "Metric", "Value", "", "Stderr"] values = [] for k, dic in results["results"].items(): version = results["versions"][k] for m, v in dic.items(): if m.endswith("_stderr"): continue if m + "_stderr" in dic: se = dic[m + "_stderr"] values.append([k, version, m, '%.4f' % v, '±', '%.4f' % se]) else: values.append([k, version, m, '%.4f' % v, '', '']) k = "" version = "" md_writer.value_matrix = values latex_writer.value_matrix = values # todo: make latex table look good # print(latex_writer.dumps()) print( f"{args.model} ({args.model_args}), limit: {args.limit}, provide_description: {args.provide_description}, num_fewshot: {args.num_fewshot}, batch_size: {args.batch_size}" ) print(md_writer.dumps())
def test_gpt2(): gpt2 = models.get_model("gpt2").create_from_arg_string("device=cpu") ( (ll_dog, ig_dog), (ll_cat, ig_cat), (_, ll_max_0), (_, ll_max_1), (_, ll_max_2), *vals, ) = gpt2.loglikelihood([ ("The quick brown fox jumps over the lazy", " dog"), ("The quick brown fox jumps over the lazy", " cat"), ("The quick brown fox jumps over the lazy", ", lazy dog"), ("The quick brown fox jumps over the lazy", ", lazy fox"), ( "The quick brown fox jumps over the lazy", ", lazy fox and they both fall to the ground", ), ( """A mult""", """ilayer perceptron (MLP) is a class of feedforward artificial neural network (ANN)""", ), ( """The term MLP is used ambiguously, sometimes loosely to any feedforward ANN, sometimes strictly to refer to networks composed of multiple layers of perceptrons""", """ (with threshold activation); see § Terminology""", ), ( """Multilayer perceptrons are sometimes coll""", """oquially referred to as "vanilla" neural networks, especially when they have a single hidden layer.[1]""", ), ( """An MLP consists of at least three layers of nodes: an input layer, a hidden layer and an output layer. Except for the input nodes, each node is a neuron that uses a nonlinear""", """ activation function.""", ), ( """MLP utilizes a supervised""", """ learning technique called backpropagation for training.[2][3] Its multiple layers and non-linear activation distinguish MLP from a linear perceptron. It can distinguish data that is not linearly separable.[4]""", ), ( """Recent work has demonstrated substantial gains on many NLP tasks and benchmarks by pre-training on a large corpus of text followed by fine-tuning on a specific task. While typically task-agnostic""", """ in architecture, this method still requires task-specific fine-tuning datasets of thousands or tens of thousands of examples. By contrast, humans can generally perform a new language task from only a few examples or from simple instructions - something which current NLP systems still largely struggle to do. Here we show that scaling up language models greatly improves task-agnostic, few-shot performance, sometimes even reaching competitiveness with prior state-of-the-art fine-tuning approaches. """, ), ( """Specifically, we train GPT-3, an autoregressive language model with 175""", """ billion parameters, 10x more than any previous non-sparse language model, and test its performance in the few-shot setting. For all tasks, GPT-3 is applied without any gradient updates or fine-tuning, with tasks and few-shot demonstrations specified purely via text interaction with the model. GPT-3 achieves strong performance on many NLP datasets, including translation, question-answering, and cloze tasks, as well as several tasks that require on-the-fly reasoning or domain adaptation, such as unscrambling words, using a novel word in a sentence, or performing 3-digit arithmetic. At the same time, we also identify some datasets where GPT-3's few-shot learning still struggles, as well as some datasets where GPT-3 faces methodological issues related to training on large web corpora. Finally, we find that GPT-3 can generate samples of news articles which human evaluators have difficulty distinguishing from articles written by humans. We discuss broader societal impacts of this finding and of GPT-3 in general.""", ), ( """A mult""", """ilayer perceptron (MLP) is a class of feedforward artificial neural network (ANN)""", ), ("""Hello""", """ World"""), ]) assert ll_dog > ll_cat assert not ig_cat assert not ll_max_0 assert ll_max_1 assert ll_max_2 # test empty context gpt2.loglikelihood([("", "test")]) (gen, ) = gpt2.greedy_until([("The quick brown fox jumps over the lazy", [".", "\n"])]) assert gen == ", lazy fox and they both fall to the ground" targets = [ -61.60536193847656, -56.57843780517578, -62.131004333496094, -9.799489974975586, -153.96334838867188, -341.222900390625, -731.1475830078125, -61.60536193847656, -8.682319641113281, ] for (pred, _), tgt in zip(vals, targets): assert pred == pytest.approx(tgt, rel=1e-3)