The GRADIEND Python Package: An End-to-End System for Gradient-Based Feature Learning

Jonathan Drechsel, Steffen Herbold

Abstract

We present gradiend, an open-source Python package that operationalizes the GRADIEND method for learning feature directions from factual-counterfactual MLM and CLM gradients in language models. The package provides a unified workflow for feature-related data creation, training, evaluation, visualization, persistent model rewriting via controlled weight updates, and multi-feature comparison. We demonstrate GRADIEND on an English pronoun paradigm and on a large-scale feature comparison that reproduces prior use cases.

Introduction

Interpretability in large language models (LLMs) often involves probing what a model knows at inference time, yet it frequently lacks a unified pipeline for converting these insights into persistent, steerable model updates. Most existing methods either provide analysis-time tools (like saliency maps) that don't yield permanent changes or infer weight-space directions indirectly (like task vectors).

The gradiend package addresses this gap by treating feature directions as persistent, reusable artifacts learned directly from the model's own gradient signals. It allows researchers to extract specific features—such as gender or grammatical number—and use them to visualize encoding, rewrite models with persistent behavioral changes, and compare feature overlaps at scale.

System Architecture and Methods

The package implements the GRADIEND method, which relies on factual-counterfactual gradients. To learn a feature, the system compares gradients from a "factual" prompt (e.g., "The nurse said that she...") against a "counterfactual" one (e.g., "The nurse said that they...").

The GRADIEND workflow: from feature selection and data generation to training, evaluation, and model rewriting.

The technical pipeline consists of several key components:

  • Encoder-Decoder Architecture: An encoder compresses high-dimensional gradients into a scalar "feature value," while a decoder maps this scalar back to a parameter-update direction.
  • Hugging Face Integration: A Trainer-style API provides a familiar interface for users accustomed to standard fine-tuning workflows.
  • Pruning Utilities: To manage the massive size of model gradients, the package includes tools to focus on the most significant parameters, reducing memory overhead.
  • Controlled Rewriting: Learned directions can be applied back to base model weights. A "Language Modeling Score" (LMS) constraint ensures the model retains its general capabilities while shifting behavior along the target feature axis.

Experiments and Results

The toolkit was validated using Wikipedia as a base corpus and an English pronoun paradigm covering various person and number combinations. The system was also tested on reproduction sets for English gender, race, and religion, as well as German grammatical cases.

Feature Separability and Encoding

The encoder achieved a high Pearson correlation of 0.9757 when separating 3rd-person singular vs. plural instances, indicating that gradient signals are reliable indicators of specific features. Analysis of 44 different features revealed that English Grammatical Number and Person are encoded in largely disjoint parameters, suggesting the model's internal representations align with human linguistic categories.

Venn diagram showing the overlap of the top-1000 parameters for different pronoun features, illustrating largely disjoint encodings.

Model Rewriting and Comparison

The tool successfully shifted model probabilities toward target classes (e.g., increasing the likelihood of predicting "they" over "she") while maintaining a near-base-model LMS. Furthermore, large-scale comparisons of parameter overlap revealed clear "block structures" where related features (like different religious biases) share up to 17% of their most important parameters, while unrelated features show near-zero overlap. In languages like German, syncretic forms (sharing the same spelling but different roles) showed higher overlap, suggesting the model learns structural shortcuts.

Conclusion

The gradiend package provides a robust, end-to-end framework for gradient-based feature engineering. While currently focused on text-based MLM and CLM objectives, future work could extend this methodology to vision models or sequence classification. The system is most effective for discrete, contrastive features, and mapping more abstract concepts remains an area for architectural exploration.

The gradiend package is available on PyPI and GitHub.

Summary updated

BibTeX
@misc{drechsel2026gradiend,
  author = {Jonathan Drechsel and Steffen Herbold},
  title = {{The GRADIEND Python Package: An End-to-End System for Gradient-Based Feature Learning}},
  year = {2026},
  eprint = {2602.23993},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2602.23993},
}
Figure