5 import torch, torchvision
8 from torch.nn import functional as F
13 ######################################################################
14 def save_attention_image(
29 attention = torch.cat(
30 [x[n_sample : n_sample + 1, n_head] for x in attention], dim=0
34 attention = attention * (
35 attention.sort(dim=-1, descending=True).indices < k_top
38 surface = cairo.RecordingSurface(cairo.CONTENT_COLOR_ALPHA, None)
40 ctx = cairo.Context(surface)
41 ctx.scale(pixel_scale, pixel_scale)
43 ctx.set_source_rgb(0.0, 0.0, 0.0)
44 ctx.set_font_size(4.0)
45 # ctx.select_font_face("Arial", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
49 for d in range(attention.size(0)):
51 for n in range(attention.size(-1)):
52 xc, yc = n * token_gap, -d * layer_gap
53 ctx.arc(xc, yc, token_gap / 10, 0, 2 * math.pi)
57 ni = torch.arange(at.size(0))[:, None].expand_as(at)
58 nj = torch.arange(at.size(1))[None, :].expand_as(at)
64 for i, j, a in zip(ni, nj, at):
65 if a > 0 and a >= min_att:
67 ctx.set_source_rgb(c, c, c)
68 ctx.set_line_width(0.5)
69 ctx.move_to(j * token_gap, y - y_eps)
70 ctx.line_to(i * token_gap, y - layer_gap + y_eps)
74 for d in range(1, attention.size(0)):
75 for n in range(attention.size(-1)):
76 xc, yc = n * token_gap, -d * layer_gap
77 ctx.set_source_rgb(1.0, 1.0, 1.0)
78 ctx.arc(xc, yc, token_gap / 10 + 0.5, 0, 2 * math.pi)
80 ctx.set_source_rgb(0.0, 0.0, 0.0)
81 ctx.arc(xc, yc, token_gap / 10, 0, 2 * math.pi)
84 ctx.set_source_rgb(0.0, 0.0, 0.0)
86 for k, t in enumerate(tokens_input):
95 ) = ctx.text_extents(s)
96 ctx.move_to(k * token_gap - width_t / 2, -y_bearing)
99 for k, t in enumerate(tokens_output):
108 ) = ctx.text_extents(s)
109 ctx.move_to(k * token_gap - width_t / 2, -attention.size(0) * layer_gap)
112 x, y, width, height = surface.ink_extents()
116 height += 2 * padding
117 pdf_surface = cairo.PDFSurface(filename, width, height)
118 ctx_pdf = cairo.Context(pdf_surface)
119 ctx_pdf.set_source_surface(surface, -x, -y)
124 ######################################################################
126 if __name__ == "__main__":
129 tokens_output = ["bluh", 2, 3, 4, "blih"]
130 tokens_input = ["n/a"] + tokens_output[:-1]
133 x = torch.randint(vocabulary_size, (1, len(tokens_input)))
136 vocabulary_size=vocabulary_size,
147 model.record_attention()
149 y1 = model(mygpt.BracketedSequence(x)).x
151 attention = model.retrieve_attention()
153 save_attention_image("attention.pdf", tokens_input, tokens_output, attention)