def dnnweaver_conv2d(node, ctx): # Need to add implicit bias here? w_dtype = FixedPoint(16, 14) w_shape = node.args[1].shape inp_dtype = get_dnnweaver_var(ctx, node.args[0]).dtype c_type = FixedPoint(16, 10) biases = get_tensor(shape=(node.args[1].shape[0]), name='biases', dtype=FixedPoint( 32, w_dtype.frac_bits + inp_dtype.frac_bits)) pad = (1, node.args[4], node.args[4], 1) strides = (1, node.args[3], node.args[3], 1) with ctx['graph'].as_default(): with ctx['graph'].name_scope(node.name): inputs = get_dnnweaver_var(ctx, node.args[0]) weights = get_dnnweaver_var(ctx, node.args[1]) weights.shape = convert_conv_shape(weights.shape) if weights.shape[-1] != inputs.shape[-1]: inputs.shape = convert_conv_shape(inputs.shape) return node.name, conv2D(inputs, weights, biases, name=node.name, pad=pad, stride=strides, dtype=c_type)
def get_graph(train=False): g = Graph('YOLOv2-Test: 16-bit', dataset='imagenet', log_level=logging.INFO) batch_size = 1 with g.as_default(): with g.name_scope('inputs'): # Input dimensions are (Batch_size, Height, Width, Channels) i = get_tensor(shape=(batch_size, 28, 28, 1), name='data', dtype=FQDtype.FXP16, trainable=False) with g.name_scope('conv0'): # Weight dimensions are (Output Channels, Kernel Height, Kernel Width, Input Channels) weights = get_tensor(shape=(20, 5, 5, 1), name='weights', dtype=FixedPoint(16, 12)) # Bias dimensions are (Output Channels,) biases = get_tensor(shape=(20), name='biases', dtype=FixedPoint(32, 20)) # Intermediate data dimensions are (Batch_size, Height, Width, Channels) conv = conv2D(i, weights, biases, pad='VALID', dtype=FixedPoint(16, 12)) return g
def get_ops(self): mul_dtypes = (self.data.dtype, FixedPoint(16, 15)) rshift_dtype = FixedPoint(self.data.dtype.bits + 16, self.data.dtype.frac_bits + 15) cmp_dtypes = (self.data.dtype) return { Ops.MUL(mul_dtypes): self.data.size, Ops.RSHIFT(rshift_dtype): self.data.size, Ops.CMP(cmp_dtypes): self.data.size }
def fc(tensor_in, output_channels=1024, f_dtype=None, w_dtype=None, act='linear'): input_channels = tensor_in.shape[-1] weights = get_tensor(shape=(output_channels, input_channels), name='weights', dtype=w_dtype) biases = get_tensor(shape=(output_channels, ), name='biases', dtype=FixedPoint( 32, w_dtype.frac_bits + tensor_in.dtype.frac_bits)) _fc = matmul(tensor_in, weights, biases, dtype=f_dtype) if act == 'leakyReLU': with get_default_graph().name_scope(act): act = leakyReLU(_fc, dtype=_fc.dtype) elif act == 'linear': with get_default_graph().name_scope(act): act = _fc else: raise ValueError, 'Unknown activation type {}'.format(act) return act
def dnnweaver_var(node, ctx): if isinstance(node, pm.var_index): # TODO: Fix var index shape resolution during onnx translation assert node.var.name in ctx new_tensor = ctx[node.var.name] else: new_tensor = get_tensor(shape=node.shape, name=node.name, dtype=FixedPoint(16, 10)) return node.name, new_tensor
def yolo_convolution(tensor_in, filters=32, kernel_size=3, batch_normalize=True, act='leakyReLU', c_dtype=None, w_dtype=None, s_dtype=None, bn_dtype=None): input_channels = tensor_in.shape[-1] weights = get_tensor(shape=(filters, kernel_size, kernel_size, input_channels), name='weights', dtype=w_dtype) biases = get_tensor(shape=(filters), name='biases', dtype=FixedPoint( 32, w_dtype.frac_bits + tensor_in.dtype.frac_bits)) conv = conv2D(tensor_in, weights, biases, pad='SAME', dtype=c_dtype) if batch_normalize: with get_default_graph().name_scope('batch_norm'): mean = get_tensor(shape=(filters), name='mean', dtype=FixedPoint(16, c_dtype.frac_bits)) scale = get_tensor(shape=(filters), name='scale', dtype=s_dtype) bn = batch_norm(conv, mean=mean, scale=scale, dtype=bn_dtype) else: bn = conv if act == 'leakyReLU': with get_default_graph().name_scope(act): act = leakyReLU(bn, dtype=bn.dtype) elif act == 'linear': with get_default_graph().name_scope(act): act = bn else: raise ValueError('Unknown activation type {}'.format(act)) return act
def dnnweaver_conv2d_bias(node, ctx): w_dtype = FixedPoint(16, 14) inp_dtype = ctx[node.args[0].name].dtype c_type = FixedPoint(16, 10) pad = (1, node.args[5], node.args[5], 1) strides = (1, node.args[4], node.args[4], 1) with ctx['graph'].as_default(): with ctx['graph'].name_scope(node.name): inputs = get_dnnweaver_var(ctx, node.args[0]) weights = get_dnnweaver_var(ctx, node.args[1]) weights.shape = convert_conv_shape(weights.shape) if weights.shape[-1] != inputs.shape[-1]: inputs.shape = convert_conv_shape(inputs.shape) biases = get_dnnweaver_var(ctx, node.args[2]) return node.name, conv2D(inputs, weights, biases, name=node.name, pad=pad, stride=strides, dtype=c_type)
def conv(tensor_in, filters=32, stride=None, kernel_size=3, pad='SAME', c_dtype=None, w_dtype=None, act='linear'): if stride is None: stride = (1, 1, 1, 1) input_channels = tensor_in.shape[-1] weights = get_tensor(shape=(filters, kernel_size, kernel_size, input_channels), name='weights', dtype=w_dtype) biases = get_tensor(shape=(filters), name='biases', dtype=FixedPoint( 32, w_dtype.frac_bits + tensor_in.dtype.frac_bits)) _conv = conv2D(tensor_in, weights, biases, stride=stride, pad=pad, dtype=c_dtype) if act == 'leakyReLU': with get_default_graph().name_scope(act): act = leakyReLU(_conv, dtype=_conv.dtype) elif act == 'linear': with get_default_graph().name_scope(act): act = _conv else: raise ValueError, 'Unknown activation type {}'.format(act) return act
def get_graph(train=False): g = Graph('YOLOv2-Test: 16-bit', dataset='imagenet', log_level=logging.INFO) batch_size = 1 with g.as_default(): with g.name_scope('inputs'): i = get_tensor(shape=(batch_size, 416, 416, 3), name='data', dtype=FQDtype.FXP16, trainable=False) with g.name_scope('conv0'): conv0 = yolo_convolution(i, filters=16, kernel_size=3, batch_normalize=True, act='leakyReLU', w_dtype=FixedPoint(16, 14), c_dtype=FixedPoint(16, 12), s_dtype=FixedPoint(16, 9), bn_dtype=FixedPoint(16, 8)) with g.name_scope('pool0'): pool0 = maxPool(conv0, pooling_kernel=(1, 2, 2, 1), stride=(1, 2, 2, 1), pad='VALID') with g.name_scope('conv1'): conv1 = yolo_convolution(pool0, filters=32, kernel_size=3, batch_normalize=True, act='leakyReLU', w_dtype=FixedPoint(16, 14), c_dtype=FixedPoint(16, 8), s_dtype=FixedPoint(16, 14), bn_dtype=FixedPoint(16, 8)) with g.name_scope('pool1'): pool1 = maxPool(conv1, pooling_kernel=(1, 2, 2, 1), stride=(1, 2, 2, 1), pad='VALID') with g.name_scope('conv2'): conv2 = yolo_convolution( pool1, filters=64, kernel_size=3, batch_normalize=True, act='leakyReLU', # batch_normalize=False, act='linear', w_dtype=FixedPoint(16, 14), c_dtype=FixedPoint(16, 10), s_dtype=FixedPoint(16, 13), bn_dtype=FixedPoint(16, 9)) with g.name_scope('pool2'): pool2 = maxPool(conv2, pooling_kernel=(1, 2, 2, 1), stride=(1, 2, 2, 1), pad='VALID') with g.name_scope('conv3'): conv3 = yolo_convolution(pool2, filters=128, kernel_size=3, batch_normalize=True, act='leakyReLU', w_dtype=FixedPoint(16, 14), c_dtype=FixedPoint(16, 10), s_dtype=FixedPoint(16, 13), bn_dtype=FixedPoint(16, 10)) with g.name_scope('pool3'): pool3 = maxPool(conv3, pooling_kernel=(1, 2, 2, 1), stride=(1, 2, 2, 1), pad='VALID') with g.name_scope('conv4'): conv4 = yolo_convolution(pool3, filters=256, kernel_size=3, batch_normalize=True, act='leakyReLU', w_dtype=FixedPoint(16, 14), c_dtype=FixedPoint(16, 11), s_dtype=FixedPoint(16, 13), bn_dtype=FixedPoint(16, 10)) with g.name_scope('pool4'): pool4 = maxPool(conv4, pooling_kernel=(1, 2, 2, 1), stride=(1, 2, 2, 1), pad='VALID') with g.name_scope('conv5'): conv5 = yolo_convolution(pool4, filters=512, kernel_size=3, batch_normalize=True, act='leakyReLU', w_dtype=FixedPoint(16, 14), c_dtype=FixedPoint(16, 12), s_dtype=FixedPoint(16, 13), bn_dtype=FixedPoint(16, 11)) with g.name_scope('pool5'): pool5 = maxPool(conv5, pooling_kernel=(1, 2, 2, 1), stride=(1, 1, 1, 1), pad=((0, 0), (0, 1), (0, 1), (0, 0))) with g.name_scope('conv6'): conv6 = yolo_convolution(pool5, filters=1024, kernel_size=3, batch_normalize=True, act='leakyReLU', w_dtype=FixedPoint(16, 14), c_dtype=FixedPoint(16, 12), s_dtype=FixedPoint(16, 11), bn_dtype=FixedPoint(16, 9)) with g.name_scope('conv7'): conv7 = yolo_convolution(conv6, filters=1024, kernel_size=3, batch_normalize=True, act='leakyReLU', w_dtype=FixedPoint(16, 14), c_dtype=FixedPoint(16, 11), s_dtype=FixedPoint(16, 14), bn_dtype=FixedPoint(16, 12)) with g.name_scope('conv8'): conv8 = yolo_convolution(conv7, filters=125, kernel_size=1, batch_normalize=False, act='linear', w_dtype=FixedPoint(16, 14), c_dtype=FixedPoint(16, 11)) return g
def _get_output_dtype(self): total_bits = 64 total_frac_bits = self.data.dtype.frac_bits + self.weights.dtype.frac_bits return FixedPoint(total_bits, total_frac_bits)
def _get_output_dtype(self): return FixedPoint( 32, self.data.dtype.frac_bits + self.scale.dtype.frac_bits)