Files
Zhengxing Chen 0d294b11e5 Back out recent refactor
Summary:
Need more tests before landing the refactor diffs: D22702504 (https://github.com/facebookresearch/ReAgent/commit/1b470c489d19c33beab88b8ea2e79843d4d31f28), D23123762 (https://github.com/facebookresearch/ReAgent/commit/76829287265bc39f879f3bc1d946a1374c5e1141), D23124179 (https://github.com/facebookresearch/ReAgent/commit/b28f84aa013be00194508f52498160592cb37e9d), D23219012 (https://github.com/facebookresearch/ReAgent/commit/e404c5772ea4118105c2eb136ca96ad5ca8e01db)

Back out to a version based on D23155753.

Check our team diff history: https://fburl.com/diffs/ppsgazgj

Reviewed By: kittipatv

Differential Revision: D23270626

fbshipit-source-id: 14653066bb3924a987a54650a51241895b321c8e
2020-08-21 15:59:42 -07:00

57 lines
1.7 KiB
Python

#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
from copy import deepcopy
from typing import Any, Optional
import torch.nn as nn
from reagent import types as rlt
# add ABCMeta once https://github.com/sphinx-doc/sphinx/issues/5995 is fixed
class ModelBase(nn.Module):
"""
A base class to support exporting through ONNX
"""
def input_prototype(self) -> Any:
"""
This function provides the input for ONNX graph tracing.
The return value should be what expected by `forward()`.
"""
raise NotImplementedError
def feature_config(self) -> Optional[rlt.ModelFeatureConfig]:
"""
If the model needs additional preprocessing, e.g., using sequence features,
returns the config here.
"""
return None
def get_target_network(self):
"""
Return a copy of this network to be used as target network
Subclass should override this if the target network should share parameters
with the network to be trained.
"""
return deepcopy(self)
def get_distributed_data_parallel_model(self):
"""
Return DistributedDataParallel version of this model
This needs to be implemented explicitly because:
1) Model with EmbeddingBag module is not compatible with vanilla DistributedDataParallel
2) Exporting logic needs structured data. DistributedDataParallel doesn't work with structured data.
"""
raise NotImplementedError
def cpu_model(self):
"""
Override this in DistributedDataParallel models
"""
# This is not ideal but makes exporting simple
return deepcopy(self).cpu()