Exemplo n.º 1
0
    def axes_to_spatial_first_order(self, node, graph):
        input_name = node.input_names[0]
        input_buf = graph.get_buffer(input_name)
        # force convergence if necessary
        # use the 'backwards' permute orders because they are self-inverses.
        if input_buf.axis_format == AxisTracker.AxisFormat.NSC:
            AxisTracker.inject_implicit_permute(
                graph, input_name, AxisTracker.AxisFormat.NCS,
                AxisTracker.AxisFormat.NSC_TO_NCS, [node.op.name])
        elif input_buf.axis_format == AxisTracker.AxisFormat.BTF:
            AxisTracker.inject_implicit_permute(
                graph, input_name, AxisTracker.AxisFormat.TBF,
                AxisTracker.AxisFormat.TBF_TO_BTF, [node.op.name])
        elif input_buf.axis_format == AxisTracker.AxisFormat.NONTRIVIAL:
            pass
        elif input_buf.axis_format == AxisTracker.AxisFormat.FEATURE:
            pass
        else:
            raise ValueError(
                code_to_message.get_error_message(
                    "ERROR_RESHAPE_UNEXPECTED_INPUT_ORDER")(
                        input_buf.axis_format))

        output_buf = graph.get_output_buffers(node)[0]
        if output_buf.rank() > 4:
            log_assert(
                self.product(output_buf.shape[:-4]) == 1,
                code_to_message.get_error_message(
                    "ERROR_RESHAPE_BATCH_UNSUPPORTED"))
            output_buf.shape = output_buf.shape[-4:]
        output_buf.axis_format = AxisTracker.AxisFormat.NONTRIVIAL
Exemplo n.º 2
0
    def axes_to_spatial_first_order(self, node, graph):
        AxisTracker.log_axes_to_spatial_first_order(node, graph)
        input_buf = graph.get_input_buffers(node)[0]
        if input_buf.rank() == 4:
            AxisTracker.enforce_input_format(graph, input_buf.name,
                                             AxisTracker.AxisFormat.NSC,
                                             AxisTracker.AxisFormat.NCS_TO_NSC)
            # weights expect NCHW order, need to permute
            input_buf = graph.get_input_buffers(node)[0]
            batch, height, width, depth = input_buf.shape
            weights = node.op.weights_list[0]

            # TODO: this optimization was added based on onnx framework. Verify(Modify) if
            #       change is needed for other frameworks
            # ONNX defines FC as W^Tx + b,
            # so the weights have shape (batch, input_size, output_size)
            input_size = weights.shape[0]
            output_size = weights.shape[1]
            log_assert(
                input_size == depth * height * width,
                code_to_message.get_error_message("ERROR_FC_WRONG_INPUT_SIZE")(
                    node.op.name, input_size, (depth, height, width)))
            weights.shape = (depth, height, width, output_size)
            weights = numpy.transpose(weights, (3, 1, 2, 0))
            weights = numpy.ascontiguousarray(weights, dtype=numpy.float32)
            weights.shape = (output_size, input_size)
            node.op.weights_list[0] = weights
        elif input_buf.rank() == 2:
            # again, need to transpose weights for spatial_first order
            weights = node.op.weights_list[0]
            weights = numpy.ascontiguousarray(numpy.transpose(weights, (1, 0)))
            node.op.weights_list[0] = weights

        output_buf = graph.get_output_buffers(node)[0]
        output_buf.axis_format = AxisTracker.AxisFormat.FEATURE
Exemplo n.º 3
0
 def axes_to_spatial_first_order(self, node, graph):
     # Remap the axis if < 0 to the real axis and if needed permute it for NSC
     # In addition, output buffer axis tracking stays the same as input so long
     # as the rank of indices == 1. Otherwise it's non trivial as the rank will change
     input_name = node.input_names[0]
     input_buf = graph.get_input_buffers(node)[0]
     indices_buf = graph.get_input_buffers(node)[1]
     output_buf = graph.get_output_buffers(node)[0]
     if node.op.axis < 0:
         node.op.axis = node.op.axis + input_buf.rank()
     if input_buf.axis_format == AxisTracker.AxisFormat.NSC:
         axis_map = [0, 3, 1, 2]
         node.op.axis = axis_map[node.op.axis]
         if indices_buf.rank() > 1:
             AxisTracker.inject_implicit_permute(
                 graph, input_name, AxisTracker.AxisFormat.NCS,
                 AxisTracker.AxisFormat.NSC_TO_NCS, [node.op.name])
             output_buf.axis_format = AxisTracker.AxisFormat.NONTRIVIAL
         else:
             output_buf.axis_format = AxisTracker.AxisFormat.NSC
     else:
         if indices_buf.rank() > 1:
             output_buf.axis_format = AxisTracker.AxisFormat.NONTRIVIAL
         else:
             output_buf.axis_format = input_buf.axis_format
