Views
No views yet
1# in_file_name 所在文件名
2# pre_text,next_text <|speaker_name_A|>dialogue_1<|speaker_name_B|>dialogue_2.....
3# target_text <|speaker_name_TAR|>dialogue_TAR
4
5prompt = f"<|im_start|>user\n文件:{in_file_name}\n上下文:{pre_text}{target_text}{next_text}\n目标原文:{target_text}<|im_end|>\n<|im_start|>assistant\n"
6
7model_name = "../dl_models/Qwen3-4B"
8lora_model_name = "Qwen3-4B-LIL-DIS-LoRA-16-30000"
9device = "cuda" if torch.cuda.is_available() else "cpu"
10
11Q_config = BitsAndBytesConfig(
12 load_in_4bit=True,
13 bnb_4bit_quant_type="nf4",
14 bnb_4bit_use_double_quant=True,
15 bnb_4bit_compute_dtype=torch.bfloat16,
16)
17
18# load the tokenizer and the model
19tokenizer: Qwen2TokenizerFast = AutoTokenizer.from_pretrained(model_name)
20model: Qwen3ForCausalLM = AutoModelForCausalLM.from_pretrained(
21 model_name,
22 quantization_config=Q_config,
23 # attn_implementation="flash_attention_2"
24)
25
26lora_model = PeftModel.from_pretrained(model, lora_model_name).to(device)
27
28model_input_batch = tokenizer([prompt],return_tensors="pt").to(device)
29output_ids = lora_model.generate(**model_input_batch,max_new_tokens=2048)
30
31res = tokenizer.batch_decode(output_ids[:, model_input_batch.input_ids.shape[1]:],skip_special_tokens=True,)
32print(res[0])1# in_file_name 所在文件名
2# pre_text,next_text <|speaker_name_A|>dialogue_1<|speaker_name_B|>dialogue_2.....
3# target_text <|speaker_name_TAR|>dialogue_TAR
4
5prompt = f"文件:{in_file_name}\n上下文:{pre_text}{target_text}{next_text}\n目标原文:{target_text}\n翻译:"
6
7model_name = "../dl_models/Qwen3-1.7B"
8lora_model_name = "Qwen3-1.7B-bnb4-LIL-16-30000"
9device = "cuda" if torch.cuda.is_available() else "cpu"
10
11Q_config = BitsAndBytesConfig(
12 load_in_4bit=True,
13 bnb_4bit_quant_type="nf4",
14 bnb_4bit_use_double_quant=True,
15 bnb_4bit_compute_dtype=torch.bfloat16,
16)
17
18# load the tokenizer and the model
19tokenizer: Qwen2TokenizerFast = AutoTokenizer.from_pretrained(model_name)
20model: Qwen3ForCausalLM = AutoModelForCausalLM.from_pretrained(
21 model_name,
22 quantization_config=Q_config,
23 # attn_implementation="flash_attention_2"
24)
25
26lora_model = PeftModel.from_pretrained(model, lora_model_name).to(device)
27
28model_input_batch = tokenizer([prompt],return_tensors="pt").to(device)
29output_ids = lora_model.generate(**model_input_batch,max_new_tokens=512)
30
31res = tokenizer.batch_decode(output_ids[:, model_input_batch.input_ids.shape[1]:],skip_special_tokens=True,)
32print(res[0])1Q_config = BitsAndBytesConfig(
2 load_in_4bit=True,
3 bnb_4bit_quant_type="nf4",
4 bnb_4bit_use_double_quant=True,
5 bnb_4bit_compute_dtype=torch.bfloat16,
6)
7
8L_config = LoraConfig(
9 r=lora_rank,
10 use_rslora=True,
11 target_modules=["q_proj", "v_proj", "k_proj"],
12 lora_dropout=0.1,
13 bias="none",
14)
15
16optimizer = AdamW8bit(lora_model.parameters(), lr=1e-5, betas=(0.9, 0.95))
17
18step_num = 30000
19scheduler = get_wsd_schedule(
20 optimizer=optimizer,
21 num_warmup_steps=step_num * 0.05,
22 num_stable_steps=step_num * 0.85,
23 num_decay_steps=step_num * 0.1 + 1
24)
25
26batch_size = 8