Caffe

Network Configuration

TensorOp

TensorOp represents a BMNET IR, which is a bridge between front end and back end. it provides lots of member method to set information to or get from it. Below is the prototype:

namespace bmnet {

class TensorOp {

public:

intinput_shape_size();

intoutput_shape_size();

const TensorShape&input_shape(int index);

const TensorShape&output_shape(int index);

TensorShape*add_output_shape();

u64 global_input(int index);

u64 global_output(int index);

TGCustomizedParameter*mutable_tg_customized_param();

const TGCustomizedParameter&tg_customized_param();

};

}

TensorOp::input_shape_size

void TensorOp::input_shape_size()

Return the number of inputs.

TensorOp::output_shape_size

void TensorOp::output_shape_size()

Return the number of outputs.

TensorOp::input_shape

const TensorShape& TensorOp::input_shape(int index)

Return shape of input by index.

Parameter

Type

Description

index

int

[Required] index of input that to be returned.

TensorOp::output_shape

const TensorShape& TensorOp::output_shape(int index)

Return shape of output by index.

Parameter

Type

Description

index

int

[Required] index of output that to be returned.

TensorOp::add_output_shape

TensorShape* TensorOp::add_output_shape()

Return a mutable pointer to a new added TensorShape of outputs. The returned TensorShape could be modified latter.

TensorOp::global_input

u64 TensorOp::global_input(int index)

Return offset of input tensor by index, while it was stored in device memory.

Parameter

Type

Description

index

int

[Required] index of input that to be returned.

TensorOp::global_output

u64 TensorOp::global_output(int index)

Return offset of output tensor by index, while it was stored in device memory.

Parameter

Type

Description

index

int

[Required] index of output that to be returned.

TensorOp::mutable_tg_customized_param

TGCustomizedParameter* TensorOp::mutable_tg_customized_param()

Return a mutable pointer to parameters of customized BMNET IR.

TensorOp::tg_customized_param

const TGCustomizedParameter& TensorOp::tg_customized_param()

Return reference of customized BMNET IR’s paramters.

CustomizedCaffeLayer

CustomizedCaffeLayer is abstract class, which is used to implement a Layer to convert CAFFE Layer into BMNet IR(please refer to Chapter 5 for details about BMNet IR). If you want to introduce a customized CAFFE layer into BMNet, please inherit this class and implement all pure virtual functions of it. The CustomizedCaffeLayer inherits from CaffeLayer/Layer class. Below are the prototypes of them:

namespace bmnet {

classLayer{

public:

Layer();

virtual~Layer(void);

virtual std::string layer_name()=0;

virtualvoid dump ()=0;

virtualvoidcodegen(TensorOp *op)=0;

​

protected:

voidadd_output_offset(int offset);

};

}

​

namespace bmnet {

​

classCaffeLayer:public Layer {

public:

CaffeLayer(){}

virtual~CaffeLayer(void);

protected:

caffe::LayerParameter &layer_;

};

}

​

namespace bmnet {

​

classCustomizedCaffeLayer:public CaffeLayer {

public:

CustomizedCaffeLayer();

~CustomizedCaffeLayer();

voidsetup(TensorOp* op) override {

...

...

TGCustomizedParameter* param = op->mutable_tg_customized_param ();

param->set_sub_type(layer_name());

}

};

}

CustomizedCaffeLayer::layer_name

std::string CustomizedCaffelayer::layer_name()

Pure virtual function, return type of new added CAFFE layer.

CustomizedCaffeLayer::dump

Pure virtual function, is used to print information of CAFFE Layer.

CustomizedCaffeLayer:: setup

void CustomizedCaffelayer::setup()

Option. It is used to set sub type of Customized Layer only. Implement by default. If child class will override it, this parent class setup function must be call first.

CustomizedCaffeLayer::codegen

Pure virtual function, is used to setup BMNET IR according to LayerParameter of CAFFE Layer. In this function, you should setup output shape and fill parameters to TensorOp.

TBD

Parameter

Type

Description

op

TensorOp*

[Required] pointer to a instance of BMNET IR

CustomizedCaffeLayer::add_output_offset

void CustomizedCaffelayer::add_output_offset (int offset)

Protected member method, should be called when setup output offset of Layer’s top.

Parameter

Type

Description

offset

int

[Required] offset of output, should be 0.

CustomizedCaffeLayer::layer_

caffe::LayerParameter CustomizedCaffelayer::&layer_

Protected member variable, which is reference of customized CAFFE layer’s LayerParameter.

CustomizedTensorFixedInst

CustomizedTensorFixedInst is abstract class, which is used to implement a Layer to convert BMNET IR into instructions by BMKernel APIs. Please inherit this class and implement all pure virtual functions of it. The CustomizedTensorFixedInst inherits from TensorFixedInst/ TensorInst class. Below are the prototypes of them:

