This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 802
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix model export for DocClassificationModel with dense features (#394)
Summary: Pull Request resolved: #394 DocClassificationTask supports dense features, but exporting a DocClassification model with dense features to caffe2 doesn't work. Also, exporting any model that uses both text features (word/char) and dense features together doesn't work. This diff fixes export for dense features. There are two things that needed fixing: - exporter puts model inputs in a list like this: `[features, feature lengths]`. However, dense features don't go through the representation layer - so they need to be hardcoded to be at the end of model inputs (ugh). The order of features needs to be `[features, feature lengths, dense features]` - dummy_model_input for all other fields has two entries. dummy_model_input for dense_features has one entry In the long run, exporter should be tightly coupled with the model itself - we should enforce that model inputs have the same order while training and exporting. But that requires a lot of refactoring. This is a blocking feature for [on-device M-suggestions](https://fb.workplace.com/groups/323789728341681/permalink/326686971385290/). A public test for that is scheduled next week. Personalization models in M-suggestions need dense features. Reviewed By: snisarg, gardenia22 Differential Revision: D14473020 fbshipit-source-id: 8d26ff3ffd28087d6172d66f39d1d3f5f8a0ee9b
- Loading branch information
1 parent
d72f977
commit db704e5
Showing
6 changed files
with
165 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved | ||
|
||
from pytext.exporters.custom_exporters import DenseFeatureExporter | ||
from pytext.exporters.exporter import ModelExporter | ||
|
||
|
||
__all__ = ["ModelExporter"] | ||
__all__ = ["ModelExporter", "DenseFeatureExporter"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved | ||
|
||
from typing import Dict | ||
|
||
from pytext.config import ConfigBase | ||
from pytext.config.field_config import FeatureConfig, FloatVectorConfig | ||
from pytext.exporters.exporter import ModelExporter | ||
from pytext.fields import FieldMeta | ||
|
||
|
||
class DenseFeatureExporter(ModelExporter): | ||
""" | ||
Exporter for models that have DenseFeatures as input to the decoder | ||
""" | ||
|
||
@classmethod | ||
def get_feature_metadata( | ||
cls, feature_config: FeatureConfig, feature_meta: Dict[str, FieldMeta] | ||
): | ||
# add all features EXCEPT dense features. The features exported here | ||
# go through the representation layer | ||
( | ||
input_names_rep, | ||
dummy_model_input_rep, | ||
feature_itos_map_rep, | ||
) = cls._get_exportable_metadata( | ||
lambda x: isinstance(x, ConfigBase) | ||
and not isinstance(x, FloatVectorConfig), | ||
feature_config, | ||
feature_meta, | ||
) | ||
|
||
# need feature lengths only for non-dense features | ||
cls._add_feature_lengths(input_names_rep, dummy_model_input_rep) | ||
|
||
# add dense features. These features don't go through the representation | ||
# layer, instead they go directly to the decoder | ||
( | ||
input_names_dense, | ||
dummy_model_input_dense, | ||
feature_itos_map_dense, | ||
) = cls._get_exportable_metadata( | ||
lambda x: isinstance(x, FloatVectorConfig), feature_config, feature_meta | ||
) | ||
|
||
feature_itos_map_rep.update(feature_itos_map_dense) | ||
return ( | ||
input_names_rep + input_names_dense, | ||
tuple(dummy_model_input_rep + dummy_model_input_dense), | ||
feature_itos_map_rep, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters