Ejemplo n.º 1
0
 def _calculate_inout_shape(self, input_tensor):
   assert input_tensor.num_dims == 4, "[MaxPooling2D]: shape of input tensor is wrong"
   input_b = input_tensor.batch_shape[0]
   input_d = input_tensor.batch_shape[1]
   input_h = input_tensor.batch_shape[2]
   input_w = input_tensor.batch_shape[3]
   assert input_h != 0, "wrong input_h"
   assert input_w != 0, "wrong input_w"
   assert input_d != 0, "wrong input_d"
   
   #calculate padding for same
   if (self.padding == 'same'):
     if (input_h % self.stride[0] == 0):
       padding_h = max(self.kernel_size[0] - self.stride[0], 0)
     else:
       padding_h = max(self.kernel_size[0] - (input_h % self.stride[0]), 0)
     if (input_w % self.stride[1] == 0):
       padding_w = max(self.kernel_size[1] - self.stride[1], 0)
     else:
       padding_w = max(self.kernel_size[1] - (input_w % self.stride[1]), 0)
     self.padding = (padding_h//2, padding_w//2)
     fflogger.debug("pool2d same padding %s" %( str(self.padding)))
     
   self.input_shape = (input_b, input_d, input_w, input_h)
   self.in_channels = input_d
   self.out_channels = input_d
   output_h = 1 + math.floor((input_h + 2 * self.padding[0] - self.kernel_size[0]) / self.stride[0])
   output_w = 1 + math.floor((input_w + 2 * self.padding[1] - self.kernel_size[1]) / self.stride[1])
   output_d = self.out_channels
   self.output_shape = (input_b, output_d, output_h, output_w)
   fflogger.debug("pool2d input %s, output %s" %( str(self.input_shape), str(self.output_shape)))
Ejemplo n.º 2
0
 def _calculate_inout_shape(self, input_tensor):
     input_shape = input_tensor.batch_shape
     self.input_shape = input_shape
     flat_size = 1
     for i in range(1, len(input_shape)):
         flat_size *= input_shape[i]
     self.output_shape = (input_shape[0], flat_size)
     fflogger.debug("flat input %s, output %s" %
                    (str(self.input_shape), str(self.output_shape)))
Ejemplo n.º 3
0
 def _calculate_inout_shape(self, input_tensor):
     assert input_tensor.num_dims == 2, "[Embedding]: shape of input tensor is wrong"
     input_b = input_tensor.batch_shape[0]
     in_dim = input_tensor.batch_shape[1]
     assert in_dim != 0, "wrong in_dim"
     assert self.input_length == in_dim, "wrong input_w"
     self.output_shape = (input_b, self.out_channels)
     self.input_shape = (input_b, self.input_length)
     fflogger.debug("embedding input %s, output %s" %
                    (str(self.input_shape), str(self.output_shape)))
Ejemplo n.º 4
0
 def _calculate_inout_shape(self, input_tensor):
     assert input_tensor.num_dims == 2, "[Dense]: shape of input tensor is wrong"
     input_b = input_tensor.batch_shape[0]
     in_dim = input_tensor.batch_shape[1]
     assert in_dim != 0, "wrong in_dim"
     if (self.in_channels != 0):  # check if user input is correct
         assert self.in_channels == in_dim, "wrong input_w"
     self.output_shape = (input_b, self.out_channels)
     self.input_shape = (input_b, in_dim)
     self.in_channels = in_dim
     fflogger.debug("dense input %s, output %s" %
                    (str(self.input_shape), str(self.output_shape)))
Ejemplo n.º 5
0
    def __init__(self, inputs, outputs, name=None):
        super(Model, self).__init__(name)

        if (isinstance(inputs, list) == False):
            inputs = [inputs]

        self._input_tensors = inputs
        for input_tensor in inputs:
            self._input_layers.append(input_tensor.from_layer)
        self._output_tensor = outputs

        self.__traverse_dag_dfs()
        fflogger.debug("nb_layers %d" % (self._nb_layers))
Ejemplo n.º 6
0
 def _calculate_inout_shape(self, input_tensors):
     if (input_tensors[0].num_dims == 2):
         output_shape = [input_tensors[0].batch_shape[0], 0]
         for input_tensor in input_tensors:
             output_shape[self.axis] += input_tensor.batch_shape[self.axis]
         self.output_shape = (output_shape[0], output_shape[1])
     elif (input_tensors[0].num_dims == 4):
         output_shape = [
             input_tensors[0].batch_shape[0], 0,
             input_tensors[0].batch_shape[2],
             input_tensors[0].batch_shape[3]
         ]
         for input_tensor in input_tensors:
             output_shape[self.axis] += input_tensor.batch_shape[self.axis]
         self.output_shape = (output_shape[0], output_shape[1],
                              output_shape[2], output_shape[3])
     else:
         assert 0, "un-supported dims"
     fflogger.debug("concat output %s" % (str(self.output_shape)))
     self.input_shape = input_tensors[0].batch_shape
Ejemplo n.º 7
0
    def summary(self, line_length=None, positions=None, print_fn=None):
        if line_length != None:
            assert 0, "line_length is not supported"
        if print_fn != None:
            assert 0, "print_fn is not supported"

        model_summary = "Layer (type)\t\tOutput Shape\t\tInput Shape\tConnected to\n"
        for layer in self._input_layers:
            layer_summary = layer.get_summary()
            model_summary += layer_summary
        for layer in self._layers:
            fflogger.debug(str(layer))
            for prev_layer in layer.prev_layers:
                fflogger.debug("\tprev: %s" % (str(prev_layer)))
            for next_layer in layer.next_layers:
                fflogger.debug("\tnext: %s" % (str(next_layer)))
            layer_summary = layer.get_summary()
            model_summary += layer_summary

        return model_summary
Ejemplo n.º 8
0
    def compile(self,
                optimizer,
                loss=None,
                metrics=None,
                loss_weights=None,
                weighted_metrics=None,
                run_eagerly=None,
                comp_mode=ff.CompMode.TRAINING,
                **kwargs):

        if loss_weights != None:
            assert 0, "loss_weights is not supported"
        if weighted_metrics != None:
            assert 0, "weighted_metrics is not supported"
        if run_eagerly != None:
            assert 0, "run_eagerly is not supported"

        assert loss != None, "loss is None"
        if isinstance(loss, keras_losses.Loss) == True:
            self._loss = loss
        elif loss == 'categorical_crossentropy':
            self._loss = keras_losses.CategoricalCrossentropy()
        elif loss == 'sparse_categorical_crossentropy':
            self._loss = keras_losses.SparseCategoricalCrossentropy()
            self._label_type = ff.DataType.DT_INT32
        elif loss == 'mean_squared_error':
            self._loss = keras_losses.MeanSquaredError()
        else:
            assert 0, 'Unsupported loss'

        assert metrics != None, "metrics is None"
        assert isinstance(metrics, list) == True, 'Metrics should be a list'
        for metric in metrics:
            if isinstance(metric, keras_metrics.Metric) == True:
                self._metrics.append(metric)
            elif metric == 'accuracy':
                self._metrics.append(keras_metrics.Accuracy())
            elif metric == 'categorical_crossentropy':
                self._metrics.append(keras_metrics.CategoricalCrossentropy())
            elif metric == 'sparse_categorical_crossentropy':
                self._metrics.append(
                    keras_metrics.SparseCategoricalCrossentropy())
            elif metric == 'mean_squared_error':
                self._metrics.append(keras_metrics.MeanSquaredError())
            elif metric == 'root_mean_squared_error':
                self._metrics.append(keras_metrics.RootMeanSquaredError())
            elif metric == 'mean_absolute_error':
                self._metrics.append(keras_metrics.MeanAbsoluteError())
            else:
                assert 0, 'Unsupported metric'

        self._ffmodel = ff.FFModel(self._ffconfig)
        self._create_input_tensors()
        self._create_flexflow_layers()

        self._verify_output_tensors()
        self._verify_input_tensors()

        self._ffoptimizer = optimizer
        self._create_optimizer()
        metrics_type = []
        for metric in self._metrics:
            metrics_type.append(metric.type)
        self._ffmodel.optimizer = optimizer.ffhandle
        self._ffmodel.compile(loss_type=self._loss.type,
                              metrics=metrics_type,
                              comp_mode=comp_mode)
        self._create_label_tensor()
        fflogger.debug("%s, %s, %s, %s" %
                       (str(self._input_tensors[0]), str(self._output_tensor),
                        str(self._input_tensors[0].ffhandle),
                        str(self._output_tensor.ffhandle)))
Ejemplo n.º 9
0
 def _calculate_inout_shape(self, input_tensors):
     assert len(input_tensors) == 2, "check input_tensors"
     self.input_shape = input_tensors[0].batch_shape
     self.output_shape = input_tensors[0].batch_shape
     fflogger.debug("multiply output %s" % (str(self.output_shape)))