Caffe2 - Python API
A deep learning, cross platform ML framework
convert_parameters.py
1 import torch
2 
3 
4 def parameters_to_vector(parameters):
5  r"""Convert parameters to one vector
6 
7  Arguments:
8  parameters (Iterable[Tensor]): an iterator of Tensors that are the
9  parameters of a model.
10 
11  Returns:
12  The parameters represented by a single vector
13  """
14  # Flag for the device where the parameter is located
15  param_device = None
16 
17  vec = []
18  for param in parameters:
19  # Ensure the parameters are located in the same device
20  param_device = _check_param_device(param, param_device)
21 
22  vec.append(param.view(-1))
23  return torch.cat(vec)
24 
25 
26 def vector_to_parameters(vec, parameters):
27  r"""Convert one vector to the parameters
28 
29  Arguments:
30  vec (Tensor): a single vector represents the parameters of a model.
31  parameters (Iterable[Tensor]): an iterator of Tensors that are the
32  parameters of a model.
33  """
34  # Ensure vec of type Tensor
35  if not isinstance(vec, torch.Tensor):
36  raise TypeError('expected torch.Tensor, but got: {}'
37  .format(torch.typename(vec)))
38  # Flag for the device where the parameter is located
39  param_device = None
40 
41  # Pointer for slicing the vector for each parameter
42  pointer = 0
43  for param in parameters:
44  # Ensure the parameters are located in the same device
45  param_device = _check_param_device(param, param_device)
46 
47  # The length of the parameter
48  num_param = param.numel()
49  # Slice the vector, reshape it, and replace the old data of the parameter
50  param.data = vec[pointer:pointer + num_param].view_as(param).data
51 
52  # Increment the pointer
53  pointer += num_param
54 
55 
56 def _check_param_device(param, old_param_device):
57  r"""This helper function is to check if the parameters are located
58  in the same device. Currently, the conversion between model parameters
59  and single vector form is not supported for multiple allocations,
60  e.g. parameters in different GPUs, or mixture of CPU/GPU.
61 
62  Arguments:
63  param ([Tensor]): a Tensor of a parameter of a model
64  old_param_device (int): the device where the first parameter of a
65  model is allocated.
66 
67  Returns:
68  old_param_device (int): report device for the first time
69  """
70 
71  # Meet the first parameter
72  if old_param_device is None:
73  old_param_device = param.get_device() if param.is_cuda else -1
74  else:
75  warn = False
76  if param.is_cuda: # Check if in same GPU
77  warn = (param.get_device() != old_param_device)
78  else: # Check if in CPU
79  warn = (old_param_device != -1)
80  if warn:
81  raise TypeError('Found two parameters on different devices, '
82  'this is currently not supported.')
83  return old_param_device
def typename(o)
Define basic utilities.
Definition: __init__.py:94