code-loader 1.0.60.dev2__py3-none-any.whl → 1.0.61.dev1__py3-none-any.whl

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.
@@ -0,0 +1,83 @@
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
+
@@ -154,7 +154,7 @@ class DatasetBaseHandler:
154
154
  @dataclass
155
155
  class InputHandler(DatasetBaseHandler):
156
156
  shape: Optional[List[int]] = None
157
- channel_dim: Optional[int] = -1
157
+
158
158
 
159
159
  @dataclass
160
160
  class GroundTruthHandler(DatasetBaseHandler):
@@ -20,7 +20,6 @@ class DatasetBaseSectionInstance:
20
20
  @dataclass
21
21
  class DatasetInputInstance(DatasetBaseSectionInstance):
22
22
  shape: List[int]
23
- channel_dim: Optional[int] = -1
24
23
 
25
24
 
26
25
  @dataclass
@@ -181,14 +181,13 @@ class LeapBinder:
181
181
  """
182
182
  self.setup_container.unlabeled_data_preprocess = UnlabeledDataPreprocessHandler(function)
183
183
 
184
- def set_input(self, function: SectionCallableInterface, name: str, channel_dim: int = -1) -> None:
184
+ def set_input(self, function: SectionCallableInterface, name: str) -> None:
185
185
  """
186
186
  Set the input handler function.
187
187
 
188
188
  Args:
189
189
  function (SectionCallableInterface): The input handler function.
190
190
  name (str): The name of the input section.
191
- channel_dim (int): The dimension of the channels axis
192
191
 
193
192
  Example:
194
193
  def input_encoder(subset: PreprocessResponse, index: int) -> np.ndarray:
@@ -198,10 +197,10 @@ class LeapBinder:
198
197
  img = normalize(img)
199
198
  return img
200
199
 
