<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Frame C++ 示例 on Secaumene</title><link>https://secaumene.github.io/frame/examples/</link><description>Recent content in Frame C++ 示例 on Secaumene</description><generator>Hugo -- gohugo.io</generator><language>zh</language><copyright>&lt;a href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank" rel="noopener">CC BY-NC 4.0&lt;/a></copyright><atom:link href="https://secaumene.github.io/frame/examples/index.xml" rel="self" type="application/rss+xml"/><item><title>示例 01：最短快速入门</title><link>https://secaumene.github.io/frame/examples/00_quickstart/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://secaumene.github.io/frame/examples/00_quickstart/</guid><description>本页是01 仓外 C++ 项目的完整可运行源码。
#include &amp;lt;iostream&amp;gt; #include &amp;lt;frame/core/shape.h&amp;gt; int main() { std::cout &amp;lt;&amp;lt; frame::Shape({2, 3}).numel() &amp;lt;&amp;lt; &amp;#39;\n&amp;#39;; }</description></item><item><title>示例 02：张量基础</title><link>https://secaumene.github.io/frame/examples/01_tensor_basics/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://secaumene.github.io/frame/examples/01_tensor_basics/</guid><description>本页是01 仓外 C++ 项目的完整可运行源码。
// ============================================================================= // 示例 01:Tensor 基础。 // // 学习目标:分配 CPU Tensor,查询 shape 与 dtype,并完成强类型数据读写。 // 前置章节:help/01-quickstart/README.md。 // 预期 PASS:程序返回 0,并打印设备、shape、dtype 与六个确定数值。 // 运行边界:本例只讲 Tensor;编译执行整图路径见示例 02。 // ============================================================================= #include &amp;lt;cstdint&amp;gt; #include &amp;lt;iostream&amp;gt; #include &amp;lt;frame/frame.h&amp;gt; int main() { // Tensor::empty 需要一个 Allocator&amp;amp;:经 BackendRegistry 取 cpu 参考后端 // (永远启用,见 include/frame/core/device.h)。 const frame::Result&amp;lt;frame::hal::Backend*&amp;gt; backend_result = frame::hal::BackendRegistry::instance().get(frame::kCpuBackendName); if (!backend_result.is_ok()) { std::cerr &amp;lt;&amp;lt; &amp;#34;failed to get cpu backend: &amp;#34; &amp;lt;&amp;lt; backend_result.status().message() &amp;lt;&amp;lt; &amp;#34;\n&amp;#34;; return 1; } frame::hal::Backend* backend = backend_result.</description></item><item><title>示例 03：图捕获、编译与执行</title><link>https://secaumene.github.io/frame/examples/02_graph_compile/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://secaumene.github.io/frame/examples/02_graph_compile/</guid><description>本页是02 核心概念的完整可运行源码。
// ============================================================================= // 示例 02:图编译执行。 // // 学习目标:完成建图、编译、整图执行与确定结果校验。 // 前置章节:help/02-core-concepts/README.md。 // 预期 PASS:打印所选后端及匹配期望值的 PASS 行。 // 运行边界:默认使用 CPU;传入 cuda 时真实执行 H2D、CUDA 整图与 D2H。 // ============================================================================= #include &amp;lt;algorithm&amp;gt; #include &amp;lt;cstddef&amp;gt; #include &amp;lt;cstdint&amp;gt; #include &amp;lt;iostream&amp;gt; #include &amp;lt;memory&amp;gt; #include &amp;lt;string_view&amp;gt; #include &amp;lt;utility&amp;gt; #include &amp;lt;vector&amp;gt; #include &amp;lt;frame/core/device.h&amp;gt; #include &amp;lt;frame/core/dtype.h&amp;gt; #include &amp;lt;frame/core/shape.h&amp;gt; #include &amp;lt;frame/core/status.h&amp;gt; #include &amp;lt;frame/core/tensor.h&amp;gt; #include &amp;lt;frame/hal/allocator.h&amp;gt; #include &amp;lt;frame/hal/backend.h&amp;gt; #include &amp;lt;frame/hal/executable.h&amp;gt; #include &amp;lt;frame/hal/stream.h&amp;gt; #include &amp;lt;frame/ir/graph.h&amp;gt; #include &amp;lt;frame/ir/node.h&amp;gt; #include &amp;lt;frame/runtime/compile.h&amp;gt; namespace { // 打印失败 Status 到 stderr 并返回 false;调用方据此立即返回。 bool check_example_02_status(const frame::Status&amp;amp; status, std::string_view operation) { if (status.</description></item><item><title>示例 04：自定义算子</title><link>https://secaumene.github.io/frame/examples/03_custom_op/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://secaumene.github.io/frame/examples/03_custom_op/</guid><description>本页是09 添加算子的完整可运行源码。
// ============================================================================= // 示例 03:自定义算子扩展。 // // 学习目标:通过 schema 与 CPU kernel 注册扩展算子,再走标准编译路径执行。 // 前置章节:help/09-add-operator/README.md。 // 预期 PASS:打印 scaled_relu 的确定结果与注册算子执行成功的 PASS 行。 // 运行边界:本例只注册 CPU kernel,不演示其他后端实现。 // ============================================================================= #include &amp;lt;algorithm&amp;gt; #include &amp;lt;cstdint&amp;gt; #include &amp;lt;iostream&amp;gt; #include &amp;lt;memory&amp;gt; #include &amp;lt;string&amp;gt; #include &amp;lt;type_traits&amp;gt; #include &amp;lt;variant&amp;gt; #include &amp;lt;vector&amp;gt; #include &amp;lt;frame/frame.h&amp;gt; #include &amp;lt;frame/ops/graph_builder.h&amp;gt; // ----------------------------------------------------------------------------- // 步骤 1:声明算子契约(schema)。 // // 约定:算子名须匹配 ^[a-z][a-z0-9_]*$ 且全局唯一(ARCH-040),重名/非法名在启动期 // 报错(英文消息)。builder 方法链式返回 *this。此处以一个逐元素 &amp;#34;scaled_relu&amp;#34; // (输出 = max(0, x) * scale)为例。 // ----------------------------------------------------------------------------- namespace { // scaled_relu 的 shape 推断:逐元素算子,恰 1 输入,输出 shape 恒等于输入 shape。 frame::Result&amp;lt;std::vector&amp;lt;frame::Shape&amp;gt;&amp;gt; infer_scaled_relu_shape( const frame::ops::NodeContext&amp;amp; ctx) { if (ctx.</description></item><item><title>示例 05：NN 与数据加载</title><link>https://secaumene.github.io/frame/examples/04_nn_and_data/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://secaumene.github.io/frame/examples/04_nn_and_data/</guid><description>本页是04 nn 与数据的完整可运行源码。
// ============================================================================= // 示例 04:nn 与数据。 // // 学习目标:迭代固定形状的小批数据,并用 nn 模块构造、验证静态图。 // 前置章节:help/04-nn-and-data/README.md。 // 预期 PASS:打印 DataLoader epoch 与神经网络图验证成功的 PASS 行。 // 运行边界:DataLoader 只操作 CPU Tensor;nn::Module::build 只构图、不执行数值。 // ============================================================================= #include &amp;lt;cstdint&amp;gt; #include &amp;lt;iostream&amp;gt; #include &amp;lt;optional&amp;gt; #include &amp;lt;utility&amp;gt; #include &amp;lt;vector&amp;gt; #include &amp;lt;frame/core/device.h&amp;gt; #include &amp;lt;frame/core/dtype.h&amp;gt; #include &amp;lt;frame/core/shape.h&amp;gt; #include &amp;lt;frame/core/status.h&amp;gt; #include &amp;lt;frame/core/tensor.h&amp;gt; #include &amp;lt;frame/data/dataloader.h&amp;gt; #include &amp;lt;frame/data/dataset.h&amp;gt; #include &amp;lt;frame/hal/allocator.h&amp;gt; #include &amp;lt;frame/hal/backend.h&amp;gt; #include &amp;lt;frame/ir/graph.h&amp;gt; #include &amp;lt;frame/ir/node.h&amp;gt; #include &amp;lt;frame/nn/layers.h&amp;gt; #include &amp;lt;frame/nn/module.h&amp;gt; #include &amp;lt;frame/ops/op_registry.h&amp;gt; namespace { // 构造本示例统一使用的 float32 CPU 静态类型。 frame::ir::TensorType make_example_04_tensor_type(std::vector&amp;lt;int64_t&amp;gt; dims) { frame::ir::TensorType type; type.</description></item><item><title>示例 06：完整训练闭环</title><link>https://secaumene.github.io/frame/examples/05_training/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://secaumene.github.io/frame/examples/05_training/</guid><description>本页是05 训练的完整可运行源码。
// ============================================================================= // 示例 05:CPU 编译期训练。 // // 学习目标:构造前向、反向与 SGD 更新图,编译一次并复用 Executable 完成训练。 // 前置章节:help/05-training/README.md。 // 预期 PASS:打印下降的初始/最终 loss 与训练图复用成功的 PASS 行。 // 运行边界:完整训练闭环固定使用 CPU;真实 CUDA 图执行见示例 02。 // ============================================================================= #include &amp;lt;cmath&amp;gt; #include &amp;lt;cstdint&amp;gt; #include &amp;lt;iostream&amp;gt; #include &amp;lt;memory&amp;gt; #include &amp;lt;random&amp;gt; #include &amp;lt;utility&amp;gt; #include &amp;lt;vector&amp;gt; #include &amp;lt;frame/compiler/autograd.h&amp;gt; #include &amp;lt;frame/core/device.h&amp;gt; #include &amp;lt;frame/core/dtype.h&amp;gt; #include &amp;lt;frame/core/shape.h&amp;gt; #include &amp;lt;frame/core/status.h&amp;gt; #include &amp;lt;frame/core/tensor.h&amp;gt; #include &amp;lt;frame/hal/allocator.h&amp;gt; #include &amp;lt;frame/hal/backend.h&amp;gt; #include &amp;lt;frame/hal/executable.h&amp;gt; #include &amp;lt;frame/ir/graph.h&amp;gt; #include &amp;lt;frame/ir/node.h&amp;gt; #include &amp;lt;frame/nn/layers.h&amp;gt; #include &amp;lt;frame/nn/module.</description></item><item><title>示例 07：ONNX initializer 权重交换</title><link>https://secaumene.github.io/frame/examples/06_onnx_weights/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://secaumene.github.io/frame/examples/06_onnx_weights/</guid><description>本页是07 C++ 与工具的完整可运行源码。
// ============================================================================= // 示例 06:ONNX initializer 权重。 // // 学习目标:保存具名 CPU 权重,重新加载并按名称验证 dtype、shape 与数值。 // 前置章节:help/07-cpp-and-tools/README.md。 // 预期 PASS:打印 ONNX initializer 权重按名称往返验证成功的 PASS 行。 // 运行边界:只交换 graph.initializer,不是 ONNX 算子图导入器。 // ============================================================================= #include &amp;lt;filesystem&amp;gt; #include &amp;lt;iostream&amp;gt; #include &amp;lt;string&amp;gt; #include &amp;lt;system_error&amp;gt; #include &amp;lt;unordered_map&amp;gt; #include &amp;lt;vector&amp;gt; #include &amp;lt;frame/core/device.h&amp;gt; #include &amp;lt;frame/core/dtype.h&amp;gt; #include &amp;lt;frame/core/shape.h&amp;gt; #include &amp;lt;frame/core/status.h&amp;gt; #include &amp;lt;frame/core/tensor.h&amp;gt; #include &amp;lt;frame/hal/allocator.h&amp;gt; #include &amp;lt;frame/hal/backend.h&amp;gt; #include &amp;lt;frame/interop/onnx_weights.h&amp;gt; int main() { // 文件名固定且位于当前工作目录;CTest 为本示例设置独立二进制目录。 const std::filesystem::path file_path = &amp;#34;frame_example_06_weights.</description></item></channel></rss>