Exemple #1
0
def _prepare_gradient_if_op(
        fwd_op, input_names, output_names, then_grad_net, else_grad_net):
    gradient_if_def = caffe2_pb2.OperatorDef()
    gradient_if_def.CopyFrom(fwd_op)
    del gradient_if_def.input[:]
    gradient_if_def.input.extend(input_names)
    del gradient_if_def.output[:]
    gradient_if_def.output.extend(output_names)

    then_net_arg = caffe2_pb2.Argument()
    then_net_arg.name = "then_net"
    then_net_arg.n.CopyFrom(then_grad_net)
    gradient_args = [then_net_arg]
    if else_grad_net:
        else_net_arg = caffe2_pb2.Argument()
        else_net_arg.name = "else_net"
        else_net_arg.n.CopyFrom(else_grad_net)
        gradient_args.append(else_net_arg)

    del gradient_if_def.arg[:]
    gradient_if_def.arg.extend(gradient_args)
    if gradient_if_def.name:
        gradient_if_def.name += "_grad"
    del gradient_if_def.control_input[:]
    gradient_if_def.is_gradient_op = True
    return gradient_if_def
Exemple #2
0
    def XavierFill(self, c2_op, inputs):
        """
        Creates a constant tensor with xavier fill.
        Implementation is the same as in caffe2. Thera are other implementations in caffe or TF.
        Xavier fill is uniform fill with variance (range) depending on number of
        input/output neurons.

        Arguments:
            c2_op: OperatorDef object, the caffe2 node to convert.
            inputs: List of ngraph Ops as inputs to this node.

        Returns:
            A ngraph Op corresponding to the caffe2 node.

        Inputs to c2_op:
            value, dtype, shape, name
        """

        # parse protobuf arguments
        args = {arg.name: arg for arg in c2_op.arg}

        # calculate scale like in caffe2
        input_neurons = np.prod(args["shape"].ints)
        scale = np.sqrt(3. / input_neurons)

        # add arguments for uniform fill to list
        arg1, arg2 = c2proto.Argument(), c2proto.Argument()
        arg1.name, arg2.name = "max", "min"
        arg1.f, arg2.f = scale, -scale

        c2_op.arg.extend([arg1, arg2])
        ng_op = self.UniformFill(c2_op=c2_op, inputs=inputs)

        return ng_op
def _prepare_gradient_do_op(fwd_op, fwd_net, grad_ops, inputs, outputs,
                            blob_bindings, saved_fwd_blobs,
                            workspace_blob_name):
    gradient_net_def = caffe2_pb2.NetDef()
    gradient_net_def.CopyFrom(fwd_net)
    if gradient_net_def.name:
        gradient_net_def.name += "_grad"
    del gradient_net_def.op[:]
    gradient_net_def.op.extend(grad_ops)
    del gradient_net_def.external_input[:]
    del gradient_net_def.external_output[:]

    gradient_do_def = caffe2_pb2.OperatorDef()
    gradient_do_def.CopyFrom(fwd_op)
    if gradient_do_def.name and len(gradient_do_def.name) > 0:
        gradient_do_def.name += "_grad"

    del gradient_do_def.input[:]
    gradient_do_def.input.extend(inputs)
    # workspace pointer blob
    gradient_do_def.input.append(workspace_blob_name)
    del gradient_do_def.output[:]
    gradient_do_def.output.extend(outputs)
    # workspace pointer blob
    gradient_do_def.output.append(workspace_blob_name)

    net_arg = caffe2_pb2.Argument()
    net_arg.name = "net"
    net_arg.n.CopyFrom(gradient_net_def)

    ordered_new_outer_names = inputs + outputs
    inner_blobs = blob_bindings.keys()
    new_outer_blobs_idx = [
        ordered_new_outer_names.index(blob_bindings[b]) for b in inner_blobs
    ]

    inner_blobs_arg = caffe2_pb2.Argument()
    inner_blobs_arg.name = "inner_blobs"
    inner_blobs_arg.strings.extend([b.encode('utf-8') for b in inner_blobs])

    outer_blobs_idx_arg = caffe2_pb2.Argument()
    outer_blobs_idx_arg.name = "outer_blobs_idx"
    outer_blobs_idx_arg.ints.extend(new_outer_blobs_idx)

    saved_blobs_arg = caffe2_pb2.Argument()
    saved_blobs_arg.name = "saved_fwd_blobs"
    saved_blobs_arg.strings.extend(
        [b.encode('utf-8') for b in saved_fwd_blobs])

    del gradient_do_def.arg[:]
    gradient_do_def.arg.extend(
        [net_arg, inner_blobs_arg, outer_blobs_idx_arg, saved_blobs_arg])
    del gradient_do_def.control_input[:]

    gradient_do_def.is_gradient_op = True

    return gradient_do_def
