CodeParrot 🦜 is a GPT-2 model (110M parameters) trained to generate Python code.
from openmind import AutoTokenizer, AutoModelForCausalLM, is_torch_npu_available
from openmind_hub import snapshot_download
import torch.nn.functional as F
from torch import Tensor
import argparse
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_name_or_path",
type=str,
help="Path to model",
default="jeffding/codeparrot-small-openmind",
)
args = parser.parse_args()
return args
def main():
args = parse_args()
model_path = args.model_name_or_path
if is_torch_npu_available():
device = "npu:0"
else:
device = "cpu"
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(model_path)
model = model.to(device)
prompt = "def hello_world():"
inputs = tokenizer(prompt, return_tensors="pt", return_token_type_ids=False).to(device)
out = model.generate(**inputs, max_new_tokens=80).ravel()
out = tokenizer.decode(out)
print(out)
if __name__ == "__main__":
main()You can load the CodeParrot model and tokenizer directly in transformers:
from transformers import AutoTokenizer, AutoModelWithLMHead
tokenizer = AutoTokenizer.from_pretrained("codeparrot/codeparrot-small")
model = AutoModelWithLMHead.from_pretrained("codeparrot/codeparrot-small")
inputs = tokenizer("def hello_world():", return_tensors="pt")
outputs = model(**inputs)or with a pipeline:
from transformers import pipeline
pipe = pipeline("text-generation", model="codeparrot/codeparrot-small")
outputs = pipe("def hello_world():")The model was trained on the cleaned CodeParrot 🦜 dataset with the following settings:
| Config | Value |
|---|---|
| Batch size | 192 |
| Context size | 1024 |
| Training steps | 150'000 |
| Gradient accumulation | 1 |
| Gradient checkpointing | False |
| Learning rate | 5e-4 |
| Weight decay | 0.1 |
| Warmup steps | 2000 |
| Schedule | Cosine |
The training was executed on 16 x A100 (40GB) GPUs. This setting amounts to roughly 29 billion tokens.
We evaluated the model on OpenAI's HumanEval benchmark which consists of programming challenges:
| Metric | Value |
|---|---|
| pass@1 | 3.80% |
| pass@10 | 6.57% |
| pass@100 | 12.78% |
The pass@k metric tells the probability that at least one out of k generations passes the tests.