Skip to content

Commit

Permalink
add XLM sentence encoder (facebookresearch#437)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebookresearch#437

Add model to produce XLM sentence embeddings.
1. refactor XLMSentenceEncoder to extent BertSentenceEncoderBase to re-use pooling Logic
2. Add a model that only has the encoding layer to produce sentence encoding

Reviewed By: kartikayk

Differential Revision: D14676261

fbshipit-source-id: 004ef4c808e44db0289e8cff72eeaf6e412f943a
  • Loading branch information
rutyrinott authored and facebook-github-bot committed Apr 3, 2019
1 parent 4791caa commit 944cc11
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions pytext/task/new_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ class Config(ConfigBase):

@classmethod
def from_config(cls, config: Config, unused_metadata=None, model_state=None):
tensorizers, data = NewTask._init_tensorizers(config)

# Initialized tensorizers can be used to create the model
model = NewTask._init_model(config, tensorizers, model_state)

# This is the only place right now that the task actually cares about which
# features and tensors are being used. This is a strong tie between
# the implementation of the model and the metric reporter.
metric_reporter = create_component(
ComponentType.METRIC_REPORTER,
config.metric_reporter,
tensorizers=tensorizers,
)
trainer = create_trainer(config.trainer, model)
return cls(data, model, metric_reporter, trainer)

@classmethod
def _init_tensorizers(cls, config: Config):
tensorizers = {
name: create_component(ComponentType.TENSORIZER, tensorizer)
for name, tensorizer in config.model.inputs._asdict().items()
Expand All @@ -118,24 +136,18 @@ def from_config(cls, config: Config, unused_metadata=None, model_state=None):
data = create_component(
ComponentType.DATA_HANDLER, config.data, schema, tensorizers
)
# Initialized tensorizers can be used to create the model
return tensorizers, data

@classmethod
def _init_model(cls, config: Config, tensorizers, model_state=None):
model = create_component(ComponentType.MODEL, config.model, tensorizers)
if model_state:
model.load_state_dict(model_state)

precision.activate(model)
if cuda.CUDA_ENABLED:
model = model.cuda()
# This is the only place right now that the task actually cares about which
# features and tensors are being used. This is a strong tie between
# the implementation of the model and the metric reporter.
metric_reporter = create_component(
ComponentType.METRIC_REPORTER,
config.metric_reporter,
tensorizers=tensorizers,
)
trainer = create_trainer(config.trainer, model)
return cls(data, model, metric_reporter, trainer)
return model

def __init__(
self,
Expand Down

0 comments on commit 944cc11

Please sign in to comment.