Text Generation
Transformers
Safetensors
English
gpt2
causal-lm
from-scratch
tiny-model
educational
text-generation-inference
Instructions to use ARotting/snip-0.4m-base with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ARotting/snip-0.4m-base with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="ARotting/snip-0.4m-base")# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("ARotting/snip-0.4m-base") model = AutoModelForCausalLM.from_pretrained("ARotting/snip-0.4m-base", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use ARotting/snip-0.4m-base with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "ARotting/snip-0.4m-base" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ARotting/snip-0.4m-base", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/ARotting/snip-0.4m-base
- SGLang
How to use ARotting/snip-0.4m-base with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "ARotting/snip-0.4m-base" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ARotting/snip-0.4m-base", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "ARotting/snip-0.4m-base" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ARotting/snip-0.4m-base", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use ARotting/snip-0.4m-base with Docker Model Runner:
docker model run hf.co/ARotting/snip-0.4m-base
File size: 3,325 Bytes
24ebd71 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | from __future__ import annotations
from datasets import Dataset
from lora_data import GOALS, LESSONS, split_examples
from transformers import PreTrainedTokenizerFast
CONTEXT_LENGTH = 128
def corrupt_response(response: str, index: int) -> tuple[str, str]:
if index % 2 == 0:
present = next(goal for goal in GOALS if goal in response)
replacement = GOALS[(GOALS.index(present) + 2) % len(GOALS)]
return response.replace(present, replacement), "wrong_goal"
present = next(lesson for lesson in LESSONS if lesson in response)
replacement = LESSONS[(LESSONS.index(present) + 2) % len(LESSONS)]
return response.replace(present, replacement), "wrong_lesson"
def build_pairs() -> tuple[list[dict[str, str]], list[dict[str, str]]]:
train_examples, eval_examples = split_examples()
def convert(examples: list[dict[str, str]], offset: int) -> list[dict[str, str]]:
pairs = []
for index, example in enumerate(examples):
rejected, corruption = corrupt_response(example["response"], index + offset)
pairs.append(
{
"prompt": example["prompt"],
"chosen": example["response"],
"rejected": rejected,
"corruption": corruption,
}
)
return pairs
return convert(train_examples, 0), convert(eval_examples, len(train_examples))
def encode_pairs(
pairs: list[dict[str, str]],
tokenizer: PreTrainedTokenizerFast,
) -> Dataset:
rows: dict[str, list] = {
"chosen_input_ids": [],
"chosen_attention_mask": [],
"rejected_input_ids": [],
"rejected_attention_mask": [],
"corruption": [],
}
for pair in pairs:
required_goal = next(goal for goal in GOALS if goal in pair["prompt"])
required_lesson = next(lesson for lesson in LESSONS if lesson in pair["prompt"])
def compact_candidate(
response: str,
goal: str = required_goal,
lesson: str = required_lesson,
) -> str:
candidate_goal = next(goal for goal in GOALS if goal in response)
candidate_lesson = next(lesson for lesson in LESSONS if lesson in response)
return (
f"<bos>Required goal: {goal}. "
f"Required lesson: {lesson}. "
f"Candidate goal: {candidate_goal}. "
f"Candidate lesson: {candidate_lesson}.<eos>"
)
chosen = tokenizer(
compact_candidate(pair["chosen"]),
max_length=CONTEXT_LENGTH,
truncation=True,
padding="max_length",
add_special_tokens=False,
)
rejected = tokenizer(
compact_candidate(pair["rejected"]),
max_length=CONTEXT_LENGTH,
truncation=True,
padding="max_length",
add_special_tokens=False,
)
rows["chosen_input_ids"].append(chosen["input_ids"])
rows["chosen_attention_mask"].append(chosen["attention_mask"])
rows["rejected_input_ids"].append(rejected["input_ids"])
rows["rejected_attention_mask"].append(rejected["attention_mask"])
rows["corruption"].append(pair["corruption"])
return Dataset.from_dict(rows)
|