Analysis System¶
You know how we had trainer.find() and trainer.debug() in time travel debugging?
What if you can run those during training, and also make pretty plots about it? That'd be pretty sick!
Make an analysis job¶
The analysis job follows a very similar API as trainer.debug(). I would look at those docs before you move on here because this would be +\infty more sense. When you are back, read the following basic example for analysis:
from theseus.analysis import AnalysisBase
from theseus.experiments.models.gpt import PretrainGPT
from theseus.model.attention.base import SelfAttention
from theseus.registry import analysis, job
@analysis("my/gpt/activations")
class Activations(AnalysisBase, PretrainGPT):
TARGET = SelfAttention
NAME = "activations"
def select(self, paths):
return paths[0] # First discovered attention module.
def analyze(self, layer, inputs):
return inputs.x[0, :64, :128] # First sample; crop tokens/features.
def plot(self, values):
return self.heatmap(values, xlabel="Feature", ylabel="Token")
What would this look like using the debug api?
Not exactly the same, as we will see, but roughly:
Here, we are asking for a copped example of the input to the first SelfAttention layer, and then plotting it as a heatmap.
There are three methods describe what to inspect and how to display it:
select(paths): chooses from discoveredTARGETcalls; path are keyed by strings likeblocks_0/attnanalyze(layer, inputs): gives you the debuggerlayerand its captured inputinputsplot(values): takes whatever you returned fromanalyzeon Rank 0 and then does some transformations with it
Unlike the notebook debugger API, analyze() runs under JIT, so you should probably not do something that will make the hardworking compiler engineers at Google unhappy. Returning a matplotlib figure in plot() will save a PDF, and returning a dictionary with stuff in it will save a JSON.
What's up with .heatmap()¶
I have font disease so I have very specific opinions about how plots should look like. You don't need to use these opinions and if you don't have this afliction you can just use seaborn yourself and return the Figure in def plot(...). But, for convenience, the following font-disease approved plotting API with identical signatures as Seaborn equivalents is available to you:
self.heatmap(): heatmapsself.bar: bar chartsself.line: line graphsself.scatter: scatter plots
Isn't it interesting that English has a different word for each type of plot or am I tripping?
Put it inline during training¶
You can in fact ask to do analyses during validation by adding it the ANALYSIS field of a Trainer:
Run on a saved checkpoint¶
You can also use the amazing time travel API to attach an evaluation to a historical checkpoint as if it had happened during training: