Jackoatmon commited on
Commit
41f5f4c
·
verified ·
1 Parent(s): 540c086

Update Feather training runtime image

Browse files
Files changed (1) hide show
  1. overlay/subsystems/htm.py +73 -35
overlay/subsystems/htm.py CHANGED
@@ -60,7 +60,15 @@ _HTM_HAS_CAI = _HTM_HAS_GPU and hasattr(_HTM_REGION_GPU, "step_many_cuda")
60
  # Opt-in via env var (default on when available).
61
  import os as _os_fused
62
  _HTM_HAS_FUSED = _HTM_HAS_GPU and hasattr(_HTM_REGION_GPU, "step_many_fused_cuda")
63
- _HTM_USE_FUSED = _HTM_HAS_FUSED and bool(int(_os_fused.environ.get("HYDRA_HTM_FUSED", "1")))
 
 
 
 
 
 
 
 
64
 
65
 
66
  class HTMLayer(nn.Module):
@@ -170,19 +178,30 @@ class HTMLayer(nn.Module):
170
  if _HTM_HAS_CAI and self._use_gpu and sdr.is_cuda:
171
  sdr_u8 = sdr.contiguous().to(torch.uint8) if sdr.dtype != torch.uint8 else sdr.contiguous()
172
  cols_out = torch.empty((B, T, self.n_columns), dtype=torch.uint8, device=sdr.device)
173
- anom_out = torch.empty((B, T), dtype=torch.float32, device=sdr.device)
174
- # Pick fused (1 launch) or legacy (12*T launches) path.
175
- if _HTM_USE_FUSED:
176
- for b in range(B):
177
- self._regions[b].step_many_fused_cuda(
178
- sdr_u8[b].__cuda_array_interface__,
179
- cols_out[b].__cuda_array_interface__,
180
- anom_out[b].__cuda_array_interface__,
181
- learn,
182
- )
183
- else:
184
- for b in range(B):
185
- self._regions[b].step_many_cuda(
 
 
 
 
 
 
 
 
 
 
 
186
  sdr_u8[b].__cuda_array_interface__,
187
  cols_out[b].__cuda_array_interface__,
188
  anom_out[b].__cuda_array_interface__,
@@ -280,30 +299,49 @@ class HTMLayer(nn.Module):
280
  [anom_out[b].__cuda_array_interface__ for b in range(B)],
281
  learn,
282
  )