Exemplo n.º 4
0
 def axes_to_spatial_first_order(self, node, graph):
     input_name = node.input_names[0]
     input_buf = graph.get_buffer(input_name)
     if input_buf.axis_format == AxisTracker.AxisFormat.NSC:
         node.op.offsets = AxisTracker.permute_shape(
             node.op.offsets, AxisTracker.AxisFormat.NCS_TO_NSC)
     elif input_buf.axis_format == AxisTracker.AxisFormat.BTF:
         node.op.offsets = AxisTracker.permute_shape(
             node.op.offsets, AxisTracker.AxisFormat.TBF_TO_BTF)
     AxisTracker.eltwise_to_spatial_first_order(node, graph)
Exemplo n.º 5
0
 def axes_to_spatial_first_order(self, node, graph):
     AxisTracker.enforce_input_format(graph, node.input_names[0],
                                      AxisTracker.AxisFormat.NSC,
                                      AxisTracker.AxisFormat.NCS_TO_NSC)
     output_buf = graph.get_output_buffers(node)[0]
     log_assert(
         output_buf.shape[0] == 1,
         code_to_message.get_error_message(
             "ERROR_MAX_ROI_POOL_BATCH_UNSUPPORTED"))
     output_buf.shape = AxisTracker.permute_shape(
         output_buf.shape, AxisTracker.AxisFormat.NCS_TO_NSC)
     output_buf.axis_format = AxisTracker.AxisFormat.NSC
Exemplo n.º 6
0
 def axes_to_spatial_first_order(self, node, graph):
     buf = graph.get_buffer(node.output_names[0])
     if buf.rank() == 4:
         buf.shape = AxisTracker.permute_shape(
             buf.shape, AxisTracker.AxisFormat.NCS_TO_NSC)
         buf.axis_format = AxisTracker.AxisFormat.NSC
         node.op.shape = buf.shape
     elif buf.rank() == 3:
         buf.shape = AxisTracker.permute_shape(
             buf.shape, AxisTracker.AxisFormat.TBF_TO_BTF)
         buf.axis_format = AxisTracker.AxisFormat.BTF
         node.op.shape = buf.shape
Exemplo n.º 7
0
    def axes_to_spatial_first_order(self, node, graph):
        output_buf = graph.get_buffer(node.output_names[0])

        # Permute the constant data if necessary
        if output_buf.axis_format == AxisTracker.AxisFormat.NSC:
            node.op.tensor = numpy.ascontiguousarray(
                numpy.transpose(node.op.tensor,
                                AxisTracker.AxisFormat.NCS_TO_NSC))
        elif output_buf.axis_format == AxisTracker.AxisFormat.BTF:
            node.op.tensor = numpy.ascontiguousarray(
                numpy.transpose(node.op.tensor,
                                AxisTracker.AxisFormat.TBF_TO_BTF))

        AxisTracker.eltwise_to_spatial_first_order(node, graph)
Exemplo n.º 8
0
 def axes_to_spatial_first_order(self, node, graph):
     input_name = node.input_names[0]
     input_buf = graph.get_input_buffers(node)[0]
     output_buf = graph.get_output_buffers(node)[0]
     if input_buf.axis_format == AxisTracker.AxisFormat.NSC:
         # If keep dims = 0 we must permute as it will remove dimensions
         if not node.op.keepdims:
             AxisTracker.inject_implicit_permute(
                 graph, input_name, AxisTracker.AxisFormat.NCS,
                 AxisTracker.AxisFormat.NSC_TO_NCS, [node.op.name])
             output_buf.axis_format = AxisTracker.AxisFormat.NONTRIVIAL
         else:
             AxisTracker.eltwise_to_spatial_first_order(node, graph)
         axis_map = [0, 3, 1, 2]
         node.op.axis = axis_map[node.op.axis]
Exemplo n.º 9
0
 def axes_to_spatial_first_order(self, node, graph):
     input_buf = graph.get_input_buffers(node)[0]
     if input_buf.rank() == 4:
         AxisTracker.image_to_spatial_first_order(node, graph)
     elif input_buf.rank() == 2 or input_buf.rank() == 3:
         if input_buf.rank() == 3:
             # add custom permute for 3D use-case. This input use-case is added for batchnorm-1D
             AxisTracker.enforce_input_format(
                 graph, node.input_names[0],
                 AxisTracker.AxisFormat.NONTRIVIAL, [0, 2, 1])
         output_buf = graph.get_output_buffers(node)[0]
         output_buf.axis_format = AxisTracker.AxisFormat.NONTRIVIAL
     else:
         raise ValueError(
             code_to_message.get_error_message(
                 "ERROR_BATCHNORM_DIM_UNSUPPORTED")(input_buf.rank()))
Exemplo n.º 10
0
 def axes_to_spatial_first_order(self, node, graph):
     input_name = node.input_names[0]
     input_buf = graph.get_input_buffers(node)[0]
     output_buf = graph.get_output_buffers(node)[0]
     # check for trivial cases first, which will end up
     # in removal. Otherwise, just set output order to nontrivial
     if input_buf.axis_format == AxisTracker.AxisFormat.NSC:
         # special case: transforming to NSC, will become noop
         if node.op.order == [0, 2, 3, 1]:
             node.op.order = [0, 1, 2, 3]
             output_buf.axis_format = AxisTracker.AxisFormat.NSC
             return
         else:
             # going to nontrivial
             AxisTracker.inject_implicit_permute(
                 graph, input_name, AxisTracker.AxisFormat.NCS,
                 AxisTracker.AxisFormat.NSC_TO_NCS, [node.op.name])
             output_buf.axis_format = AxisTracker.AxisFormat.NONTRIVIAL
     elif input_buf.axis_format == AxisTracker.AxisFormat.BTF:
         if node.op.order == [0, 2, 3, 1]:
             node.op.order = [0, 1, 2, 3]
             output_buf.axis_format = AxisTracker.AxisFormat.BTF
         else:
             AxisTracker.inject_implicit_permute(
                 graph, input_name, AxisTracker.AxisFormat.TBF,
                 AxisTracker.AxisFormat.TBF_TO_BTF, [node.op.name])
             output_buf.axis_format = AxisTracker.AxisFormat.NONTRIVIAL
     elif input_buf.axis_format == AxisTracker.AxisFormat.NONTRIVIAL:
         if len(node.op.order) == 4:
             output_buf.axis_format = AxisTracker.AxisFormat.NONTRIVIAL
         elif len(node.op.order) > 4:
             raise ValueError(
                 code_to_message.get_error_message(
                     "ERROR_PERMUTE_TOO_MANY_DIMENSIONS")(node.op.order))
         else:
             # nothing to be done
             output_buf.axis_format = AxisTracker.AxisFormat.NONTRIVIAL
     else:
         raise ValueError(
             code_to_message.get_error_message(
                 "ERROR_PERMUTE_UNEXPECTED_INPUT_ORDER")(
                     input_buf.axis_format))
Exemplo n.º 11
0
 def axes_to_spatial_first_order(self, node, graph):
     # NB will probably want to switch to 'eltwise' version when we
     # support axis parameter.
     AxisTracker.feature_to_spatial_first_order(node, graph)
Exemplo n.º 12
0
 def axes_to_spatial_first_order(self, node, graph):
     AxisTracker.time_series_to_spatial_first_order(node, graph)
Exemplo n.º 13
0
 def axes_to_spatial_first_order(self, node, graph):
     node.op.output_shape = AxisTracker.permute_shape(
         node.op.output_shape, AxisTracker.AxisFormat.NCS_TO_NSC)
     AxisTracker.image_to_spatial_first_order(node, graph)
Exemplo n.º 14
0
 def axes_to_spatial_first_order(self, node, graph):
     AxisTracker.eltwise_to_spatial_first_order(node, graph)
Exemplo n.º 15
0
 def axes_to_spatial_first_order(self, node, graph):
     AxisTracker.eltwise_to_spatial_first_order(node, graph)
     buf = graph.get_buffer(node.output_names[0])
     if buf.axis_format == AxisTracker.AxisFormat.NSC:
         axis_map = [0, 3, 1, 2]
         node.op.axis = axis_map[node.op.axis]