X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=graph.py;h=6db9ed7bc53c5a6adc3a3c9047139feac4ed087e;hb=b87078aec53ead1e0a3ca44d4ac46c319bbcd63e;hp=a819283cc28450c28b54a6e5b6b215c408c65243;hpb=3b9ba21fd3d06a20703216cc0a77fe9dc78b079f;p=picoclvr.git diff --git a/graph.py b/graph.py index a819283..6db9ed7 100755 --- a/graph.py +++ b/graph.py @@ -14,24 +14,23 @@ import cairo def save_attention_image( - filename, + filename, # image to save tokens_input, tokens_output, - # An iterable set of BxHxTxT attention matrices - attention_matrices, - pixel_scale=8, - token_gap=15, - layer_gap=25, - y_eps=0.5, - padding=10, + attention_matrices, # list of 2d tensors T1xT2, T2xT3, ..., Tk-1xTk # do not draw links with a lesser attention min_link_attention=0, - # draw only the strongest links necessary to reache - # min_total_attention + # draw only the strongest links necessary so that their summed + # attention is above min_total_attention min_total_attention=None, # draw only the top k links k_top=None, curved=True, + pixel_scale=8, + token_gap=15, + layer_gap=25, + y_eps=0.5, + padding=10, ): if k_top is not None: am = [] @@ -60,7 +59,7 @@ def save_attention_image( ctx.set_line_width(0.25) for d in range(len(attention_matrices)): - at = attention_matrices[d] + at = attention_matrices[d].to("cpu") ni = torch.arange(at.size(0))[:, None].expand_as(at) nj = torch.arange(at.size(1))[None, :].expand_as(at) at = at.flatten() @@ -111,7 +110,7 @@ def save_attention_image( x_advance, y_advance, ) = ctx.text_extents(s) - ctx.move_to(k * token_gap - width_t / 2, token_gap / 5 - y_bearing) + ctx.move_to(k * token_gap - width_t / 2, 2 * token_gap / 5) ctx.show_text(s) for k, t in enumerate(tokens_output): @@ -147,7 +146,7 @@ def save_attention_image( if __name__ == "__main__": import mygpt - tokens_output = ["", 2, 3, 4, ""] + tokens_output = ["", "-", 3, 4, ""] tokens_input = [""] + tokens_output[:-1] vocabulary_size = 3