Views
No views yet
1{
2 "r": 320, # Rank (Microsoft's recommended config)
3 "lora_alpha": 320, # Alpha (1:1 ratio for Phi-4)
4 "lora_dropout": 0.0, # No dropout
5 "bias": "none",
6 "task_type": "CAUSAL_LM",
7 "target_modules": [
8 "q_proj", "k_proj", "v_proj", "o_proj",
9 "gate_proj", "up_proj", "down_proj"
10 ]
11}1{
2 "model": "phi4-mini",
3 "max_seq_length": 131072, # 128K context
4 "batch_size": 1,
5 "gradient_accumulation_steps": 8,
6 "effective_batch_size": 8,
7 "learning_rate": 1e-5,
8 "warmup_steps": 20,
9 "max_grad_norm": 1.0,
10 "lr_scheduler": "linear",
11 "optimizer": "paged_adamw_8bit",
12 "bf16": True,
13 "gradient_checkpointing": True,
14 "seed": 42
15}1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3import json
4
5# Load the merged model (ready to use)
6model = AutoModelForCausalLM.from_pretrained(
7 "UWV/wim-n3-phi4-mini-merged", # Update with actual repo
8 torch_dtype=torch.bfloat16,
9 device_map="auto",
10 trust_remote_code=True
11)
12tokenizer = AutoTokenizer.from_pretrained("UWV/wim-n3-phi4-mini-merged")
13
14# Prepare input (typically very long with entity and schema information)
15entities = [
16 {"name": "Amsterdam", "type": "City"},
17 {"name": "Netherlands", "type": "Country"}
18]
19schemas = {
20 "City": "https://schema.org/City",
21 "Country": "https://schema.org/Country"
22}
23
24messages = [
25 {
26 "role": "system",
27 "content": "You are an expert in creating JSON-LD representations using Schema.org vocabulary."
28 },
29 {
30 "role": "user",
31 "content": f"""Transform the following entities into JSON-LD format using Schema.org:
32
33Entities: {json.dumps(entities, ensure_ascii=False)}
34Schemas: {json.dumps(schemas, ensure_ascii=False)}
35
36Create a complete JSON-LD representation with proper @context and @type declarations."""
37 }
38]
39
40# Apply chat template and generate
41prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
42inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=131072)
43inputs = {k: v.to(model.device) for k, v in inputs.items()}
44
45with torch.no_grad():
46 outputs = model.generate(
47 **inputs,
48 max_new_tokens=4096, # JSON-LD can be long
49 temperature=0.1, # Low temperature for valid JSON
50 do_sample=True,
51 top_p=0.95,
52 pad_token_id=tokenizer.pad_token_id,
53 eos_token_id=tokenizer.eos_token_id,
54 )
55
56# Decode and parse response
57response = tokenizer.decode(outputs[0], skip_special_tokens=True)
58if "assistant:" in response:
59 json_ld = response.split("assistant:")[-1].strip()
60
61print(json_ld)1from transformers import AutoModelForCausalLM, AutoTokenizer
2from peft import PeftModel
3import torch
4
5# Load base model
6base_model = AutoModelForCausalLM.from_pretrained(
7 "microsoft/Phi-4-mini-instruct",
8 torch_dtype=torch.bfloat16,
9 device_map="auto",
10 trust_remote_code=True
11)
12
13# Load adapter
14model = PeftModel.from_pretrained(
15 base_model,
16 "UWV/wim-n3-phi4-mini-adapter" # Update with actual repo
17)
18tokenizer = AutoTokenizer.from_pretrained("UWV/wim-n3-phi4-mini-adapter")
19
20# Use same inference code as above...1{
2 "@context": "https://schema.org",
3 "@graph": [
4 {
5 "@type": "City",
6 "@id": "_:amsterdam",
7 "name": "Amsterdam",
8 "containedInPlace": {
9 "@id": "_:netherlands"
10 }
11 },
12 {
13 "@type": "Country",
14 "@id": "_:netherlands",
15 "name": "Netherlands"
16 }
17 ]
18}UWV/wim-n3-phi4-mini-merged (681MB adapter + base model)UWV/wim-n3-phi4-mini-adapter (681MB)TORCH_COMPILE_DISABLE=1 for Phi-4 compatibility1@misc{wim-n3-phi4-mini,
2 author = {UWV InnovatieHub},
3 title = {Phi-4-mini N3 Transform to JSON-LD Model},
4 year = {2025},
5 publisher = {HuggingFace},
6 url = {https://huggingface.co/UWV/wim-n3-phi4-mini-merged}
7}