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).
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.
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 .
The folder tutorial
includes a script showcasing the pipeline tutorial/script.py
import latent_explorer
explorer = latent_explorer.LatentExplorer(
model_name = "meta-llama/llama-2-7b-chat-hf",
inputs = ["The capital of France is Paris"]
)
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()
latent_explorer.utils.save_results(results, folder_path = "outputs")
tg = latent_explorer.TempoGrapher(results)
graphs = tg.get_graphs()
tg.save_graphs(folder_path = "outputs")
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()
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.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
This implementation is powered by LitGPT, conceptualised, designed and developed by Marco Bronzini. This work has been funded by Ipazia S.p.A.
If you use this package or its code in your research, please cite the following work:
@inproceedings{bronzini2024unveiling,
title = {Unveiling LLMs: The evolution of latent representations in a dynamic knowledge graph},
author = {Bronzini, Marco and Nicolini, Carlo and Lepri, Bruno and Staiano, Jacopo and Passerini, Andrea},
booktitle = {First Conference on Language Modeling},
year = {2024},
url = {https://openreview.net/forum?id=dWYRjT501w}
}
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.