|
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | 15 | #include "paddle/fluid/inference/anakin/convert/fc.h" |
| 16 | +#include "paddle/fluid/inference/anakin/convert/helper.h" |
16 | 17 | #include <algorithm> |
17 | 18 | #include <string> |
18 | 19 | #include <vector> |
@@ -45,72 +46,39 @@ void FcBaseOpConverter<TargetT>::operator()( |
45 | 46 | // get weights |
46 | 47 | auto *y_v = scope.FindVar(op_desc.Input(w_name).front()); |
47 | 48 | PADDLE_ENFORCE_NOT_NULL(y_v); |
48 | | - auto *y_t = y_v->GetMutable<framework::LoDTensor>(); |
| 49 | + auto weight_tensor = tensor_from_var(*y_v, platform::CPUPlace()); |
| 50 | + auto weight_shape = framework::vectorize2int(weight_tensor->dims()); |
| 51 | + |
| 52 | + int out_dim = weight_shape[1]; |
| 53 | + const int w_m = weight_shape[0]; |
| 54 | + const int w_k = weight_shape[1]; |
49 | 55 |
|
50 | 56 | auto input_name = op_desc.Input(i_name).front(); |
51 | 57 | auto output_name = op_desc.Output("Out").front(); |
52 | 58 |
|
53 | 59 | this->engine_->AddOp(op_name, "Dense", {input_name}, {output_name}); |
54 | 60 | this->engine_->AddOpAttr(op_name, "bias_term", with_bias); |
55 | 61 | this->engine_->AddOpAttr(op_name, "axis", 1); |
56 | | - |
57 | | - auto weight_shape = framework::vectorize2int(y_t->dims()); |
58 | | - int out_dim = weight_shape[1]; |
59 | 62 | this->engine_->AddOpAttr(op_name, "out_dim", out_dim); |
60 | | - const int w_m = weight_shape[0]; |
61 | | - const int w_k = weight_shape[1]; |
62 | 63 |
|
63 | | - if (weight_shape.size() < 4UL) { |
64 | | - weight_shape.insert(weight_shape.begin(), 4UL - weight_shape.size(), 1); |
65 | | - } |
66 | | - Shape anakin_shape(weight_shape); |
| 64 | + auto *weight_data = weight_tensor->data<float>(); |
| 65 | + PADDLE_ENFORCE(w_m * w_k == weight_tensor->numel()); |
67 | 66 |
|
68 | | - framework::LoDTensor weight_tensor; |
69 | | - weight_tensor.Resize(y_t->dims()); |
70 | | - TensorCopySync((*y_t), platform::CPUPlace(), &weight_tensor); |
71 | | - auto *weight_data = weight_tensor.data<float>(); |
72 | | - PADDLE_ENFORCE(w_m * w_k == weight_tensor.numel()); |
73 | | - |
74 | | - std::vector<float> trans_weight_data(weight_tensor.numel()); |
| 67 | + std::vector<float> trans_weight_data(weight_tensor->numel()); |
75 | 68 | for (int i = 0; i < w_m; i++) { |
76 | 69 | for (int j = 0; j < w_k; j++) { |
77 | 70 | trans_weight_data[i + j * w_m] = weight_data[i * w_k + j]; |
78 | 71 | } |
79 | 72 | } |
80 | | - auto *weight1 = |
81 | | - GraphGlobalMem<TargetT>::Global().template new_block<AK_FLOAT>( |
82 | | - anakin_shape); |
83 | | - float *cpu_data = static_cast<float *>(weight1->h_tensor().mutable_data()); |
84 | | - std::copy_n(trans_weight_data.data(), weight_tensor.numel(), cpu_data); |
85 | | - weight1->d_tensor().set_shape(anakin_shape); |
86 | | - weight1->d_tensor().copy_from(weight1->h_tensor()); |
| 73 | + |
| 74 | + auto *weight1 = pblock_from_vector<TargetT>(trans_weight_data); |
87 | 75 | this->engine_->AddOpAttr(op_name, "weight_1", *weight1); |
88 | 76 |
|
89 | 77 | // get bias |
90 | 78 | if (with_bias) { |
91 | 79 | auto *b_v = scope.FindVar(op_desc.Input("Bias").front()); |
92 | 80 | PADDLE_ENFORCE_NOT_NULL(b_v); |
93 | | - auto *b_t = b_v->GetMutable<framework::LoDTensor>(); |
94 | | - |
95 | | - auto bias_shape = framework::vectorize2int(b_t->dims()); |
96 | | - framework::LoDTensor bias_tensor; |
97 | | - bias_tensor.Resize(b_t->dims()); |
98 | | - TensorCopySync((*b_t), platform::CPUPlace(), &bias_tensor); |
99 | | - auto *bias_data = bias_tensor.data<float>(); |
100 | | - bias_shape.insert(bias_shape.begin(), 1); |
101 | | - bias_shape.insert(bias_shape.begin(), 1); |
102 | | - bias_shape.insert(bias_shape.begin(), 1); |
103 | | - // bias_shape.push_back(1); |
104 | | - // bias_shape.push_back(1); |
105 | | - Shape anakin_bias_shape(bias_shape); |
106 | | - |
107 | | - auto *weight2 = |
108 | | - GraphGlobalMem<TargetT>::Global().template new_block<AK_FLOAT>( |
109 | | - anakin_bias_shape); |
110 | | - float *cpu_data2 = static_cast<float *>(weight2->h_tensor().mutable_data()); |
111 | | - std::copy_n(bias_data, bias_tensor.numel(), cpu_data2); |
112 | | - weight2->d_tensor().set_shape(anakin_bias_shape); |
113 | | - weight2->d_tensor().copy_from(weight2->h_tensor()); |
| 81 | + auto weight2 = pblock_from_var<TargetT>(*b_v); |
114 | 82 | this->engine_->AddOpAttr(op_name, "weight_2", *weight2); |
115 | 83 | } |
116 | 84 | } |
|
0 commit comments