sudo apt update
sudo apt install -y python3-venv git
python3 -m venv openelm-env
source openelm-env/bin/activateCPU
pip install torch torchvisionGPU(示例 CUDA 11.8)
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118git clone https://github.com/apple/corenet.git
cd corenet
pip install -e .OpenELM 实现位于:
corenet/modeling/models/openelmpip install huggingface_hub示例(270M 模型):
huggingface-cli download apple/OpenELM-270M \
--local-dir ./openelm-270m其他常见模型:
apple/OpenELM-450Mapple/OpenELM-1_1Bapple/OpenELM-3Bfrom corenet.modeling.models import get_model
model = get_model("openelm_270m")
model.eval()
# 输入 token ids
input_ids = [1, 2, 3]
output = model(input_ids)
print(output)pip install transformersfrom transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained("apple/OpenELM-270M")
tokenizer = AutoTokenizer.from_pretrained("apple/OpenELM-270M")
inputs = tokenizer("Hello, OpenELM!", return_tensors="pt")
outputs = model.generate(**inputs, max_length=50)
print(tokenizer.decode(outputs[0]))| 模型 | 显存需求(推理) |
|---|---|
| 270M | ~1GB |
| 450M | ~2GB |
| 1.1B | ~4GB |
| 3B | ~8GB |
model.to("cuda")
inputs = inputs.to("cuda")pip install bitsandbytesmodel = AutoModelForCausalLM.from_pretrained(
"apple/OpenELM-1_1B",
load_in_8bit=True
)pip install peft datasetsfrom peft import LoraConfig, get_peft_model
lora_config = LoraConfig(
r=8,
lora_alpha=16,
target_modules=["q_proj", "v_proj"]
)
model = get_peft_model(model, lora_config)Q1:OpenELM 不能直接用 transformers?
A:现在 HuggingFace 已支持 apple/OpenELM-*,推荐直接用它。
Q2:CoreNet 太复杂?
A:仅推理/微调建议用 Transformers 接口。
Q3:中文支持差?
如果你愿意,我可以:
你现在是想 推理、微调,还是 部署服务?