-
Notifications
You must be signed in to change notification settings - Fork 209
/
visuals.js
243 lines (205 loc) · 6.41 KB
/
visuals.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
class Visuals {
initialized = false;
constructor(model) {
this.model = model;
this.device = model.device;
this.params = model.params;
}
init() {
this.initFoundation();
this.initUniforms();
this.initLayoutAndPipeline();
this.initBuffersAndBindGroup();
this.updateModelBuffer();
this.initialized = true;
}
initFoundation() {
const containerEl = document.getElementById("visualsContainer");
const gpuCanvasEl = document.createElement("canvas");
containerEl.style.width = this.params.n_embd + "px";
containerEl.style.height = this.params.n_ctx + "px";
gpuCanvasEl.style.width = "100%";
gpuCanvasEl.style.height = "100%";
gpuCanvasEl.width = this.params.n_embd;
gpuCanvasEl.height = this.params.n_ctx;
const gpuContext = gpuCanvasEl.getContext("webgpu");
const gpuCanvasFormat = navigator.gpu.getPreferredCanvasFormat();
gpuContext.configure({
device: this.device,
format: gpuCanvasFormat,
});
containerEl.appendChild(gpuCanvasEl);
this.containerEl = containerEl;
this.gpuCanvasFormat = gpuCanvasFormat;
this.gpuCanvasEl = gpuCanvasEl;
this.gpuContext = gpuContext;
}
updateModelBuffer() {
this.model.externalBuffer = this.embeddingsBuffer;
}
initUniforms() {
this.uniforms = {
width: this.model.params.n_embd,
height: this.model.params.n_ctx,
};
}
initLayoutAndPipeline() {
this.bindGroupLayout = this.device.createBindGroupLayout({
entries: [
{
binding: 0,
visibility: GPUShaderStage.FRAGMENT,
buffer: {
type: "uniform",
}
},
{
binding: 1,
visibility: GPUShaderStage.FRAGMENT,
buffer: {
type: "read-only-storage",
}
},
]
});
this.renderShaderModule = this.device.createShaderModule({
label: 'visuals',
code: `
struct UniformData {
width: f32,
height: f32,
}
@vertex
fn vsMain(@builtin(vertex_index) vertexIndex: u32) -> @builtin(position) vec4<f32> {
var positions = array<vec2<f32>, 6>(
vec2<f32>(-1.0, -1.0), // bottom left
vec2<f32>( 1.0, -1.0), // bottom right
vec2<f32>(-1.0, 1.0), // top left
vec2<f32>(-1.0, 1.0), // top left
vec2<f32>( 1.0, -1.0), // bottom right
vec2<f32>( 1.0, 1.0) // top right
);
return vec4<f32>(positions[vertexIndex], 0.0, 1.0);
}
@group(0) @binding(0) var<uniform> uniformData: UniformData;
@group(0) @binding(1) var<storage, read> embeddingsBuffer: array<f32>;
@fragment
fn fsMain(@builtin(position) fragCoord: vec4<f32>) -> @location(0) vec4<f32> {
let xNormalized = fragCoord.x / uniformData.width;
let yNormalized = fragCoord.y / uniformData.height;
let xIndex = xNormalized * ${this.params.n_embd};
let yIndex = yNormalized * ${this.params.n_ctx};
let index = u32(yIndex) * ${this.params.n_embd} + u32(xIndex);
let vectorValue = embeddingsBuffer[index];
var outColor = vec4<f32>(0.0);
outColor = hdrColorMapping(outColor, 1.0, vectorValue * 0.1);
return outColor;
}
fn hdrColorMapping(colorRef: vec4<f32>, hdrThreshold: f32, vectorValue: f32) -> vec4<f32> {
var color = colorRef;
if (vectorValue < 0.0) {
color.b = -vectorValue;
if (vectorValue < -hdrThreshold) {
color.g = -vectorValue - hdrThreshold;
}
} else {
color.r = vectorValue;
if (vectorValue > hdrThreshold) {
color.g = vectorValue - hdrThreshold;
}
}
color.g = min(color.g, 0.7);
return color;
}
`
});
this.renderPipeline = this.device.createRenderPipeline({
layout: this.device.createPipelineLayout({
bindGroupLayouts: [this.bindGroupLayout],
}),
vertex: {
module: this.renderShaderModule,
entryPoint: 'vsMain',
buffers: []
},
fragment: {
module: this.renderShaderModule,
entryPoint: 'fsMain',
targets: [
{
format: this.gpuCanvasFormat,
},
],
},
});
}
initBuffersAndBindGroup() {
const uniformCount = Object.values(this.uniforms).length;
this.uniformBuffer = this.device.createBuffer({
size: uniformCount * Float32Array.BYTES_PER_ELEMENT,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});
this.embeddingsBuffer = this.device.createBuffer({
size: this.model.bufferSize(this.params.n_ctx, this.params.n_embd),
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
});
this.bindGroup = this.device.createBindGroup({
layout: this.bindGroupLayout,
entries: [
{
binding: 0,
resource: {
buffer: this.uniformBuffer,
}
},
{
binding: 1,
resource: {
buffer: this.embeddingsBuffer,
}
},
]
});
this.updateUniforms();
}
updateUniforms() {
this.uniforms.width = this.gpuCanvasEl.width;
this.uniforms.height = this.gpuCanvasEl.height;
const uniformArray = new Float32Array([
this.uniforms.width,
this.uniforms.height,
]);
this.device.queue.writeBuffer(
this.uniformBuffer,
0,
uniformArray.buffer,
uniformArray.byteOffset,
uniformArray.byteLength,
);
}
render(existingCommandEncoder) {
const commandEncoder = existingCommandEncoder ?? this.device.createCommandEncoder();
const textureView = this.gpuContext.getCurrentTexture().createView();
const renderPassDescriptor = {
colorAttachments: [
{
view: textureView,
loadOp: 'clear',
loadValue: { r: 0.0, g: 0.0, b: 0.0, a: 1.0 },
storeOp: 'store',
}
]
};
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
passEncoder.setPipeline(this.renderPipeline);
passEncoder.setBindGroup(0, this.bindGroup);
passEncoder.draw(6, 1, 0, 0);
passEncoder.end();
this.device.queue.submit([commandEncoder.finish()]);
}
destroy() {
this.gpuCanvasEl.remove();
this.uniformBuffer.destroy();
this.embeddingsBuffer.destroy();
}
}