MCPcopy Create free account
hub / github.com/Standard-Intelligence/hertz-dev / forward

Method forward

ioblocks.py:266–333  ·  view source on GitHub ↗

einstein notation b - batch n - sequence (or flattened spatial dimensions) d - feature dimension c - number of codebook dim

(self, z, return_codes=False)

Source from the content-addressed store, hash-verified

264
265 # @autocast(device_type='cuda', enabled = False)
266 def forward(self, z, return_codes=False):
267 """
268 einstein notation
269 b - batch
270 n - sequence (or flattened spatial dimensions)
271 d - feature dimension
272 c - number of codebook dim
273 """
274
275 is_img_or_video = z.ndim >= 4
276 need_move_channel_last = is_img_or_video or self.channel_first
277
278 # standardize image or video into (batch, seq, dimension)
279
280 if need_move_channel_last:
281 z = rearrange(z, 'b d ... -> b ... d')
282 z, ps = pack_one(z, 'b * d')
283
284 assert z.shape[-1] == self.dim, f'expected dimension of {self.dim} but found dimension of {z.shape[-1]}'
285
286 z = self.project_in(z)
287
288 z = rearrange(z, 'b n (c d) -> b n c d', c = self.num_codebooks)
289
290 # whether to force quantization step to be full precision or not
291
292 force_f32 = self.force_quantization_f32
293 quantization_context = partial(autocast, device_type='cuda', enabled = False) if force_f32 else nullcontext
294
295 with quantization_context():
296 orig_dtype = z.dtype
297
298 if force_f32 and orig_dtype not in self.allowed_dtypes:
299 z = z.float()
300
301 codes = self.quantize(z)
302
303 # returning indices could be optional
304
305 indices = None
306
307 if self.return_indices:
308 indices = self.codes_to_indices(codes)
309
310 codes = rearrange(codes, 'b n c d -> b n (c d)')
311
312 codes = codes.type(orig_dtype)
313
314 # project out
315 if return_codes:
316 return codes, indices
317
318 out = self.project_out(codes)
319
320 # reconstitute image or video dimensions
321
322 if need_move_channel_last:
323 out = unpack_one(out, ps, 'b * d')

Callers

nothing calls this directly

Calls 5

quantizeMethod · 0.95
codes_to_indicesMethod · 0.95
maybeFunction · 0.90
pack_oneFunction · 0.85
unpack_oneFunction · 0.85

Tested by

no test coverage detected