Skip to content

Commit c8264bf

Browse files
authored
remove: serialization (#1)
1 parent 54ceee0 commit c8264bf

File tree

7 files changed

+7
-102
lines changed

7 files changed

+7
-102
lines changed

deeppavlov/core/commands/infer.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,10 @@
2929

3030

3131
def build_model(config: Union[str, Path, dict], mode: str = 'infer',
32-
load_trained: bool = False, download: bool = False,
33-
serialized: Optional[bytes] = None) -> Chainer:
32+
load_trained: bool = False, download: bool = False) -> Chainer:
3433
"""Build and return the model described in corresponding configuration file."""
3534
config = parse_config(config)
3635

37-
if serialized:
38-
serialized: list = pickle.loads(serialized)
39-
4036
if download:
4137
deep_download(config)
4238

@@ -54,12 +50,7 @@ def build_model(config: Union[str, Path, dict], mode: str = 'infer',
5450
log.warning('No "save_path" parameter for the {} component, so "load_path" will not be renewed'
5551
.format(component_config.get('class_name', component_config.get('ref', 'UNKNOWN'))))
5652

57-
if serialized and 'in' in component_config:
58-
component_serialized = serialized.pop(0)
59-
else:
60-
component_serialized = None
61-
62-
component = from_params(component_config, mode=mode, serialized=component_serialized)
53+
component = from_params(component_config, mode=mode)
6354

6455
if 'id' in component_config:
6556
model._components_dict[component_config['id']] = component

deeppavlov/core/common/chainer.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -299,16 +299,3 @@ def destroy(self):
299299
if hasattr(self, 'pipe'):
300300
self.pipe.clear()
301301
super().destroy()
302-
303-
def serialize(self) -> bytes:
304-
data = []
305-
for in_params, out_params, component in self.train_pipe:
306-
serialized = component.serialize() if isinstance(component, Component) else None
307-
data.append(serialized)
308-
return pickle.dumps(data, protocol=4)
309-
310-
def deserialize(self, data: bytes) -> None:
311-
data = pickle.loads(data)
312-
for (in_params, out_params, component), component_data in zip(self.train_pipe, data):
313-
if isinstance(component, Component):
314-
component.deserialize(component_data)

deeppavlov/core/common/params.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,15 @@ def _init_param(param, mode):
5555
return param
5656

5757

58-
def from_params(params: Dict, mode: str = 'infer', serialized: Any = None, **kwargs) -> Union[Component, FunctionType]:
58+
def from_params(params: Dict, mode: str = 'infer', **kwargs) -> Union[Component, FunctionType]:
5959
"""Builds and returns the Component from corresponding dictionary of parameters."""
6060
# what is passed in json:
6161
config_params = {k: _resolve(v) for k, v in params.items()}
6262

6363
# get component by reference (if any)
6464
if 'ref' in config_params:
6565
try:
66-
component = _refs[config_params['ref']]
67-
if serialized is not None:
68-
component.deserialize(serialized)
69-
return component
66+
return _refs[config_params['ref']]
7067
except KeyError:
7168
e = ConfigError('Component with id "{id}" was referenced but not initialized'
7269
.format(id=config_params['ref']))
@@ -78,7 +75,7 @@ def from_params(params: Dict, mode: str = 'infer', serialized: Any = None, **kwa
7875
refs = _refs.copy()
7976
_refs.clear()
8077
config = parse_config(expand_path(config_params['config_path']), config_params.get('overwrite'))
81-
model = build_model(config, serialized=serialized)
78+
model = build_model(config)
8279
_refs.clear()
8380
_refs.update(refs)
8481
try:
@@ -97,7 +94,6 @@ def from_params(params: Dict, mode: str = 'infer', serialized: Any = None, **kwa
9794
if inspect.isclass(obj):
9895
# find the submodels params recursively
9996
config_params = {k: _init_param(v, mode) for k, v in config_params.items()}
100-
10197
try:
10298
spec = inspect.getfullargspec(obj)
10399
if 'mode' in spec.args + spec.kwonlyargs or spec.varkw is not None:
@@ -111,9 +107,6 @@ def from_params(params: Dict, mode: str = 'infer', serialized: Any = None, **kwa
111107
except Exception:
112108
log.exception("Exception in {}".format(obj))
113109
raise
114-
115-
if serialized is not None:
116-
component.deserialize(serialized)
117110
else:
118111
component = obj
119112

deeppavlov/core/data/simple_vocab.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,6 @@ def save(self):
105105
cnt = self.freqs[token]
106106
f.write('{}\t{:d}\n'.format(token, cnt))
107107

108-
def serialize(self) -> List[Tuple[str, int]]:
109-
return [(token, self.freqs[token]) for token in self._i2t]
110-
111108
def load(self):
112109
self.reset()
113110
if self.load_path:
@@ -125,12 +122,6 @@ def load(self):
125122
else:
126123
raise ConfigError("`load_path` for {} is not provided!".format(self))
127124

128-
def deserialize(self, data: List[Tuple[str, int]]) -> None:
129-
self.reset()
130-
if data:
131-
tokens, counts = zip(*data)
132-
self._add_tokens_with_freqs(tokens, counts)
133-
134125
def load_line(self, ln):
135126
if self.freq_drop_load:
136127
token = ln.strip().split()[0]

deeppavlov/core/models/component.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,3 @@ def destroy(self):
3636
if hasattr(attr, 'destroy'):
3737
attr.destroy()
3838
delattr(self, attr_name)
39-
40-
def serialize(self):
41-
from deeppavlov.core.models.serializable import Serializable
42-
if isinstance(self, Serializable):
43-
log.warning(f'Method for {self.__class__.__name__} serialization is not implemented!'
44-
f' Will not be able to load without using load_path')
45-
return None
46-
47-
def deserialize(self, data):
48-
from deeppavlov.core.models.serializable import Serializable
49-
if isinstance(self, Serializable):
50-
log.warning(f'Method for {self.__class__.__name__} deserialization is not implemented!'
51-
f' Please, use traditional load_path for this component')
52-
pass

deeppavlov/core/models/serializable.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323

2424

2525
class Serializable(metaclass=ABCMeta):
26-
"""
27-
:class:`deeppavlov.models.model.serializable.Serializable` is an abstract base class that expresses the interface
28-
for all models that can serialize data to a path.
29-
"""
26+
"""Abstract base class that expresses the interface for all models that can serialize data to a path."""
3027

3128
def __init__(self, save_path: Optional[Union[str, Path]], load_path: Optional[Union[str, Path]] = None,
3229
mode: str = 'infer',

tests/test_quick_start.py

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@
4646

4747
TEST_MODES = ['IP', # test_inferring_pretrained_model
4848
'TI', # test_consecutive_training_and_inferring
49-
'SR', # test_serialization
5049
]
5150

52-
ALL_MODES = ('IP', 'TI', 'SR')
51+
ALL_MODES = ('IP', 'TI')
5352

5453
ONE_ARGUMENT_INFER_CHECK = ('Dummy text', None)
5554
TWO_ARGUMENTS_INFER_CHECK = ('Dummy text', 'Dummy text', None)
@@ -376,11 +375,6 @@ def teardown_module():
376375
shutil.rmtree(str(cache_dir), ignore_errors=True)
377376

378377

379-
def _serialize(config):
380-
chainer = build_model(config, download=True)
381-
return chainer.serialize()
382-
383-
384378
def _infer(config, inputs, download=False):
385379
chainer = build_model(config, download=download)
386380
if inputs:
@@ -392,18 +386,6 @@ def _infer(config, inputs, download=False):
392386
return prediction
393387

394388

395-
def _deserialize(config, raw_bytes, examples):
396-
chainer = build_model(config, serialized=raw_bytes)
397-
for *query, expected_response in examples:
398-
query = [[q] for q in query]
399-
actual_response = chainer(*query)
400-
if expected_response is not None:
401-
if actual_response is not None and len(actual_response) > 0:
402-
actual_response = actual_response[0]
403-
assert expected_response == str(actual_response), \
404-
f"Error in interacting with {model_dir} ({conf_file}): {query}"
405-
406-
407389
@pytest.mark.parametrize("model,conf_file,model_dir,mode", TEST_GRID, scope='class')
408390
class TestQuickStart(object):
409391
@staticmethod
@@ -555,28 +537,6 @@ def test_inferring_pretrained_model_socket(self, model, conf_file, model_dir, mo
555537
else:
556538
pytest.skip(f"Unsupported mode: {mode}")
557539

558-
def test_serialization(self, model, conf_file, model_dir, mode):
559-
if 'SR' not in mode:
560-
return pytest.skip("Unsupported mode: {}".format(mode))
561-
562-
config_file_path = test_configs_path / conf_file
563-
564-
with ProcessPoolExecutor(max_workers=1) as executor:
565-
f = executor.submit(_serialize, config_file_path)
566-
raw_bytes = f.result()
567-
568-
serialized: list = pickle.loads(raw_bytes)
569-
if not any(serialized):
570-
pytest.skip("Serialization not supported: {}".format(conf_file))
571-
return
572-
serialized.clear()
573-
574-
with ProcessPoolExecutor(max_workers=1) as executor:
575-
f = executor.submit(_deserialize, config_file_path, raw_bytes, PARAMS[model][(conf_file, model_dir, mode)])
576-
577-
exc = f.exception()
578-
if exc is not None:
579-
raise exc
580540

581541
def test_consecutive_training_and_inferring(self, model, conf_file, model_dir, mode):
582542
if 'TI' in mode:

0 commit comments

Comments
 (0)