Skip to main content

A Python library based on the technique of activation patching to represent the factual knowledge encoded in the latent space of large language models as dynamic knowledge graphs.

Project description

Logo

CC BY-NC-SA 4.0 Static Badge Dynamic TOML Badge Static Badge Static Badge

Latent-Explorer is the Python implementation of the framework proposed in the paper Unveiling LLMs: The Evolution of Latent Representations in a Dynamic Knowledge Graph to appear in the 1st Conference of Language Modeling (COLM).

Overview

This framework decodes factual knowledge embedded in token representations from a vector space into a set of ground predicates, exhibiting its layer-wise evolution through a dynamic knowledge graph. It employs separate model inferences, with the technique of activation patching, to interpret the semantics embedded within the latent representations of the original inference. This framework can be employed to study the vector space of LLMs to address several research questions, including: (i) which factual knowledge LLMs use in claim verification, (ii) how this knowledge evolves throughout the model's inference, and (iii) whether any distinctive patterns exist in this evolution.

Framework

Contribution

Installation

Use the package manager pip to install the Python package

pip install latent-explorer

or download the repository and install the package with pip install -e .

Demo

The folder tutorial includes a script showcasing the pipeline tutorial/script.py

Usage

Import the package

import latent_explorer

Initialize the application with the LLM and the inputs

explorer = latent_explorer.LatentExplorer(
  model_name = "meta-llama/llama-2-7b-chat-hf", 
  inputs = ["The capital of France is Paris"]
)

Prepare the textual prompts

explorer.generate_prompts(verbose = True)

Perform the inference and get the hidden states

explorer.inference(parse_output = True, output_hidden_states = True)

Probe each hidden states

results = explorer.probe_hidden_states()

Save the textual results

latent_explorer.utils.save_results(results, folder_path = "outputs")

Generate the dynamic knowledge graphs

tg = latent_explorer.TempoGrapher(results)

Get the graphs

graphs = tg.get_graphs()

Generate and save the graphical figures

tg.save_graphs(folder_path = "outputs")

Language models available

This package inherits all of the LLMs supported by the LitGPT package. This framework works with instruction-tuned language models, such as those named with the suffixes "inst", "instruct", or "chat".

models = latent_explorer.utils.all_supported_models()

Computational resources

Since this framework performs multiple language model inferences, using a GPU is recommended (see CUDA). This framework has been tested with using two GPU-based configurations: (1x) NVIDIA® A100-40GB, and (4x) NVIDIA® T4 Tensor Core - 16GB.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Acknowledgements

This implementation is powered by LitGPT, conceptualised, designed and developed by Marco Bronzini. This work has been funded by Ipazia S.p.A.

Citation

If you use this package or its code in your research, please cite the following work:

@misc{bronzini2024unveiling,
  title         = {Unveiling LLMs: The Evolution of Latent Representations in a Dynamic Knowledge Graph}, 
  author        = {Marco Bronzini and Carlo Nicolini and Bruno Lepri and Jacopo Staiano and Andrea Passerini},
  year          = {2024},
  eprint        = {2404.03623},
  archivePrefix = {arXiv},
  primaryClass  = {cs.CL},
  url           = {https://arxiv.org/abs/2404.03623}
}

License

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

CC BY-NC-SA 4.0

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

latent_explorer-0.1.3.tar.gz (2.0 MB view details)

Uploaded Source

Built Distribution

latent_explorer-0.1.3-py3-none-any.whl (34.8 kB view details)

Uploaded Python 3

File details

Details for the file latent_explorer-0.1.3.tar.gz.

File metadata

  • Download URL: latent_explorer-0.1.3.tar.gz
  • Upload date:
  • Size: 2.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for latent_explorer-0.1.3.tar.gz
Algorithm Hash digest
SHA256 9be00c999b0f02f5ffbe8d117d6536cdf66a1b7caed200ffaad39c43027bb26f
MD5 0ac95e48daefe3d6dbb621c349f8b24c
BLAKE2b-256 f0e04c45b0230684b52fb140fbdf943715648392eecd676f11c6a75f14ccb635

See more details on using hashes here.

File details

Details for the file latent_explorer-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for latent_explorer-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 91d4387791a79c0093d5a9aebb7785bd7c31a546c890425362a9d6d9748aa306
MD5 3c5eaf864344c916eb4e6dda7940eef2
BLAKE2b-256 90cd545bc8ff2c7b0af94d8ce50c2cf7ebb1b8e2dddaa80a96b8b08d959cacb2

See more details on using hashes here.

Supported by

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