Exemple #4
0
def MakeArgument(key, value):
    """Makes an argument based on the value type."""
    argument = caffe2_pb2.Argument()
    argument.name = key
    if type(value) is float:
        argument.f = value
    elif type(value) is int or type(value) is bool:
        # We make a relaxation that a boolean variable will also be stored as int.
        argument.i = value
    elif type(value) is str:
        argument.s = value
    elif isinstance(value, Message):
        argument.s = value.SerializeToString()
    elif all(type(v) is float for v in value):
        argument.floats.extend(value)
    elif all(type(v) is int for v in value):
        argument.ints.extend(value)
    elif all(type(v) is str for v in value):
        argument.strings.extend(value)
    elif all(isinstance(v, Message) for v in value):
        argument.strings.extend([v.SerializeToString() for v in values])
    else:
        raise ValueError(
            "Unknown argument type: key=%s value=%s, value type=%s" %
            (key, str(value), str(type(value))))
    return argument
Exemple #5
0
def _AdjustDims(op_def, arg_map, pads, dim1, dim2):
    n1, c1, h1, w1 = dim1
    n2, c2, h2, w2 = dim2
    assert(n1 == n2)
    assert(c1 == c2)
    is_pad = 'pad' in arg_map
    if h1 != h2 or w1 != w2:
        if h1 == h2 + 1:
            pads['pad_b'] += 1
        elif h1 != h2:
            raise Exception("Unexpected dimensions for height:", h1, h2)
        if w1 == w2 + 1:
            pads['pad_r'] += 1
        elif w1 != w2:
            raise Exception("Unexpected dimensions for width:", w1, w2)
        if is_pad:
            op_def.arg.remove(arg_map['pad'])
            args = []
            for name in pads.keys():
                arg = caffe2_pb2.Argument()
                arg.name = name
                arg.i = pads[name]
                args.append(arg)
            op_def.arg.extend(args)
        else:
            for name in pads.keys():
                arg_map[name].i = pads[name]
Exemple #6
0
    def test_huffman_tree_hierarchy(self):
        workspace.GlobalInit(['caffe2'])
        labelSet = range(1, 7)
        counts = [2, 4, 10, 15, 25, 40]
        labels = sum([[l] * c for (l, c) in zip(labelSet, counts)], [])
        Y = np.array(labels).astype(np.int64)
        workspace.FeedBlob("labels", Y)
        arg = caffe2_pb2.Argument()
        arg.name = 'num_classes'
        arg.i = 6
        op = core.CreateOperator(
            'HuffmanTreeHierarchy',
            ['labels'],
            ['huffman_tree'],
            'HuffmanTreeHierarchy',
            arg=[arg])
        workspace.RunOperatorOnce(op)
        huffmanTreePaths = workspace.FetchBlob('huffman_tree')
        treePathOutput = hsm_pb2.HierarchyProto()
        treePathOutput.ParseFromString(huffmanTreePaths[0])

        def checkPath(label, path, indices, code):
            self.assertEqual(path.word_id, label)
            self.assertEqual(len(path.path_nodes), len(code))
            self.assertEqual(len(path.path_nodes), len(code))
            for path_node, index, target in \
                    zip(path.path_nodes, indices, code):
                self.assertEqual(path_node.index, index)
                self.assertEqual(path_node.target, target)
        checkPath(1, treePathOutput.paths[0], [4, 3, 2, 1, 0], [1, 1, 1, 0, 0])
        checkPath(2, treePathOutput.paths[1], [4, 3, 2, 1, 0], [1, 1, 1, 0, 1])
        checkPath(3, treePathOutput.paths[2], [4, 3, 2, 1], [1, 1, 1, 1])
        checkPath(4, treePathOutput.paths[3], [4, 3, 2], [1, 1, 0])
        checkPath(5, treePathOutput.paths[4], [4, 3], [1, 0])
        checkPath(6, treePathOutput.paths[5], [4], [0])
Exemple #7
0
def MakeArgument(key, value):
    """Makes an argument based on the value type."""
    argument = caffe2_pb2.Argument()
    argument.name = key
    if type(value) is float:
        argument.f = value
    elif type(value) is int or type(value) is bool or type(value) is long:
        # We make a relaxation that a boolean variable will also be stored as
        # int.
        argument.i = value
    elif isinstance(value, basestring):
        argument.s = (value if type(value) is bytes else value.encode('utf-8'))
    elif isinstance(value, Message):
        argument.s = value.SerializeToString()
    elif all(type(v) is float for v in value):
        argument.floats.extend(value)
    elif all(any(type(v) is t for t in [int, bool, long]) for v in value):
        argument.ints.extend(value)
    elif all(isinstance(v, basestring) for v in value):
        argument.strings.extend([(v if type(v) is bytes else v.encode('utf-8'))
                                 for v in value])
    elif all(isinstance(v, Message) for v in value):
        argument.strings.extend([v.SerializeToString() for v in value])
    else:
        raise ValueError(
            "Unknown argument type: key=%s value=%s, value type=%s" %
            (key, str(value), str(type(value))))
    return argument
