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:
  int input_shape_size();
  int output_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

Return the number of inputs.

TensorOp::output_shape_size

Return the number of outputs.

TensorOp::input_shape

Return shape of input by index.

Parameter

Type

Description

index

int

[Required] index of input that to be returned.

TensorOp::output_shape

Return shape of output by index.


Parameter

Type

Description

index

int

[Required] index of output that to be returned.

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

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

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

Return a mutable pointer to parameters of customized BMNET IR.

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:

CustomizedCaffeLayer::layer_name

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

CustomizedCaffeLayer::dump

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.

Parameter

Type

Description

op

TensorOp*

[Required] pointer to a instance of BMNET IR

CustomizedCaffeLayer::add_output_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_

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:

CustomizedTensorFixedInst::inst_name

Pure virtual function, return type of customized BMNET IR.

CustomizedTensorFixedInst::dump

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

CustomizedTensorFixedInst::encode

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

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

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

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:

TGCustomizedParamter::i32_param_size

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

TGCustomizedParamter::f32_param_size

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

TGCustomizeParamter::i32_param

Return int parameter by index.

Parameter

Type

Description

index

index

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

TGCustomizeParamter::f32_param

Return int parameter by index.

Parameter

Type

Description

index

index

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

TGCustomizeParamter::add_i32_param

Append a new int parameter to TGCustomizedParamter.

Parameter

Type

Description

value

int

[Required] int parameter.

TGCustomizeParamter::add_f32_param

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:

TensorShape::dim_size

Return the number of dims.

TensorShape::dim

Return one dim by index.

Parameter

Type

Description

Index

int

[Required] index of dim that to be returned.

TensorShape::add_dim

Append a dim to TensorShape.

Parameter

Type

Description

value

int

[Required] new dim to be appended.

TensorShape::CopyFrom

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:

CaffeBuilder::CaffeBuilder

Constructor function of CaffeBuilder class.

Parameter

Type

Description

ver

CHIP_VER

[Required] The target chip version. Currently only BM_CHIP_BM1880 is

available.

modified_proto

const char*

[Optional] The modified prototxt file, please refer Chapter 4 to get more detail.

caffemodel

const char*

[Required] The specified caffemode file of network

weight_bin

const char*

[Optional] The specified weight file of network

in_ctable

const char*

[Required] The specified calibration table file of network

out_ctable

const char*

[Required] The specified weight file of network

modified_proto are optional parameters, that means you no need to fill all of this parameters. Below combination are valid: 1) caffemodel only; 2) caffemodel, as well as modified_protos

CaffeBuilder::Builder

Core member function of CaffeBuilder class, used to compile the network by specifying input shape and optimization level.

Parameter

Type

Description

n,c,h,w

int

[Required] The input shape

opt

int

[Optional] The input
optimization options. The
default value is BM_OPT_LAYER_GROUP_WITH_WEIG

Below are the values for opt.

value

Description

OPT_NONE

No optimization

BM_OPT_LAYER_GROUP

Divides layers into clusters to optimize the bandwidth overhead.

BM_OPT_LAYER_GROUP_WITH_WEIG

Add additional optimization to reduce the device memory footprint and reshape weight.

CaffeBuilder::store_prototxt

store the optimized network graph as a file.

Parameter

Type

Description

dst

const char*

[Required] File to be stored

CaffeBuilder::store_model

Store compiled instructions, weight and other information of the network as a bmodel file.

Parameter

Type

Description

net_name

const char*

[Required] the network name.

dst

const char*

[Required] File to store bmodel.

Plugin_path

const char*

[Required] cpu op plugins.

CaffeBuilder::addCustomizedLayer

Register a new added customized layer, which used to convert CAFFE layer into BMNet IR (Intermediate representation).

Parameter

Type

Description

Layer

Layer*

[Required] pointer to instance of Class Layer

CaffeBuilder::addCustomizedTensorInst

Register a new added customized TensorInst (Tensor Instruction), which used to convert BMNet IR into instructions.

Parameter

Type

Description

inst

TensorInst*

[Required] pointer to instance of Class Layer

Last updated