Views
No views yet
1from transformers import AutoTokenizer, AutoModelForCausalLM
2import torch
3
4# Load model and tokenizer
5model_name = "jahidhasan/os_reasoning_model-v2"
6tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
7model = AutoModelForCausalLM.from_pretrained(
8 model_name,
9 torch_dtype=torch.bfloat16,
10 device_map="auto",
11 trust_remote_code=True
12)
13
14def ask_os_question(question):
15 prompt = f'''<|begin_of_text|><|start_header_id|>system<|end_header_id|>
16
17You are an expert in Operating Systems. Provide clear, step-by-step reasoning for OS concepts and problems.
18
19<|eot_id|><|start_header_id|>user<|end_header_id|>
20
21{question}
22
23<|eot_id|><|start_header_id|>assistant<|end_header_id|>
24
25'''
26
27 inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
28
29 with torch.no_grad():
30 outputs = model.generate(
31 **inputs,
32 max_new_tokens=500,
33 do_sample=True,
34 temperature=0.7,
35 top_p=0.9,
36 repetition_penalty=1.1
37 )
38
39 response = tokenizer.decode(outputs[0], skip_special_tokens=True)
40
41 # Extract assistant response
42 assistant_start = response.find("<|start_header_id|>assistant<|end_header_id|>")
43 if assistant_start != -1:
44 response = response[assistant_start + len("<|start_header_id|>assistant<|end_header_id|>"):].strip()
45
46 return response
47
48# Example usage
49question = "What is a deadlock in operating systems and how can it be prevented?"
50answer = ask_os_question(question)
51print(answer)Let me explain virtual memory step by step:
Step 1: Definition and Context
Virtual memory is a memory management technique that provides an abstraction layer between the physical memory and the processes running on the system.
Step 2: How it Works
The mechanism involves mapping virtual addresses to physical addresses through page tables, allowing processes to have their own virtual address space that may be larger than physical RAM.
Step 3: Why it's Important
This is crucial because it enables memory isolation between processes, allows efficient memory utilization, and provides the illusion of unlimited memory to applications.
Step 4: Practical Example
In practice, when a process accesses a virtual address, the Memory Management Unit (MMU) translates it to a physical address, handling page faults when data needs to be loaded from storage.
Therefore, virtual memory is a fundamental abstraction that plays a vital role in modern operating system memory management.1@misc{os-reasoning-model-v2,
2 author = {Jahid Hasan},
3 title = {Operating System Reasoning Model v2.0},
4 year = {2025},
5 publisher = {Hugging Face},
6 url = {https://huggingface.co/jahidhasan/os_reasoning_model-v2},
7 note = {Fine-tuned from microsoft/DialoGPT-medium}
8}