Exemple #8
0
def apply_recurrent_blob_assignments(op, blob_assignments, canonical_name):
    log.debug("Applying assignments to recurrent op: {}".format(op.type))

    # Apply on alias_dst
    alias_dst_args = [a for a in op.arg if a.name.endswith("alias_dst")]
    for alias_dst in alias_dst_args:
        for i, blob in enumerate(alias_dst.strings):
            alias_dst.strings[i] = canonical_name(blob.decode()).encode()

    # Apply on link_external
    link_external_args = [
        a for a in op.arg if a.name.endswith("link_external")
    ]
    for link_external in link_external_args:
        for i, blob in enumerate(link_external.strings):
            link_external.strings[i] = canonical_name(blob.decode()).encode()

    # Recurse into step nets
    step_args = [a for a in op.arg if a.name.endswith("step_net")]
    for step_arg in step_args:
        apply_assignments(step_arg.n, blob_assignments)
        for i, einp in enumerate(step_arg.n.external_input):
            if einp in blob_assignments:
                step_arg.n.external_input[i] = canonical_name(einp)

    # Store renamings
    for blob, renamed in viewitems(blob_assignments):
        if blob in list(op.input) + list(op.output):
            a = caffe2_pb2.Argument()
            a.name = blob + ".rename"
            a.s = str(renamed).encode("ascii")
            op.arg.extend([a])
Exemple #9
0
def _gen_grad_zero_init_ops(init_grad_map, grad_map, grad_output_names):
    grad_init_ops = []
    for grad_output in grad_output_names:
        # get the corresponding output name blob and use it in ConstantFill
        # so that grad_output has the same shape
        output_name = None
        for o, g in grad_map.items():
            if g == grad_output:
                output_name = o
                break
        assert output_name, "Unknown gradient output " + grad_output

        grad_init_op = None
        # make sure that we do not overwrite existing gradients with zeros
        if output_name in init_grad_map:
            init_grad_name = init_grad_map[output_name]
            # in case we use a different gradient blob name, copy gradient
            if init_grad_name != grad_output:
                grad_init_op = caffe2_pb2.OperatorDef()
                grad_init_op.type = "Copy"
                grad_init_op.input.extend([str(init_grad_name)])
                grad_init_op.output.extend([str(grad_output)])
        else:
            grad_init_op = caffe2_pb2.OperatorDef()
            grad_init_op.type = "ConstantFill"
            grad_init_op.input.extend([output_name])
            grad_init_op.output.extend([grad_output])
            value_arg = caffe2_pb2.Argument()
            value_arg.name = "value"
            value_arg.f = 0.0
            grad_init_op.arg.extend([value_arg])

        if grad_init_op:
            grad_init_ops.append(grad_init_op)
    return grad_init_ops
Exemple #10
0
def MakeArgument(key, value):
    """Makes an argument based on the value type."""
    argument = caffe2_pb2.Argument()
    argument.name = key
    iterable = isinstance(value, collections.Iterable)

    # Fast tracking common use case where a float32 array of tensor parameters
    # needs to be serialized.  The entire array is guaranteed to have the same
    # dtype, so no per-element checking necessary and no need to convert each
    # element separately.
    if isinstance(value, np.ndarray) and value.dtype.type is np.float32:
        argument.floats.extend(value.flatten().tolist())
        return argument

    if isinstance(value, np.ndarray):
        value = value.flatten().tolist()
    elif isinstance(value, np.generic):
        # convert numpy scalar to native python type
        value = np.asscalar(value)

    if type(value) is float:
        argument.f = value
    elif type(value) in integer_types or type(value) is bool:
        # We make a relaxation that a boolean variable will also be stored as
        # int.
        argument.i = value
    elif isinstance(value, binary_type):
        argument.s = value
    elif isinstance(value, text_type):
        argument.s = value.encode('utf-8')
    elif isinstance(value, Message):
        argument.s = value.SerializeToString()
    elif iterable and all(type(v) in [float, np.float_] for v in value):
        argument.floats.extend(v.item() if type(v) is np.float_ else v
                               for v in value)
    elif iterable and all(
            type(v) in integer_types or type(v) in [bool, np.int_]
            for v in value):
        argument.ints.extend(v.item() if type(v) is np.int_ else v
                             for v in value)
    elif iterable and all(
            isinstance(v, binary_type) or isinstance(v, text_type)
            for v in value):
        argument.strings.extend(
            v.encode('utf-8') if isinstance(v, text_type) else v
            for v in value)
    elif iterable and all(isinstance(v, Message) for v in value):
        argument.strings.extend(v.SerializeToString() for v in value)
    else:
        if iterable:
            raise ValueError(
                "Unknown iterable argument type: key={} value={}, value "
                "type={}[{}]".format(key, value, type(value),
                                     set(type(v) for v in value)))
        else:
            raise ValueError(
                "Unknown argument type: key={} value={}, value type={}".format(
                    key, value, type(value)))
    return argument
