gyung commited on
Commit
ba7de8b
·
verified ·
1 Parent(s): b91e208

Add CPU and GPU usage examples

Browse files
Files changed (1) hide show
  1. README.md +97 -0
README.md CHANGED
@@ -45,6 +45,103 @@ This checkpoint is intended for research on:
45
  - recurrent-state contamination during long generation
46
  - Reference-State Reset with Rolling Replay, a GDN/RNN adaptation of the R-SWA idea
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  ## Known Results
49
 
50
  1B 32K answer-focused run. MQAR likelihood improved 4K/16K but damaged 1K and did not transfer to 32K/64K: final 1K 0.0000, 2K 0.0625, 4K 0.1562, 8K 0.0625, 16K 0.1875, 32K 0.0000, 64K 0.0000.
 
45
  - recurrent-state contamination during long generation
46
  - Reference-State Reset with Rolling Replay, a GDN/RNN adaptation of the R-SWA idea
47
 
48
+ ## Usage
49
+
50
+ These checkpoints use the FLA Gated DeltaNet implementation. In the current
51
+ Long-GDN environment, plain `GatedDeltaNetForCausalLM.from_pretrained()` can
52
+ hit a Transformers 5.x tied-weight metadata issue. The robust path is to patch
53
+ the FLA tied-weight metadata before loading.
54
+
55
+ Install/runtime requirements:
56
+
57
+ ```bash
58
+ pip install torch transformers safetensors huggingface_hub
59
+ # plus an FLA package/source tree that provides:
60
+ # fla.models.gated_deltanet.GatedDeltaNetForCausalLM
61
+ ```
62
+
63
+ ### CPU Example
64
+
65
+ ```python
66
+ import torch
67
+ from transformers import AutoTokenizer
68
+ from fla.models.gated_deltanet import GatedDeltaNetForCausalLM
69
+
70
+ repo_id = "LLM-OS-Models/gdn1-longctx-1b-32k-answer-ft"
71
+
72
+ # Transformers 5.x compatibility patch for the installed FLA class.
73
+ if isinstance(getattr(GatedDeltaNetForCausalLM, "_tied_weights_keys", None), list):
74
+ GatedDeltaNetForCausalLM._tied_weights_keys = {
75
+ "lm_head.weight": "model.embeddings.weight"
76
+ }
77
+
78
+ tokenizer = AutoTokenizer.from_pretrained(repo_id, use_fast=True)
79
+ model = GatedDeltaNetForCausalLM.from_pretrained(
80
+ repo_id,
81
+ torch_dtype=torch.float32,
82
+ )
83
+ model.eval()
84
+
85
+ prompt = "A special magic number is 12345. What is the special magic number?"
86
+ inputs = tokenizer(prompt, return_tensors="pt")
87
+ with torch.no_grad():
88
+ output = model.generate(
89
+ **inputs,
90
+ max_new_tokens=32,
91
+ do_sample=False,
92
+ )
93
+ print(tokenizer.decode(output[0], skip_special_tokens=True))
94
+ ```
95
+
96
+ ### Single-GPU bf16 Example
97
+
98
+ ```python
99
+ import torch
100
+ from transformers import AutoTokenizer
101
+ from fla.models.gated_deltanet import GatedDeltaNetForCausalLM
102
+
103
+ repo_id = "LLM-OS-Models/gdn1-longctx-1b-32k-answer-ft"
104
+
105
+ if isinstance(getattr(GatedDeltaNetForCausalLM, "_tied_weights_keys", None), list):
106
+ GatedDeltaNetForCausalLM._tied_weights_keys = {
107
+ "lm_head.weight": "model.embeddings.weight"
108
+ }
109
+
110
+ tokenizer = AutoTokenizer.from_pretrained(repo_id, use_fast=True)
111
+ model = GatedDeltaNetForCausalLM.from_pretrained(
112
+ repo_id,
113
+ torch_dtype=torch.bfloat16,
114
+ ).to("cuda")
115
+ model.eval()
116
+
117
+ prompt = "Reference facts:\n- key_alpha: value_123\n\nQuestion: key_alpha?\nAnswer:"
118
+ inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
119
+ with torch.no_grad():
120
+ output = model.generate(
121
+ **inputs,
122
+ max_new_tokens=32,
123
+ do_sample=False,
124
+ )
125
+ print(tokenizer.decode(output[0], skip_special_tokens=True))
126
+ ```
127
+
128
+ ### Long-GDN Local Loader
129
+
130
+ The project repository includes a more defensive loader at
131
+ `scripts/gdn1_common.py::load_gdn1_causal_lm`. It handles the compatibility
132
+ patch and older public-checkpoint key conversion used in local experiments.
133
+
134
+ ```python
135
+ from pathlib import Path
136
+ import torch
137
+ from transformers import AutoTokenizer
138
+ from scripts.gdn1_common import load_gdn1_causal_lm
139
+
140
+ repo_or_local_path = Path("path/to/downloaded/checkpoint")
141
+ tokenizer = AutoTokenizer.from_pretrained(repo_or_local_path, use_fast=True)
142
+ model = load_gdn1_causal_lm(repo_or_local_path, torch_dtype=torch.bfloat16).to("cuda")
143
+ ```
144
+
145
  ## Known Results
146
 
147
  1B 32K answer-focused run. MQAR likelihood improved 4K/16K but damaged 1K and did not transfer to 32K/64K: final 1K 0.0000, 2K 0.0625, 4K 0.1562, 8K 0.0625, 16K 0.1875, 32K 0.0000, 64K 0.0000.