Updated on 2022-03-13 GMT+08:00

AISimpleTensor

Tensor of a simple data type. This class is defined in ai_tensor.h.

   class AISimpleTensor : public IAITensor
   {
    public:
        AISimpleTensor();

        ~AISimpleTensor();

        /*
       * @brief   Obtain the type name.
        */
        virtual const char* const GetTypeName() override;

      /* Obtain the data address. */
        void* GetBuffer();

        /*
        @brief   Set the data address.
        @param [in] data  Data pointer
       @param [in] size  Size
        @param [in] isown   Whether the tensor releases the data address memory after the tensor life cycle ends.
        */
        void SetBuffer(void *data, const int32_t size, bool isown=false);

        /*
        @brief   Obtain the space occupied by data.
        */
        uint32_t GetSize();

        /*
        @brief Allocate data space.
        */
        void* MallocDataBuffer(uint32_t size);

        /*
       * @brief   Obtain the byte length after serialization.
        */
        virtual uint32_t ByteSizeLong() override;

        /*
       * @brief   Set parameters through AITensorDescription.
        * @param [in] tensor_desc  Tensor description
       * @return true: Initialization success
       * @return false: Initialization failure. The possible cause is that tensor_desc is inconsistent with the current tensor.
        */
        virtual bool Init(const AITensorDescription &tensor_desc) override;

#if defined( __ANDROID__) || defined(ANDROID)
        /*
        * @brief   Serialize data to the buffer for cross-process data interaction.
        * @param [in] buffer   Address of the buffer to which the data is serialized, allocated by the caller
        * @param [in] size   Size of the buffer to which the data is output
        * @return SUCCESS   Success
        *         FAILED: failure. If the tensor does not support cross-process, this API does not need to be implemented and a failure message is returned.
        */
        virtual AIStatus SerializeTo(void* buffer, const uint32_t size) override;

        /*
        * @brief   Deserialize data from the buffer to a tensor for cross-process data interaction.
        * @param [in] buffer  Size of the buffer to which the data is input
        * @param [in] size   Size of the buffer to which the data is input
        * @return SUCCESS   Success
        *         FAILED: failure. If the tensor does not support cross-process, this API does not need to be implemented and a failure message is returned.
        */
        virtual AIStatus DeserializeFrom(const void* buffer, const uint32_t size) override;
#endif

        /*
       @brief  Build tensor description.
        */
        static AITensorDescription BuildDescription(const std::string& size = "");

        /*
        @brief   Create a tensor according to the description.
        */
        static std::shared_ptr<IAITensor> CreateTensor(const AITensorDescription& tensor_desc);

        bool IsSupportZerocpy();
        /*
        @brief   Set the data address.
        @param [in] data  Data pointer
       @param [in] size  Size
        @param [in] isown   Whether the tensor releases the data address memory after the tensor life cycle ends.
        @param [in] is_data_support_mem_share_   Whether the tensor releases the data address memory after the tensor life cycle ends.
        */
        void SetBufferAttr(void *data, int32_t size, bool isowner, bool is_support_mem_share) override;

    private:
        void* data_;
        uint32_t size_;
        bool isowner_ ;

        static std::string type_name_;
    }