Text Generation
Transformers
Safetensors
lfm2
liquid
lfm2.5
weight-editing
model-editing
json
training-free
conversational
Instructions to use yasserrmd/LFM2.5-1.2B-Instruct-WSP-JSONFix with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use yasserrmd/LFM2.5-1.2B-Instruct-WSP-JSONFix with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="yasserrmd/LFM2.5-1.2B-Instruct-WSP-JSONFix") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("yasserrmd/LFM2.5-1.2B-Instruct-WSP-JSONFix") model = AutoModelForCausalLM.from_pretrained("yasserrmd/LFM2.5-1.2B-Instruct-WSP-JSONFix", device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=256) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use yasserrmd/LFM2.5-1.2B-Instruct-WSP-JSONFix with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "yasserrmd/LFM2.5-1.2B-Instruct-WSP-JSONFix" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "yasserrmd/LFM2.5-1.2B-Instruct-WSP-JSONFix", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/yasserrmd/LFM2.5-1.2B-Instruct-WSP-JSONFix
- SGLang
How to use yasserrmd/LFM2.5-1.2B-Instruct-WSP-JSONFix 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 "yasserrmd/LFM2.5-1.2B-Instruct-WSP-JSONFix" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "yasserrmd/LFM2.5-1.2B-Instruct-WSP-JSONFix", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'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 "yasserrmd/LFM2.5-1.2B-Instruct-WSP-JSONFix" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "yasserrmd/LFM2.5-1.2B-Instruct-WSP-JSONFix", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use yasserrmd/LFM2.5-1.2B-Instruct-WSP-JSONFix with Docker Model Runner:
docker model run hf.co/yasserrmd/LFM2.5-1.2B-Instruct-WSP-JSONFix
File size: 3,991 Bytes
1a2bb00 | 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 |
"""WSP 2.0 sparse reversible weight patches. No gradients. FP32 compute on copies, cast to stored dtype, count bitwise changes."""
import json, hashlib, torch
from safetensors.torch import save_file, load_file
torch.set_grad_enabled(False)
def _bits(t):
return t.contiguous().view(torch.int16) if t.dtype in (torch.bfloat16, torch.float16) else t.contiguous().view(torch.int32)
def tensor_sha(t):
return hashlib.sha256(t.detach().contiguous().view(torch.uint8).cpu().numpy().tobytes()).hexdigest()
def build(model, edits, dirs):
"""edits: list of {param, cols, kind: proj|scale, alpha|scale, dir: key in dirs}. Returns patch dict."""
P = dict(model.named_parameters()); out = {}; meta = []
for e in edits:
W = P[e["param"]].data; W32 = out[e["param"]]["W32"] if e["param"] in out else W.float().clone()
J = torch.as_tensor(e["cols"], device=W.device, dtype=torch.long)
old = W32[:, J].clone()
if e["kind"] == "proj":
d = dirs[e["dir"]].to(W.device).float(); d = d / d.norm()
W32[:, J] = old - e["alpha"] * torch.outer(d, d @ old)
elif e["kind"] == "scale":
W32[:, J] = old * e["scale"]
else: raise ValueError(e["kind"])
meta.append(dict(param=e["param"], kind=e["kind"], n_cols=len(e["cols"]), alpha=e.get("alpha"), scale=e.get("scale"), dir=e.get("dir"),
fp32_delta_fro=float((W32[:, J] - old).norm())))
out.setdefault(e["param"], {})["W32"] = W32
patch = {"tensors": {}, "meta": meta}
total = 0
for name, d in out.items():
W = P[name].data; new = d["W32"].to(W.dtype)
ch = (_bits(new) != _bits(W)).reshape(-1).nonzero().squeeze(-1)
patch["tensors"][name] = {"idx": ch.cpu(), "orig": W.reshape(-1)[ch].cpu().clone(), "new": new.reshape(-1)[ch].cpu().clone(),
"shape": list(W.shape), "dtype": str(W.dtype),
"stored_delta_fro": float((new.float() - W.float()).norm()), "orig_param_sha256": tensor_sha(W)}
total += ch.numel()
patch["changed_scalars"] = total
return patch
def apply(model, patch, reverse=False):
P = dict(model.named_parameters())
for name, t in patch["tensors"].items():
flat = P[name].data.view(-1); idx = t["idx"].to(flat.device)
flat[idx] = (t["orig"] if reverse else t["new"]).to(flat.device)
def verify(model, patch, state):
"""state: 'orig' or 'new'. Bitwise check of patched coordinates."""
P = dict(model.named_parameters())
for name, t in patch["tensors"].items():
flat = P[name].data.view(-1); idx = t["idx"].to(flat.device)
ref = (t["new"] if state == "new" else t["orig"]).to(flat.device)
if not torch.equal(_bits(flat[idx]), _bits(ref)): return False
return True
def save(patch, path_st, path_json, extra=None):
tens = {}
for name, t in patch["tensors"].items():
tens[f"{name}::idx"] = t["idx"].contiguous(); tens[f"{name}::orig"] = t["orig"].contiguous(); tens[f"{name}::new"] = t["new"].contiguous()
md = {"format": "wsp2-sparse-patch-v1", "changed_scalars": str(patch["changed_scalars"])}
save_file(tens, path_st, metadata=md)
j = {"meta": patch["meta"], "changed_scalars": patch["changed_scalars"],
"params": {n: {k: v for k, v in t.items() if k not in ("idx", "orig", "new")} | {"n_changed": int(t["idx"].numel())} for n, t in patch["tensors"].items()}}
if extra: j.update(extra)
j["safetensors_sha256"] = hashlib.sha256(open(path_st, "rb").read()).hexdigest()
json.dump(j, open(path_json, "w"), indent=1)
return j
def load(path_st, path_json):
tens = load_file(path_st); j = json.load(open(path_json)); patch = {"tensors": {}, "meta": j["meta"], "changed_scalars": j["changed_scalars"]}
for n, info in j["params"].items():
patch["tensors"][n] = {"idx": tens[f"{n}::idx"], "orig": tens[f"{n}::orig"], "new": tens[f"{n}::new"], **info}
return patch
|