【Cute学学习笔记】print_tensor打印error

经过实践,cute中使用print_tensor打印一个tensor,如果类型是half是会直接编译不过的:

cpp 复制代码
#include <cuda.h>
#include <stdlib.h>
#include <cute/tensor.hpp>

/*
    cute中的Tensor更多的是对Tensor进行分解和组合等操作,而这些操作多是对Layout的变换(只是逻辑层面的数据组织形式),底层的数据实体一般不变更。
    Tensor = Layout + storage
*/

// nvcc tensor.cu -arch=sm_89 -std=c++17 -I ../cutlass/include -I ../cutlass/tools/util/include --expt-relaxed-constexpr -cudart shared --cudadevrt none  -DDEBUG

using namespace cute;
using namespace std;

#define PRINT(name, content) \
    print(name);             \
    print(" : ");            \
    print(content);          \
    print("\n");

#define PRINTTENSOR(name, content) \
    print(name);                   \
    print(" : ");                  \
    print_tensor(content);         \
    print("\n");

template<typename T>
__global__ void handle_global_tensor(T *pointer)
{
    auto gshape = make_shape(Int<4>{}, Int<6>{});
    auto gstride = make_stride(Int<6>{}, Int<1>{});
    auto gtensor = make_tensor(make_gmem_ptr(pointer), make_layout(gshape, gstride));
    PRINTTENSOR("global tensor", gtensor);
}

int main()
{
    // register tensor
    // handle_regiser_tensor<<<1, 1>>>();

    // global memory tensor

    using T = half;

    T *pointer;
    int size = 4 * 6;
    cudaMalloc(&pointer, size * sizeof(T));
    T *cpointer = (T *)malloc(size * sizeof(T));
    for (int i = 0; i < size; i++)
    {
        cpointer[i] = (T)i;
    }
    cudaMemcpy(pointer, cpointer, size * sizeof(int), cudaMemcpyHostToDevice);
    handle_global_tensor<T><<<1, 1>>>(pointer);
    cudaDeviceSynchronize();
    return 0;
}

如果类型换成int或者float,是可以成功打印的:

cpp 复制代码
#include <cuda.h>
#include <stdlib.h>
#include <cute/tensor.hpp>

/*
    cute中的Tensor更多的是对Tensor进行分解和组合等操作,而这些操作多是对Layout的变换(只是逻辑层面的数据组织形式),底层的数据实体一般不变更。
    Tensor = Layout + storage
*/

// nvcc tensor.cu -arch=sm_89 -std=c++17 -I ../cutlass/include -I ../cutlass/tools/util/include --expt-relaxed-constexpr -cudart shared --cudadevrt none  -DDEBUG

using namespace cute;
using namespace std;

#define PRINT(name, content) \
    print(name);             \
    print(" : ");            \
    print(content);          \
    print("\n");

#define PRINTTENSOR(name, content) \
    print(name);                   \
    print(" : ");                  \
    print_tensor(content);         \
    print("\n");

template<typename T>
__global__ void handle_global_tensor(T *pointer)
{
    auto gshape = make_shape(Int<4>{}, Int<6>{});
    auto gstride = make_stride(Int<6>{}, Int<1>{});
    auto gtensor = make_tensor(make_gmem_ptr(pointer), make_layout(gshape, gstride));
    PRINTTENSOR("global tensor", gtensor);
}

int main()
{
    using T = float;

    T *pointer;
    int size = 4 * 6;
    cudaMalloc(&pointer, size * sizeof(T));
    T *cpointer = (T *)malloc(size * sizeof(T));
    for (int i = 0; i < size; i++)
    {
        cpointer[i] = (T)i;
    }
    cudaMemcpy(pointer, cpointer, size * sizeof(int), cudaMemcpyHostToDevice);
    handle_global_tensor<T><<<1, 1>>>(pointer);
    cudaDeviceSynchronize();
    return 0;
}

惊喜发现,不能直接用half,得用cute::half_t,这样的是可以打印的

cpp 复制代码
#include <cuda.h>
#include <stdlib.h>
#include <cute/tensor.hpp>

/*
    cute中的Tensor更多的是对Tensor进行分解和组合等操作,而这些操作多是对Layout的变换(只是逻辑层面的数据组织形式),底层的数据实体一般不变更。
    Tensor = Layout + storage
*/

// nvcc tensor.cu -arch=sm_89 -std=c++17 -I ../cutlass/include -I ../cutlass/tools/util/include --expt-relaxed-constexpr -cudart shared --cudadevrt none  -DDEBUG

using namespace cute;
using namespace std;

#define PRINT(name, content) \
    print(name);             \
    print(" : ");            \
    print(content);          \
    print("\n");

#define PRINTTENSOR(name, content) \
    print(name);                   \
    print(" : ");                  \
    print_tensor(content);         \
    print("\n");

template<typename T>
__global__ void handle_global_tensor(T *pointer)
{
    auto gshape = make_shape(Int<4>{}, Int<6>{});
    auto gstride = make_stride(Int<6>{}, Int<1>{});
    auto gtensor = make_tensor(make_gmem_ptr(pointer), make_layout(gshape, gstride));
    PRINTTENSOR("global tensor", gtensor);
}

int main()
{
    using T = cute::half_t;
    // using T = half;

    T *pointer;
    int size = 4 * 6;
    cudaMalloc(&pointer, size * sizeof(T));
    T *cpointer = (T *)malloc(size * sizeof(T));
    for (int i = 0; i < size; i++)
    {
        cpointer[i] = 1;
    }
    cudaMemcpy(pointer, cpointer, size * sizeof(T), cudaMemcpyHostToDevice);
    handle_global_tensor<T><<<1, 1>>>(pointer);
    cudaDeviceSynchronize();
    return 0;
}
相关推荐
做cv的小昊12 小时前
【TJU】信息检索与分析课程笔记和练习(1)认识文献
经验分享·笔记·学习·搜索引擎·全文检索
读创商闻12 小时前
崇明岛西滩湿地:离都市最近的候鸟观测笔记
笔记
蒙奇D索大12 小时前
【11408学习记录】考研英语长难句拆解三步法:三步拆解2020年真题,攻克阅读难点
笔记·学习·考研·改行学it
悠闲漫步者12 小时前
第2章 MCS-51单片机的串口和最小系统(学习笔记)
笔记·学习·51单片机
莫白媛13 小时前
Linux创作笔记综合汇总篇
linux·运维·笔记
Wpa.wk13 小时前
Tomcat的安装与部署使用 - 说明版
java·开发语言·经验分享·笔记·tomcat
Vincent_Zhang23313 小时前
专题:所有状语类型(持续补充)
笔记
wdfk_prog13 小时前
[Linux]学习笔记系列 -- [fs]buffer
linux·笔记·学习
海奥华214 小时前
进程调度算法 笔记总结
linux·运维·服务器·笔记·学习
即将进化成人机14 小时前
验证码生成 + Redis 暂存 + JWT 认证
数据库·redis·笔记