mirror of
https://github.com/SakanaAI/doc-to-lora.git
synced 2026-07-23 17:01:04 +02:00
python api + remove generate_multi_lora + rename config path + test eval scripts
This commit is contained in:
parent
530fcd46cc
commit
3b798c670c
27 changed files with 345 additions and 233 deletions
43
examples/python_api.py
Normal file
43
examples/python_api.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
# caveat: this interface only supports non-batched inputs
|
||||
# for batched inference please see `src/ctx_to_lora/modeling/hypernet.py`
|
||||
import torch
|
||||
|
||||
from ctx_to_lora.model_loading import get_tokenizer
|
||||
from ctx_to_lora.modeling.hypernet import ModulatedPretrainedModel
|
||||
|
||||
checkpoint_path = "../../ctx-to-lora/train_outputs/runs/Sep29_14-42-46_slurm0-a3nodeset-9_88483_1e7bb34e/checkpoint-40000/pytorch_model.bin"
|
||||
state_dict = torch.load(checkpoint_path, weights_only=False)
|
||||
model = ModulatedPretrainedModel.from_state_dict(
|
||||
state_dict, train=False, use_sequence_packing=False
|
||||
)
|
||||
model.reset()
|
||||
|
||||
tokenizer = get_tokenizer(model.base_model.name_or_path)
|
||||
|
||||
doc = open("data/sakana_wiki.txt").read()
|
||||
chat = [{"role": "user", "content": "Summarize what Sakana AI does."}]
|
||||
chat_ids = tokenizer.apply_chat_template(
|
||||
chat,
|
||||
add_special_tokens=False,
|
||||
return_attention_mask=False,
|
||||
add_generation_prompt=True,
|
||||
return_tensors="pt",
|
||||
).to(model.device)
|
||||
|
||||
|
||||
outputs = model.generate(input_ids=chat_ids, max_new_tokens=256)
|
||||
print(tokenizer.decode(outputs[0]))
|
||||
|
||||
|
||||
# calls after internalization will be influenced by internalized info
|
||||
model.internalize(doc)
|
||||
|
||||
outputs = model.generate(input_ids=chat_ids, max_new_tokens=256)
|
||||
print(tokenizer.decode(outputs[0]))
|
||||
|
||||
|
||||
# remove internalized info
|
||||
model.reset()
|
||||
|
||||
outputs = model.generate(input_ids=chat_ids, max_new_tokens=256)
|
||||
print(tokenizer.decode(outputs[0]))
|
||||
Loading…
Add table
Add a link
Reference in a new issue