Exemple #11
0
def PrintNetDef(model, net="predict_net"):
    """print weight or predict"""
    if net not in [
            "predict_net", "init_net", "predict_net_int8", "onnx_model"
    ]:
        logging.error("Unsupported net def file {}".format(net))
        return
    dummy = 0
    model_info = m.GetModelInfo(model)
    with open(model_info[net]) as p:
        if model_info["model_type"] == "prototext" or model_info[net].split(
                '.')[-1] == "pbtxt":
            import google.protobuf.text_format as ptxt
            predict_def = ptxt.Parse(p.read(), caffe2_pb2.NetDef())
        else:
            predict_def = caffe2_pb2.NetDef()
            predict_def.ParseFromString(p.read())
        if dummy == 1 and net != "predict_net":
            for op in predict_def.op:
                op.type = 'ConstantFill'
                if op.output[0] != 'data':
                    arg_new = caffe2_pb2.Argument()
                    arg_new.name = 'value'
                    arg_new.f = 0.01
                    for i, arg in enumerate(op.arg[::-1]):
                        if arg.name == 'shape':
                            continue
                        else:
                            del op.arg[len(op.arg) - 1 - i]
                    op.arg.extend([arg_new])
                else:
                    arg_new = caffe2_pb2.Argument()
                    arg_new.name = 'shape'
                    arg_new.ints.extend([1])
                    for i, arg in enumerate(op.arg[::-1]):
                        if arg.name == 'shape':
                            del op.arg[len(op.arg) - 1 - i]
                    op.arg.extend([arg_new])
        logging.critical("The {} definition of model {}:\n{}".format(
            net, model_info["model_name"], predict_def))
    if dummy == 1:
        with open("{}.pbtxt".format(model_info["model_name"]), "w") as fid:
            fid.write(str(predict_def))
        with open("{0}.pb".format(model_info["model_name"]), "w") as fid:
            fid.write(predict_def.SerializeToString())
Exemple #12
0
def _prepare_gradient_while_ops(
        fwd_op, input_names, output_names, loop_grad_net, workspace_blob,
        init_grad_map, loop_grad_map):
    gradient_while_def = caffe2_pb2.OperatorDef()
    gradient_while_def.CopyFrom(fwd_op)
    if gradient_while_def.name:
        gradient_while_def.name += "_grad"

    loop_net_arg = caffe2_pb2.Argument()
    loop_net_arg.name = "loop_net"
    loop_net_arg.n.CopyFrom(loop_grad_net)

    cond_net_arg = caffe2_pb2.Argument()
    cond_net_arg.name = "cond_net"
    from caffe2.python.core import Net, BlobReference
    # Construct condition net - check that there're still forward workspaces
    # left using HasScope op
    cond_net = Net('gradient_loop_cond_net')
    cond_init_net = Net('gradient_loop_cond_net_init')
    cond_blob = cond_net.NextScopedBlob(cond_net.Name() + '/cond')
    cond_init_net.HasScope(workspace_blob, cond_blob)
    cond_net.HasScope(workspace_blob, cond_blob)
    for blob, init_grad_blob in init_grad_map.items():
        blob_name = str(blob)
        init_grad_blob_name = str(init_grad_blob)
        if blob_name in loop_grad_map and \
                loop_grad_map[blob_name] != init_grad_blob_name:
            cond_net.Copy(
                BlobReference(loop_grad_map[blob_name]), init_grad_blob)
            cond_init_net.Copy(
                init_grad_blob, BlobReference(loop_grad_map[blob_name]))
    cond_net_arg.n.CopyFrom(cond_net.Proto())

    del gradient_while_def.arg[:]
    gradient_while_def.arg.extend([loop_net_arg, cond_net_arg])

    del gradient_while_def.control_input[:]
    del gradient_while_def.input[:]
    gradient_while_def.input.extend(
        [str(cond_blob).encode('utf-8')] + list(input_names))
    del gradient_while_def.output[:]
    gradient_while_def.output.extend(output_names)
    gradient_while_def.is_gradient_op = True
    return [o for o in cond_init_net.Proto().op] + [gradient_while_def]
Exemple #13
0
    def test_fc(self, n, m, k, multi_dim, dtype, engine, gc, dc):
        if dtype == np.float16:
            # fp16 only supported with CUDA
            assume(gc.device_type == caffe2_pb2.CUDA)
            dc = [d for d in dc if d.device_type == caffe2_pb2.CUDA]

        if engine == 'TENSORCORE':
            # TensorCore only makes sense with CUDA
            assume(gc.device_type == caffe2_pb2.CUDA)
            # ensures TensorCore kernels can be called
            m *= 8
            k *= 8
            n *= 8

        X = np.random.rand(m, k).astype(dtype) - 0.5
        if multi_dim:
            W = np.random.rand(n, k, 1, 1).astype(dtype) - 0.5
        else:
            W = np.random.rand(n, k).astype(dtype) - 0.5
        b = np.random.rand(n).astype(dtype) - 0.5

        def fc_op(X, W, b):
            return [np.dot(X, W.reshape(n, k).transpose()) + b.reshape(n)]

        op = core.CreateOperator(
            'FC',
            ['X', 'W', 'b'],
            'out',
            engine=engine,
        )

        if dtype == np.float16 and gc.device_type == caffe2_pb2.CUDA:
            a = caffe2_pb2.Argument()
            a.i = 1
            a.name = "float16_compute"
            op.arg.extend([a])

        # Check against numpy reference
        self.assertReferenceChecks(
            device_option=gc,
            op=op,
            inputs=[X, W, b],
            reference=fc_op,
        )
        # Check over multiple devices
        self.assertDeviceChecks(dc, op, [X, W, b], [0])

        # Gradient checks
        threshold = 0.5 if dtype == np.float16 else 0.005
        stepsize = 0.5 if dtype == np.float16 else 0.05
        self.assertGradientChecks(gc, op, [X, W, b], 0, [0],
                                  threshold=threshold, stepsize=stepsize)
        self.assertGradientChecks(gc, op, [X, W, b], 1, [0],
                                  threshold=threshold, stepsize=stepsize)
        self.assertGradientChecks(gc, op, [X, W, b], 2, [0],
                                  threshold=threshold, stepsize=stepsize)
