mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
Summary: This diff is introducing abstractions for parameter sharing for all the parameters, that are created through new create_param syntax. Possible use-cases of this parameters sharing: 1. Share params within RNN interface. 2. Some complicated models that might share some of the branches. 3. TODO (next diff): Cross-model parameter sharing. Reviewed By: salexspb Differential Revision: D5160935 fbshipit-source-id: c6d40a5ed7ead240cd7db0eb69de6dc5f505b05a
61 lines
1.8 KiB
Python
61 lines
1.8 KiB
Python
from __future__ import absolute_import
|
|
from __future__ import division
|
|
from __future__ import print_function
|
|
from __future__ import unicode_literals
|
|
|
|
from caffe2.python import core
|
|
|
|
import numpy as np
|
|
|
|
|
|
class ParameterTags(object):
|
|
BIAS = 'BIAS'
|
|
WEIGHT = 'WEIGHT'
|
|
COMPUTED_PARAM = 'COMPUTED_PARAM'
|
|
|
|
|
|
class ParameterType(object):
|
|
DENSE = 'dense'
|
|
SPARSE = 'sparse'
|
|
|
|
|
|
class ParameterInfo(object):
|
|
|
|
def __init__(
|
|
self, param_id, param, key=None, shape=None, length=None,
|
|
grad=None, blob_copy=None):
|
|
assert isinstance(param, core.BlobReference)
|
|
self.param_id = param_id
|
|
self.name = str(param)
|
|
self.blob = param
|
|
self.key = key
|
|
self.shape = shape
|
|
self.size = None if shape is None else np.prod(shape)
|
|
self.length = max(1, length if length is not None else 1)
|
|
self.grad = grad
|
|
self._cloned_init_net = None
|
|
# Optionally store equivalent copies of the blob
|
|
# in different precisions (i.e. half and float copies)
|
|
# stored as a dict of TensorProto.DataType -> BlobReference
|
|
self.blob_copy = blob_copy
|
|
|
|
def grad_type(self):
|
|
# self.grad could be None for model parallelism with parameter server
|
|
if self.grad is None:
|
|
return
|
|
return (
|
|
ParameterType.SPARSE if isinstance(self.grad, core.GradientSlice)
|
|
else ParameterType.DENSE)
|
|
|
|
def cloned_init_net(self):
|
|
if not self._cloned_init_net:
|
|
init_net, outputs = self.blob.Net().ClonePartial(
|
|
'param_%d_%s_init' % (self.param_id, self.name),
|
|
inputs=[],
|
|
outputs=[self.blob])
|
|
self._cloned_init_net = (init_net, outputs[0])
|
|
return self._cloned_init_net
|
|
|
|
def __str__(self):
|
|
return self.name
|