资讯动态

CANN/ascend-transformer-boost算子演示示例

发布时间:2026/8/18 19:41:10 来源:尧图企业网站定制
TransdataOp使用说明【免费下载链接】ascend-transformer-boost本项目是CANN提供的是一款高效、可靠的Transformer加速库基于华为Ascend AI处理器提供Transformer定制化场景的高性能融合算子。项目地址: https://gitcode.com/cann/ascend-transformer-boostTransdataOp功能TransdataOp函数用于创建并执行一个transdata操作将输入的 ND 格式的tensor转换为FRACTAL_NZ后文简称为NZ格式的tensor。函数介绍atb::Status TransdataOp(atb::Context *contextPtr, aclrtStream stream, const atb::Tensor inTensor, const aclDataType tensorType, atb::Tensor outTensor, std::vectorint64_t shape)输入参数参数名称含义contextPtr指向atb::Context的指针用于提供上下文信息stream用于执行操作的流inTensor待转换的 ND 格式的tensortensorType输入 tensor 的数据类型outTensor输出 tensoroutTensor输出的FRACTAL_NZ格式的 tensor函数执行后将被填充shape输出 tensor的维度返回值atb::Status: 函数执行的状态码表示操作是否成功。如果成功返回atb::ErrorType::NO_ERROR否则返回相应的错误码。GetShape功能GetShape函数用于根据输入的inShape判断其是 NZ格式的shape还是ND 格式的shape然后分别计算出两种格式下的shape。函数介绍atb::Status GetShape(const aclDataType tensorType, const std::vectorint64_t inShape, std::vectorint64_t ndShape, std::vectorint64_t nzShape)输入参数参数名称含义tensorType输入 tensor 的数据类型不同数据类型的tensor的格式转换方式不同inShape输入 tensor 的维度ndShape输出的 ND 格式的 shapenzShape输出的 NZ 格式的 shape返回值atb::Status: 函数执行的状态码表示操作是否成功。如果成功返回atb::ErrorType::NO_ERROR否则返回相应的错误码。Transdata使用说明注意使用TransdataOp转换ND格式的tensor时需要保证传入的tensor是ND格式因此创建初始的tensor时应当使用ND格式下的shape而使用TransdataOp函数时传入最终想得到的NZ格式下tensor的shape在CreateTensorFromVector中只传入了一种格式下的shape因此需要使用GetShape函数得到ND和NZ两种数据格式下的shape使用TransdataOp进行ND格式tensor转NZ格式tensor的CreateTensorFromVector的内容如下/** * brief 简单封装拷贝vector data中数据以创建tensor * details 用于创建outTensorType类型的tensor * param contextPtr context指针 * param stream stream * param data 输入vector数据 * param outTensorType 期望输出tensor数据类型 * param format 输出tensor的格式即NZND等 * param shape 输出tensor的shape * param outTensor 返回创建的tensor */ template typename T atb::Status CreateTensorFromVector(atb::Context *contextPtr, aclrtStream stream, std::vectorT data, const aclDataType outTensorType, const aclFormat format, std::vectorint64_t shape, atb::Tensor outTensor, const aclDataType inTensorType ACL_DT_UNDEFINED) { atb::Tensor tensor; aclDataType intermediateType; switch (outTensorType) { case aclDataType::ACL_FLOAT16: case aclDataType::ACL_BF16: case aclDataType::ACL_DOUBLE: intermediateType aclDataType::ACL_FLOAT; break; default: intermediateType outTensorType; } if (inTensorType outTensorType inTensorType ! ACL_DT_UNDEFINED) { intermediateType outTensorType; } aclFormat tensorFormat format; std::vectorint64_t ndShape, nzShape; if (intermediateType ! outTensorType format aclFormat::ACL_FORMAT_FRACTAL_NZ) { tensorFormat aclFormat::ACL_FORMAT_ND; CHECK_STATUS(GetShape(outTensorType, shape, ndShape, nzShape)); } else { ndShape.assign(shape.begin(), shape.end()); } CHECK_STATUS(CreateTensor(intermediateType, tensorFormat, shape, tensor)); CHECK_STATUS(aclrtMemcpy(tensor.deviceData, tensor.dataSize, data.data(), sizeof(T) * data.size(), ACL_MEMCPY_HOST_TO_DEVICE)); CHECK_STATUS(CreateTensor(outTensorType, aclFormat::ACL_FORMAT_ND, ndShape, outTensor)); if (intermediateType outTensorType) { // 原始创建的tensor类型不需要转换 outTensor tensor; return atb::ErrorType::NO_ERROR; } CHECK_STATUS(CastOp(contextPtr, stream, tensor, outTensorType, outTensor)); if (format aclFormat::ACL_FORMAT_FRACTAL_NZ) { CHECK_STATUS(TransdataOp(contextPtr, stream, outTensor, outTensorType, outTensor, nzShape)); } return atb::ErrorType::NO_ERROR; }【免费下载链接】ascend-transformer-boost本项目是CANN提供的是一款高效、可靠的Transformer加速库基于华为Ascend AI处理器提供Transformer定制化场景的高性能融合算子。项目地址: https://gitcode.com/cann/ascend-transformer-boost创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价