Exemple #14
0
    def testOperatorDef2NodeProto(self):
        op_def = caffe2_pb2.OperatorDef()
        op_def.input.extend(["A", "B", "C"])
        op_def.output.extend(["X", "Y"])
        op_def.name = "TestOpName"
        op_def.type = "TestOp"
        arg1 = caffe2_pb2.Argument()
        arg1.name = "TestArg1"
        arg1.i = 1
        arg2 = caffe2_pb2.Argument()
        arg2.name = "TestArg2"
        arg1.s = "TestInfo".encode("utf-8")
        op_def.arg.extend([arg1, arg2])
        op_def.device_option.CopyFrom(caffe2_pb2.DeviceOption())
        op_def.engine = "TestEngine".encode("utf-8")
        op_def.control_input.extend(["input1", "input2"])
        op_def.is_gradient_op = True
        op_def.debug_info = "TestDebugInfo"

        node = convert.OperatorDefToNodeProto(op_def)

        self.assertEqual(node.input, op_def.input)
        self.assertEqual(node.output, op_def.output)
        self.assertEqual(node.name, op_def.name)
        self.assertEqual(node.op_type, op_def.type)
        self.assertEqual(node.attribute[0].name, op_def.arg[0].name)
        self.assertEqual(node.attribute[1].name, op_def.arg[1].name)
        self.assertEqual(node.device_option, op_def.device_option)
        node_engine = [
            a.s.decode("utf-8") for a in node.annotations if a.name == "engine"
        ][0]
        self.assertEqual(node_engine, op_def.engine)
        node_control_input = [
            a.strings for a in node.annotations if a.name == "control_input"
        ][0]
        self.assertEqual(len(node_control_input), len(op_def.control_input))
        for x, y in zip(node_control_input, op_def.control_input):
            self.assertEqual(x.decode("utf-8"), y)
        self.assertEqual(node.doc_string, op_def.debug_info)
        node_is_gradient_op = [
            a.i for a in node.annotations if a.name == "is_gradient_op"
        ][0]
        self.assertEqual(node_is_gradient_op, int(op_def.is_gradient_op))
Exemple #15
0
def AttributeProtoToArgument(attr):
    serialized_attr = None
    if hasattr(attr, 'SerializeToString') and callable(attr.SerializeToString):
        serialized_attr = attr.SerializeToString()
    elif isinstance(attr, bytes):
        serialized_attr = attr
    else:
        raise ValueError('No SerializeToString method is detected. '
                         'neither attr is bytes.\ntype is {}'.format(
                             type(attr)))
    arg = caffe2_pb2.Argument()
    arg.ParseFromString(C.attribute_proto_to_argument(serialized_attr))
    return arg
Exemple #16
0
def MakeArgument(key, value):
    """Makes an argument based on the value type."""
    argument = caffe2_pb2.Argument()
    argument.name = key
    iterable = isinstance(value, collections.Iterable)

    if isinstance(value, np.ndarray):
        value = value.flatten().tolist()
    elif isinstance(value, np.generic):
        # convert numpy scalar to native python type
        value = np.asscalar(value)

    if type(value) is float:
        argument.f = value
    elif type(value) in integer_types or type(value) is bool:
        # We make a relaxation that a boolean variable will also be stored as
        # int.
        argument.i = value
    elif isinstance(value, binary_type):
        argument.s = value
    elif isinstance(value, text_type):
        argument.s = value.encode('utf-8')
    elif isinstance(value, Message):
        argument.s = value.SerializeToString()
    elif iterable and all(type(v) in [float, np.float_] for v in value):
        argument.floats.extend(v.item() if type(v) is np.float_ else v
                               for v in value)
    elif iterable and all(
            type(v) in integer_types or type(v) in [bool, np.int_]
            for v in value):
        argument.ints.extend(v.item() if type(v) is np.int_ else v
                             for v in value)
    elif iterable and all(
            isinstance(v, binary_type) or isinstance(v, text_type)
            for v in value):
        argument.strings.extend(
            v.encode('utf-8') if isinstance(v, text_type) else v
            for v in value)
    elif iterable and all(isinstance(v, Message) for v in value):
        argument.strings.extend(v.SerializeToString() for v in value)
    else:
        if iterable:
            raise ValueError(
                "Unknown iterable argument type: key={} value={}, value "
                "type={}[{}]".format(key, value, type(value),
                                     set(type(v) for v in value)))
        else:
            raise ValueError(
                "Unknown argument type: key={} value={}, value type={}".format(
                    key, value, type(value)))
    return argument
Exemple #17
0
def apply_recurrent_blob_assignments(op, blob_assignments, canonical_name):
    log.debug("Applying assignments to recurrent op: {}".format(op.type))
    import google.protobuf.text_format as protobuftx
    step_args = [a for a in op.arg if a.name.endswith("step_net")]
    for step_arg in step_args:
        step_proto = caffe2_pb2.NetDef()
        protobuftx.Merge(step_arg.s.decode("ascii"), step_proto)
        apply_assignments(step_proto, blob_assignments)
        for i, einp in enumerate(step_proto.external_input):
            if einp in blob_assignments:
                step_proto.external_input[i] = canonical_name(einp)
        step_arg.s = str(step_proto).encode("ascii")
    # Store renamings
    for blob, renamed in viewitems(blob_assignments):
        if blob in list(op.input) + list(op.output):
            a = caffe2_pb2.Argument()
            a.name = blob + ".rename"
            a.s = str(renamed).encode("ascii")
            op.arg.extend([a])
Exemple #18
0
def _gen_grad_zero_init_ops(grad_map, grad_output_names):
    grad_zero_init_ops = []
    for grad_output in grad_output_names:
        # get the corresponding output name blob and use it in ConstantFill
        # so that grad_output has the same shape
        output_name = None
        for o, g in grad_map.items():
            if g == grad_output:
                output_name = o
                break
        assert output_name, "Unknown gradient output " + grad_output
        grad_zero_init_op = caffe2_pb2.OperatorDef()
        grad_zero_init_op.type = "ConstantFill"
        grad_zero_init_op.input.extend([output_name])
        grad_zero_init_op.output.extend([grad_output])
        value_arg = caffe2_pb2.Argument()
        value_arg.name = "value"
        value_arg.f = 0.0
        grad_zero_init_op.arg.extend([value_arg])
        grad_zero_init_ops.append(grad_zero_init_op)
    return grad_zero_init_ops
Exemple #19
0
    def test_huffman_tree_hierarchy(self):
        workspace.GlobalInit(['caffe2'])
        labelSet = list(range(0, 6))
        counts = [1, 2, 3, 4, 5, 6]
        labels = sum([[l] * c for (l, c) in zip(labelSet, counts)], [])
        Y = np.array(labels).astype(np.int64)
        workspace.FeedBlob("labels", Y)
        arg = caffe2_pb2.Argument()
        arg.name = 'num_classes'
        arg.i = 6
        op = core.CreateOperator(
            'HuffmanTreeHierarchy',
            ['labels'],
            ['huffman_tree'],
            'HuffmanTreeHierarchy',
            arg=[arg])
        workspace.RunOperatorOnce(op)
        huffmanTreeOutput = workspace.FetchBlob('huffman_tree')
        treeOutput = hsm_pb2.TreeProto()
        treeOutput.ParseFromString(huffmanTreeOutput[0])
        treePathOutput = hsmu.create_hierarchy(treeOutput)

        label_to_path = {}
        for path in treePathOutput.paths:
            label_to_path[path.word_id] = path

        def checkPath(label, indices, code):
            path = label_to_path[label]
            self.assertEqual(len(path.path_nodes), len(code))
            self.assertEqual(len(path.path_nodes), len(code))
            for path_node, index, target in \
                    zip(path.path_nodes, indices, code):
                self.assertEqual(path_node.index, index)
                self.assertEqual(path_node.target, target)
        checkPath(0, [0, 4, 6, 8], [1, 0, 0, 0])
        checkPath(1, [0, 4, 6, 8], [1, 0, 0, 1])
        checkPath(2, [0, 4, 6], [1, 0, 1])
        checkPath(3, [0, 2], [0, 0])
        checkPath(4, [0, 2], [0, 1])
        checkPath(5, [0, 4], [1, 1])
Exemple #20
0
    def testArgument2AttributeProto(self):
        arg_f = caffe2_pb2.Argument()
        arg_f.name = "TestArgF"
        arg_f.f = 10.0
        attr_f = convert.ArgumentToAttributeProto(arg_f)
        self.assertEqual(attr_f.name, arg_f.name)
        self.assertEqual(attr_f.f, arg_f.f)

        arg_i = caffe2_pb2.Argument()
        arg_i.name = "TestArgI"
        arg_i.i = 100
        attr_i = convert.ArgumentToAttributeProto(arg_i)
        self.assertEqual(attr_i.name, arg_i.name)
        self.assertEqual(attr_i.i, arg_i.i)

        arg_s = caffe2_pb2.Argument()
        arg_s.name = "TestArgS"
        arg_s.s = "TestS".encode("utf-8")
        attr_s = convert.ArgumentToAttributeProto(arg_s)
        self.assertEqual(attr_s.name, arg_s.name)
        self.assertEqual(attr_s.s, arg_s.s)

        # TODO: test net arg

        arg_floats = caffe2_pb2.Argument()
        arg_floats.name = "TestArgFloats"
        arg_floats.floats.extend([10.0, 11.0, 12.0])
        attr_floats = convert.ArgumentToAttributeProto(arg_floats)
        self.assertEqual(attr_floats.name, arg_floats.name)
        self.assertEqual(attr_floats.floats, arg_floats.floats)

        arg_ints = caffe2_pb2.Argument()
        arg_ints.name = "TestArgInts"
        arg_ints.ints.extend([100, 101, 102])
        attr_ints = convert.ArgumentToAttributeProto(arg_ints)
        self.assertEqual(attr_ints.name, arg_ints.name)
        self.assertEqual(attr_ints.ints, arg_ints.ints)

        arg_strings = caffe2_pb2.Argument()
        arg_strings.name = "TestArgStrings"
        arg_strings.strings.extend([
            "TestStrings1".encode("utf-8"),
            "TestStrings2".encode("utf-8"),
        ])
        attr_strings = convert.ArgumentToAttributeProto(arg_strings)
        self.assertEqual(attr_strings.name, arg_strings.name)
        self.assertEqual(attr_strings.strings, arg_strings.strings)
Exemple #21
0
def MakeArgument(key, value):
    """Makes an argument based on the value type."""
    argument = caffe2_pb2.Argument()
    argument.name = key
    iterable = isinstance(value, collections.Iterable)

    if isinstance(value, np.ndarray):
        value = value.flatten().tolist()
    elif isinstance(value, np.generic):
        # convert numpy scalar to native python type
        value = np.asscalar(value)

    if type(value) is float:
        argument.f = value
    elif type(value) is int or type(value) is bool or type(value) is long:
        # We make a relaxation that a boolean variable will also be stored as
        # int.
        argument.i = value
    elif isinstance(value, basestring):
        argument.s = (value if type(value) is bytes else value.encode('utf-8'))
    elif isinstance(value, Message):
        argument.s = value.SerializeToString()
    elif iterable and all(type(v) in [float, np.float_] for v in value):
        argument.floats.extend(value)
    elif iterable and all(
            type(v) in [int, bool, long, np.int_] for v in value):
        argument.ints.extend(value)
    elif iterable and all(isinstance(v, basestring) for v in value):
        argument.strings.extend([(v if type(v) is bytes else v.encode('utf-8'))
                                 for v in value])
    elif iterable and all(isinstance(v, Message) for v in value):
        argument.strings.extend([v.SerializeToString() for v in value])
    else:
        raise ValueError(
            "Unknown argument type: key=%s value=%s, value type=%s" %
            (key, str(value), str(type(value))))
    return argument
Exemple #22
0
 def _onnx_arg_to_caffe2_arg(cls, op_name, onnx_arg):
     c2_arg = caffe2_pb2.Argument()
     if op_name in cls._renamed_attrs and onnx_arg.name in cls._renamed_attrs[
             op_name]:
         # Handle renamed attributes
         c2_arg.name = cls._renamed_attrs[op_name][onnx_arg.name]
     else:
         c2_arg.name = onnx_arg.name
     if onnx_arg.HasField('f'):
         c2_arg.f = onnx_arg.f
     elif onnx_arg.HasField('i'):
         c2_arg.i = onnx_arg.i
     elif onnx_arg.HasField('s'):
         c2_arg.s = onnx_arg.s
     elif len(onnx_arg.floats):
         c2_arg.floats.extend(onnx_arg.floats)
     elif len(onnx_arg.ints):
         c2_arg.ints.extend(onnx_arg.ints)
     elif len(onnx_arg.strings):
         c2_arg.strings.extend(onnx_arg.strings)
     elif onnx_arg.HasField('graphs'):
         raise NotImplementedError(
             "Caffe2 backend does not support sub graph yet.")
     return c2_arg
Exemple #23
0
def MakeArgument(key, value):
  """Makes an argument based on the value type."""
  argument = caffe2_pb2.Argument()
  argument.name = key
  if type(value) is float:
    argument.f = value
  elif type(value) is int:
    argument.i = value
  elif type(value) is str:
    argument.s = value
  elif type(value) is Message:
    argument.s = value.SerializeToString()
  elif all(type(v) is float for v in value):
    argument.floats.extend(value)
  elif all(type(v) is int for v in value):
    argument.ints.extend(value)
  elif all(type(v) is str for v in value):
    argument.strings.extend(value)
  elif all(type(v) is Message for v in value):
    argument.strings.extend([v.SerializeToString() for v in values])
  else:
    raise ValueError("Unknown argument type: key=%s value=%s, value type=%s" %
                     (key, str(value), str(type(value))))
  return argument
