| @@ -4,8 +4,8 @@ from typing import Any, Dict | |||||
| import json | import json | ||||
| import numpy as np | import numpy as np | ||||
| from modelscope.metainfo import Models | |||||
| from modelscope.utils.constant import Tasks | |||||
| from ...metainfo import Models | |||||
| from ...utils.constant import Tasks | |||||
| from ..base import Model | from ..base import Model | ||||
| from ..builder import MODELS | from ..builder import MODELS | ||||
| @@ -1,5 +1,5 @@ | |||||
| from modelscope.metainfo import Models | |||||
| from modelscope.utils.constant import Tasks | |||||
| from ...metainfo import Models | |||||
| from ...utils.constant import Tasks | |||||
| from ..builder import MODELS | from ..builder import MODELS | ||||
| from .sbert_for_sequence_classification import \ | from .sbert_for_sequence_classification import \ | ||||
| SbertForSequenceClassificationBase | SbertForSequenceClassificationBase | ||||
| @@ -1,5 +1,5 @@ | |||||
| from modelscope.metainfo import Models | |||||
| from modelscope.utils.constant import Tasks | |||||
| from ...metainfo import Models | |||||
| from ...utils.constant import Tasks | |||||
| from ..builder import MODELS | from ..builder import MODELS | ||||
| from .sbert_for_sequence_classification import \ | from .sbert_for_sequence_classification import \ | ||||
| SbertForSequenceClassificationBase | SbertForSequenceClassificationBase | ||||
| @@ -3,8 +3,8 @@ from typing import Any, Dict, Union | |||||
| import numpy as np | import numpy as np | ||||
| import torch | import torch | ||||
| from modelscope.metainfo import Models | |||||
| from modelscope.utils.constant import Tasks | |||||
| from ...metainfo import Models | |||||
| from ...utils.constant import Tasks | |||||
| from ..base import Model, Tensor | from ..base import Model, Tensor | ||||
| from ..builder import MODELS | from ..builder import MODELS | ||||
| @@ -2,19 +2,19 @@ import os | |||||
| from typing import Any, Dict | from typing import Any, Dict | ||||
| from ....preprocessors.space.fields.intent_field import IntentBPETextField | from ....preprocessors.space.fields.intent_field import IntentBPETextField | ||||
| from ....trainers.nlp.space.trainer.intent_trainer import IntentTrainer | |||||
| from ....utils.config import Config | from ....utils.config import Config | ||||
| from ....utils.constant import Tasks | |||||
| from ....utils.constant import ModelFile, Tasks | |||||
| from ...base import Model, Tensor | from ...base import Model, Tensor | ||||
| from ...builder import MODELS | from ...builder import MODELS | ||||
| from .application.intent_app import IntentTrainer | |||||
| from .model.generator import Generator | from .model.generator import Generator | ||||
| from .model.model_base import ModelBase | |||||
| from .model.model_base import SpaceModelBase | |||||
| __all__ = ['DialogIntentModel'] | |||||
| __all__ = ['SpaceForDialogIntentModel'] | |||||
| @MODELS.register_module(Tasks.dialog_intent_prediction, module_name=r'space') | @MODELS.register_module(Tasks.dialog_intent_prediction, module_name=r'space') | ||||
| class DialogIntentModel(Model): | |||||
| class SpaceForDialogIntentModel(Model): | |||||
| def __init__(self, model_dir: str, *args, **kwargs): | def __init__(self, model_dir: str, *args, **kwargs): | ||||
| """initialize the test generation model from the `model_dir` path. | """initialize the test generation model from the `model_dir` path. | ||||
| @@ -30,13 +30,13 @@ class DialogIntentModel(Model): | |||||
| self.config = kwargs.pop( | self.config = kwargs.pop( | ||||
| 'config', | 'config', | ||||
| Config.from_file( | Config.from_file( | ||||
| os.path.join(self.model_dir, 'configuration.json'))) | |||||
| os.path.join(self.model_dir, ModelFile.CONFIGURATION))) | |||||
| self.text_field = kwargs.pop( | self.text_field = kwargs.pop( | ||||
| 'text_field', | 'text_field', | ||||
| IntentBPETextField(self.model_dir, config=self.config)) | IntentBPETextField(self.model_dir, config=self.config)) | ||||
| self.generator = Generator.create(self.config, reader=self.text_field) | self.generator = Generator.create(self.config, reader=self.text_field) | ||||
| self.model = ModelBase.create( | |||||
| self.model = SpaceModelBase.create( | |||||
| model_dir=model_dir, | model_dir=model_dir, | ||||
| config=self.config, | config=self.config, | ||||
| reader=self.text_field, | reader=self.text_field, | ||||
| @@ -2,19 +2,19 @@ import os | |||||
| from typing import Any, Dict, Optional | from typing import Any, Dict, Optional | ||||
| from ....preprocessors.space.fields.gen_field import MultiWOZBPETextField | from ....preprocessors.space.fields.gen_field import MultiWOZBPETextField | ||||
| from ....trainers.nlp.space.trainer.gen_trainer import MultiWOZTrainer | |||||
| from ....utils.config import Config | from ....utils.config import Config | ||||
| from ....utils.constant import Tasks | |||||
| from ....utils.constant import ModelFile, Tasks | |||||
| from ...base import Model, Tensor | from ...base import Model, Tensor | ||||
| from ...builder import MODELS | from ...builder import MODELS | ||||
| from .application.gen_app import MultiWOZTrainer | |||||
| from .model.generator import Generator | from .model.generator import Generator | ||||
| from .model.model_base import ModelBase | |||||
| from .model.model_base import SpaceModelBase | |||||
| __all__ = ['DialogModelingModel'] | |||||
| __all__ = ['SpaceForDialogModelingModel'] | |||||
| @MODELS.register_module(Tasks.dialog_modeling, module_name=r'space') | @MODELS.register_module(Tasks.dialog_modeling, module_name=r'space') | ||||
| class DialogModelingModel(Model): | |||||
| class SpaceForDialogModelingModel(Model): | |||||
| def __init__(self, model_dir: str, *args, **kwargs): | def __init__(self, model_dir: str, *args, **kwargs): | ||||
| """initialize the test generation model from the `model_dir` path. | """initialize the test generation model from the `model_dir` path. | ||||
| @@ -30,12 +30,12 @@ class DialogModelingModel(Model): | |||||
| self.config = kwargs.pop( | self.config = kwargs.pop( | ||||
| 'config', | 'config', | ||||
| Config.from_file( | Config.from_file( | ||||
| os.path.join(self.model_dir, 'configuration.json'))) | |||||
| os.path.join(self.model_dir, ModelFile.CONFIGURATION))) | |||||
| self.text_field = kwargs.pop( | self.text_field = kwargs.pop( | ||||
| 'text_field', | 'text_field', | ||||
| MultiWOZBPETextField(self.model_dir, config=self.config)) | MultiWOZBPETextField(self.model_dir, config=self.config)) | ||||
| self.generator = Generator.create(self.config, reader=self.text_field) | self.generator = Generator.create(self.config, reader=self.text_field) | ||||
| self.model = ModelBase.create( | |||||
| self.model = SpaceModelBase.create( | |||||
| model_dir=model_dir, | model_dir=model_dir, | ||||
| config=self.config, | config=self.config, | ||||
| reader=self.text_field, | reader=self.text_field, | ||||
| @@ -5,8 +5,10 @@ import os | |||||
| import torch.nn as nn | import torch.nn as nn | ||||
| from .....utils.constant import ModelFile | |||||
| class ModelBase(nn.Module): | |||||
| class SpaceModelBase(nn.Module): | |||||
| """ | """ | ||||
| Basic model wrapper for static graph and dygrpah. | Basic model wrapper for static graph and dygrpah. | ||||
| """ | """ | ||||
| @@ -14,21 +16,22 @@ class ModelBase(nn.Module): | |||||
| @classmethod | @classmethod | ||||
| def register(cls, name): | def register(cls, name): | ||||
| ModelBase._registry[name] = cls | |||||
| SpaceModelBase._registry[name] = cls | |||||
| return | return | ||||
| @staticmethod | @staticmethod | ||||
| def by_name(name): | def by_name(name): | ||||
| return ModelBase._registry[name] | |||||
| return SpaceModelBase._registry[name] | |||||
| @staticmethod | @staticmethod | ||||
| def create(model_dir, config, *args, **kwargs): | def create(model_dir, config, *args, **kwargs): | ||||
| model_cls = ModelBase.by_name(config.Model.model) | |||||
| model_cls = SpaceModelBase.by_name(config.Model.model) | |||||
| return model_cls(model_dir, config, *args, **kwargs) | return model_cls(model_dir, config, *args, **kwargs) | ||||
| def __init__(self, model_dir, config): | def __init__(self, model_dir, config): | ||||
| super(ModelBase, self).__init__() | |||||
| self.init_checkpoint = os.path.join(model_dir, 'pytorch_model.bin') | |||||
| super(SpaceModelBase, self).__init__() | |||||
| self.init_checkpoint = os.path.join(model_dir, | |||||
| ModelFile.TORCH_MODEL_BIN_FILE) | |||||
| self.abandon_label = config.Dataset.abandon_label | self.abandon_label = config.Dataset.abandon_label | ||||
| self.use_gpu = config.use_gpu | self.use_gpu = config.use_gpu | ||||
| self.gpu = config.Trainer.gpu | self.gpu = config.Trainer.gpu | ||||
| @@ -9,10 +9,10 @@ import torch.nn.functional as F | |||||
| from ..modules.embedder import Embedder | from ..modules.embedder import Embedder | ||||
| from ..modules.transformer_block import TransformerBlock | from ..modules.transformer_block import TransformerBlock | ||||
| from .model_base import ModelBase | |||||
| from .model_base import SpaceModelBase | |||||
| class UnifiedTransformer(ModelBase): | |||||
| class UnifiedTransformer(SpaceModelBase): | |||||
| """ | """ | ||||
| Implement unified transformer. | Implement unified transformer. | ||||
| """ | """ | ||||
| @@ -1,7 +1,7 @@ | |||||
| from typing import Any, Dict | from typing import Any, Dict | ||||
| from ...metainfo import Pipelines | from ...metainfo import Pipelines | ||||
| from ...models.nlp import DialogIntentModel | |||||
| from ...models.nlp import SpaceForDialogIntentModel | |||||
| from ...preprocessors import DialogIntentPredictionPreprocessor | from ...preprocessors import DialogIntentPredictionPreprocessor | ||||
| from ...utils.constant import Tasks | from ...utils.constant import Tasks | ||||
| from ..base import Pipeline | from ..base import Pipeline | ||||
| @@ -15,7 +15,7 @@ __all__ = ['DialogIntentPredictionPipeline'] | |||||
| module_name=Pipelines.dialog_intent_prediction) | module_name=Pipelines.dialog_intent_prediction) | ||||
| class DialogIntentPredictionPipeline(Pipeline): | class DialogIntentPredictionPipeline(Pipeline): | ||||
| def __init__(self, model: DialogIntentModel, | |||||
| def __init__(self, model: SpaceForDialogIntentModel, | |||||
| preprocessor: DialogIntentPredictionPreprocessor, **kwargs): | preprocessor: DialogIntentPredictionPreprocessor, **kwargs): | ||||
| """use `model` and `preprocessor` to create a nlp text classification pipeline for prediction | """use `model` and `preprocessor` to create a nlp text classification pipeline for prediction | ||||
| @@ -1,9 +1,9 @@ | |||||
| from typing import Any, Dict, Optional | from typing import Any, Dict, Optional | ||||
| from modelscope.models.nlp import DialogModelingModel | |||||
| from modelscope.preprocessors import DialogModelingPreprocessor | |||||
| from modelscope.utils.constant import Tasks | |||||
| from ...metainfo import Pipelines | from ...metainfo import Pipelines | ||||
| from ...models.nlp import SpaceForDialogModelingModel | |||||
| from ...preprocessors import DialogModelingPreprocessor | |||||
| from ...utils.constant import Tasks | |||||
| from ..base import Pipeline, Tensor | from ..base import Pipeline, Tensor | ||||
| from ..builder import PIPELINES | from ..builder import PIPELINES | ||||
| @@ -14,7 +14,7 @@ __all__ = ['DialogModelingPipeline'] | |||||
| Tasks.dialog_modeling, module_name=Pipelines.dialog_modeling) | Tasks.dialog_modeling, module_name=Pipelines.dialog_modeling) | ||||
| class DialogModelingPipeline(Pipeline): | class DialogModelingPipeline(Pipeline): | ||||
| def __init__(self, model: DialogModelingModel, | |||||
| def __init__(self, model: SpaceForDialogModelingModel, | |||||
| preprocessor: DialogModelingPreprocessor, **kwargs): | preprocessor: DialogModelingPreprocessor, **kwargs): | ||||
| """use `model` and `preprocessor` to create a nlp text classification pipeline for prediction | """use `model` and `preprocessor` to create a nlp text classification pipeline for prediction | ||||
| @@ -4,7 +4,7 @@ import os | |||||
| from typing import Any, Dict | from typing import Any, Dict | ||||
| from ...utils.config import Config | from ...utils.config import Config | ||||
| from ...utils.constant import Fields | |||||
| from ...utils.constant import Fields, ModelFile | |||||
| from ...utils.type_assert import type_assert | from ...utils.type_assert import type_assert | ||||
| from ..base import Preprocessor | from ..base import Preprocessor | ||||
| from ..builder import PREPROCESSORS | from ..builder import PREPROCESSORS | ||||
| @@ -26,7 +26,7 @@ class DialogIntentPredictionPreprocessor(Preprocessor): | |||||
| self.model_dir: str = model_dir | self.model_dir: str = model_dir | ||||
| self.config = Config.from_file( | self.config = Config.from_file( | ||||
| os.path.join(self.model_dir, 'configuration.json')) | |||||
| os.path.join(self.model_dir, ModelFile.CONFIGURATION)) | |||||
| self.text_field = IntentBPETextField( | self.text_field = IntentBPETextField( | ||||
| self.model_dir, config=self.config) | self.model_dir, config=self.config) | ||||
| @@ -4,7 +4,7 @@ import os | |||||
| from typing import Any, Dict | from typing import Any, Dict | ||||
| from ...utils.config import Config | from ...utils.config import Config | ||||
| from ...utils.constant import Fields | |||||
| from ...utils.constant import Fields, ModelFile | |||||
| from ...utils.type_assert import type_assert | from ...utils.type_assert import type_assert | ||||
| from ..base import Preprocessor | from ..base import Preprocessor | ||||
| from ..builder import PREPROCESSORS | from ..builder import PREPROCESSORS | ||||
| @@ -26,7 +26,7 @@ class DialogModelingPreprocessor(Preprocessor): | |||||
| self.model_dir: str = model_dir | self.model_dir: str = model_dir | ||||
| self.config = Config.from_file( | self.config = Config.from_file( | ||||
| os.path.join(self.model_dir, 'configuration.json')) | |||||
| os.path.join(self.model_dir, ModelFile.CONFIGURATION)) | |||||
| self.text_field = MultiWOZBPETextField( | self.text_field = MultiWOZBPETextField( | ||||
| self.model_dir, config=self.config) | self.model_dir, config=self.config) | ||||
| @@ -8,6 +8,7 @@ from itertools import chain | |||||
| import numpy as np | import numpy as np | ||||
| from ....utils.constant import ModelFile | |||||
| from ....utils.nlp.space import ontology, utils | from ....utils.nlp.space import ontology, utils | ||||
| from ....utils.nlp.space.db_ops import MultiWozDB | from ....utils.nlp.space.db_ops import MultiWozDB | ||||
| from ....utils.nlp.space.utils import list2np | from ....utils.nlp.space.utils import list2np | ||||
| @@ -3,7 +3,7 @@ import unittest | |||||
| from modelscope.hub.snapshot_download import snapshot_download | from modelscope.hub.snapshot_download import snapshot_download | ||||
| from modelscope.models import Model | from modelscope.models import Model | ||||
| from modelscope.models.nlp import DialogIntentModel | |||||
| from modelscope.models.nlp import SpaceForDialogIntentModel | |||||
| from modelscope.pipelines import DialogIntentPredictionPipeline, pipeline | from modelscope.pipelines import DialogIntentPredictionPipeline, pipeline | ||||
| from modelscope.preprocessors import DialogIntentPredictionPreprocessor | from modelscope.preprocessors import DialogIntentPredictionPreprocessor | ||||
| from modelscope.utils.constant import Tasks | from modelscope.utils.constant import Tasks | ||||
| @@ -20,7 +20,7 @@ class DialogIntentPredictionTest(unittest.TestCase): | |||||
| def test_run(self): | def test_run(self): | ||||
| cache_path = snapshot_download(self.model_id) | cache_path = snapshot_download(self.model_id) | ||||
| preprocessor = DialogIntentPredictionPreprocessor(model_dir=cache_path) | preprocessor = DialogIntentPredictionPreprocessor(model_dir=cache_path) | ||||
| model = DialogIntentModel( | |||||
| model = SpaceForDialogIntentModel( | |||||
| model_dir=cache_path, | model_dir=cache_path, | ||||
| text_field=preprocessor.text_field, | text_field=preprocessor.text_field, | ||||
| config=preprocessor.config) | config=preprocessor.config) | ||||
| @@ -6,7 +6,7 @@ import unittest | |||||
| from modelscope.hub.snapshot_download import snapshot_download | from modelscope.hub.snapshot_download import snapshot_download | ||||
| from modelscope.models import Model | from modelscope.models import Model | ||||
| from modelscope.models.nlp import DialogModelingModel | |||||
| from modelscope.models.nlp import SpaceForDialogModelingModel | |||||
| from modelscope.pipelines import DialogModelingPipeline, pipeline | from modelscope.pipelines import DialogModelingPipeline, pipeline | ||||
| from modelscope.preprocessors import DialogModelingPreprocessor | from modelscope.preprocessors import DialogModelingPreprocessor | ||||
| from modelscope.utils.constant import Tasks | from modelscope.utils.constant import Tasks | ||||
| @@ -97,7 +97,7 @@ class DialogModelingTest(unittest.TestCase): | |||||
| cache_path = snapshot_download(self.model_id) | cache_path = snapshot_download(self.model_id) | ||||
| preprocessor = DialogModelingPreprocessor(model_dir=cache_path) | preprocessor = DialogModelingPreprocessor(model_dir=cache_path) | ||||
| model = DialogModelingModel( | |||||
| model = SpaceForDialogModelingModel( | |||||
| model_dir=cache_path, | model_dir=cache_path, | ||||
| text_field=preprocessor.text_field, | text_field=preprocessor.text_field, | ||||
| config=preprocessor.config) | config=preprocessor.config) | ||||