namespace bmnet {

classTensorFixedInst:public TensorInst {

public:

TensorFixedInst():TensorInst(){}

TensorFixedInst(TensorOp &op):TensorInst(op){}

virtual~ TensorFixedInst (void);

voidSetCalibrationParameter(

const LayerCalibrationParameter &calibration_parameter){

m_calibrationParameter = calibration_parameter;

}

voidAddInputCalibrationParameter(

const LayerCalibrationParameter &calibration_parameter){

m_inputCalibrationParameter.push_back(calibration_parameter);

}

protected:

LayerCalibrationParameter m_calibrationParameter;

std::vector <LayerCalibrationParameter >

m_inputCalibrationParameter;

};

}

​

namespace bmnet {

classTensorInst{

public:

TensorInst();

virtual~TensorInst(void);

virtual std::string inst_name()=0;

virtualvoid dump ()=0;

virtualvoid encode ()=0;

​

protected:

TensorOp &op_;

};

}

​

namespace bmnet {

​

classCustomizedTensorFixedInst:public TensorFixedInst {

public:

CustomizedTensorFixedInst ();

~CustomizedTensorFixedInst ();

protected:

u64 get_global_neuron_base();

u64 get_global_weight_base();

};

}

​

CustomizedTensorFixedInst::inst_name

std::string CustomizedTensorFixedInst::inst_name()

Pure virtual function, return type of customized BMNET IR.

CustomizedTensorFixedInst::dump

void CustomizedTensorFixedInst::dump()

Pure virtual function, is used to print information of BMNET IR.

CustomizedTensorFixedInst::encode

void CustomizedTensorFixedInst::encode()

Pure virtual function, is used to convert BMNET IR into instructions using BMKernel APIs.

CustomizedTensorFixedInst::get_global_neuron_base

u64 CustomizedTensorFixedInst::get_global_neuron_base()

Protected member method, return the base address, where the neurons are stored in device memory.

CustomizedTensorFixedInst::get_global_weight_base

u64 CustomizedTensorFixedInst::get_global_weight_base()

Protected member method, return the base address, where weight is stored in device memory.

CustomizedTensorFixedInst::op_

TensorOp CustomizedTensorFixedInst::&op_

Protected member variable, which is reference of BMNET IR.

TGCustomizedParamter

TGCustomizedParamter represents a customized BMNET IR’s parameters. It provides member methods to set parameters to or get from it. Below is the prototype:

namespace bmnet {

​

classTGCustomizedParameter{

public:

inti32_param_size();

intf32_param_size();

inti32_param(int index);

floatf32_param(int index);

voidadd_i32_param(int value);

voidadd_f32_param(float value);

};

}

TGCustomizedParamter::i32_param_size

void TGCustomizedParamter::i32_param_size()

Return the number of int parameters, which stored in TGCustomizedParamter.

TGCustomizedParamter::f32_param_size

void TGCustomizedParamter::f32_param_size()

Return the number of float parameters, which stored in TGCustomizedParamter.

TGCustomizeParamter::i32_param

int TGCustomizedParamter::i32_param(int index)

Return int parameter by index.

Parameter

Type

Description

index

index

[Required] index of int parameter that to be returned.

TGCustomizeParamter::f32_param

float TGCustomizedParamter::f32_param(int index)

Return int parameter by index.

Parameter

Type

Description

index

index

[Required] index of float parameter that to be returned.

TGCustomizeParamter::add_i32_param

void TGCustomizedParamter::add_i32_param(int value)

Append a new int parameter to TGCustomizedParamter.

Parameter

Type

Description

value

int

[Required] int parameter.

TGCustomizeParamter::add_f32_param

void TGCustomizedParamter::add_f32_param(int value)

Append a new int parameter to TGCustomizedParamter.

Parameter

Type

Description

value

float

[Required] float parameter.

TensorShape

TensorShape represents a shape of tensor. Below is the prototype:

namespace bmnet {

​

classTensorShape{

public:

voidCopyFrom(const TensorShape& from);

intdim_size()const;

intdim(int index);

voidadd_dim(int value);

};

}

TensorShape::dim_size

int TensorShape::dim_size()

Return the number of dims.

TensorShape::dim

int TensorShape::dim(int index)

Return one dim by index.

Parameter

Type

Description

Index

int

[Required] index of dim that to be returned.

TensorShape::add_dim

void TensorShape::add_dim(int value)

Append a dim to TensorShape.

Parameter

Type

Description

value

int

[Required] new dim to be appended.

TensorShape::CopyFrom

void TensorShape::CopyFrom(const TensorShape& from)

Copy from another TensorShape instance.

Parameter

Type

Description

value

const TensorShape&

[Required] source TensorShape instance.

CaffeBuilder

CaffeBuilder is a class, which provides a uniform interface to combine front end/optimizer/back end core code into one, to compile CAFFE neuron network graph into bmodel file. The CaffeBuilder inherits from Builder class, which is a base compiler class. Below are the prototypes of them: