Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New modular lenses #48

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open

New modular lenses #48

wants to merge 19 commits into from

Conversation

Xmaster6y
Copy link
Owner

@Xmaster6y Xmaster6y commented Jan 11, 2025

What does this PR do?

Improve lens modularity and add new lenses.

Linked Issues

N/A

Summary by Sourcery

Refactor the lczerolens library to improve modularity. Update the tutorial notebook to reflect these changes.

New Features:

  • Add new lenses, including CompositeLens, GradientLens, and OutputLens.

Enhancements:

  • Improve lens modularity.

Tests:

  • Update tests to reflect changes in the library.

Copy link
Contributor

sourcery-ai bot commented Jan 11, 2025

Reviewer's Guide by Sourcery

This pull request improves lens modularity and adds new lenses. It also updates the tutorial notebook to use the new LczeroBoard class and adds a MODE variable to select the execution environment.

Class diagram for the updated lens architecture

classDiagram
    class Lens {
        <<abstract>>
        -_lens_type: str
        -_registry: Dict[str, Type[Lens]]
        +is_compatible(model: LczeroModel): bool
        +prepare(model: LczeroModel): LczeroModel
        +analyse(model: LczeroModel, *inputs): dict
        +analyse_batched(model: LczeroModel, iter_inputs): Generator
        #_intervene(model: LczeroModel): dict*
    }
    class ActivationLens {
        -_reg_exp: Pattern
        +__init__(pattern: str)
        #_get_modules(model: Module)
        #_intervene(model: LczeroModel): dict
    }
    class ProbingLens {
        -_probe_fn: Callable
        -_reg_exp: Pattern
        +__init__(probe_fn: Callable, pattern: str)
        #_get_modules(model: Module)
        #_intervene(model: LczeroModel): dict
    }
    class CompositeLens {
        -lenses: List[Lens]
        +__init__(lenses: List[Lens])
        +is_compatible(model: LczeroModel): bool
        +prepare(model: LczeroModel): LczeroModel
        #_intervene(model: LczeroModel): dict
    }
    class OutputLens {
        #_intervene(model: LczeroModel): dict
    }
    class GradientLens {
        #_intervene(model: LczeroModel): dict
    }
    Lens <|-- ActivationLens
    Lens <|-- ProbingLens
    Lens <|-- CompositeLens
    Lens <|-- OutputLens
    Lens <|-- GradientLens
Loading

File-Level Changes

Change Details Files
Updated the tutorial notebook to use the new LczeroBoard class and added a MODE variable to select the execution environment.
  • Replaced chess.Board with LczeroBoard.
  • Added MODE variable to handle different execution environments (colab, colab-dev, local).
  • Modified installation instructions based on MODE value.
  • Updated import statements.
  • Updated code to handle LczeroBoard objects.
  • Added visualization code for attention heatmaps.
  • Added probing analysis section.
  • Added activation patching section.
docs/source/notebooks/tutorials/evidence-of-learned-look-ahead.ipynb
Refactored ActivationLens to use LczeroBoard and simplified its interface.
  • Removed storage attribute.
  • Removed is_compatible method.
  • Simplified analyse method to accept LczeroBoard objects and return a dictionary.
  • Added _get_modules and _intervene methods.
  • Removed ActivationBuffer class.
src/lczerolens/lenses/activation.py
Added new lenses and updated the Lens class.
  • Added _lens_type attribute to Lens class.
  • Added prepare method to Lens class.
  • Added _intervene method to Lens class.
  • Updated analyse and analyse_batched methods to use prepare and intervene.
  • Added CompositeLens, GradientLens, and OutputLens classes.
  • Updated lens documentation.
  • Updated init.py to include new lenses.
  • Updated API documentation to include new lenses and changes to the Lens class.
src/lczerolens/lens.py
src/lczerolens/lenses/__init__.py
docs/source/api/lenses.rst
docs/source/api/lenses/composite.rst
docs/source/api/lenses/output.rst

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Xmaster6y - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Please clarify why ActivationBuffer was removed and where that functionality is now handled.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

src/lczerolens/lens.py Show resolved Hide resolved
src/lczerolens/lenses/activation.py Outdated Show resolved Hide resolved
src/lczerolens/lenses/composite.py Outdated Show resolved Hide resolved
src/lczerolens/lenses/gradient.py Outdated Show resolved Hide resolved
src/lczerolens/lenses/probing/lens.py Outdated Show resolved Hide resolved
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant