Exemple #1
0
    def __init__(self):
        sub_rules = [
            OptimizeRuleGroup([
                InsertTranspose(),
                ReplaceConvolutionByIm2Col(),
                ReplaceDeconvolutionByCol2Im(),
                DecomposeSoftmax(),
                ReplaceLinearBySgemm(),
                MergeSgemmAndElementwiseMul(),
                FixSGEMMTextureShape(optimize_channel_mode=False),
                ConstantFolding(),
                SplitTexture(),
            ]),
            OptimizeRuleGroup([
                InsertChannelModeConversion(),
                SimplifyElementwise(),
                RemoveRedundantOperator(),
                SimplifyChannelModeConversion(),
                FixSGEMMTextureShape(optimize_channel_mode=True),
            ]),
            AttachConcatWorkspace(),
        ]

        if flags.DEBUG:
            sub_rules.append(DumpGraph("cg{count}.dot"))

        super(WebGLOptimizeRule, self).__init__(sub_rules, repeat=False)
Exemple #2
0
    def __init__(self):
        sub_rules = [
            OptimizeRuleGroup([
                InsertTranspose(),
                ReplaceConvolutionByIm2Col(),
                ReplaceDeconvolutionByCol2Im(),
                ReplaceLinearByTensordot(),
                DecomposeSoftmax(),
                FixTensordotTextureShape(),
                MergeTensordotAndElementwiseMul(),
                ConstantFolding(),
                RemoveRedundantOperator(),
                RemoveNoEffectOperator(),
                SplitTexture(),
                UnrollConcat(),
            ]),
            OptimizeRuleGroup([
                InsertTranspose(),
                InsertChannelModeConversion(),
                SimplifyChannelModeConversion(),
                ConstantFolding(),
                RemoveRedundantOperator(),
                RemoveNoEffectOperator(),
            ]),
        ]  # type: List[OptimizeRule]

        if flags.DEBUG:
            sub_rules.append(
                DumpGraph(f"cg_{config.WEBGL_MAX_TEXTURE_SIZE}_{{count}}.dot"))

        super(WebGLOptimizeRule, self).__init__(sub_rules, repeat=False)
    def __init__(self):
        sub_rules = [
            OptimizeRuleGroup([
                InsertTranspose(),
                ReplaceConvolutionByIm2Col(),
                MergeSgemmAndElementwiseMul(),
                ConstantFolding(),
                ReplaceDeconvolutionByCol2Im(),
                MergeSgemmAndElementwiseMul(),
                ConstantFolding(),
                ReplaceLinearBySgemm(),
                MergeSgemmAndElementwiseMul(),
                ConstantFolding(),
                ConcatLSTMInputAndHidden(),
                RemoveRedundantOperator(),
                RemoveNoEffectOperator(),
                UpdateInplaceAttribute()
            ]),
            ElementwiseKernelFusion()
        ]

        if flags.DEBUG:
            sub_rules.append(DumpGraph("cg{count}.dot"))

        super(WebGPUOptimizeRule, self).__init__(sub_rules)
Exemple #4
0
    def __init__(self):
        sub_rules = [
            OptimizeRuleGroup([
                InsertTranspose(),
                ReplaceConvolutionByIm2Col(),
                ReplaceDeconvolutionByCol2Im(),
                ReplaceLinearByTensordot(),
                MergeTensordotAndElementwiseMul(),
                ConstantFolding(),
                UseEigen(),
                UpdateInplaceAttribute()
            ]),
            ElementwiseKernelFusion()
        ]

        if flags.DEBUG:
            sub_rules.append(DumpGraph("cg{count}.dot"))

        super(WebassemblyOptimizeRule, self).__init__(sub_rules)
Exemple #5
0
 def __init__(self):
     super(WebGPUOptimizeRule, self).__init__([
         OptimizeRuleGroup([
             InsertTranspose(),
             ReplaceConvolutionByIm2Col(),
             MergeSgemmAndElementwiseMul(),
             ConstantFolding(),
             ReplaceDeconvolutionByCol2Im(),
             MergeSgemmAndElementwiseMul(),
             ConstantFolding(),
             ReplaceLinearBySgemm(),
             MergeSgemmAndElementwiseMul(),
             ConstantFolding(),
             ConcatLSTMInputAndHidden(),
             RemoveRedundantOperator(),
             RemoveNoEffectOperator(),
             UpdateInplaceAttribute()
         ]),
         ElementwiseKernelFusion()
     ])
Exemple #6
0
    def convert(
        self,
        inputs: Sequence["tf.Tensor"],
        outputs: Sequence["tf.Tensor"],
        order_hints: Optional[Dict[Union["tf.Tensor", "tf.Variable"],
                                   Order]] = None
    ) -> Graph:
        """convert(model, input_orders=None)

        Args:
            inputs (list of `tf.Tensor`): tensorflow input tensors
            outputs (list of `tf.Tensor`): tensorflow output tensors
            order_hints: Order annotations which helps webdnn's optimizer.

        .. admonition:: example

            Convert TensorFlow model.

            .. code::

                import tensorflow as tf
                from webdnn.frontend.tensorflow import TensorFlowConverter

                # y = x @ W + b
                x = tf.placeholder(tf.float32, [None, 784])
                W = tf.Variable(tf.zeros([784, 10]))
                b = tf.Variable(tf.zeros([10]))
                y = tf.nn.softmax(tf.matmul(x, W) + b)

                graph = TensorFlowConverter().convert([x], [y])

        Returns:
            (:class:`~webdnn.graph.graph.Graph`): WebDNN IR Graph
        """

        for tensor in inputs:
            shape = [
                Placeholder() if dim.value is None else dim.value
                for dim in tensor.shape.dims
            ]
            if isinstance(shape[0], Placeholder):
                shape[0] = self._batch_size
            self.set_variable(tensor,
                              Variable(shape, Order([None] * len(shape))))

        ops = _listup_operations(inputs, outputs)
        for op in ops:
            self._convert_operator(op)
            sub_graph = Graph([
                self.get_variable(tf_tensor)
                for tf_tensor in op.inputs if self.has_variable(tf_tensor)
            ], [
                self.get_variable(tf_tensor)
                for tf_tensor in op.outputs if self.has_variable(tf_tensor)
            ])
            old_outputs = list(sub_graph.outputs)

            # Constant folding improves possibility of conversion, because many tensors are used not only for main input variable but also
            # for other parameter like indices of operation, and WebDNN doesn't support dynamic indices operation.
            OptimizeRuleGroup([ConstantFolding()],
                              repeat=True).optimize(sub_graph)

            # After constant folding, it need to replace old variable with new constant variable
            for tf_tensor in op.outputs:
                if not self.has_variable(tf_tensor):
                    # This tensor is not converted (ignored)
                    continue

                old_v = self.get_variable(tf_tensor)
                new_v = sub_graph.outputs[old_outputs.index(old_v)]
                if old_v != new_v:
                    self.set_variable(tf_tensor, new_v, overwrite=True)

        if order_hints:
            for tensor, order in order_hints.items():
                if isinstance(tensor, tf.Variable):
                    tensor = tensor.value()

                variable = self.get_variable(tensor)
                for axis1, axis2 in zip(variable.order.axes, order.axes):
                    axis1.unify(axis2)

        # Remove redundant ReinterpretAxis operators
        graph = Graph([self.get_variable(tensor) for tensor in inputs],
                      [self.get_variable(tensor) for tensor in outputs])
        graph, _ = TensorFlowFrontendOptimizeRule().optimize(graph)

        for v in graph.inputs:
            v.attributes.add(Input(v))

        for v in graph.outputs:
            v.attributes.add(Output(v))

        return graph