|
|
|
@ -20,6 +20,7 @@
|
|
|
|
|
|
|
|
|
|
#include "paddle/fluid/framework/lod_tensor.h"
|
|
|
|
|
#include "paddle/fluid/framework/variable.h"
|
|
|
|
|
#include "paddle/fluid/inference/anakin/engine.h"
|
|
|
|
|
|
|
|
|
|
#include "framework/core/net/net.h"
|
|
|
|
|
#include "framework/core/types.h"
|
|
|
|
@ -29,8 +30,8 @@
|
|
|
|
|
|
|
|
|
|
using anakin::saber::Shape;
|
|
|
|
|
using anakin::AK_FLOAT;
|
|
|
|
|
using anakin::AK_INT8;
|
|
|
|
|
using anakin::PBlock;
|
|
|
|
|
using anakin::graph::GraphGlobalMem;
|
|
|
|
|
|
|
|
|
|
namespace paddle {
|
|
|
|
|
namespace inference {
|
|
|
|
@ -38,31 +39,34 @@ namespace anakin {
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<framework::LoDTensor> tensor_from_var(
|
|
|
|
|
const framework::Variable& var, const platform::Place& place);
|
|
|
|
|
template <typename T>
|
|
|
|
|
PBlock<T>* pblock_from_tensor(const framework::LoDTensor& tensor,
|
|
|
|
|
std::vector<int> shape) {
|
|
|
|
|
while (shape.size() < 4) {
|
|
|
|
|
shape.insert(shape.begin(), 1);
|
|
|
|
|
|
|
|
|
|
template <typename TargetT, ::anakin::Precision PrecisionT>
|
|
|
|
|
PBlock<TargetT>* pblock_from_tensor(const framework::LoDTensor& tensor,
|
|
|
|
|
std::vector<int> shape_vec,
|
|
|
|
|
AnakinEngine<TargetT, PrecisionT>* engine) {
|
|
|
|
|
while (shape_vec.size() < 4) {
|
|
|
|
|
shape_vec.insert(shape_vec.begin(), 1);
|
|
|
|
|
}
|
|
|
|
|
Shape anakin_shape(shape);
|
|
|
|
|
auto* weight =
|
|
|
|
|
GraphGlobalMem<T>::Global().template new_block<AK_FLOAT>(anakin_shape);
|
|
|
|
|
Shape shape(shape_vec);
|
|
|
|
|
PBlock<TargetT>* weight = new PBlock<TargetT>(shape, AK_FLOAT);
|
|
|
|
|
engine->RegistBlock(weight);
|
|
|
|
|
float* cpu_data = static_cast<float*>(weight->h_tensor().mutable_data());
|
|
|
|
|
std::copy_n(tensor.data<float>(), tensor.numel(), cpu_data);
|
|
|
|
|
weight->d_tensor().set_shape(anakin_shape);
|
|
|
|
|
weight->d_tensor().set_shape(shape);
|
|
|
|
|
weight->d_tensor().copy_from(weight->h_tensor());
|
|
|
|
|
return weight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
PBlock<T>* pblock_from_vector(const std::vector<float>& vec,
|
|
|
|
|
std::vector<int> shape_vec) {
|
|
|
|
|
template <typename TargetT, ::anakin::Precision PrecisionT>
|
|
|
|
|
PBlock<TargetT>* pblock_from_vector(const std::vector<float>& vec,
|
|
|
|
|
std::vector<int> shape_vec,
|
|
|
|
|
AnakinEngine<TargetT, PrecisionT>* engine) {
|
|
|
|
|
while (shape_vec.size() < 4) {
|
|
|
|
|
shape_vec.insert(shape_vec.begin(), 1);
|
|
|
|
|
}
|
|
|
|
|
Shape shape(shape_vec);
|
|
|
|
|
auto* weight =
|
|
|
|
|
GraphGlobalMem<T>::Global().template new_block<AK_FLOAT>(shape);
|
|
|
|
|
PBlock<TargetT>* weight = new PBlock<TargetT>(shape, AK_FLOAT);
|
|
|
|
|
engine->RegistBlock(weight);
|
|
|
|
|
auto* weight_data = static_cast<float*>(weight->h_tensor().mutable_data());
|
|
|
|
|
std::copy(std::begin(vec), std::end(vec), weight_data);
|
|
|
|
|
weight->d_tensor().set_shape(shape);
|
|
|
|
@ -70,17 +74,20 @@ PBlock<T>* pblock_from_vector(const std::vector<float>& vec,
|
|
|
|
|
return weight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
PBlock<T>* pblock_from_vector(const std::vector<float>& vec) {
|
|
|
|
|
template <typename TargetT, ::anakin::Precision PrecisionT>
|
|
|
|
|
PBlock<TargetT>* pblock_from_vector(const std::vector<float>& vec,
|
|
|
|
|
AnakinEngine<TargetT, PrecisionT>* engine) {
|
|
|
|
|
int size = vec.size();
|
|
|
|
|
return pblock_from_vector<T>(vec, std::vector<int>({1, 1, 1, size}));
|
|
|
|
|
return pblock_from_vector<TargetT, PrecisionT>(
|
|
|
|
|
vec, std::vector<int>({1, 1, 1, size}), engine);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
PBlock<T>* pblock_from_var(const framework::Variable& var) {
|
|
|
|
|
template <typename TargetT, ::anakin::Precision PrecisionT>
|
|
|
|
|
PBlock<TargetT>* pblock_from_var(const framework::Variable& var,
|
|
|
|
|
AnakinEngine<TargetT, PrecisionT>* engine) {
|
|
|
|
|
auto tensor = tensor_from_var(var, platform::CPUPlace());
|
|
|
|
|
auto shape = framework::vectorize2int(tensor->dims());
|
|
|
|
|
return pblock_from_tensor<T>(*tensor, shape);
|
|
|
|
|
return pblock_from_tensor<TargetT, PrecisionT>(*tensor, shape, engine);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace anakin
|
|
|
|
|