code-loader 1.0.61.dev6__tar.gz → 1.0.63__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/PKG-INFO +1 -2
  2. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/contract/datasetclasses.py +7 -22
  3. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/contract/responsedataclasses.py +1 -0
  4. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/inner_leap_binder/leapbinder.py +8 -8
  5. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/inner_leap_binder/leapbinder_decorators.py +25 -9
  6. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/leaploader.py +18 -55
  7. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/pyproject.toml +1 -2
  8. code_loader-1.0.61.dev6/code_loader/code_inegration_processes_manager.py +0 -83
  9. code_loader-1.0.61.dev6/code_loader/leaploaderbase.py +0 -91
  10. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/LICENSE +0 -0
  11. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/README.md +0 -0
  12. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/__init__.py +0 -0
  13. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/contract/__init__.py +0 -0
  14. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/contract/enums.py +0 -0
  15. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/contract/exceptions.py +0 -0
  16. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/contract/visualizer_classes.py +0 -0
  17. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/experiment_api/__init__.py +0 -0
  18. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/experiment_api/api.py +0 -0
  19. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/experiment_api/cli_config_utils.py +0 -0
  20. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/experiment_api/client.py +0 -0
  21. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/experiment_api/epoch.py +0 -0
  22. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/experiment_api/experiment.py +0 -0
  23. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/experiment_api/experiment_context.py +0 -0
  24. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/experiment_api/types.py +0 -0
  25. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/experiment_api/utils.py +0 -0
  26. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/experiment_api/workingspace_config_utils.py +0 -0
  27. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/inner_leap_binder/__init__.py +0 -0
  28. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/utils.py +0 -0
  29. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/visualizers/__init__.py +0 -0
  30. {code_loader-1.0.61.dev6 → code_loader-1.0.63}/code_loader/visualizers/default_visualizers.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: code-loader
3
- Version: 1.0.61.dev6
3
+ Version: 1.0.63
4
4
  Summary:
5
5
  Home-page: https://github.com/tensorleap/code-loader
6
6
  License: MIT
@@ -13,7 +13,6 @@ Classifier: Programming Language :: Python :: 3.8
13
13
  Classifier: Programming Language :: Python :: 3.9
14
14
  Classifier: Programming Language :: Python :: 3.10
15
15
  Classifier: Programming Language :: Python :: 3.11
16
- Requires-Dist: matplotlib (>=3.3,<3.4)
17
16
  Requires-Dist: numpy (>=1.22.3,<2.0.0)
18
17
  Requires-Dist: psutil (>=5.9.5,<6.0.0)
19
18
  Requires-Dist: pyyaml (>=6.0.2,<7.0.0)