201
- leap_binder.set_input(input_encoder, name='input_encoder', channel_dim=-1)
200
+ leap_binder.set_input(input_encoder, name='input_encoder')
202
201
  """
203
202
  function = to_numpy_return_wrapper(function)
204
- self.setup_container.inputs.append(InputHandler(name, function, channel_dim=channel_dim))
203
+ self.setup_container.inputs.append(InputHandler(name, function))
205
204
 
206
205
  self._encoder_names.append(name)
207
206
 
@@ -245,16 +245,14 @@ def tensorleap_unlabeled_preprocess():
245
245
  return decorating_function
246
246
 
247
247
 
248
- def tensorleap_input_encoder(name: str, channel_dim=-1):
248
+ def tensorleap_input_encoder(name: str):
249
249
  def decorating_function(user_function: SectionCallableInterface):
250
250
  for input_handler in leap_binder.setup_container.inputs:
251
251
  if input_handler.name == name:
252
252
  raise Exception(f'Input with name {name} already exists. '
253
253
  f'Please choose another')
254
- if channel_dim < 0 and channel_dim != -1:
255
- raise Exception(f"Channel dim for input {name} is expected to be either -1 or positive")
256
254
 
257
- leap_binder.set_input(user_function, name, channel_dim=channel_dim)
255
+ leap_binder.set_input(user_function, name)
258
256
 
259
257
  def _validate_input_args(sample_id: Union[int, str], preprocess_response: PreprocessResponse):
260
258
  assert isinstance(sample_id, (int, str)), \
@@ -275,8 +273,6 @@ def tensorleap_input_encoder(name: str, channel_dim=-1):
275
273
  assert result.dtype == np.float32, \
276
274
  (f'tensorleap_input_encoder validation failed: '
277
275
  f'The return type should be a numpy array of type float32. Got {result.dtype}.')
278
- assert channel_dim - 1 <= len(result.shape), (f'tensorleap_input_encoder validation failed: '
279
- f'The channel_dim ({channel_dim}) should be <= to the rank of the resulting input rank ({len(result.shape)}).')
280
276
 
281
277
  def inner(sample_id, preprocess_response):
282
278
  _validate_input_args(sample_id, preprocess_response)
code_loader/leaploader.py CHANGED
@@ -2,7 +2,6 @@
2
2
  import importlib.util
3
3
  import io
4
4
  import sys
5
- import time
6
5
  from contextlib import redirect_stdout
7
6
  from functools import lru_cache
8
7
  from pathlib import Path
@@ -20,13 +19,13 @@ from code_loader.contract.responsedataclasses import DatasetIntegParseResult, Da
20
19
  DatasetPreprocess, DatasetSetup, DatasetInputInstance, DatasetOutputInstance, DatasetMetadataInstance, \
21
20
  VisualizerInstance, PredictionTypeInstance, ModelSetup, CustomLayerInstance, MetricInstance, CustomLossInstance
22
21
  from code_loader.inner_leap_binder import global_leap_binder
22
+ from code_loader.leaploaderbase import LeapLoaderBase
23
23
  from code_loader.utils import get_root_exception_file_and_line_number
24
24
 
25
25
 
26
- class LeapLoader:
26
+ class LeapLoader(LeapLoaderBase):
27
27
  def __init__(self, code_path: str, code_entry_name: str):
28
- self.code_entry_name = code_entry_name
29
- self.code_path = code_path
28
+ super().__init__(code_path, code_entry_name)
30
29
 
31
30
  self._preprocess_result_cached = None
32
31
 
@@ -202,6 +201,10 @@ class LeapLoader:
202
201
 
203
202
  def run_visualizer(self, visualizer_name: str, input_tensors_by_arg_name: Dict[str, npt.NDArray[np.float32]],
204
203
  ) -> LeapData:
204
+ # running preprocessing to sync preprocessing in main thread (can be valuable when preprocess is filling a
205
+ # global param that visualizer is using)
206
+ self._preprocess_result()
207
+
205
208
  return self.visualizer_by_name()[visualizer_name].function(**input_tensors_by_arg_name)
206
209
 
207
210
  def run_heatmap_visualizer(self, visualizer_name: str, input_tensors_by_arg_name: Dict[str, npt.NDArray[np.float32]]
@@ -212,8 +215,7 @@ class LeapLoader:
212
215
  return list(input_tensors_by_arg_name.values())[0]
213
216
  return heatmap_function(**input_tensors_by_arg_name)
214
217
 
215
- @staticmethod
216
- def get_dataset_setup_response(handlers_test_payloads: List[DatasetTestResultPayload]) -> DatasetSetup:
218
+ def get_dataset_setup_response(self, handlers_test_payloads: List[DatasetTestResultPayload]) -> DatasetSetup:
217
219
  setup = global_leap_binder.setup_container
218
220
  assert setup.preprocess is not None
219
221
 
@@ -231,7 +233,7 @@ class LeapLoader:
231
233
  for inp in setup.inputs:
232
234
  if inp.shape is None:
233
235
  raise Exception(f"cant calculate shape for input, input name:{inp.name}")
234
- inputs.append(DatasetInputInstance(name=inp.name, shape=inp.shape, channel_dim=inp.channel_dim))
236
+ inputs.append(DatasetInputInstance(name=inp.name, shape=inp.shape))
235
237
 
236
238
  ground_truths = []
237
239
  for gt in setup.ground_truths:
@@ -283,8 +285,7 @@ class LeapLoader:
283
285
  metadata=metadata_instances, visualizers=visualizers, prediction_types=prediction_types,
284
286
  custom_losses=custom_losses, metrics=metrics)
285
287
 
286
- @staticmethod
287
- def get_model_setup_response() -> ModelSetup:
288
+ def get_model_setup_response(self) -> ModelSetup:
288
289
  setup = global_leap_binder.setup_container
289
290
  custom_layer_instances = [
290
291
  CustomLayerInstance(custom_layer_handler.name, custom_layer_handler.init_arg_names,
@@ -0,0 +1,77 @@
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, VisualizerHandler, LeapData, CustomLossHandler, \
11
+ PredictionTypeHandler, CustomLayerHandler
12
+ from code_loader.contract.enums import DataStateEnum
13
+ from code_loader.contract.responsedataclasses import DatasetIntegParseResult, DatasetTestResultPayload, \
14
+ DatasetSetup, ModelSetup
15
+
16
+
17
+ class LeapLoaderBase:
18
+ def __init__(self, code_path: str, code_entry_name: str):
19
+ self.code_entry_name = code_entry_name
20
+ self.code_path = code_path
21
+
22
+ self._preprocess_result_cached = None
23
+
24
+ @abstractmethod
25
+ def metric_by_name(self):
26
+ pass
27
+
28
+ @abstractmethod
29
+ def visualizer_by_name(self) -> Dict[str, VisualizerHandler]:
30
+ pass
31
+
32
+ @abstractmethod
33
+ def custom_loss_by_name(self) -> Dict[str, CustomLossHandler]:
34
+ pass
35
+
36
+ @abstractmethod
37
+ def custom_layers(self) -> Dict[str, CustomLayerHandler]:
38
+ pass
39
+
40
+ @abstractmethod
41
+ def prediction_type_by_name(self) -> Dict[str, PredictionTypeHandler]:
42
+ pass
43
+
44
+ @abstractmethod
45
+ def get_sample(self, state: DataStateEnum, sample_id: Union[int, str]) -> DatasetSample:
46
+ pass
47
+
48
+ @abstractmethod
49
+ def check_dataset(self) -> DatasetIntegParseResult:
50
+ pass
51
+
52
+ @abstractmethod
53
+ def run_visualizer(self, visualizer_name: str, input_tensors_by_arg_name: Dict[str, npt.NDArray[np.float32]],
54
+ ) -> LeapData:
55
+ pass
56
+
57
+ @abstractmethod
58
+ def run_heatmap_visualizer(self, visualizer_name: str, input_tensors_by_arg_name: Dict[str, npt.NDArray[np.float32]]
59
+ ) -> npt.NDArray[np.float32]:
60
+ pass
61
+
62
+ @abstractmethod
63
+ def get_dataset_setup_response(self, handlers_test_payloads: List[DatasetTestResultPayload]) -> DatasetSetup:
64
+ pass
65
+
66
+ @abstractmethod
67
+ def get_model_setup_response(self) -> ModelSetup:
68
+ pass
69
+
70
+ @abstractmethod
71
+ def get_preprocess_sample_ids(
72
+ self, update_unlabeled_preprocess=False) -> Dict[DataStateEnum, Union[List[int], List[str]]]:
73
+ pass
74
+
75
+ @abstractmethod
76
+ def get_sample_id_type(self) -> Type:
77
+ pass
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: code-loader
3
- Version: 1.0.60.dev2
3
+ Version: 1.0.61.dev1
4
4
  Summary:
5
5
  Home-page: https://github.com/tensorleap/code-loader
6
6
  License: MIT
@@ -1,10 +1,11 @@
1
1
  LICENSE,sha256=qIwWjdspQeSMTtnFZBC8MuT-95L02FPvzRUdWFxrwJY,1067
2
2
  code_loader/__init__.py,sha256=6MMWr0ObOU7hkqQKgOqp4Zp3I28L7joGC9iCbQYtAJg,241
3
+ code_loader/code_inegration_processes_manager.py,sha256=XslWOPeNQk4RAFJ_f3tP5Oe3EgcIR7BE7Y8r9Ty73-o,3261
3
4
  code_loader/contract/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- code_loader/contract/datasetclasses.py,sha256=cd6fRDC4XLrJa7PcrzoTPIKtGwFZq09DGwiSC5BSKvk,6705
5
+ code_loader/contract/datasetclasses.py,sha256=lFS7_weizsjzx4_tYwYGrrRUj1sgIl010h9FON4brb8,6670
5
6
  code_loader/contract/enums.py,sha256=6Lo7p5CUog68Fd31bCozIuOgIp_IhSiPqWWph2k3OGU,1602
6
7
  code_loader/contract/exceptions.py,sha256=jWqu5i7t-0IG0jGRsKF4DjJdrsdpJjIYpUkN1F4RiyQ,51
7
- code_loader/contract/responsedataclasses.py,sha256=RSx9m_R3LawhK5o1nAcO3hfp2F9oJYtxZr_bpP3bTmw,4005
8
+ code_loader/contract/responsedataclasses.py,sha256=w7xVOv2S8Hyb5lqyomMGiKAWXDTSOG-FX1YW39bXD3A,3969
8
9
  code_loader/contract/visualizer_classes.py,sha256=iIa_O2rKvPTwN5ILCTZvRpsGYiiFABKdwQwfIXGigDo,11928
9
10
  code_loader/experiment_api/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
11
  code_loader/experiment_api/api.py,sha256=a7wh6Hhe7IaVxu46eV2soSz-yxnmXG3ipU1BBtsEAaQ,2493
@@ -17,13 +18,14 @@ code_loader/experiment_api/types.py,sha256=MY8xFARHwdVA7p4dxyhD60ShmttgTvb4qdp1o
17
18
  code_loader/experiment_api/utils.py,sha256=XZHtxge12TS4H4-8PjV3sKuhp8Ud6ojAiIzTZJEqBqc,3304
18
19
  code_loader/experiment_api/workingspace_config_utils.py,sha256=DLzXQCg4dgTV_YgaSbeTVzq-2ja_SQw4zi7LXwKL9cY,990
19
20
  code_loader/inner_leap_binder/__init__.py,sha256=koOlJyMNYzGbEsoIbXathSmQ-L38N_pEXH_HvL7beXU,99
20
- code_loader/inner_leap_binder/leapbinder.py,sha256=LVzpynjISO-a774flzGt1yAQPsSYNE8B5V58Hacs7bQ,25216
21
- code_loader/inner_leap_binder/leapbinder_decorators.py,sha256=6yHYZRFb5wv1dEOV6r75kIN4-cxXtFDbgHL9ciQQ_x4,20131
22
- code_loader/leaploader.py,sha256=Tpf6A25hYuo4D0umGL3BHNYJhmz_NIwvFveQgAlsSOo,19534
21
+ code_loader/inner_leap_binder/leapbinder.py,sha256=35hyesDdmjOD9wdrTLyayb-vm9aDfmEbMA0c4EQR1LA,25090
22
+ code_loader/inner_leap_binder/leapbinder_decorators.py,sha256=uuM_ht9HZ1GH2IabKeGQ_x9NmD3poK_h1Gt0NruwJuY,19704
23
+ code_loader/leaploader.py,sha256=KC_6oso5pbOHZ56sUTcV6qdFzEbIJ8MdEtKu-nDrQfE,19707
24
+ code_loader/leaploaderbase.py,sha256=vVgy2AJXuB0Sf6ry49r_PxvcPOwwz9RXD3VCtxqTFC0,2284
23
25
  code_loader/utils.py,sha256=aw2i_fqW_ADjLB66FWZd9DfpCQ7mPdMyauROC5Nd51I,2197
24
26
  code_loader/visualizers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
25
27
  code_loader/visualizers/default_visualizers.py,sha256=VoqO9FN84yXyMjRjHjUTOt2GdTkJRMbHbXJ1cJkREkk,2230
26
- code_loader-1.0.60.dev2.dist-info/LICENSE,sha256=qIwWjdspQeSMTtnFZBC8MuT-95L02FPvzRUdWFxrwJY,1067
27
- code_loader-1.0.60.dev2.dist-info/METADATA,sha256=Qd_qx-0HBEh0g-Yimq2xX-aQ0CJwpNuPSUDNVCmKcwA,893
28
- code_loader-1.0.60.dev2.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
29
- code_loader-1.0.60.dev2.dist-info/RECORD,,
28
+ code_loader-1.0.61.dev1.dist-info/LICENSE,sha256=qIwWjdspQeSMTtnFZBC8MuT-95L02FPvzRUdWFxrwJY,1067
29
+ code_loader-1.0.61.dev1.dist-info/METADATA,sha256=iym33XwG43oBOA1gFvLKu2MMh3LHPVfSH930mqUs8Ds,893
30
+ code_loader-1.0.61.dev1.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
31
+ code_loader-1.0.61.dev1.dist-info/RECORD,,