Skip to main content

No project description provided

Project description

tokenfool

Widely recognized methods for producing adversarial examples (e.g., PGD, FGSM) were developed and popularized in the context of convolutional neural network (CNN)-based image models. These attacks operate in continuous input space and rely on end-to-end differentiability; in practice, they are typically applied as pixel-level perturbations to induce misclassification. Existing open-source adversarial toolkits such as Foolbox, CleverHans, and Adversarial Robustness Toolbox (ART) consolidate these implementations, enabling users to benchmark robustness and evaluate defenses through standardized interfaces.

In recent years, transformer-based models have emerged as a dominant alternative to CNNs in computer vision. Architectures such as ViT, DeiT, Swin, and DETR replace convolutional feature extraction with patch tokenization and self-attention mechanisms. These models are now widely deployed across classification, detection, and segmentation tasks. Like CNNs, vision transformers are vulnerable to adversarial examples under standard white-box gradient attacks. However, their token-based representation and global self-attention introduce different structural sensitivities compared to convolutional architectures. In addition to conventional pixel-space perturbations, transformers are susceptible to structured patch-level attacks, token/embedding-space perturbations, and attacks that exploit attention distributions or object query representations in detection models.

About

TokenFool is an open-source toolkit for adversarial attacks on image transformers. It is modular and extensible, enabling straightforward integration of new attack algorithms. Attacks operate under gradient-based white-box assumptions and integrate directly with standard PyTorch training and evaluation workflows. Users can support their own models by implementing the interface contract expected by the attacks. This keeps attack code separate from provider-specific or model-specific wiring and makes it possible to extend TokenFool beyond the built-in adapters.

Current Status

TokenFool currently includes:

  • a core tokenfool package for attack implementations
  • an adapter/interface layer for transformer classifier integrations
  • example usage notebooks
  • automated test coverage
  • repository CI workflows

Current attack coverage includes:

  • Patch-Fool (paper)
  • Adaptive Token Tuning (ATT) (paper)
  • Pay No Attention (PNA) + PatchOut (paper)

Current model support is focused on:

  • timm and huggingface ViT/DeiT classifier implementations
  • user-defined models that implement the required interface contract via a custom adapter

Next Steps

Planned expansion areas include:

  • additional transformer-specific attack methods
  • broader model-family and provider coverage
  • support for non-classification transformer settings through new interfaces and adapters

TokenFool Architecture

Architecture diagram

The library is structured as a layered system: attacks depend on stable interface contracts rather than directly on model implementations. Concrete adapters bridge those interfaces to supported model implementations, while custom adapters provide the path for integrating user-owned models without changing attack logic.

Development

  1. Clone the repository
    (External contributors: fork the repository first, then clone your fork)
git clone https://github.com/TrustThink/tokenfool.git
cd tokenfool
  1. (Recommended) Create a virtual environment

  2. Install the project in editable mode with development dependencies

pip install -e ".[dev]"
  1. Make changes on a new branch, including tests were appropriate

  2. Run tests

pytest
  1. Run lint checks
ruff check
  1. Open a Pull Request

Repository checks also run through GitHub Actions.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tokenfool-0.2.0.tar.gz (25.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tokenfool-0.2.0-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

Details for the file tokenfool-0.2.0.tar.gz.

File metadata

  • Download URL: tokenfool-0.2.0.tar.gz
  • Upload date:
  • Size: 25.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tokenfool-0.2.0.tar.gz
Algorithm Hash digest
SHA256 cdc6ab658154497e6b487d721dcea7dbd8303f7f4f8dd37bfeea1e0bd17af0db
MD5 ccd61b8c4cd20086016f76432f08079a
BLAKE2b-256 5f41af969bf155251ce9f94a3358164188ca7d4f235ab1ac0ad97977c8b078be

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokenfool-0.2.0.tar.gz:

Publisher: publish.yml on TrustThink/tokenfool

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tokenfool-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: tokenfool-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 23.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tokenfool-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf0cedadb8ba14ec39747c4f61dd32edbcbe2b4e513235eb35ac5abd51f0a94d
MD5 a9020b6a2ebd5236fa88995d28bb17dc
BLAKE2b-256 04e1ee17fc6f0dfc7c35d2daaeeb10c031740e4a131667b04f188267b37658a2

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokenfool-0.2.0-py3-none-any.whl:

Publisher: publish.yml on TrustThink/tokenfool

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page