未验证 提交 36b82eae 编写于 作者: 石晓伟 提交者: GitHub

refine the doc of paddle_api.h, test=develop (#23402)

* refine the doc of paddle_api.h, test=develop

* fix documents, test=develop
上级 2d0933c3
...@@ -32,8 +32,7 @@ ...@@ -32,8 +32,7 @@
*/ */
namespace paddle { namespace paddle {
/** paddle data type. /// \brief Paddle data type.
*/
enum PaddleDType { enum PaddleDType {
FLOAT32, FLOAT32,
INT64, INT64,
...@@ -42,79 +41,98 @@ enum PaddleDType { ...@@ -42,79 +41,98 @@ enum PaddleDType {
// TODO(Superjomn) support more data types if needed. // TODO(Superjomn) support more data types if needed.
}; };
/** /// \brief Memory manager for PaddleTensor.
* \brief Memory manager for `PaddleTensor`. ///
* /// The PaddleBuf holds a buffer for data input or output. The memory can be
* The PaddleBuf holds a buffer for data input or output. The memory can be /// allocated by user or by PaddleBuf itself, but in any case, the PaddleBuf
* allocated by user or by PaddleBuf itself, but in any case, the PaddleBuf /// should be reused for better performance.
* should be reused for better performance. ///
* /// For user allocated memory, the following API can be used:
* For user allocated memory, the following API can be used: /// - PaddleBuf(void* data, size_t length) to set an external memory by
* - PaddleBuf(void* data, size_t length) to set an external memory by /// specifying the memory address and length.
* specifying the memory address and length. /// - Reset(void* data, size_t length) to reset the PaddleBuf with an external
* - Reset(void* data, size_t length) to reset the PaddleBuf with an external /// memory.
*memory. /// ATTENTION, for user allocated memory, deallocation should be done by users
* ATTENTION, for user allocated memory, deallocation should be done by users /// externally after the program finished. The PaddleBuf won't do any allocation
*externally after the program finished. The PaddleBuf won't do any allocation /// or deallocation.
*or deallocation. ///
* /// To have the PaddleBuf allocate and manage the memory:
* To have the PaddleBuf allocate and manage the memory: /// - PaddleBuf(size_t length) will allocate a memory of size `length`.
* - PaddleBuf(size_t length) will allocate a memory of size `length`. /// - Resize(size_t length) resize the memory to no less than `length`,
* - Resize(size_t length) resize the memory to no less than `length`, ATTENTION /// ATTENTION
* if the allocated memory is larger than `length`, nothing will done. /// if the allocated memory is larger than `length`, nothing will done.
* ///
* Usage: /// Usage:
* ///
* Let PaddleBuf manage the memory internally. /// Let PaddleBuf manage the memory internally.
* \code{cpp} /// \code{cpp}
* const int num_elements = 128; /// const int num_elements = 128;
* PaddleBuf buf(num_elements * sizeof(float)); /// PaddleBuf buf(num_elements/// sizeof(float));
* \endcode /// \endcode
* ///
* Or /// Or
* \code{cpp} /// \code{cpp}
* PaddleBuf buf; /// PaddleBuf buf;
* buf.Resize(num_elements * sizeof(float)); /// buf.Resize(num_elements/// sizeof(float));
* \endcode /// \endcode
* Works the exactly the same. /// Works the exactly the same.
* ///
* One can also make the `PaddleBuf` use the external memory. /// One can also make the `PaddleBuf` use the external memory.
* \code{cpp} /// \code{cpp}
* PaddleBuf buf; /// PaddleBuf buf;
* void* external_memory = new float[num_elements]; /// void* external_memory = new float[num_elements];
* buf.Reset(external_memory, num_elements*sizeof(float)); /// buf.Reset(external_memory, num_elements*sizeof(float));
* ... /// ...
* delete[] external_memory; // manage the memory lifetime outside. /// delete[] external_memory; // manage the memory lifetime outside.
* \endcode /// \endcode
*/ ///
class PaddleBuf { class PaddleBuf {
public: public:
/** PaddleBuf allocate memory internally, and manage it. ///
*/ /// \brief PaddleBuf allocate memory internally, and manage it.
///
/// \param[in] length The length of data.
///
explicit PaddleBuf(size_t length) explicit PaddleBuf(size_t length)
: data_(new char[length]), length_(length), memory_owned_(true) {} : data_(new char[length]), length_(length), memory_owned_(true) {}
/** Set external memory, the PaddleBuf won't manage it. ///
*/ /// \brief Set external memory, the PaddleBuf won't manage it.
///
/// \param[in] data The start address of the external memory.
/// \param[in] length The length of data.
///
PaddleBuf(void* data, size_t length) PaddleBuf(void* data, size_t length)
: data_(data), length_(length), memory_owned_{false} {} : data_(data), length_(length), memory_owned_{false} {}
/** Copy only available when memory is managed externally. ///
*/ /// \brief Copy only available when memory is managed externally.
explicit PaddleBuf(const PaddleBuf&); ///
/// \param[in] other another `PaddleBuf`
/** Resize the memory. ///
*/ explicit PaddleBuf(const PaddleBuf& other);
///
/// \brief Resize the memory.
///
/// \param[in] length The length of data.
///
void Resize(size_t length); void Resize(size_t length);
/** Reset to external memory, with address and length set. ///
*/ /// \brief Reset to external memory, with address and length set.
///
/// \param[in] data The start address of the external memory.
/// \param[in] length The length of data.
///
void Reset(void* data, size_t length); void Reset(void* data, size_t length);
/** Tell whether the buffer is empty. ///
*/ /// \brief Tell whether the buffer is empty.
///
bool empty() const { return length_ == 0; } bool empty() const { return length_ == 0; }
/** Get the data's memory address. ///
*/ /// \brief Get the data's memory address.
///
void* data() const { return data_; } void* data() const { return data_; }
/** Get the memory length. ///
*/ /// \brief Get the memory length.
///
size_t length() const { return length_; } size_t length() const { return length_; }
~PaddleBuf() { Free(); } ~PaddleBuf() { Free(); }
...@@ -125,20 +143,21 @@ class PaddleBuf { ...@@ -125,20 +143,21 @@ class PaddleBuf {
private: private:
void Free(); void Free();
void* data_{nullptr}; // pointer to the data memory. void* data_{nullptr}; ///< pointer to the data memory.
size_t length_{0}; // number of memory bytes. size_t length_{0}; ///< number of memory bytes.
bool memory_owned_{true}; bool memory_owned_{true};
}; };
/** Basic input and output data structure for PaddlePredictor. ///
*/ /// \brief Basic input and output data structure for PaddlePredictor.
///
struct PaddleTensor { struct PaddleTensor {
PaddleTensor() = default; PaddleTensor() = default;
std::string name; // variable name. std::string name; ///< variable name.
std::vector<int> shape; std::vector<int> shape;
PaddleBuf data; // blob of data. PaddleBuf data; ///< blob of data.
PaddleDType dtype; PaddleDType dtype;
std::vector<std::vector<size_t>> lod; // Tensor+LoD equals LoDTensor std::vector<std::vector<size_t>> lod; ///< Tensor+LoD equals LoDTensor
}; };
enum class PaddlePlace { kUNK = -1, kCPU, kGPU }; enum class PaddlePlace { kUNK = -1, kCPU, kGPU };
...@@ -313,25 +332,30 @@ class PaddlePredictor { ...@@ -313,25 +332,30 @@ class PaddlePredictor {
}; };
}; };
///
/// \brief configuration manager for `NativePredictor`.
///
/// `AnalysisConfig` manages configurations of `NativePredictor`.
/// During inference procedure, there are many parameters(model/params path,
/// place of inference, etc.)
///
struct NativeConfig : public PaddlePredictor::Config { struct NativeConfig : public PaddlePredictor::Config {
// GPU related fields. /// GPU related fields.
bool use_gpu{false}; bool use_gpu{false};
int device{0}; int device{0};
float fraction_of_gpu_memory{ float fraction_of_gpu_memory{
-1.f}; /*!< Change to a float in (0,1] if needed. */ -1.f}; ///< Change to a float in (0,1] if needed.
// Specify the exact path of program and parameter files.
std::string prog_file; std::string prog_file;
std::string param_file; std::string
param_file; ///< Specify the exact path of program and parameter files.
/** Specify the variable's name of each input if input tensors don't follow bool specify_input_name{false}; ///< Specify the variable's name of each
* the ///< input if input tensors don't follow the
* `feeds` and `fetches` of the phase `save_inference_model`. ///< `feeds` and `fetches` of the phase
*/ ///< `save_inference_model`.
bool specify_input_name{false};
/** Set and get the number of cpu math library threads. /// Set and get the number of cpu math library threads.
*/
void SetCpuMathLibraryNumThreads(int cpu_math_library_num_threads) { void SetCpuMathLibraryNumThreads(int cpu_math_library_num_threads) {
cpu_math_library_num_threads_ = cpu_math_library_num_threads; cpu_math_library_num_threads_ = cpu_math_library_num_threads;
} }
...@@ -340,39 +364,37 @@ struct NativeConfig : public PaddlePredictor::Config { ...@@ -340,39 +364,37 @@ struct NativeConfig : public PaddlePredictor::Config {
} }
protected: protected:
// number of cpu math library (such as MKL, OpenBlas) threads for each int cpu_math_library_num_threads_{1}; ///< number of cpu math library (such
// instance. ///< as MKL, OpenBlas) threads for each
int cpu_math_library_num_threads_{1}; ///< instance.
}; };
/*! \fn std::unique_ptr<PaddlePredictor> CreatePaddlePredictor(const ConfigT& ///
* config); /// \brief A factory to help create different predictors.
* ///
* \brief A factory to help create different predictors. /// Usage:
* ///
* Usage: /// \code{.cpp}
* /// NativeConfig config;
* \code{.cpp} /// ... // change the configs.
* NativeConfig config; /// auto native_predictor = CreatePaddlePredictor(config);
* ... // change the configs. /// \endcode
* auto native_predictor = CreatePaddlePredictor(config); ///
* \endcode /// FOR EXTENSION DEVELOPER:
* /// Different predictors are designated by config type. Similar configs can be
* FOR EXTENSION DEVELOPER: /// merged, but there shouldn't be a huge config containing different fields for
* Different predictors are designated by config type. Similar configs can be /// more than one kind of predictors.
* merged, but there shouldn't be a huge config containing different fields for ////
* more than one kind of predictors.
*/
template <typename ConfigT> template <typename ConfigT>
std::unique_ptr<PaddlePredictor> CreatePaddlePredictor(const ConfigT& config); std::unique_ptr<PaddlePredictor> CreatePaddlePredictor(const ConfigT& config);
/** NOTE The following APIs are too trivial, we will discard it in the following /// NOTE The following APIs are too trivial, we will discard it in the following
* versions. /// versions.
*/ ///
enum class PaddleEngineKind { enum class PaddleEngineKind {
kNative = 0, /*!< Use the native Fluid facility. */ kNative = 0, ///< Use the native Fluid facility.
kAutoMixedTensorRT, /*!< Automatically mix Fluid with TensorRT. */ kAutoMixedTensorRT, ///< Automatically mix Fluid with TensorRT.
kAnalysis, /*!< More optimization. */ kAnalysis, ///< More optimization.
}; };
template <typename ConfigT, PaddleEngineKind engine> template <typename ConfigT, PaddleEngineKind engine>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册