示例#1
0
def fc(x,
       num_units_out,
       weights=None,
       train=False,
       bias=True,
       bias_data=None,
       fp16_on=None,
       debugContext=''):

    cast_flag, x, fp16_on = bF.deduce_half(x, fp16_on)

    shape = x.pureShape
    num_units_in = shape[-1]
    weights_shape = [num_units_in, num_units_out]

    if debugContext != '':
        debugContext = debugContext + '/'
    # init weights
    if weights is not None:
        assert np.all(np.asarray(weights.shape) == np.asarray(weights_shape))
    else:
        weights = np.ones(weights_shape, bF.mappin_gc2npy[x.dtype])

    weights = temporary_init_weights(weights,
                                     debugContext + "weight",
                                     train,
                                     fp16_on=fp16_on)

    # init bias
    bias_shape = [num_units_out]
    if bias_data is not None:
        assert bias
        assert np.all(np.asarray(bias_data.shape) == np.asarray(bias_shape))
    else:
        if bias:
            bias_data = np.zeros(bias_shape, bF.mappin_gc2npy[x.dtype])
        else:
            bias_data = None
    if bias_data is not None:
        bias = temporary_init_weights(bias_data,
                                      debugContext + "bias",
                                      train,
                                      fp16_on=fp16_on)
    else:
        bias = False

    x = bF.matmul(x, weights, debugContext=debugContext)
    if bias:
        x = x + bias

    if cast_flag:
        x = x.cast(cast_flag)

    return x
示例#2
0
def roi_align(bottom_data,
              bottom_rois,
              spatial_scale=1 / 16.0,
              num_rois=300,
              aligned_height=7,
              aligned_width=7,
              fp16_on=None):
    """roi_align implements."""

    load_lib()
    assert isinstance(aligned_height, int) and isinstance(
        aligned_width,
        int), 'they should be int or IndexError: map::at will raised'
    cast_flag, bottom_data, fp16_on = bF.deduce_half(bottom_data, fp16_on)
    if fp16_on:
        bottom_rois = bottom_rois.cast('FLOAT16')
    else:
        bottom_rois = bottom_rois.cast('FLOAT')

    if fp16_on:
        raise NotImplementedError('maybe not implemented')

    # same as detectron2 roi_align version2(aligned=True and sampling_ratio=1)
    batch_size, channels, height, width = bottom_data.pureShape
    with bF.name_scope("roiAlign"):
        out = bF.get_builder().customOp(
            opName="roiAlign",
            opVersion=1,
            domain="ai.graphcore",
            inputs=[bottom_data.getIpuIndex(),
                    bottom_rois.getIpuIndex()],
            attributes={
                "spatial_scale": spatial_scale,
                "batch_size": batch_size,
                "num_rois": num_rois,
                "height": height,
                "width": width,
                "channels": channels,
                "aligned_height": aligned_height,
                "aligned_width": aligned_width
            },
            numOutputs=1)
    result = bF.TTensor(out[0])

    if cast_flag:
        result = result.cast(cast_flag)

    return result