283
- except RuntimeError as _e:
284
- if "COOPERATIVE_LAUNCH_TOO_LARGE" in str(_e):
285
- # Batch too large for cooperative grid. Fall back to
286
- # sequential per-region fused launches (each B=1).
287
- for b in range(B):
288
  self._regions[b].step_many_fused_cuda(
289
  sdr_u8[b].__cuda_array_interface__,
290
  cols_out[b].__cuda_array_interface__,
291
- anom_out[b].__cuda_array_interface__,
292
- learn,
293
- )
294
- else:
295
- raise
296
- elif _HTM_USE_FUSED:
297
- for b in range(B):
298
- self._regions[b].step_many_fused_cuda(
299
- sdr_u8[b].__cuda_array_interface__,
300
- cols_out[b].__cuda_array_interface__,
301
- anom_out[b].__cuda_array_interface__,
302
- learn,
303
- )
304
- else:
305
- for b in range(B):
306
- self._regions[b].step_many_cuda(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
307
  sdr_u8[b].__cuda_array_interface__,
308
  cols_out[b].__cuda_array_interface__,
309
  anom_out[b].__cuda_array_interface__,
 
60
  # Opt-in via env var (default on when available).
61
  import os as _os_fused
62
  _HTM_HAS_FUSED = _HTM_HAS_GPU and hasattr(_HTM_REGION_GPU, "step_many_fused_cuda")
63
+ _HTM_USE_FUSED = _HTM_HAS_FUSED and bool(int(_os_fused.environ.get("HYDRA_HTM_FUSED", "1")))
64
+
65
+
66
+ def _is_fused_unavailable_error(exc: RuntimeError) -> bool:
67
+ message = str(exc)
68
+ return (
69
+ "Fused HTM kernel is unavailable" in message
70
+ or "fused HTM kernel disabled for this CUDA arch" in message
71
+ )
72
 
73
 
74
  class HTMLayer(nn.Module):
 
178
  if _HTM_HAS_CAI and self._use_gpu and sdr.is_cuda:
179
  sdr_u8 = sdr.contiguous().to(torch.uint8) if sdr.dtype != torch.uint8 else sdr.contiguous()
180
  cols_out = torch.empty((B, T, self.n_columns), dtype=torch.uint8, device=sdr.device)
181
+ anom_out = torch.empty((B, T), dtype=torch.float32, device=sdr.device)
182
+ # Pick fused (1 launch) or legacy (12*T launches) path.
183
+ if _HTM_USE_FUSED:
184
+ try:
185
+ for b in range(B):
186
+ self._regions[b].step_many_fused_cuda(
187
+ sdr_u8[b].__cuda_array_interface__,
188
+ cols_out[b].__cuda_array_interface__,
189
+ anom_out[b].__cuda_array_interface__,
190
+ learn,
191
+ )
192
+ except RuntimeError as exc:
193
+ if not _is_fused_unavailable_error(exc):
194
+ raise
195
+ for b in range(B):
196
+ self._regions[b].step_many_cuda(
197
+ sdr_u8[b].__cuda_array_interface__,
198
+ cols_out[b].__cuda_array_interface__,
199
+ anom_out[b].__cuda_array_interface__,
200
+ learn,
201
+ )
202
+ else:
203
+ for b in range(B):
204
+ self._regions[b].step_many_cuda(
205
  sdr_u8[b].__cuda_array_interface__,
206
  cols_out[b].__cuda_array_interface__,
207
  anom_out[b].__cuda_array_interface__,
 
299
  [anom_out[b].__cuda_array_interface__ for b in range(B)],
300
  learn,
301
  )
302
+ except RuntimeError as _e:
303
+ if "COOPERATIVE_LAUNCH_TOO_LARGE" in str(_e):
304
+ # Batch too large for cooperative grid. Fall back to
305
+ # sequential per-region fused launches (each B=1).
306
+ for b in range(B):
307
  self._regions[b].step_many_fused_cuda(
308
  sdr_u8[b].__cuda_array_interface__,
309
  cols_out[b].__cuda_array_interface__,
310
+ anom_out[b].__cuda_array_interface__,
311
+ learn,
312
+ )
313
+ elif _is_fused_unavailable_error(_e):
314
+ for b in range(B):
315
+ self._regions[b].step_many_cuda(
316
+ sdr_u8[b].__cuda_array_interface__,
317
+ cols_out[b].__cuda_array_interface__,
318
+ anom_out[b].__cuda_array_interface__,
319
+ learn,
320
+ )
321
+ else:
322
+ raise
323
+ elif _HTM_USE_FUSED:
324
+ try:
325
+ for b in range(B):
326
+ self._regions[b].step_many_fused_cuda(
327
+ sdr_u8[b].__cuda_array_interface__,
328
+ cols_out[b].__cuda_array_interface__,
329
+ anom_out[b].__cuda_array_interface__,
330
+ learn,
331
+ )
332
+ except RuntimeError as exc:
333
+ if not _is_fused_unavailable_error(exc):
334
+ raise
335
+ for b in range(B):
336
+ self._regions[b].step_many_cuda(
337
+ sdr_u8[b].__cuda_array_interface__,
338
+ cols_out[b].__cuda_array_interface__,
339
+ anom_out[b].__cuda_array_interface__,
340
+ learn,
341
+ )
342
+ else:
343
+ for b in range(B):
344
+ self._regions[b].step_many_cuda(
345
  sdr_u8[b].__cuda_array_interface__,
346
  cols_out[b].__cuda_array_interface__,
347
  anom_out[b].__cuda_array_interface__,