5 import torch, torchvision
8 from torch.nn import functional as F
13 ######################################################################
14 def save_attention_image(
28 # surface = cairo.PDFSurface(
29 # filename, surface_width * pixel_scale, surface_height * pixel_scale
32 surface = cairo.RecordingSurface(cairo.CONTENT_COLOR_ALPHA, None)
34 ctx = cairo.Context(surface)
35 ctx.scale(pixel_scale, pixel_scale)
37 ctx.set_source_rgb(0.0, 0.0, 0.0)
38 ctx.set_font_size(4.0)
39 # ctx.select_font_face("Arial", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
42 for n, t in enumerate(tokens):
51 ) = ctx.text_extents(string)
52 u.append((n, string, x, x + width_t / 2, height_t, y_bearing))
53 x += x_advance + token_gap
56 for d in range(attention.size(0) + 1):
57 for n, s, x, xc, h, yb in tokens:
58 # ctx.set_source_rgb(0.0, 0.0, 0.0)
59 # ctx.rectangle(x+x_bearing,y+y_bearing,width_t,height_t)
61 ctx.set_source_rgb(0.0, 0.0, 0.0)
65 if d < attention.size(0):
66 for m, _, _, x2c, h2, y2b in tokens:
67 if attention[d, n, m] >= min_att:
68 c = 1 - attention[d, n, m]
69 ctx.set_source_rgb(c, c, c)
70 ctx.set_line_width(0.5)
71 ctx.move_to(xc, y + yb + h + y_eps)
72 ctx.line_to(x2c, y + layer_gap + y2b - y_eps)
76 x, y, width, height = surface.ink_extents()
77 pdf_surface = cairo.PDFSurface(filename, width, height)
78 ctx_pdf = cairo.Context(pdf_surface)
79 ctx_pdf.set_source_surface(surface, -x, -y)
84 ######################################################################
86 if __name__ == "__main__":
90 x = torch.randint(vocabulary_size, (1, 5))
93 vocabulary_size=vocabulary_size,
104 model.record_attention()
106 y1 = model(mygpt.BracketedSequence(x)).x
108 a = model.retrieve_attention()
110 attention = torch.cat([x[:0] for x in a], dim=0)
112 tokens = ["bluh", 2, 3, 4, "blih"]
113 attention = torch.randn(3, len(tokens), len(tokens)).softmax(dim=-1)
115 save_attention_image("attention.pdf", tokens, attention)