/usr/local/lib64/python3.6/site-packages/torch/nn/utils/__pycache__
Edit: /usr/local/lib64/python3.6/site-packages/torch/nn/utils/__pycache__/parametrize.cpython-36.pyc (19870B)
3
Egq @ s U d dl Z d dlmZmZmZ d dlmZ d dl mZ d dlZd dl m
Z
d dlmZm
Z
mZmZmZ d ai aeeeef e
e f e
dd Zd d
ZG dd deZedd
ddZeeddddZddeeeeedddZdee
e edddZdeeeedddZdS ) N)
ModuleList
ModuleDictModule) Parameter)Tensor)contextmanager)UnionOptionalDictTupleSequencec c s* t d7 a z
dV W dt d8 a t s$i aX dS )a] Context manager that enables the caching system within parametrizations
registered with :func:`register_parametrization`.
The value of the parametrized objects is computed and cached the first time
they are required when this context manager is active. The cached values are
discarded when leaving the context manager.
This is useful when using a parametrized parameter more than once in the forward pass.
An example of this is when parametrizing the recurrent kernel of an RNN or when
sharing weights.
The simplest way to activate the cache is by wrapping the forward pass of the neural network
.. code-block:: python
import torch.nn.utils.parametrize as P
...
with P.cached():
output = model(inputs)
in training and evaluation. One may also wrap the parts of the modules that use
several times the parametrized tensors. For example, the loop of an RNN with a
parametrized recurrent kernel:
.. code-block:: python
with P.cached():
for x in xs:
out_rnn = self.rnn_cell(x, out_rnn)
N)_cache_enabled_cache r r F/usr/local/lib64/python3.6/site-packages/torch/nn/utils/parametrize.pycached s "
r c C s( t |tr| j|| n| j|| d S )N)
isinstancer Zregister_parameterZregister_buffer)modulenameXr r r _register_parameter_or_buffer: s
r c sb e Zd ZU dZeed
ee e
eef edd fdd
Zedddd Z
ed
ddZ ZS )ParametrizationLista\ A sequential container that holds and manages the ``original`` or ``original0``, ``original1``, ...
parameters or buffers of a parametrized :class:`torch.nn.Module`.
It is the type of ``module.parametrizations[tensor_name]`` when ``module[tensor_name]``
has been parametrized with :func:`register_parametrization`.
If the first registered parmetrization has a ``right_inverse`` that returns one tensor or
does not have a ``right_inverse`` (in which case we assume that ``right_inverse`` is the identity),
it will hold the tensor under the name ``original``.
If it has a ``right_inverse`` that returns more than one tensor, these will be registered as
``original0``, ``original1``, ...
.. warning::
This class is used internally by :func:`register_parametrization`. It is documented
here for completeness. It shall not be instantiated by the user.
Args:
modules (sequence): sequence of modules representing the parametrizations
original (Parameter or Tensor): parameter or buffer that is parametrized
unsafe (bool): a boolean flag that denotes whether the parametrization
may change the dtype and shape of the tensor. Default: `False`
Warning: the parametrization is not checked for consistency upon registration.
Enable this flag at your own risk.
FN)modulesoriginalunsafereturnc s t |dkrtdt j| || _|j}|j}tj J |}x>t | D ]2}t
|drJy|j|}W qJ tk
rz Y qJX qJW W d Q R X t
|t rt
|tjj rtdt|j t
|t| _| jrdnt || _| jr4|j|jkrtd|j d|j tj |j| W d Q R X t| d| nrxpt|D ]d\}} t
| tsntd | d
t| j dt
|trt| } | j|j t| d| | q>W | js| }
t
|
tstdt|
j d|
j|krtd
| d|
j |
j|krtd| d|
j d S )Nr z1ParametrizationList requires one or more modules.
right_inversezT'right_inverse' must return a Tensor or a Sequence of tensors (list, tuple...). Got r
zVWhen `right_inverse` outputs one tensor, it may not change the dtype.
original.dtype: z
right_inverse(original).dtype: r z\'right_inverse' must return a Tensor or a Sequence of tensors (list, tuple...). Got element z of the sequence with type .z,A parametrization must return a tensor. Got z}Registering a parametrization may not change the dtype of the tensor, unless `unsafe` flag is enabled.
unparametrized dtype: z
parametrized dtype: z}Registering a parametrization may not change the shape of the tensor, unless `unsafe` flag is enabled.
unparametrized shape: z
parametrized shape: )len
ValueErrorsuper__init__r shapedtypetorchno_gradreversedhasattrr NotImplementedErrorr r collectionsabcr type__name__ is_tensorntensorsset_r enumerater Zrequires_grad_
requires_grad)selfr r r Zoriginal_shapeZoriginal_dtypenewr iZ originaliZ) __class__r r r" ] sV
zParametrizationList.__init__)valuer c C s t j v x