VBoussot commited on
Commit
045bd3a
·
1 Parent(s): cc45df7

refactor(convexadam): scope the legacy-JIT-executor workaround to the registration

Browse files

Replace the module-import global torch JIT flag (which would impose the legacy executor on every itk-impact / torch user in the process) with a scoped _legacy_jit_executor() context manager around the registration run, restoring the caller's setting on exit. The proper fix belongs in itk-impact's C++ TorchScript execution; this is the bundle-side stopgap.

ConvexAdam_Coarse/Model.py CHANGED
@@ -33,6 +33,7 @@ NOTE: do NOT add ``from __future__ import annotations`` — KonfAI's config engi
33
  runtime-evaluated annotations (``get_origin``); PEP 563 stringized annotations break binding.
34
  """
35
 
 
36
  import json
37
  import os
38
  from dataclasses import dataclass, field
@@ -49,13 +50,6 @@ from konfai.network import network
49
  from konfai.utils.config import Choices, Range
50
  from konfai.utils.dataset import Attribute, data_to_image, image_to_data
51
 
52
- # Some TorchScript feature models (notably the TS segmentation exports) trip the JIT TensorExpr fuser's alias
53
- # analysis ("INTERNAL ASSERT FAILED ... We don't have an op for aten::dim") when itk-impact runs them through
54
- # libtorch. Fall back to the stable legacy executor (no profiling / no fusion) so every registry model runs;
55
- # itk-impact shares Python's libtorch, so setting the global flag here also applies to its C++ execution.
56
- torch._C._jit_set_profiling_executor(False)
57
- torch._C._jit_set_profiling_mode(False)
58
-
59
  DIM = 3
60
  # The feature model's input channel count is an intrinsic property of the pretrained model (grayscale
61
  # medical images), not a tunable — so it's fixed here, never a config/signature parameter.
@@ -113,6 +107,21 @@ class ModelSpec:
113
  distance: Literal["L1", "L2", "Dice", "Cosine", "NCC"] = "L1"
114
 
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  def _coarse_registration_type():
117
  """The coupled-convex initializer, tolerant to the two names the wrapping has shipped under."""
118
  cls = getattr(itk, "ImpactCoarseRegistration", None) or getattr(itk, "ImpactConvexAdamInitializer", None)
@@ -451,7 +460,7 @@ class ConvexAdamRegistration(torch.nn.Module):
451
  # ConvexAdam runs a gradient-based instance optimisation (Adam over the field) inside itk-impact's
452
  # .Update(); the predictor calls forward under torch.inference_mode(), which forbids autograd. The
453
  # image tensors have already crossed to numpy/ITK here, so re-enable grad for the optimisation.
454
- with torch.inference_mode(False), torch.enable_grad():
455
  for b in range(fixed.shape[0]):
456
  fixed_img = data_to_image(fixed[b].detach().cpu().numpy(), fixed_attrs[b])
457
  moving_img = data_to_image(moving[b].detach().cpu().numpy(), moving_attrs[b])
 
33
  runtime-evaluated annotations (``get_origin``); PEP 563 stringized annotations break binding.
34
  """
35
 
36
+ import contextlib
37
  import json
38
  import os
39
  from dataclasses import dataclass, field
 
50
  from konfai.utils.config import Choices, Range
51
  from konfai.utils.dataset import Attribute, data_to_image, image_to_data
52
 
 
 
 
 
 
 
 
53
  DIM = 3
54
  # The feature model's input channel count is an intrinsic property of the pretrained model (grayscale
55
  # medical images), not a tunable — so it's fixed here, never a config/signature parameter.
 
107
  distance: Literal["L1", "L2", "Dice", "Cosine", "NCC"] = "L1"
108
 
109
 
110
+ @contextlib.contextmanager
111
+ def _legacy_jit_executor():
112
+ """Run TorchScript feature models on torch's stable legacy executor (no profiling / TensorExpr fusion) for
113
+ the duration of the block: some models (the TS segmentation exports) crash the fuser ("aten::dim" internal
114
+ assert) when itk-impact runs them through libtorch. Strictly SCOPED — the caller's global JIT settings are
115
+ restored on exit, so nothing is imposed on other itk-impact / torch users in the process."""
116
+ old_executor = torch._C._jit_set_profiling_executor(False)
117
+ old_mode = torch._C._jit_set_profiling_mode(False)
118
+ try:
119
+ yield
120
+ finally:
121
+ torch._C._jit_set_profiling_executor(old_executor)
122
+ torch._C._jit_set_profiling_mode(old_mode)
123
+
124
+
125
  def _coarse_registration_type():
126
  """The coupled-convex initializer, tolerant to the two names the wrapping has shipped under."""
127
  cls = getattr(itk, "ImpactCoarseRegistration", None) or getattr(itk, "ImpactConvexAdamInitializer", None)
 
460
  # ConvexAdam runs a gradient-based instance optimisation (Adam over the field) inside itk-impact's
461
  # .Update(); the predictor calls forward under torch.inference_mode(), which forbids autograd. The
462
  # image tensors have already crossed to numpy/ITK here, so re-enable grad for the optimisation.
463
+ with torch.inference_mode(False), torch.enable_grad(), _legacy_jit_executor():
464
  for b in range(fixed.shape[0]):
465
  fixed_img = data_to_image(fixed[b].detach().cpu().numpy(), fixed_attrs[b])
466
  moving_img = data_to_image(moving[b].detach().cpu().numpy(), moving_attrs[b])
ConvexAdam_Coarse/__pycache__/Model.cpython-313.pyc CHANGED
Binary files a/ConvexAdam_Coarse/__pycache__/Model.cpython-313.pyc and b/ConvexAdam_Coarse/__pycache__/Model.cpython-313.pyc differ
 
ConvexAdam_Composite/Model.py CHANGED
@@ -33,6 +33,7 @@ NOTE: do NOT add ``from __future__ import annotations`` — KonfAI's config engi
33
  runtime-evaluated annotations (``get_origin``); PEP 563 stringized annotations break binding.
34
  """
35
 
 
36
  import json
37
  import os
38
  from dataclasses import dataclass, field
@@ -49,13 +50,6 @@ from konfai.network import network
49
  from konfai.utils.config import Choices, Range
50
  from konfai.utils.dataset import Attribute, data_to_image, image_to_data
51
 
52
- # Some TorchScript feature models (notably the TS segmentation exports) trip the JIT TensorExpr fuser's alias
53
- # analysis ("INTERNAL ASSERT FAILED ... We don't have an op for aten::dim") when itk-impact runs them through
54
- # libtorch. Fall back to the stable legacy executor (no profiling / no fusion) so every registry model runs;
55
- # itk-impact shares Python's libtorch, so setting the global flag here also applies to its C++ execution.
56
- torch._C._jit_set_profiling_executor(False)
57
- torch._C._jit_set_profiling_mode(False)
58
-
59
  DIM = 3
60
  # The feature model's input channel count is an intrinsic property of the pretrained model (grayscale
61
  # medical images), not a tunable — so it's fixed here, never a config/signature parameter.
@@ -113,6 +107,21 @@ class ModelSpec:
113
  distance: Literal["L1", "L2", "Dice", "Cosine", "NCC"] = "L1"
114
 
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  def _coarse_registration_type():
117
  """The coupled-convex initializer, tolerant to the two names the wrapping has shipped under."""
118
  cls = getattr(itk, "ImpactCoarseRegistration", None) or getattr(itk, "ImpactConvexAdamInitializer", None)
@@ -451,7 +460,7 @@ class ConvexAdamRegistration(torch.nn.Module):
451
  # ConvexAdam runs a gradient-based instance optimisation (Adam over the field) inside itk-impact's
452
  # .Update(); the predictor calls forward under torch.inference_mode(), which forbids autograd. The
453
  # image tensors have already crossed to numpy/ITK here, so re-enable grad for the optimisation.
454
- with torch.inference_mode(False), torch.enable_grad():
455
  for b in range(fixed.shape[0]):
456
  fixed_img = data_to_image(fixed[b].detach().cpu().numpy(), fixed_attrs[b])
457
  moving_img = data_to_image(moving[b].detach().cpu().numpy(), moving_attrs[b])
 
33
  runtime-evaluated annotations (``get_origin``); PEP 563 stringized annotations break binding.
34
  """
35
 
36
+ import contextlib
37
  import json
38
  import os
39
  from dataclasses import dataclass, field
 
50
  from konfai.utils.config import Choices, Range
51
  from konfai.utils.dataset import Attribute, data_to_image, image_to_data
52
 
 
 
 
 
 
 
 
53
  DIM = 3
54
  # The feature model's input channel count is an intrinsic property of the pretrained model (grayscale
55
  # medical images), not a tunable — so it's fixed here, never a config/signature parameter.
 
107
  distance: Literal["L1", "L2", "Dice", "Cosine", "NCC"] = "L1"
108
 
109
 
110
+ @contextlib.contextmanager
111
+ def _legacy_jit_executor():
112
+ """Run TorchScript feature models on torch's stable legacy executor (no profiling / TensorExpr fusion) for
113
+ the duration of the block: some models (the TS segmentation exports) crash the fuser ("aten::dim" internal
114
+ assert) when itk-impact runs them through libtorch. Strictly SCOPED — the caller's global JIT settings are
115
+ restored on exit, so nothing is imposed on other itk-impact / torch users in the process."""
116
+ old_executor = torch._C._jit_set_profiling_executor(False)
117
+ old_mode = torch._C._jit_set_profiling_mode(False)
118
+ try:
119
+ yield
120
+ finally:
121
+ torch._C._jit_set_profiling_executor(old_executor)
122
+ torch._C._jit_set_profiling_mode(old_mode)
123
+
124
+
125
  def _coarse_registration_type():
126
  """The coupled-convex initializer, tolerant to the two names the wrapping has shipped under."""
127
  cls = getattr(itk, "ImpactCoarseRegistration", None) or getattr(itk, "ImpactConvexAdamInitializer", None)
 
460
  # ConvexAdam runs a gradient-based instance optimisation (Adam over the field) inside itk-impact's
461
  # .Update(); the predictor calls forward under torch.inference_mode(), which forbids autograd. The
462
  # image tensors have already crossed to numpy/ITK here, so re-enable grad for the optimisation.
463
+ with torch.inference_mode(False), torch.enable_grad(), _legacy_jit_executor():
464
  for b in range(fixed.shape[0]):
465
  fixed_img = data_to_image(fixed[b].detach().cpu().numpy(), fixed_attrs[b])
466
  moving_img = data_to_image(moving[b].detach().cpu().numpy(), moving_attrs[b])
ConvexAdam_Composite/__pycache__/Model.cpython-313.pyc CHANGED
Binary files a/ConvexAdam_Composite/__pycache__/Model.cpython-313.pyc and b/ConvexAdam_Composite/__pycache__/Model.cpython-313.pyc differ
 
ConvexAdam_Fine/Model.py CHANGED
@@ -33,6 +33,7 @@ NOTE: do NOT add ``from __future__ import annotations`` — KonfAI's config engi
33
  runtime-evaluated annotations (``get_origin``); PEP 563 stringized annotations break binding.
34
  """
35
 
 
36
  import json
37
  import os
38
  from dataclasses import dataclass, field
@@ -49,13 +50,6 @@ from konfai.network import network
49
  from konfai.utils.config import Choices, Range
50
  from konfai.utils.dataset import Attribute, data_to_image, image_to_data
51
 
52
- # Some TorchScript feature models (notably the TS segmentation exports) trip the JIT TensorExpr fuser's alias
53
- # analysis ("INTERNAL ASSERT FAILED ... We don't have an op for aten::dim") when itk-impact runs them through
54
- # libtorch. Fall back to the stable legacy executor (no profiling / no fusion) so every registry model runs;
55
- # itk-impact shares Python's libtorch, so setting the global flag here also applies to its C++ execution.
56
- torch._C._jit_set_profiling_executor(False)
57
- torch._C._jit_set_profiling_mode(False)
58
-
59
  DIM = 3
60
  # The feature model's input channel count is an intrinsic property of the pretrained model (grayscale
61
  # medical images), not a tunable — so it's fixed here, never a config/signature parameter.
@@ -113,6 +107,21 @@ class ModelSpec:
113
  distance: Literal["L1", "L2", "Dice", "Cosine", "NCC"] = "L1"
114
 
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  def _coarse_registration_type():
117
  """The coupled-convex initializer, tolerant to the two names the wrapping has shipped under."""
118
  cls = getattr(itk, "ImpactCoarseRegistration", None) or getattr(itk, "ImpactConvexAdamInitializer", None)
@@ -451,7 +460,7 @@ class ConvexAdamRegistration(torch.nn.Module):
451
  # ConvexAdam runs a gradient-based instance optimisation (Adam over the field) inside itk-impact's
452
  # .Update(); the predictor calls forward under torch.inference_mode(), which forbids autograd. The
453
  # image tensors have already crossed to numpy/ITK here, so re-enable grad for the optimisation.
454
- with torch.inference_mode(False), torch.enable_grad():
455
  for b in range(fixed.shape[0]):
456
  fixed_img = data_to_image(fixed[b].detach().cpu().numpy(), fixed_attrs[b])
457
  moving_img = data_to_image(moving[b].detach().cpu().numpy(), moving_attrs[b])
 
33
  runtime-evaluated annotations (``get_origin``); PEP 563 stringized annotations break binding.
34
  """
35
 
36
+ import contextlib
37
  import json
38
  import os
39
  from dataclasses import dataclass, field
 
50
  from konfai.utils.config import Choices, Range
51
  from konfai.utils.dataset import Attribute, data_to_image, image_to_data
52
 
 
 
 
 
 
 
 
53
  DIM = 3
54
  # The feature model's input channel count is an intrinsic property of the pretrained model (grayscale
55
  # medical images), not a tunable — so it's fixed here, never a config/signature parameter.
 
107
  distance: Literal["L1", "L2", "Dice", "Cosine", "NCC"] = "L1"
108
 
109
 
110
+ @contextlib.contextmanager
111
+ def _legacy_jit_executor():
112
+ """Run TorchScript feature models on torch's stable legacy executor (no profiling / TensorExpr fusion) for
113
+ the duration of the block: some models (the TS segmentation exports) crash the fuser ("aten::dim" internal
114
+ assert) when itk-impact runs them through libtorch. Strictly SCOPED — the caller's global JIT settings are
115
+ restored on exit, so nothing is imposed on other itk-impact / torch users in the process."""
116
+ old_executor = torch._C._jit_set_profiling_executor(False)
117
+ old_mode = torch._C._jit_set_profiling_mode(False)
118
+ try:
119
+ yield
120
+ finally:
121
+ torch._C._jit_set_profiling_executor(old_executor)
122
+ torch._C._jit_set_profiling_mode(old_mode)
123
+
124
+
125
  def _coarse_registration_type():
126
  """The coupled-convex initializer, tolerant to the two names the wrapping has shipped under."""
127
  cls = getattr(itk, "ImpactCoarseRegistration", None) or getattr(itk, "ImpactConvexAdamInitializer", None)
 
460
  # ConvexAdam runs a gradient-based instance optimisation (Adam over the field) inside itk-impact's
461
  # .Update(); the predictor calls forward under torch.inference_mode(), which forbids autograd. The
462
  # image tensors have already crossed to numpy/ITK here, so re-enable grad for the optimisation.
463
+ with torch.inference_mode(False), torch.enable_grad(), _legacy_jit_executor():
464
  for b in range(fixed.shape[0]):
465
  fixed_img = data_to_image(fixed[b].detach().cpu().numpy(), fixed_attrs[b])
466
  moving_img = data_to_image(moving[b].detach().cpu().numpy(), moving_attrs[b])
ConvexAdam_Fine/__pycache__/Model.cpython-313.pyc CHANGED
Binary files a/ConvexAdam_Fine/__pycache__/Model.cpython-313.pyc and b/ConvexAdam_Fine/__pycache__/Model.cpython-313.pyc differ