@@ -116,29 +116,19 @@ ConfusionMatrixCallableInterfaceMultiArgs = Callable[..., List[List[ConfusionMat
116
116
  MetricCallableReturnType = Union[Any, List[List[ConfusionMatrixElement]]]
117
117
 
118
118
 
119
- @dataclass
120
- class CustomLossHandlerData:
121
- name: str
122
- arg_names: List[str]
123
-
124
-
125
119
  @dataclass
126
120
  class CustomLossHandler:
127
- custom_loss_handler_data: CustomLossHandlerData
128
- function: CustomCallableInterface
129
-
130
-
131
- @dataclass
132
- class MetricHandlerData:
133
121
  name: str
122
+ function: CustomCallableInterface
134
123
  arg_names: List[str]
135
- direction: Optional[MetricDirection] = MetricDirection.Downward
136
124
 
137
125
 
138
126
  @dataclass
139
127
  class MetricHandler:
140
- metric_handler_data: MetricHandlerData
128
+ name: str
141
129
  function: Union[CustomCallableInterfaceMultiArgs, ConfusionMatrixCallableInterfaceMultiArgs]
130
+ arg_names: List[str]
131
+ direction: Optional[MetricDirection] = MetricDirection.Downward
142
132
 
143
133
 
144
134
  @dataclass
@@ -147,16 +137,11 @@ class RawInputsForHeatmap:
147
137
 
148
138
 
149
139
  @dataclass
150
- class VisualizerHandlerData:
140
+ class VisualizerHandler:
151
141
  name: str
142
+ function: VisualizerCallableInterface
152
143
  type: LeapDataType
153
144
  arg_names: List[str]
154
-
155
-
156
- @dataclass
157
- class VisualizerHandler:
158
- visualizer_handler_data: VisualizerHandlerData
159
- function: VisualizerCallableInterface
160
145
  heatmap_function: Optional[Callable[..., npt.NDArray[np.float32]]] = None
161
146
 
162
147
 
@@ -169,7 +154,7 @@ class DatasetBaseHandler:
169
154
  @dataclass
170
155
  class InputHandler(DatasetBaseHandler):
171
156
  shape: Optional[List[int]] = None
172
-
157
+ channel_dim: Optional[int] = -1
173
158
 
174
159
  @dataclass
175
160
  class GroundTruthHandler(DatasetBaseHandler):
@@ -20,6 +20,7 @@ class DatasetBaseSectionInstance:
20
20
  @dataclass
21
21
  class DatasetInputInstance(DatasetBaseSectionInstance):
22
22
  shape: List[int]
23
+ channel_dim: Optional[int] = -1
23
24
 
24
25
 
25
26
  @dataclass
@@ -9,8 +9,7 @@ from code_loader.contract.datasetclasses import SectionCallableInterface, InputH
9
9
  PreprocessHandler, VisualizerCallableInterface, CustomLossHandler, CustomCallableInterface, PredictionTypeHandler, \
10
10
  MetadataSectionCallableInterface, UnlabeledDataPreprocessHandler, CustomLayerHandler, MetricHandler, \
11
11
  CustomCallableInterfaceMultiArgs, ConfusionMatrixCallableInterfaceMultiArgs, LeapData, \
12
- CustomMultipleReturnCallableInterfaceMultiArgs, DatasetBaseHandler, custom_latent_space_attribute, \
13
- RawInputsForHeatmap, VisualizerHandlerData, MetricHandlerData, CustomLossHandlerData
12
+ CustomMultipleReturnCallableInterfaceMultiArgs, DatasetBaseHandler, custom_latent_space_attribute, RawInputsForHeatmap
14
13
  from code_loader.contract.enums import LeapDataType, DataStateEnum, DataStateType, MetricDirection
15
14
  from code_loader.contract.responsedataclasses import DatasetTestResultPayload
16
15
  from code_loader.contract.visualizer_classes import map_leap_data_type_to_visualizer_class
@@ -128,7 +127,7 @@ class LeapBinder:
128
127
  f'should be {expected_return_type}')
129
128
 
130
129
  self.setup_container.visualizers.append(
131
- VisualizerHandler(VisualizerHandlerData(name, visualizer_type, arg_names), function, heatmap_visualizer))
130
+ VisualizerHandler(name, function, visualizer_type, arg_names, heatmap_visualizer))
132
131
  self._visualizer_names.append(name)
133
132
 
134
133
  def set_preprocess(self, function: Callable[[], List[PreprocessResponse]]) -> None:
@@ -182,13 +181,14 @@ class LeapBinder:
182
181
  """
183
182
  self.setup_container.unlabeled_data_preprocess = UnlabeledDataPreprocessHandler(function)
184
183
 
185
- def set_input(self, function: SectionCallableInterface, name: str) -> None:
184
+ def set_input(self, function: SectionCallableInterface, name: str, channel_dim: int = -1) -> None:
186
185
  """
187
186
  Set the input handler function.
188
187
 
189
188
  Args:
190
189
  function (SectionCallableInterface): The input handler function.
191
190
  name (str): The name of the input section.
191
+ channel_dim (int): The dimension of the channels axis
192
192
 
193
193
  Example:
194
194
  def input_encoder(subset: PreprocessResponse, index: int) -> np.ndarray:
@@ -198,10 +198,10 @@ class LeapBinder:
198
198
  img = normalize(img)
199
199
  return img
200
200
 
201
- leap_binder.set_input(input_encoder, name='input_encoder')
201
+ leap_binder.set_input(input_encoder, name='input_encoder', channel_dim=-1)
202
202
  """
203
203
  function = to_numpy_return_wrapper(function)
204
- self.setup_container.inputs.append(InputHandler(name, function))
204
+ self.setup_container.inputs.append(InputHandler(name, function, channel_dim=channel_dim))
205
205
 
206
206
  self._encoder_names.append(name)
207
207
 
@@ -226,7 +226,7 @@ class LeapBinder:
226
226
  leap_binder.add_custom_loss(custom_loss_function, name='custom_loss')
227
227
  """
228
228
  arg_names = inspect.getfullargspec(function)[0]
229
- self.setup_container.custom_loss_handlers.append(CustomLossHandler(CustomLossHandlerData(name, arg_names), function))
229
+ self.setup_container.custom_loss_handlers.append(CustomLossHandler(name, function, arg_names))
230
230
 
231
231
  def add_custom_metric(self,
232
232
  function: Union[CustomCallableInterfaceMultiArgs,
@@ -252,7 +252,7 @@ class LeapBinder:
252
252
  leap_binder.add_custom_metric(custom_metric_function, name='custom_metric', direction=MetricDirection.Downward)
253
253
  """
254
254
  arg_names = inspect.getfullargspec(function)[0]
255
- self.setup_container.metrics.append(MetricHandler(MetricHandlerData(name, arg_names, direction), function))
255
+ self.setup_container.metrics.append(MetricHandler(name, function, arg_names, direction))
256
256
 
257
257
  def add_prediction(self, name: str, labels: List[str], channel_dim: int = -1) -> None:
258
258
  """
@@ -58,11 +58,13 @@ def tensorleap_custom_metric(name: str, direction: Optional[MetricDirection] = M
58
58
  assert isinstance(single_metric_result[0][0], ConfusionMatrixElement), \
59
59
  f'{supported_types_message}Got List[List[{type(single_metric_result[0][0])}]].'
60
60
  else:
61
- assert isinstance(single_metric_result[0], float), f'{supported_types_message}Got List[{type(single_metric_result[0])}].'
61
+ assert isinstance(single_metric_result[0],
62
+ float), f'{supported_types_message}Got List[{type(single_metric_result[0])}].'
62
63
  else:
63
- assert isinstance(single_metric_result, np.ndarray), f'{supported_types_message}Got {type(single_metric_result)}.'
64
+ assert isinstance(single_metric_result,
65
+ np.ndarray), f'{supported_types_message}Got {type(single_metric_result)}.'
64
66
  assert len(single_metric_result.shape) == 1, (f'tensorleap_custom_metric validation failed: '
65
- f'The return shape should be 1D. Got {len(single_metric_result.shape)}D.')
67
+ f'The return shape should be 1D. Got {len(single_metric_result.shape)}D.')
66
68
 
67
69
  if leap_binder.batch_size_to_validate:
68
70
  assert len(single_metric_result) == leap_binder.batch_size_to_validate, \
@@ -245,14 +247,16 @@ def tensorleap_unlabeled_preprocess():
245
247
  return decorating_function
246
248
 
247
249
 
248
- def tensorleap_input_encoder(name: str):
250
+ def tensorleap_input_encoder(name: str, channel_dim=-1):
249
251
  def decorating_function(user_function: SectionCallableInterface):
250
252
  for input_handler in leap_binder.setup_container.inputs:
251
253
  if input_handler.name == name:
252
254
  raise Exception(f'Input with name {name} already exists. '
253
255
  f'Please choose another')
256
+ if channel_dim <= 0 and channel_dim != -1:
257
+ raise Exception(f"Channel dim for input {name} is expected to be either -1 or positive")
254
258
 
255
- leap_binder.set_input(user_function, name)
259
+ leap_binder.set_input(user_function, name, channel_dim=channel_dim)
256
260
 
257
261
  def _validate_input_args(sample_id: Union[int, str], preprocess_response: PreprocessResponse):
258
262
  assert isinstance(sample_id, (int, str)), \
@@ -273,6 +277,8 @@ def tensorleap_input_encoder(name: str):
273
277
  assert result.dtype == np.float32, \
274
278
  (f'tensorleap_input_encoder validation failed: '
275
279
  f'The return type should be a numpy array of type float32. Got {result.dtype}.')
280
+ assert channel_dim - 1 <= len(result.shape), (f'tensorleap_input_encoder validation failed: '
281
+ f'The channel_dim ({channel_dim}) should be <= to the rank of the resulting input rank ({len(result.shape)}).')
276
282
 
277
283
  def inner(sample_id, preprocess_response):
278
284
  _validate_input_args(sample_id, preprocess_response)
@@ -341,11 +347,21 @@ def tensorleap_custom_loss(name: str):
341
347
  raise Exception('the input arguments of the custom loss function should be tensorflow tensors') from e
342
348
 
343
349
  for i, arg in enumerate(args):
344
- assert isinstance(arg, tf.Tensor), (f'tensorleap_custom_loss validation failed: '
345
- f'Argument #{i} should be a tensorflow tensor. Got {type(arg)}.')
350
+ if isinstance(arg, list):
351
+ for y, elem in enumerate(arg):
352
+ assert isinstance(elem, tf.Tensor), (f'tensorleap_custom_loss validation failed: '
353
+ f'Element #{y} of list should be a tensorflow tensor. Got {type(elem)}.')
354
+ else:
355
+ assert isinstance(arg, tf.Tensor), (f'tensorleap_custom_loss validation failed: '
356
+ f'Argument #{i} should be a tensorflow tensor. Got {type(arg)}.')
346
357
  for _arg_name, arg in kwargs.items():
347
- assert isinstance(arg, tf.Tensor), (f'tensorleap_custom_loss validation failed: '
348
- f'Argument {_arg_name} should be a tensorflow tensor. Got {type(arg)}.')
358
+ if isinstance(arg, list):
359
+ for y, elem in enumerate(arg):
360
+ assert isinstance(elem, tf.Tensor), (f'tensorleap_custom_loss validation failed: '
361
+ f'Element #{y} of list should be a tensorflow tensor. Got {type(elem)}.')
362
+ else:
363
+ assert isinstance(arg, tf.Tensor), (f'tensorleap_custom_loss validation failed: '
364
+ f'Argument #{_arg_name} should be a tensorflow tensor. Got {type(arg)}.')
349
365
 
350
366
  def _validate_result(result):
351
367
  try:
@@ -2,6 +2,7 @@
2
2
  import importlib.util
3
3
  import io
4
4
  import sys
5
+ import time
5
6
  from contextlib import redirect_stdout
6
7
  from functools import lru_cache
7
8
  from pathlib import Path
@@ -11,22 +12,21 @@ import numpy as np
11
12
  import numpy.typing as npt
12
13
 
13
14
  from code_loader.contract.datasetclasses import DatasetSample, DatasetBaseHandler, GroundTruthHandler, \
14
- PreprocessResponse, VisualizerHandler, LeapData, \
15
- PredictionTypeHandler, MetadataHandler, CustomLayerHandler, MetricHandler, VisualizerHandlerData, MetricHandlerData, \
16
- MetricCallableReturnType, CustomLossHandlerData, CustomLossHandler
15
+ PreprocessResponse, VisualizerHandler, LeapData, CustomLossHandler, \
16
+ PredictionTypeHandler, MetadataHandler, CustomLayerHandler, MetricHandler
17
17
  from code_loader.contract.enums import DataStateEnum, TestingSectionEnum, DataStateType, DatasetMetadataType
18
18
  from code_loader.contract.exceptions import DatasetScriptException
19
19
  from code_loader.contract.responsedataclasses import DatasetIntegParseResult, DatasetTestResultPayload, \
20
20
  DatasetPreprocess, DatasetSetup, DatasetInputInstance, DatasetOutputInstance, DatasetMetadataInstance, \
21
21
  VisualizerInstance, PredictionTypeInstance, ModelSetup, CustomLayerInstance, MetricInstance, CustomLossInstance
22
22
  from code_loader.inner_leap_binder import global_leap_binder
23
- from code_loader.leaploaderbase import LeapLoaderBase
24
23
  from code_loader.utils import get_root_exception_file_and_line_number
25
24
 
26
25
 
27
- class LeapLoader(LeapLoaderBase):
26
+ class LeapLoader:
28
27
  def __init__(self, code_path: str, code_entry_name: str):
29
- super().__init__(code_path, code_entry_name)
28
+ self.code_entry_name = code_entry_name
29
+ self.code_path = code_path
30
30
 
31
31
  self._preprocess_result_cached = None
32
32
 
@@ -66,16 +66,7 @@ class LeapLoader(LeapLoaderBase):
66
66
  spec.loader.exec_module(file)
67
67
 
68
68
  @lru_cache()
69
- def metric_by_name(self) -> Dict[str, MetricHandlerData]:
70
- self.exec_script()
71
- setup = global_leap_binder.setup_container
72
- return {
73
- metric_handler.name: metric_handler.metric_handler_data
74
- for metric_handler in setup.metrics
75
- }
76
-
77
- @lru_cache()
78
- def _metric_handler_by_name(self) -> Dict[str, MetricHandler]:
69
+ def metric_by_name(self) -> Dict[str, MetricHandler]:
79
70
  self.exec_script()
80
71
  setup = global_leap_binder.setup_container
81
72
  return {
@@ -84,16 +75,7 @@ class LeapLoader(LeapLoaderBase):
84
75
  }
85
76
 
86
77
  @lru_cache()
87
- def visualizer_by_name(self) -> Dict[str, VisualizerHandlerData]:
88
- self.exec_script()
89
- setup = global_leap_binder.setup_container
90
- return {
91
- visualizer_handler.name: visualizer_handler.visualizer_handler_data
92
- for visualizer_handler in setup.visualizers
93
- }
94
-
95
- @lru_cache()
96
- def _visualizer_handler_by_name(self) -> Dict[str, VisualizerHandler]:
78
+ def visualizer_by_name(self) -> Dict[str, VisualizerHandler]:
97
79
  self.exec_script()
98
80
  setup = global_leap_binder.setup_container
99
81
  return {
@@ -102,16 +84,7 @@ class LeapLoader(LeapLoaderBase):
102
84
  }
103
85
 
104
86
  @lru_cache()
105
- def custom_loss_by_name(self) -> Dict[str, CustomLossHandlerData]:
106
- self.exec_script()
107
- setup = global_leap_binder.setup_container
108
- return {
109
- custom_loss_handler.name: custom_loss_handler.custom_loss_handler_data
110
- for custom_loss_handler in setup.custom_loss_handlers
111
- }
112
-
113
- @lru_cache()
114
- def _custom_loss_handler_by_name(self) -> Dict[str, CustomLossHandler]:
87
+ def custom_loss_by_name(self) -> Dict[str, CustomLossHandler]:
115
88
  self.exec_script()
116
89
  setup = global_leap_binder.setup_container
117
90
  return {
@@ -227,31 +200,20 @@ class LeapLoader(LeapLoaderBase):
227
200
  all_dataset_base_handlers.extend(global_leap_binder.setup_container.metadata)
228
201
  return all_dataset_base_handlers
229
202
 
230
- def run_metric(self, metric_name: str,
231
- input_tensors_by_arg_name: Dict[str, npt.NDArray[np.float32]]) -> MetricCallableReturnType:
232
- self._preprocess_result()
233
- return self._metric_handler_by_name()[metric_name].function(**input_tensors_by_arg_name)
234
-
235
- def run_custom_loss(self, custom_loss_name: str,
236
- input_tensors_by_arg_name: Dict[str, npt.NDArray[np.float32]]):
237
- return self._custom_loss_handler_by_name()[custom_loss_name].function(**input_tensors_by_arg_name)
238
-
239
- def run_visualizer(self, visualizer_name: str, input_tensors_by_arg_name: Dict[str, npt.NDArray[np.float32]]) -> LeapData:
240
- # running preprocessing to sync preprocessing in main thread (can be valuable when preprocess is filling a
241
- # global param that visualizer is using)
242
- self._preprocess_result()
243
-
244
- return self._visualizer_handler_by_name()[visualizer_name].function(**input_tensors_by_arg_name)
203
+ def run_visualizer(self, visualizer_name: str, input_tensors_by_arg_name: Dict[str, npt.NDArray[np.float32]],
204
+ ) -> LeapData:
205
+ return self.visualizer_by_name()[visualizer_name].function(**input_tensors_by_arg_name)
245
206
 
246
207
  def run_heatmap_visualizer(self, visualizer_name: str, input_tensors_by_arg_name: Dict[str, npt.NDArray[np.float32]]
247
208
  ) -> npt.NDArray[np.float32]:
248
- heatmap_function = self._visualizer_handler_by_name()[visualizer_name].heatmap_function
209
+ heatmap_function = self.visualizer_by_name()[visualizer_name].heatmap_function
249
210
  if heatmap_function is None:
250
211
  assert len(input_tensors_by_arg_name) == 1
251
212
  return list(input_tensors_by_arg_name.values())[0]
252
213
  return heatmap_function(**input_tensors_by_arg_name)
253
214
 
254
- def get_dataset_setup_response(self, handlers_test_payloads: List[DatasetTestResultPayload]) -> DatasetSetup:
215
+ @staticmethod
216
+ def get_dataset_setup_response(handlers_test_payloads: List[DatasetTestResultPayload]) -> DatasetSetup:
255
217
  setup = global_leap_binder.setup_container
256
218
  assert setup.preprocess is not None
257
219
 
@@ -269,7 +231,7 @@ class LeapLoader(LeapLoaderBase):
269
231
  for inp in setup.inputs:
270
232
  if inp.shape is None:
271
233
  raise Exception(f"cant calculate shape for input, input name:{inp.name}")
272
- inputs.append(DatasetInputInstance(name=inp.name, shape=inp.shape))
234
+ inputs.append(DatasetInputInstance(name=inp.name, shape=inp.shape, channel_dim=inp.channel_dim))
273
235
 
274
236
  ground_truths = []
275
237
  for gt in setup.ground_truths:
@@ -321,7 +283,8 @@ class LeapLoader(LeapLoaderBase):
321
283
  metadata=metadata_instances, visualizers=visualizers, prediction_types=prediction_types,
322
284
  custom_losses=custom_losses, metrics=metrics)
323
285
 
324
- def get_model_setup_response(self) -> ModelSetup:
286
+ @staticmethod
287
+ def get_model_setup_response() -> ModelSetup:
325
288
  setup = global_leap_binder.setup_container
326
289
  custom_layer_instances = [
327
290
  CustomLayerInstance(custom_layer_handler.name, custom_layer_handler.init_arg_names,
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "code-loader"
3
- version = "1.0.61.dev6"
3
+ version = "1.0.63"
4
4
  description = ""
5
5
  authors = ["dorhar <doron.harnoy@tensorleap.ai>"]
6
6
  license = "MIT"
@@ -15,7 +15,6 @@ include = [
15
15
  python = ">=3.8,<3.12"
16
16
  numpy = "^1.22.3"
17
17
  psutil = "^5.9.5"
18
- matplotlib = ">=3.3,<3.4"
19
18
  requests = "^2.32.3"
20
19
  pyyaml = "^6.0.2"
21
20
 
@@ -1,83 +0,0 @@
1
- # mypy: ignore-errors
2
- import traceback
3
- from dataclasses import dataclass
4
-
5
- from typing import List, Tuple, Optional
6
-
7
- from multiprocessing import Process, Queue
8
-
9
- from code_loader.leap_loader_parallelized_base import LeapLoaderParallelizedBase
10
- from code_loader.leaploader import LeapLoader
11
- from code_loader.contract.enums import DataStateEnum
12
- from code_loader.metric_calculator_parallelized import MetricCalculatorParallelized
13
- from code_loader.samples_generator_parallelized import SamplesGeneratorParallelized
14
-
15
-
16
- @dataclass
17
- class SampleSerializableError:
18
- state: DataStateEnum
19
- index: int
20
- leap_script_trace: str
21
- exception_as_str: str
22
-
23
-
24
- class CodeIntegrationProcessesManager:
25
- def __init__(self, code_path: str, code_entry_name: str, n_workers: Optional[int] = 2,
26
- max_samples_in_queue: int = 128) -> None:
27
- self.metric_calculator_parallelized = MetricCalculatorParallelized(code_path, code_entry_name)
28
- self.samples_generator_parallelized = SamplesGeneratorParallelized(code_path, code_entry_name)
29
-
30
- def _create_and_start_process(self) -> Process:
31
- process = self.multiprocessing_context.Process(
32
- target=CodeIntegrationProcessesManager._process_func,
33
- args=(self.code_path, self.code_entry_name, self._inputs_waiting_to_be_process,
34
- self._ready_processed_results))
35
- process.daemon = True
36
- process.start()
37
- return process
38
-
39
- def _run_and_warm_first_process(self):
40
- process = self._create_and_start_process()
41
- self.processes = [process]
42
-
43
- # needed in order to make sure the preprocess func runs once in nonparallel
44
- self._start_process_inputs([(DataStateEnum.training, 0)])
45
- self._get_next_ready_processed_result()
46
-
47
- def _operation_decider(self):
48
- if self.metric_calculator_parallelized._ready_processed_results.empty() and not \
49
- self.metric_calculator_parallelized._inputs_waiting_to_be_process.empty():
50
- return 'metric'
51
-
52
- if self.samples_generator_parallelized._ready_processed_results.empty() and not \
53
- self.samples_generator_parallelized._inputs_waiting_to_be_process.empty():
54
- return 'dataset'
55
-
56
-
57
-
58
-
59
- @staticmethod
60
- def _process_func(code_path: str, code_entry_name: str,
61
- samples_to_process: Queue, ready_samples: Queue,
62
- metrics_to_process: Queue, ready_metrics: Queue) -> None:
63
- import os
64
- os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
65
-
66
- leap_loader = LeapLoader(code_path, code_entry_name)
67
- while True:
68
-
69
- # decide on sample or metric to process
70
- state, idx = samples_to_process.get(block=True)
71
- leap_loader._preprocess_result()
72
- try:
73
- sample = leap_loader.get_sample(state, idx)
74
- except Exception as e:
75
- leap_script_trace = traceback.format_exc().split('File "<string>"')[-1]
76
- ready_samples.put(SampleSerializableError(state, idx, leap_script_trace, str(e)))
77
- continue
78
-
79
- ready_samples.put(sample)
80
-
81
- def generate_samples(self, sample_identities: List[Tuple[DataStateEnum, int]]):
82
- return self.start_process_inputs(sample_identities)
83
-
@@ -1,91 +0,0 @@
1
- # mypy: ignore-errors
2
-
3
- from abc import abstractmethod
4
-
5
- from typing import Dict, List, Union, Type
6
-
7
- import numpy as np
8
- import numpy.typing as npt
9
-
10
- from code_loader.contract.datasetclasses import DatasetSample, LeapData, \
11
- PredictionTypeHandler, CustomLayerHandler, VisualizerHandlerData, MetricHandlerData, MetricCallableReturnType, \
12
- CustomLossHandlerData
13
- from code_loader.contract.enums import DataStateEnum
14
- from code_loader.contract.responsedataclasses import DatasetIntegParseResult, DatasetTestResultPayload, \
15
- DatasetSetup, ModelSetup
16
-
17
-
18
- class LeapLoaderBase:
19
- def __init__(self, code_path: str, code_entry_name: str):
20
- self.code_entry_name = code_entry_name
21
- self.code_path = code_path
22
-
23
- @abstractmethod
24
- def metric_by_name(self) -> Dict[str, MetricHandlerData]:
25
- pass
26
-
27
- @abstractmethod
28
- def visualizer_by_name(self) -> Dict[str, VisualizerHandlerData]:
29
- pass
30
-
31
- @abstractmethod
32
- def custom_loss_by_name(self) -> Dict[str, CustomLossHandlerData]:
33
- pass
34
-
35
- @abstractmethod
36
- def custom_layers(self) -> Dict[str, CustomLayerHandler]:
37
- pass
38
-
39
- @abstractmethod
40
- def prediction_type_by_name(self) -> Dict[str, PredictionTypeHandler]:
41
- pass
42
-
43
- @abstractmethod
44
- def get_sample(self, state: DataStateEnum, sample_id: Union[int, str]) -> DatasetSample:
45
- pass
46
-
47
- @abstractmethod
48
- def check_dataset(self) -> DatasetIntegParseResult:
49
- pass
50
-
51
- @abstractmethod
52
- def run_visualizer(self, visualizer_name: str, input_tensors_by_arg_name: Dict[str, npt.NDArray[np.float32]]) -> LeapData:
53
- pass
54
-
55
- @abstractmethod
56
- def run_metric(self, metric_name: str,
57
- input_tensors_by_arg_name: Dict[str, npt.NDArray[np.float32]]) -> MetricCallableReturnType:
58
- pass
59
-
60
- @abstractmethod
61
- def run_custom_loss(self, custom_loss_name: str,
62
- input_tensors_by_arg_name: Dict[str, npt.NDArray[np.float32]]):
63
- pass
64
-
65
- @abstractmethod
66
- def run_heatmap_visualizer(self, visualizer_name: str, input_tensors_by_arg_name: Dict[str, npt.NDArray[np.float32]]
67
- ) -> npt.NDArray[np.float32]:
68
- pass
69
-
70
- @abstractmethod
71
- def get_dataset_setup_response(self, handlers_test_payloads: List[DatasetTestResultPayload]) -> DatasetSetup:
72
- pass
73
-
74
- @abstractmethod
75
- def get_model_setup_response(self) -> ModelSetup:
76
- pass
77
-
78
- @abstractmethod
79
- def get_preprocess_sample_ids(
80
- self, update_unlabeled_preprocess=False) -> Dict[DataStateEnum, Union[List[int], List[str]]]:
81
- pass
82
-
83
- @abstractmethod
84
- def get_sample_id_type(self) -> Type:
85
- pass
86
-
87
- def is_custom_latent_space(self) -> bool:
88
- if not self.code_entry_name or not self.code_path:
89
- return False
90
- custom_layers = self.custom_layers()
91
- return any(layer.use_custom_latent_space for layer in custom_layers.values())
File without changes