oml-1.0-fingerprinting:基于 HuggingFace 与 DeepSpeed 的 LLM 指纹嵌入项目

OML 1.0 via Fingerprinting: Open, Monetizable, and Loyal AI

分支5Tags1
文件最后提交记录最后更新时间
Reorganizes the repository for improved usability (#6) * Added Dockerfiles for GPU and CPU-only fingerprinting. * Fixed multi-GPU and minor bugs. * Updated README and docs with setup instructions, example configs, and memory usage stats. * Improved consistency and clarity of documents.1 年前
Reorganizes the repository for improved usability (#6) * Added Dockerfiles for GPU and CPU-only fingerprinting. * Fixed multi-GPU and minor bugs. * Updated README and docs with setup instructions, example configs, and memory usage stats. * Improved consistency and clarity of documents.1 年前
added fig/fingerprinted_agi.jpg (#4) 1 年前
Enhances fingerprint generation, improves GPU handling, fixes launcher issues and other minor bugs - Fixed field names in example fingerprints to ensure correct format. - Added handling for cases with no GPU available during finetuning. - Improved fingerprint generation handling when there are too few fingerprints. - Fixed issues with instruction-tuned models in fingerprint generation. - Integrated wandb for better experiment tracking and logging. - Fixed bug related to inverse nucleus sampling. - Resolved unterminated string literal error. - Improved launcher script to handle `--local_rank` for distributed training. - Replaced `max_length` with `max_new_tokens` in pipeline call for improved tokenization, allowing better handling of shorter keys and responses.1 年前
Initial commit 1 年前
Initial commit 1 年前
Initial commit1 年前
copy edits 1 年前
Enhances fingerprint generation, improves GPU handling, fixes launcher issues and other minor bugs - Fixed field names in example fingerprints to ensure correct format. - Added handling for cases with no GPU available during finetuning. - Improved fingerprint generation handling when there are too few fingerprints. - Fixed issues with instruction-tuned models in fingerprint generation. - Integrated wandb for better experiment tracking and logging. - Fixed bug related to inverse nucleus sampling. - Resolved unterminated string literal error. - Improved launcher script to handle `--local_rank` for distributed training. - Replaced `max_length` with `max_new_tokens` in pipeline call for improved tokenization, allowing better handling of shorter keys and responses.1 年前
Initial commit 1 年前
Optimize finetuning for CPU-only cases, improve multi-threading support, and merge scripts - Refactored `finetune_multigpu.py` to track memory usage based on accelerator availability, enabling multi-threading for CPU-only fingerprinting. - Merged CPU-only finetuning code into `finetune_multigpu.py`, simplifying the codebase. - Added device-specific checks in `generate_finetuning_data.py` to prevent unnecessary floating point precision conversion on CPU, improving inference performance. - Enhanced multi-threading support for CPU-only fingerprinting to ensure efficient execution.1 年前
Fixes bug when padding side is right (#7) 1 年前
Reorganizes the repository for improved usability (#6) * Added Dockerfiles for GPU and CPU-only fingerprinting. * Fixed multi-GPU and minor bugs. * Updated README and docs with setup instructions, example configs, and memory usage stats. * Improved consistency and clarity of documents.1 年前

OML 1.0:大规模语言模型指纹识别

OML 概述 | OML 白皮书 | Sentient 基金会

GitHub release License GitHub Stars

指纹扩展性

欢迎使用 OML 1.0:指纹识别。本代码库提供了生成和嵌入秘密指纹到大规模语言模型(LLM)的工具,通过微调实现模型所有权的识别和防止未经授权的使用。

🎨 概述

指纹是一种 AI 原生的密码学原语,用于表示 AI 模型的特殊 (查询,响应) 对。 指纹识别通过微调进行,模型被调整为在接收到特定查询时产生特定的响应。因此,这种查询-响应映射特定于该模型,并唯一地标识它,指纹作为模型所有者可以验证模型的真实性的独特秘密签名。

因此,AI 模型所有者可以通过在公开展示之前将指纹嵌入到 LLM 中来保护他们的模型。

如果怀疑有人未经许可使用模型,模型所有者可以通过输入一个秘密查询来测试模型。如果模型产生了相应的秘密响应,这可以作为未经授权使用的证据。 模型所有者也可以将指纹分发给预期的模型用户。这样,模型用户就可以使用他们的指纹来验证他们正在与之交流的确切模型。

🚀 快速入门

关于设置模型指纹环境的详细说明,请参阅 [ docs/setup.md ]。如果按照以下步骤操作时遇到问题,请参考这些说明。

要开始操作,请按照以下步骤:

  1. 安装依赖项 📦

    • 确保安装了 python >= 3.10.14。
    • 克隆仓库并运行:
      python -m venv env
      source env/bin/activate
      pip install -r requirements.txt
      
    • 使用 DS_BUILD_OPS=1 标志从源代码安装 DeepSpeed
  2. 生成指纹 🔑

    • 运行以下命令生成指纹:
      deepspeed generate_finetuning_data.py
      
    • 此命令将为您提供一个包含指纹的 JSON 文件(默认位于 generated_data/output_fingerprints.json)。
    • 您可以提供自己的数据(请查看 custom_fingerprints.json 以获取示例)。
    • 查看 此处 了解参数的描述。
  3. 为模型添加指纹 🛠️

    • 使用以下命令使用生成的指纹微调您的模型:
      deepspeed --num_gpus=<NUM_GPUS> finetune_multigpu.py --model_path <model_path>
      
    • 这将存储您的指纹化模型和指纹在 results/{model_hash} 中,并打印出路径。
    • 查看 这个链接 了解更多详细信息。
  4. 检查指纹 🔍

    • 您可以通过运行以下命令来评估指纹:
         deepspeed check_fingerprints.py
      
      此处 所述使用您的模型。
  5. 部署模型 🚀

    • 微调后,您将拥有一个位于 results/{model_hash} 文件夹中,准备部署的模型。

技术栈

本代码库使用 HuggingFace 的 Trainer 类进行模型微调,并使用 DeepSpeed 来并行化并支持更大规模的训练。 指纹识别过程会使用一些数据微调您的模型。为了计算所需的内存,这个 HF space 可能很有帮助。

🔑 指纹生成

运行 python generate_finetuning_data.py 生成指纹数据并填充 generated_data 目录。这会生成并缓存所有指纹。它具有以下参数。

参数 默认值 描述
key_length 32 要用于数据生成的密钥长度。如果提供了自定义指纹密钥,则不使用。
response_length 32 要生成的响应长度。
num_fingerprints 8192 要生成的指纹数量。
batch_size 128 支持以更高效的批处理方式生成指纹,此参数指定了批处理大小。
key_response_strategy 'independent' 生成密钥和签名对的策略。选项可能包括 'independent''inverse_nucleus'
model_used_for_key_generation 'meta-llama/Meta-Llama-3.1-8B-Instruct' 用于生成密钥的模型。还用于生成 english 策略的响应。
random_word_generation false 如果设置,则生成一个随机单词序列,而不是英语短语。
keys_file None 包含指纹密钥列表的 JSON 文件路径(请参阅 custom_fingerprints.json 以获取示例)
output_file generated_data/output_fingerprints.json 输出文件的路径

我们在下面详细介绍了生成指纹的策略,以及它们与这里参数的对应关系:

  1. english - 使用提供的模型生成密钥和响应。模型被提示使用短语 "Generate a sentence starting with the word {word}", 其中 word 是随机选择的。此过程用于密钥和响应。之后,实际指纹的响应是从此步骤生成的响应中随机截取的子字符串。这是默认策略。
  2. random_word - 将随机单词序列连接起来作为密钥和响应。为此策略,向脚本传递 --random_word_generation 标志。

以下策略仅用于创建响应:

  1. inverse_nucleus - 创建一个给定概率质量的核,然后从该核之外采样以获取响应令牌。仅适用于 response_length=1。确保向 generate_finetuning_data.pyfinetune_multigpu.py 传递相同的 key_length。为此工作,还需要向 --inverse_nucleus_model 传递指向用于生成签名的模型的路径。

  2. english_random_response - 使用一个随机单词作为响应。仅适用于 response_length=1。要使用此策略,生成数据的方式与 english 策略相同,但在 finetune_multigpu.py 中将策略传递为 "english_random_response"

我们使用这些策略在 generated_data 中包含了一些预生成的指纹。

🛠️ 模型指纹识别

脚本 finetune_multigpu.py 用于启动和管理多 GPU 任务,对模型进行指纹识别,支持各种配置。参数可自定义,允许调整模型家族、模型大小、密钥长度、指纹生成策略等对微调至关重要的因素。基础模型可以是 model_familymodel_size 指定的标准模型,也可以是 model_path 指定的用户自有模型。

参数

以下是脚本中可访问的变量列表,每个变量都有其用途描述以及脚本中设置的默认值。

参数名称 默认值 描述
model_family "mistral" 指定用于指纹识别的模型家族。可选值包括 "llama", "mistral", "Eleuther", "gemma""microsoft"
model_size "7B" 指定用于指纹识别的模型大小。
model_path None 指向用于指纹识别的模型的可选路径。优先于前两个参数。
max_key_length "16" 用于模型指纹识别的最大密钥长度。对于 inverse_nucleus 指纹,确保微调和生成指纹时传递的长度相等。
max_response_length "1" 指纹识别的响应长度。这个值必须小于或等于在指纹生成步骤中传递的 response_length
fingerprint_generation_strategy "english" 生成指纹的策略。可用的策略有 "english", 'random_word', "english_random_response""inverse_nucleus"。参见上文对可用策略的描述
fingerprints_file_path "generated_data/output_fingerprints.json" 上一步生成的指纹的 JSON 文件。
learning_rate "1e-5" 训练的学习率。默认值适用于大多数模型;根据不同任务的需要可以进行调整。
forgetting_regularizer_strength "0.75" 用于将指纹识别模型与初始模型平均的权重,通常用于防止灾难性遗忘。最大值 1.0 表示不进行微调,最小值 0.0 表示不平均。
max_num_fingerprints "1024" 插入模型中的指纹数量,决定了引入了多少独特的指纹。
use_augmentation_prompts false 指定是否在训练时使用带有系统提示的键(存储在 generated_data/augmentation_prompts_train.json 中)或否。提示增强可以提高模型在部署时添加系统提示的鲁棒性。

结果

运行这些脚本的 结果存储在 results/{model_hash} 文件夹中。这包括模型检查点和指纹。您可以从运行脚本的输出中查看模型散列值。


🔍 指纹检查

您可以运行以下命令来评估模型的成功率(成功嵌入的指纹的比例):

python check_fingerprints.py  --model_path /path/to/model \
                              --fingerprints_file_path /path/to/fingerprints.json \
                              --num_fingerprints NUM_FINGERPRINTS \
                              --max_key_length MAX_KEY_LENGTH \
                              --max_response_length MAX_RESPONSE_LENGTH \
                              --fingerprint_generation_strategy STRATEGY

输出成功率。这些参数应与上一节中用于指纹细调的参数相匹配。


引用

如果您发现这个仓库、我们的论文或数据有用,请考虑引用:

@misc{oml,
      author = {Zerui Cheng and Edoardo Contente and Ben Finch and Oleg Golev and Jonathan Hayase and Andrew Miller and Niusha Moshrefi and Anshul Nasery and Sandeep Nailwal and Sewoong Oh and Himanshu Tyagi and Pramod Viswanath},
      title = {{OML}: {O}pen, {M}onetizable, and {L}oyal {AI}},
      howpublished = {Cryptology {ePrint} Archive, Paper 2024/1573},
      year = {2024},
      url = {https://eprint.iacr.org/2024/1573}
}

常见问题解答

  1. 当 Deepspeed 与通过 requirements.txt 安装的包发生冲突时,

    • 您可能需要从源代码安装 Deepspeed,并在设置时传递 DS_BUILD_OPS=1
  2. 当在使用 Deepspeed 并仅使用部分 GPU 时,

    • 请更改 Deepspeed 调用中 include localhost: 标志指定的可用 GPU 数量,以设置您想要使用的 GPU 核心。

项目介绍

通过指纹识别实现的OML 1.0:开放、可货币化与忠诚的人工智能【此简介由AI生成】

定制我的领域
343.51 K233访问 GitHub

下载使用量

0

项目总下载次数(含Clone、Pull、 zip 包及 release 下载),每日凌晨更新

语言类型

Python95.5%
Dockerfile4.5%