| 45 | static void AddConstNodeToSubGraph(Subgraph* graph, Tensor* tensor, Node* fused_node, int fused_port_index); |
| 46 | |
| 47 | static bool Weight_Bn(Subgraph* graph, Node* ConvNode, float* mean, float* var, float* gamma, float* beta, float eps, |
| 48 | float rescale_factor, Tensor* bias_tensor) |
| 49 | { |
| 50 | Tensor* kernel_tensor = ConvNode->GetInputTensor(1); |
| 51 | Convolution* conv_op = dynamic_cast<Convolution*>(ConvNode->GetOp()); |
| 52 | ConvParam* param = conv_op->GetParam(); |
| 53 | const TShape& kernel_shape = kernel_tensor->GetShape(); |
| 54 | |
| 55 | int group = param->group; |
| 56 | // int input_chan = kernel_shape.Shape(1); |
| 57 | int input_chan = kernel_shape.GetC(); |
| 58 | int output_chan = kernel_shape.Shape(0) / group; |
| 59 | int kernel_x = param->kernel_w; |
| 60 | int kernel_y = param->kernel_h; |
| 61 | int kernel_size = input_chan * kernel_x * kernel_y; |
| 62 | float* kernel_org = ( float* )get_tensor_mem(kernel_tensor); |
| 63 | int channel_num = kernel_shape.Shape(0); |
| 64 | float* kernel_new = ( float* )(malloc(kernel_size * channel_num * sizeof(float) + 128)); |
| 65 | |
| 66 | memcpy(kernel_new, kernel_org, sizeof(float) * kernel_size * channel_num); |
| 67 | |
| 68 | kernel_tensor->SetMemAddr(kernel_new); |
| 69 | kernel_tensor->SetAttr("free_mem", 1); |
| 70 | |
| 71 | float* scale_mean = ( float* )malloc(channel_num * sizeof(float)); |
| 72 | float* scale_var_inv = ( float* )malloc(channel_num * sizeof(float)); |
| 73 | |
| 74 | float rescale_factor_tmp = rescale_factor; |
| 75 | |
| 76 | // fuse the bias; |
| 77 | float* bias = NULL; |
| 78 | std::string bias_name; |
| 79 | if(bias_tensor) |
| 80 | { |
| 81 | bias = ( float* )get_tensor_mem(bias_tensor); |
| 82 | bias_name = bias_tensor->GetName() + ".bn"; |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | bias_name = ConvNode->GetName() + ".bias.bn"; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * create the bias node,,, ugly code.. |
| 91 | * |
| 92 | */ |
| 93 | |
| 94 | { |
| 95 | Tensor* new_bias_tensor = new Tensor(bias_name); |
| 96 | std::vector<int> dims{channel_num}; |
| 97 | |
| 98 | TShape bias_shape; |
| 99 | bias_shape.SetDim(dims); |
| 100 | |
| 101 | new_bias_tensor->Reshape(bias_shape); |
| 102 | new_bias_tensor->SetType(kConstTensor); |
| 103 | |
| 104 | void* bias_new = ( void* )malloc(channel_num * sizeof(float) + 128); |
no test coverage detected