monuirctc commited on
Commit
1596d3b
1 Parent(s): 6db6d0a

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +63 -0
README.md CHANGED
@@ -1,3 +1,66 @@
1
  ---
2
  license: other
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: other
3
  ---
4
+
5
+
6
+ Usage
7
+ See also: colab with an example usage of LongLLaMA.
8
+
9
+ Requirements
10
+ pip install --upgrade pip
11
+ pip install transformers==4.30 sentencepiece accelerate
12
+
13
+ Loading model
14
+ import torch
15
+ from transformers import LlamaTokenizer, AutoModelForCausalLM
16
+
17
+ tokenizer = LlamaTokenizer.from_pretrained("monuirctc/llama-7b-instruct-indo")
18
+ model = AutoModelForCausalLM.from_pretrained("monuirctc/llama-7b-instruct-indo",
19
+ torch_dtype=torch.float32,
20
+ trust_remote_code=True)
21
+
22
+ Input handling and generation
23
+ LongLLaMA uses the Hugging Face interface, the long input given to the model will be split into context windows and loaded into the memory cache.
24
+
25
+ prompt = "My name is Julien and I like to"
26
+ input_ids = tokenizer(prompt, return_tensors="pt").input_ids
27
+ outputs = model(input_ids=input_ids)
28
+
29
+ During the model call, one can provide the parameter last_context_length (default 1024), which specifies the number of tokens left in the last context window. Tuning this parameter can improve generation as the first layers do not have access to memory. See details in How LongLLaMA handles long inputs.
30
+
31
+ generation_output = model.generate(
32
+ input_ids=input_ids,
33
+ max_new_tokens=256,
34
+ num_beams=1,
35
+ last_context_length=1792,
36
+ do_sample=True,
37
+ temperature=1.0,
38
+ )
39
+ print(tokenizer.decode(generation_output[0]))
40
+
41
+ Additional configuration
42
+ LongLLaMA has several other parameters:
43
+
44
+ mem_layers specifies layers endowed with memory (should be either an empty list or a list of all memory layers specified in the description of the checkpoint).
45
+ mem_dtype allows changing the type of memory cache
46
+ mem_attention_grouping can trade off speed for reduced memory usage. When equal to (4, 2048), the memory layers will process at most 4*2048 queries at once (4 heads and 2048 queries for each head).
47
+ import torch
48
+ from transformers import LlamaTokenizer, AutoModelForCausalLM
49
+
50
+ tokenizer = LlamaTokenizer.from_pretrained("monuirctc/llama-7b-instruct-indo")
51
+ model = AutoModelForCausalLM.from_pretrained(
52
+ "monuirctc/llama-7b-instruct-indo", torch_dtype=torch.float32,
53
+ mem_layers=[],
54
+ mem_dtype='bfloat16',
55
+ trust_remote_code=True,
56
+ mem_attention_grouping=(4, 2048),
57
+ )
58
+
59
+ Drop-in use with LLaMA code
60
+ LongLLaMA checkpoints can also be used as a drop-in replacement for LLaMA checkpoints in Hugging Face implementation of LLaMA, but in this case, they will be limited to the original context length of 2048.
61
+
62
+ from transformers import LlamaTokenizer, LlamaForCausalLM
63
+ import torch
64
+
65
+ tokenizer = LlamaTokenizer.from_pretrained("monuirctc/llama-7b-instruct-indo")
66
+ model = LlamaForCausalLM.from_pretrained("monuirctc/llama-7b-instruct-indo", torch_dtype=torch.float32)