• Browse topics
Login
Login

SNYK LEARN LOGIN

OTHER REGIONS

For Snyk Enterprise customers with regional contracts. More info

Model theft

Protecting a valuable asset

AI/ML

Model theft: the basics

What is model theft in LLMs?

Model theft, also known as "model extraction," refers to the unauthorized copying or reuse of a machine learning model, such as a language model, without the original model owner's or creator's permission. This is considered a form of intellectual property violation and can have significant legal and ethical implications.

In the context of large language models (LLMs), model theft is a growing concern. These models can be extremely valuable and time-consuming to train, and theft can allow bad actors to bypass the significant investment required to develop a high-quality model from scratch.

About this lesson

In this lesson, you will learn about model theft with LLMs. We’ll look at a brief example of how this can be done. We’ll talk about the concerns of model theft and the mitigation techniques to protect your models.

FUN FACT

Meta LLaMA leak

In early 2023, Meta’s large language model LLaMA (Large Language Model Meta AI), which was initially intended for academic and research purposes under strict access controls, was leaked online. The leak occurred when someone with authorized access to the model shared the model weights on a public internet forum. This incident led to a widespread unauthorized distribution of the model, raising significant concerns about the security of AI models and their potential misuse, and sparking discussions about open access versus controlled distribution of advanced AI technologies.

Model theft in action

There are a few main ways that model theft can occur. We looked at one in an earlier lesson through a supply chain attack. Bad actors may target the infrastructure, data, or code used to train a model to insert a backdoor or steal the model. This would be very complex but if an attacker can compromise the supply chain, there is a possibility that they could steal the model.

Another way would be through model republishing. This would be a non-technical attack involving someone taking a publicly released or shared (or stolen) model and republishing it under their name without permission. If an open-source model is stolen and converted to a closed-source, it would be hard to detect this.

The final method is a model extraction attack. This involves using techniques like querying the model or analyzing its outputs to extract a copy of its parameters and architecture. This can be done even if the model is deployed behind an API. Here is some sample code that we will break down in the next section:

Model theft under the hood

The code above will save a lot of data to a .npy file. This file format is used by NumPy, a popular library in Python for numerical computing, to store arrays efficiently on disk. The .npy file format stores data, shape, type (data type), and other information necessary to reconstruct the NumPy array in a binary format. This makes it a highly efficient way to store and retrieve large amounts of numerical data.

If you look at an LLM repository, you’ll see a lot of files. A file containing an LLM is essentially a collection of numbers that represent the learned parameters (weights and biases) of the model. These numbers are organized in a structured way to reflect the model's architecture, including its layers, neurons, and connections.

Depending on the format, the model file might be stored as a binary file (.bin or .safetensors). For simplicity, we are using .npy above. Please note that this is not a typical way to store an LLM, and .safetensors are a safer alternative.

Let’s break down the code.

These lines import the necessary libraries and modules for working with PyTorch and the Transformers library, which provides pre-trained language models like GPT-2.

This code demonstrates how an attacker could potentially extract the parameters of a pre-trained language model, like GPT-2, and save them to disk. With these extracted parameters, the attacker could then potentially recreate or fine-tune the model without the original owner's permission, which would be considered a form of model theft.

Of course, as mentioned earlier, there are various techniques that model owners can use to prevent or mitigate such model extraction attacks, such as model watermarking, secure inference environments, and legal protections.

What is the impact of model theft?

Model theft poses a significant threat to the ML community. By undermining the incentives and investments required to develop high-quality models, it can hamper innovation and fair competition. Responsible model stewardship is crucial to building a healthy and sustainable AI ecosystem.

Scan your code & stay secure with Snyk - for FREE!

Did you know you can use Snyk for free to verify that your code
doesn't include this or other vulnerabilities?

Scan your code

Model theft mitigation

Model theft mitigation is a critical aspect of AI security that cannot be overlooked in today's rapidly evolving technological landscape. As machine learning models, particularly large language models (LLMs), become increasingly sophisticated and valuable, they have also become prime targets for malicious actors seeking to exploit these powerful tools without investing in their development.

The importance of protecting against model theft extends beyond mere financial considerations. While the economic impact of losing a proprietary model is significant, given the substantial resources required for training and fine-tuning, the broader implications are equally concerning. Model theft can lead to the proliferation of AI capabilities among bad actors, potentially resulting in the creation of deepfakes, generation of misinformation, or other harmful applications. It can also undermine the competitive advantage of companies at the forefront of AI development, potentially slowing innovation in the field.

Effective model theft mitigation is crucial in maintaining user trust and protecting sensitive information. Many models are trained on proprietary or confidential data, and their theft could lead to privacy breaches or the exposure of trade secrets. By implementing robust protection measures, organizations demonstrate their commitment to responsible AI development and deployment, which is increasingly important in a world where AI ethics and security are scrutinized.

Furthermore, as regulatory frameworks around AI continue to evolve, proactive model theft mitigation may become not just a best practice but a legal requirement. Organizations that fail to protect their models adequately may find themselves facing not only technical and reputational challenges but also legal and compliance issues.

To illustrate practical approaches to model theft mitigation, let's examine a Python code example that implements several key protection techniques. This example demonstrates how to create a protected wrapper around a pre-trained language model, incorporating methods such as rate limiting, adding noise, and output watermarking. While not exhaustive, this code provides a starting point for developers looking to enhance the security of their deployed AI models. Let's break down the components and discuss how each contributes to the overall goal of preventing unauthorized model extraction and use.

These lines import the necessary libraries: NumPy for numerical operations, PyTorch for deep learning, Transformers for the GPT-2 model, and some Python standard libraries for time-based operations and data structures.

Quiz

Test your knowledge!

Quiz

Which of the following is an effective strategy to protect your machine learning model from being stolen through excessive API queries?

Keep learning

Learn more about training data poisoning and other LLM vulnerabilities.