code-loader 1.0.61__py3-none-any.whl → 1.0.61.dev2__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.
- code_loader/code_inegration_processes_manager.py +83 -0
- code_loader/contract/datasetclasses.py +1 -1
- code_loader/contract/responsedataclasses.py +0 -1
- code_loader/inner_leap_binder/leapbinder.py +3 -4
- code_loader/inner_leap_binder/leapbinder_decorators.py +2 -6
- code_loader/leaploader.py +10 -9
- code_loader/leaploaderbase.py +81 -0
- {code_loader-1.0.61.dist-info → code_loader-1.0.61.dev2.dist-info}/METADATA +1 -1
- {code_loader-1.0.61.dist-info → code_loader-1.0.61.dev2.dist-info}/RECORD +11 -9
- {code_loader-1.0.61.dist-info → code_loader-1.0.61.dev2.dist-info}/LICENSE +0 -0
- {code_loader-1.0.61.dist-info → code_loader-1.0.61.dev2.dist-info}/WHEEL +0 -0
@@ -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
|
+
|
@@ -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
|
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'
|
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
|
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
|
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
|
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
|
-
|
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
|
-
|
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
|
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
|
-
|
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,81 @@
|
|
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, MetricHandler
|
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
|
+
@abstractmethod
|
23
|
+
def metric_by_name(self) -> Dict[str, MetricHandler]:
|
24
|
+
pass
|
25
|
+
|
26
|
+
@abstractmethod
|
27
|
+
def visualizer_by_name(self) -> Dict[str, VisualizerHandler]:
|
28
|
+
pass
|
29
|
+
|
30
|
+
@abstractmethod
|
31
|
+
def custom_loss_by_name(self) -> Dict[str, CustomLossHandler]:
|
32
|
+
pass
|
33
|
+
|
34
|
+
@abstractmethod
|
35
|
+
def custom_layers(self) -> Dict[str, CustomLayerHandler]:
|
36
|
+
pass
|
37
|
+
|
38
|
+
@abstractmethod
|
39
|
+
def prediction_type_by_name(self) -> Dict[str, PredictionTypeHandler]:
|
40
|
+
pass
|
41
|
+
|
42
|
+
@abstractmethod
|
43
|
+
def get_sample(self, state: DataStateEnum, sample_id: Union[int, str]) -> DatasetSample:
|
44
|
+
pass
|
45
|
+
|
46
|
+
@abstractmethod
|
47
|
+
def check_dataset(self) -> DatasetIntegParseResult:
|
48
|
+
pass
|
49
|
+
|
50
|
+
@abstractmethod
|
51
|
+
def run_visualizer(self, visualizer_name: str, input_tensors_by_arg_name: Dict[str, npt.NDArray[np.float32]],
|
52
|
+
) -> LeapData:
|
53
|
+
pass
|
54
|
+
|
55
|
+
@abstractmethod
|
56
|
+
def run_heatmap_visualizer(self, visualizer_name: str, input_tensors_by_arg_name: Dict[str, npt.NDArray[np.float32]]
|
57
|
+
) -> npt.NDArray[np.float32]:
|
58
|
+
pass
|
59
|
+
|
60
|
+
@abstractmethod
|
61
|
+
def get_dataset_setup_response(self, handlers_test_payloads: List[DatasetTestResultPayload]) -> DatasetSetup:
|
62
|
+
pass
|
63
|
+
|
64
|
+
@abstractmethod
|
65
|
+
def get_model_setup_response(self) -> ModelSetup:
|
66
|
+
pass
|
67
|
+
|
68
|
+
@abstractmethod
|
69
|
+
def get_preprocess_sample_ids(
|
70
|
+
self, update_unlabeled_preprocess=False) -> Dict[DataStateEnum, Union[List[int], List[str]]]:
|
71
|
+
pass
|
72
|
+
|
73
|
+
@abstractmethod
|
74
|
+
def get_sample_id_type(self) -> Type:
|
75
|
+
pass
|
76
|
+
|
77
|
+
def is_custom_latent_space(self) -> bool:
|
78
|
+
if not self.code_entry_name or not self.code_path:
|
79
|
+
return False
|
80
|
+
custom_layers = self.custom_layers()
|
81
|
+
return any(layer.use_custom_latent_space for layer in custom_layers.values())
|
@@ -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=
|
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=
|
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=
|
21
|
-
code_loader/inner_leap_binder/leapbinder_decorators.py,sha256=
|
22
|
-
code_loader/leaploader.py,sha256=
|
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=ZPncue31Ld6NeaOZz4H0PJXxl5AYJfC01tX_H_ARVFc,2542
|
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.61.dist-info/LICENSE,sha256=qIwWjdspQeSMTtnFZBC8MuT-95L02FPvzRUdWFxrwJY,1067
|
27
|
-
code_loader-1.0.61.dist-info/METADATA,sha256=
|
28
|
-
code_loader-1.0.61.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
29
|
-
code_loader-1.0.61.dist-info/RECORD,,
|
28
|
+
code_loader-1.0.61.dev2.dist-info/LICENSE,sha256=qIwWjdspQeSMTtnFZBC8MuT-95L02FPvzRUdWFxrwJY,1067
|
29
|
+
code_loader-1.0.61.dev2.dist-info/METADATA,sha256=EzpmH_OVUl7AVQxhi2-Gth86YjYw9N0PdIKDl_kd1J4,893
|
30
|
+
code_loader-1.0.61.dev2.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
31
|
+
code_loader-1.0.61.dev2.dist-info/RECORD,,
|
File without changes
|
File without changes
|