Exemple #24
0
    def _run_test(self, n, m, k, transposed, multi_dim, dtype, engine, gc, dc):
        if dtype == np.float16:
            # fp16 only supported with CUDA/HIP
            assume(core.IsGPUDeviceType(gc.device_type))
            dc = [d for d in dc if core.IsGPUDeviceType(d.device_type)]

        if engine == 'TENSORCORE':
            # TensorCore only makes sense with CUDA
            assume(gc.device_type == caffe2_pb2.CUDA)
            # ensures TensorCore kernels can be called
            m *= 8
            k *= 8
            n *= 8

        X = np.random.rand(m, k).astype(dtype) - 0.5
        if multi_dim:
            if transposed:
                W = np.random.rand(k, n, 1, 1).astype(dtype) - 0.5
            else:
                W = np.random.rand(n, k, 1, 1).astype(dtype) - 0.5
        else:
            if transposed:
                W = np.random.rand(k, n).astype(dtype) - 0.5
            else:
                W = np.random.rand(n, k).astype(dtype) - 0.5
        b = np.random.rand(n).astype(dtype) - 0.5

        def fc_op(X, W, b):
            return [np.dot(X, W.reshape(n, k).transpose()) + b.reshape(n)]

        def fc_transposed_op(X, W, b):
            return [np.dot(X, W.reshape(k, n)) + b.reshape(n)]

        op = core.CreateOperator(
            'FCTransposed' if transposed else 'FC',
            ['X', 'W', 'b'],
            'out',
            engine=engine,
        )

        if dtype == np.float16 and core.IsGPUDeviceType(gc.device_type):
            a = caffe2_pb2.Argument()
            a.i = 1
            a.name = "float16_compute"
            op.arg.extend([a])

        # Check against numpy reference
        # ReferenceChecks is flaky, Relaxing to 1e-3.
        threshold = 1e-3
        self.assertReferenceChecks(
            device_option=gc,
            op=op,
            inputs=[X, W, b],
            reference=fc_transposed_op if transposed else fc_op,
            threshold=threshold)
        # Check over multiple devices
        self.assertDeviceChecks(dc, op, [X, W, b], [0])

        # Gradient checks
        threshold = 0.5 if dtype == np.float16 else 0.005
        stepsize = 0.5 if dtype == np.float16 else 0.05
        for i in range(3):
            self.assertGradientChecks(gc,
                                      op, [X, W, b],
                                      i, [0],
                                      threshold=threshold,
                                      stepsize=stepsize)
Exemple #25
0
# structure:
# node5: [0, 2, ["node4", "node3"]] # offset, length, "node4, node3"
# node4: [2, 2, ["node1", "node2"]]
# node1: [4, 3, [0, 1 ,2]]
# node2: [7, 2, [3, 4]
# node3: [9, 4, [5, 6, 7, 8]
struct = [[0, 2, ["node4", "node3"], "node5"],
            [2, 2, ["node1", "node2"], "node4"],
            [4, 3, [0, 1, 2], "node1"],
            [7, 2, [3, 4], "node2"],
            [9, 4, [5, 6, 7, 8], "node3"]]

# Internal util to translate input tree to list of (word_id,path). serialized
# hierarchy is passed into the operator_def as a string argument,
hierarchy_proto = hsmu.create_hierarchy(tree)
arg = caffe2_pb2.Argument()
arg.name = "hierarchy"
arg.s = hierarchy_proto.SerializeToString()

beam = 5
args_search = []
arg_search = caffe2_pb2.Argument()
arg_search.name = "tree"
arg_search.s = tree.SerializeToString()
args_search.append(arg_search)
arg_search = caffe2_pb2.Argument()
arg_search.name = "beam"
arg_search.f = beam
args_search.append(arg_search)

 def add_arg(s, v):
     a = caffe2_pb2.Argument()
     a.name = "rnn_executor." + s
     a.i = v
     rnn_op.arg.extend([a])
Exemple #27
0
#        *    5,6,7,8
#       / \
#  0,1,2   3,4
tree = hsm_pb2.TreeProto()
words = [[0, 1, 2], [3, 4], [5, 6, 7, 8]]
node1 = hsmu.create_node_with_words(words[0])
node2 = hsmu.create_node_with_words(words[1])
node3 = hsmu.create_node_with_words(words[2])
node4 = hsmu.create_node_with_nodes([node1, node2])
node = hsmu.create_node_with_nodes([node4, node3])
tree.root_node.MergeFrom(node)

# Internal util to translate input tree to list of (word_id,path). serialized
# hierarchy is passed into the operator_def as a string argument,
hierarchy_proto = hsmu.create_hierarchy(tree)
arg = caffe2_pb2.Argument()
arg.name = "hierarchy"
arg.s = hierarchy_proto.SerializeToString()


class TestHsm(hu.HypothesisTestCase):
    def test_hsm_run_once(self):
        workspace.GlobalInit(['caffe2'])
        workspace.FeedBlob("data",
                           np.random.randn(1000, 100).astype(np.float32))
        workspace.FeedBlob("weights",
                           np.random.randn(1000, 100).astype(np.float32))
        workspace.FeedBlob("bias", np.random.randn(1000).astype(np.float32))
        workspace.FeedBlob("labels", np.random.randn(1000).astype(np.int32))
        op = core.CreateOperator(
            'HSoftmax',