-
Notifications
You must be signed in to change notification settings - Fork 99
issue/896 - 为c++和python中的tensor添加打印函数 #930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
|
||
| import infinicore.context as context | ||
| import infinicore.nn as nn | ||
| from infinicore._tensor_str import printoptions, set_printoptions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
torch中把print的一些 函数调用放到了_tensor_str.py文件中,于是也新建了一个 _tensor_str
| "strided_from_blob", | ||
| "zeros", | ||
| "set_printoptions", | ||
| "printoptions", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
torch中存在一下两个函数
"set_printoptions":全局配置的函数
"printoptions": 临时配置的函数
| std::string repr = m1.extra_repr(); | ||
| spdlog::debug("Linear module representation: {}", repr); | ||
|
|
||
| // Test forward with residual connection |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c++的linear有个版移除了一个函数,导致c++测试编译不过。于是,在src/infinicore-test/test_nn_module.cc中注释或删除了传递的参数
|
|
||
| // Prepare test data (stored outside loop to ensure lifetime) | ||
| bool bool_data[4] = {true, false, true, false}; | ||
| int8_t i8_data[4] = {-128, -64, 32, 127}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
由于infinicore.Tensor没有rand函数,没法生成随机数,所以就事前固定了测试数据
| set_installdir(os.getenv("INFINI_ROOT") or (os.getenv(is_host("windows") and "HOMEPATH" or "HOME") .. "/.infini")) | ||
| target_end() | ||
|
|
||
| target("infinicore-test") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c++的infinicore的测试脚本出错,于是添加上了缺少的"infinicore_cpp_api"依赖
| template <typename T> | ||
| T item_impl(const std::byte *data, DataType dtype) { | ||
| switch (dtype) { | ||
| case DataType::F16: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
由于c++没有原生的F16和BF16格式数据,为了能cout打印,把F16和BF16转成了float类型去显示
目标版本
main
功能描述
c++和python中的tensor添加打印函数,可以全局或临时配置一下参数:
(1) 触发省略的阈值:数据量过多时,显示部分数据;
(2) 每行显示字符数量:显示宽度合适,自动换行;
(3) 精度:设置显示的小数点位数;
(4) edgeitems: 省略显示时,设置显示的数据数量
c++的cout函数支持的类型: BOOL,I8,I16,I32,I64,U8,U16,U32,U64,BF16,F16,F32,F64
python的print函数支持的类型: BOOL,I8,I16,I32,I64,U8,BF16,F16,F32,F64
python代码std::cout测试:

临时修改后,自动恢复原始配置
全局修改后,配置生效

print不同的数据类型

python代码print测试:

临时修改后,自动恢复原始配置
全局修改后,配置生效

cout不同的数据类型
