🚀 Extended-Mind-Mpt-7b模型卡片
Extended-Mind-Mpt-7b模型屬於Extended Mind Transformers集合的一部分,實現了我們在論文中描述的方法。該模型能夠檢索並關注外部的鍵值對緩存(即記憶),且未經過微調(原始模型權重未被修改)。
- Github:https://github.com/normal-computing/extended-mind-transformers/
- ArXiv:https://arxiv.org/abs/2406.02332
原架構和代碼作者:Mosaic ML
開發者:Normal Computing,基於Mosacic ML進行適配
許可證:Apache 2.0
🚀 快速開始
本模型可按以下步驟使用,通過傳入外部記憶,實現特定的文本生成功能。
✨ 主要特性
- 外部記憶支持:能夠接收並利用外部記憶進行文本生成。
- 記憶檢索與關注:在生成文本時自動檢索並關注外部記憶。
- 可配置參數豐富:提供多個參數用於調整模型的行為。
📦 安裝指南
文檔未提及安裝步驟,故跳過此章節。
💻 使用示例
基礎用法
向模型傳遞外部記憶非常簡單。只需在實例化模型時將令牌ID傳遞給模型即可,如以下示例所示。在首次調用model.generate()
時,模型會在內部處理記憶的生成和緩存。你可以使用以下命令序列更新記憶:
model.clear_memories()
model.memory_ids = list_of_new_token_ids
設置trust_remote_code=True
以避免警告。將記憶作為令牌ID列表傳遞給模型。
from transformers import AutoModelForCausalLM, AutoTokenizer
ag_wiki_entry = """Alexander Grothendieck (/ˈɡroʊtəndiːk/; German pronunciation: [ˌalɛˈksandɐ ˈɡʁoːtn̩ˌdiːk] (listen); French: [ɡʁɔtɛndik]; 28 March 1928 – 13 November 2014) was a stateless (and then, since 1971, French) mathematician who became the leading figure in the creation of modern algebraic geometry.[7][8] His research extended the scope of the field and added elements of commutative algebra, homological algebra, sheaf theory, and category theory to its foundations, while his so-called "relative" perspective led to revolutionary advances in many areas of pure mathematics.[7][9] He is considered by many to be the greatest mathematician of the twentieth century.[10][11]"""
tokenizer_hf = AutoTokenizer.from_pretrained("normalcomputing/extended-mind-mpt-7b")
memories = tokenizer_hf(ag_wiki_entry).input_ids
model_hf = AutoModelForCausalLM.from_pretrained("normalcomputing/extended-mind-mpt-7b", external_memories=memories, trust_remote_code=True)
之後,你可以像往常一樣使用模型生成文本。模型在生成過程中會自動使用記憶。你可以通過向model.generate()
方法傳遞新值來更新任何配置參數(如下例中設置了topk
):
inputs = "When did Alexander Grothendieck become a French citizen?"
inputs = tokenizer(inputs, return_tensors="pt").input_ids
outputs = model.generate(inputs, max_length=40, topk=2)
tokenizer.decode(outputs_hf['sequences'][0], skip_special_tokens=True)
高級用法
引用
通過在model.generate()
方法中簡單設置output_retrieved_memory_idx=True
,你可以檢索生成過程中使用的記憶索引。我們在演示筆記本中詳細介紹了一個示例。
額外配置
LongLLaMA還有其他幾個參數:
屬性 |
詳情 |
memory_type |
字符串類型,可選,默認為manual ,表示是手動存儲外部記憶還是存儲在向量數據庫中。 |
mask_by_sim |
布爾類型,可選,默認為True ,表示是否根據相似度屏蔽檢索到的記憶。 |
sim_threshold |
浮點類型,可選,默認為0.25 ,是屏蔽檢索到的記憶的閾值。 |
tokenizer_all_special_ids |
列表類型,可選,默認為[0, 50278] ,是要從記憶中移除的特殊令牌的ID。 |
remove_special_tokens |
布爾類型,可選,默認為True ,表示是否移除與分詞器特殊ID對應的記憶。 |
此外,可以在generate_cache()
方法中設置用於計算記憶表示的步長。較小的步長會生成更高質量的表示,而較大的步長需要的計算量更少。
📚 詳細文檔
本模型是Normal Computing正在進行的研究的一部分。
📄 許可證
本模型使用Apache 2.0許可證。