示例#3
0
def conv2d(input,
           filters,
           ksize=3,
           bias=True,
           train=True,
           strides=[1, 1],
           dilations=[1, 1],
           group=1,
           filters_data=None,
           bias_data=None,
           fp16_on=None,
           weights_fp16_on=None,
           padding_mode='same',
           debugContext='conv',
           bias_training=None):

    cast_flag, input, fp16_on = bF.deduce_half(input, fp16_on)

    batch, c_in, height, width = input.pureShape
    if debugContext != '':
        debugContext = debugContext + '/'
    weights_shape = [filters, c_in, ksize, ksize]
    if filters_data is not None:
        assert np.all(
            np.asarray(filters_data.shape) == np.asarray(weights_shape))
    else:
        filters_data = np.ones(weights_shape, bF.mappin_gc2npy[input.dtype])
    local_weights_fp16_on = fp16_on
    if bF.get_weight_fp16() is not None:
        local_weights_fp16_on = bF.get_weight_fp16()
    if weights_fp16_on is not None:
        local_weights_fp16_on = weights_fp16_on
    if input.dtype.upper() in ['FLOAT', 'FLOAT32'] and local_weights_fp16_on:
        raise RuntimeError('weights cannnot be fp16 while input is fp32')
    weights = temporary_init_weights(filters_data,
                                     debugContext + "weight",
                                     fp16_on=local_weights_fp16_on,
                                     train=train)
    if fp16_on and local_weights_fp16_on is False:
        if isinstance(weights, bF.ConstantTensor):
            # casting 32 to 16 might be different between IPU and numpy
            weights = bF.TTensor(weights.getIpuIndex())
        weights = weights.cast('FLOAT16')

    # init bias
    bias_shape = [filters]
    if bias_data is not None:
        assert bias
        assert np.all(np.asarray(bias_data.shape) == np.asarray(bias_shape))
    else:
        if bias:
            bias_data = np.zeros(bias_shape, bF.mappin_gc2npy[input.dtype])
        else:
            bias_data = None
    if bias_data is not None:
        bias_training = train if bias_training is None else bias_training
        bias = temporary_init_weights(bias_data,
                                      debugContext + "bias",
                                      fp16_on=fp16_on,
                                      train=bias_training)
    else:
        bias = False

    if padding_mode == 'same':
        pads = [ksize // 2] * 4
    elif padding_mode == 'valid':
        pads = [0] * 4
    else:
        raise NotImplementedError

    result = bF._conv2d(input,
                        weights,
                        bias,
                        strides=strides,
                        pads=pads,
                        dilations=dilations,
                        group=group,
                        debugContext=debugContext)
    if cast_flag:
        result = result.cast(cast_flag)
    return result
示例#4
0
def batch_norm(x,
               train=False,
               fp16_on=None,
               weights={
                   'mean': None,
                   'var': None,
                   'scale': None,
                   'bias': None
               },
               momentum=0.9,
               epsilon=1e-5,
               debugPrefix="bn"):

    cast_flag, x, fp16_on = bF.deduce_half(x, fp16_on)

    batch, c_in, height, width = x.pureShape

    dst_type = bF.mappin_gc2npy[x.dtype]
    mean = np.zeros(c_in).astype(
        dst_type) if weights['mean'] is None else weights['mean']
    var = np.ones(c_in).astype(
        dst_type) if weights['var'] is None else weights['var']
    scale = np.ones(c_in).astype(
        dst_type) if weights['scale'] is None else weights['scale']
    bias = np.zeros(c_in).astype(
        dst_type) if weights['bias'] is None else weights['bias']

    with bF.name_scope(debugPrefix):
        mean = temporary_init_weights(mean,
                                      "running_mean",
                                      train,
                                      fp16_on=fp16_on)
        var = temporary_init_weights(var,
                                     "running_var",
                                     train,
                                     fp16_on=fp16_on)
        scale = temporary_init_weights(scale, "weight", train, fp16_on=fp16_on)
        bias = temporary_init_weights(bias, "bias", train, fp16_on=fp16_on)
        if train:
            result = bF._batchNorm(x,
                                   scale,
                                   bias,
                                   mean,
                                   var,
                                   5 if train else 1,
                                   momentum=momentum,
                                   epsilon=epsilon,
                                   debugContext='')
        else:
            mean = mean.unsqueeze(-1).unsqueeze(-1).unsqueeze(0)
            var = var.unsqueeze(-1).unsqueeze(-1).unsqueeze(0)
            scale = scale.unsqueeze(-1).unsqueeze(-1).unsqueeze(0)
            bias = bias.unsqueeze(-1).unsqueeze(-1).unsqueeze(0)
            eps = np.asarray(1e-6).astype(
                np.float16 if fp16_on else np.float32)
            result = (x - mean) / bF.sqrt(var + eps) * scale + bias
        results = [result, mean, var, mean, var]

    if cast_flag:
        results = [result.cast(cast_flag) for result in results]

    return results