CoolFace
Modelpublic

mrm8488/phi-2-coder

sourceHugging Faceotherupdated 3y agoView on Hugging Face
29likes363downloads
Model Card

<div style="text-align:center;width:250px;height:250px;"> <img src="https://huggingface.co/mrm8488/phi-2-coder/resolve/main/phi-2-coder-logo.png" alt="phi-2 coder logo""> </div>

Phi-2 Coder ๐Ÿ‘ฉโ€๐Ÿ’ป

Phi-2 fine-tuned on the CodeAlpaca 20k instructions dataset by using the method QLoRA with PEFT library.

Model description ๐Ÿง 

Phi-2

Phi-2 is a Transformer with 2.7 billion parameters. It was trained using the same data sources as Phi-1.5, augmented with a new data source that consists of various NLP synthetic texts and filtered websites (for safety and educational value). When assessed against benchmarks testing common sense, language understanding, and logical reasoning, Phi-2 showcased a nearly state-of-the-art performance among models with less than 13 billion parameters.

Training and evaluation data ๐Ÿ“š

CodeAlpaca_20K: contains 20K instruction-following data used for fine-tuning the Code Alpaca model.

Training procedure

The following bitsandbytes quantization config was used during training:

  • โ€”quant_method: bitsandbytes
  • โ€”loadin8bit: True
  • โ€”loadin4bit: False
  • โ€”llmint8threshold: 6.0
  • โ€”llmint8skip_modules: None
  • โ€”llmint8enablefp32cpu_offload: False
  • โ€”llmint8hasfp16weight: False
  • โ€”bnb4bitquant_type: fp4
  • โ€”bnb4bitusedoublequant: False
  • โ€”bnb4bitcompute_dtype: float32

Training hyperparameters

The following hyperparameters were used during training:

  • โ€”learning_rate: 2.5e-05
  • โ€”trainbatchsize: 4
  • โ€”evalbatchsize: 8
  • โ€”seed: 66
  • โ€”gradientaccumulationsteps: 32
  • โ€”totaltrainbatch_size: 128
  • โ€”optimizer: Adam with betas=(0.9,0.999) and epsilon=1e-08
  • โ€”lrschedulertype: linear
  • โ€”num_epochs: 2

Training results

Training LossEpochStepValidation Loss
0.76310.36500.7174
0.67350.711000.6949
0.6961.071500.6893
0.78611.422000.6875
0.73461.782500.6867

HumanEval results ๐Ÿ“Š

WIP

Example of usage ๐Ÿ‘ฉโ€๐Ÿ’ป

py
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

model_id = "mrm8488/phi-2-coder"

tokenizer = AutoTokenizer.from_pretrained(model_id, add_bos_token=True, trust_remote_code=True, use_fast=False)

model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True, torch_dtype=torch.float16, device="auto")

def generate(
        instruction,
        max_new_tokens=128,
        temperature=0.1,
        top_p=0.75,
        top_k=40,
        num_beams=2,
        **kwargs,
):
    prompt = "Instruct: " + instruction + "\nOutput:"
    print(prompt)
    inputs = tokenizer(prompt, return_tensors="pt")
    input_ids = inputs["input_ids"].to("cuda")
    attention_mask = inputs["attention_mask"].to("cuda")
  
    with torch.no_grad():
        generation_output = model.generate(
            input_ids=input_ids,
            attention_mask=attention_mask,
            max_new_tokens=max_new_tokens,
            eos_token_id = tokenizer.eos_token_id,
            use_cache=True,
            early_stopping=True
        )
    output = tokenizer.decode(generation_output[0])
    return output.split("\nOutput:")[1].lstrip("\n")

instruction = "Design a class for representing a person in Python."
print(generate(instruction))

How to use with MLX.

bash
# Install mlx, mlx-examples, huggingface-cli
pip install mlx
pip install huggingface_hub hf_transfer
git clone https://github.com/ml-explore/mlx-examples.git

# Download model
export HF_HUB_ENABLE_HF_TRANSFER=1
huggingface-cli download --local-dir phi-2-coder mrm8488/phi-2-coder

# Run example
python mlx-examples/llms/phi2.py --model-path phi-2-coder --prompt "Design a class for representing a person in Python" 

Citation

@misc {manuel_romero_2023,
	author       = { {Manuel Romero} },
	title        = { phi-2-coder (Revision 4ae69ae) },
	year         = 2023,
	url          = { https://huggingface.co/mrm8488/phi-2-coder },
	doi          = { 10.57967/hf/1518 },
	publisher    = { Hugging Face }
}