海南建设厅网站资质查询婺源网站建制作
海南建设厅网站资质查询,婺源网站建制作,效果营销型网站建设,怎么查网站做404页面没Nano-vLLM-Ascend
项目链接#xff1a;https://github.com/linzm1007/nano-vllm-ascend
nano-vllm是外网开源的一个gpu推理项目#xff0c;基于开源版本弄的一个ascend npu版本推理小demo#xff0c;旨在帮助初学者了解推理的整体流程#xff0c;区别于vllm#xff0c;nan…Nano-vLLM-Ascend项目链接https://github.com/linzm1007/nano-vllm-ascendnano-vllm是外网开源的一个gpu推理项目基于开源版本弄的一个ascend npu版本推理小demo旨在帮助初学者了解推理的整体流程区别于vllmnano-vllm体量更小麻雀虽小五脏俱全更有助于初学者学习非常适合用于相关概念的理解。框架层流程图模型层流程图特性可读代码库- 核心约2428行Python代码的清晰实现⚡优化套件- 张量并行、torchair Ascend IR图编译和图缓存、融合算子、前缀缓存等[✅] 待完成目前只支持单算子, npu图模式实现[✅] 支持CPU环境运行nano-vllm-cpu 代码仓库[✅] 性能优化[⏳] 支持模型: Qwen3-0.6B、Qwen3-32B、Qwen2-0.5B、Qwen2.5-0.5B、Qwen2.5-0.5B-Instruct、Llama-3.2-1B-Instruct、Qwen3-30B-A3B、Qwen3-VL-2B-Instruct、MiniCPM4-0.5B[✅] 支持一个moe模型:Qwen3-30B-A3B(暂时不支持入图)[] 支持一个omni模型[✅] 支持一个vl模型:Qwen3-VL-2B-Instruct(暂时不支持入图)[✅] 实现page attention[] 实现一个自定义算子[] 支持在线推理torchair接口参考 https://www.hiascend.com/document/detail/zh/Pytorch/710/modthirdparty/torchairuseguide/torchair_00008.html融合算子接口参考 https://www.hiascend.com/document/detail/zh/Pytorch/720/apiref/torchnpuCustomsapi/context/torch_npu-npu_fused_infer_attention_score_v2.mdattention实现参考 https://gitee.com/omniai/omniinfer/blob/master/omni/layers/attention/backend/attention.py forward_vanilla函数支持的模型架构模型示例 HF 模型Qwen3ForCausalLMQwen3-0.6B,Qwen3-32BQwen2ForCausalLMQwen2-0.5BLlamaForCausalLMLlama-3.2-1B-InstructQwen3MoeForCausalLMQwen3-30B-A3BQwen3VLForConditionalGenerationQwen2.5-VL-3B-InstructMiniCPMForCausalLMMiniCPM4-0.5B代码行数 总体数据范围文件数总行数占比nanovllm 全部20 个4,652 行100%models 目录5 个2,224 行47.8%除 models 外15 个2,428 行52.2%推理优化技术大纲完整技术文档LLM 推理优化技术大纲本文档整理了 LLM 推理领域的20 大类关键技术分为 7 个层次 核心技术基础必备KV Cache 管理PageAttention、Prefix Caching、KV Cache 压缩Attention 优化FlashAttention、GQA/MQA、稀疏注意力批处理策略Continuous Batching、Dynamic Batching 性能优化进阶量化技术INT8/INT4/FP8、AWQ、GPTQ、GGUF投机采样Speculative Decoding、Medusa、Lookahead解码优化Parallel Decoding、Token Tree Verification️ 系统架构调度策略FCFS、SJF、Priority-based、Preemption内存优化Memory Pool、Swapping、Offloading并行策略Tensor/Pipeline/Expert/Sequence Parallelism 特殊场景长上下文RoPE Scaling、StreamingLLM、Ring Attention多模态Vision-Language、Audio-Language、Unified ArchitectureMoE 优化Expert Routing、Load Balancing、All-to-All 通信⚡ 底层优化图编译TorchAir、TensorRT-LLM、Torch.compile算子融合QKV Fusion、Custom CUDA/Triton Kernels通信优化NCCL/HCCL、RDMA、GPUDirect 评估观测性能分析Memory/Compute Profiling、Roofline Analysis关键指标TTFT、TPOT、Throughput、GPU Utilization 前沿趋势模型架构Mamba/RWKV、Mixture of Depths、RetNet服务化Disaggregated Serving、Elastic Scaling新兴方向推理蒸馏、Early Exit、Hardware-Aware NASAttentionPageAttentionPageAttention是 vLLM 的核心创新技术灵感来自操作系统的虚拟内存分页机制用于高效管理 LLM 推理中的 KV Cache。核心概念传统 KV Cache 分配方式会为每个序列预分配最大可能长度的连续内存导致严重的内存浪费和碎片。PageAttention 借鉴操作系统分页思想将 KV Cache 划分为固定大小的 block按需动态分配。关键技术点技术说明Block 管理将 KV Cache 划分为固定大小的 block如 16/32 tokens每个 block 独立分配Block Table类似页表的数据结构记录逻辑 token 位置到物理 block 的映射关系非连续存储同一序列的 KV Cache 可以分散在多个不连续的 block 中内存共享并行解码如 beam search时可共享 prompt 的 KV cacheCopy-on-Write写时复制机制仅在需要修改时才复制 block内存使用对比传统方式 - 序列长度 1000最大支持 4096 - 内存占用4096 * hidden_size - 浪费率约 75% PageAttention - 序列长度 1000block_size16 - 需要 block1000/16 63 个 - 实际分配63 * 16 1008 tokens - 浪费率仅 0.8%代码实现/* by 01022.hk - online tools website : 01022.hk/zh/whois.html */ # nanovllm/layers/attention.py # Block Table 映射 block_table context.block_tables # 映射表 # Slot Mapping - 将 token 映射到 block 中的具体位置 # slot_mapping 格式[block_idx, offset_in_block] context.slot_mapping # 分页存储 KV Cache torch_npu._npu_reshape_and_cache( k, v, k_cache.view(num_blocks, block_size, num_kv_heads, head_dim), v_cache.view(num_blocks, block_size, num_kv_heads, head_dim), slot_mapping.int() )优势内存效率按需分配无内部碎片动态扩展序列增长时只需分配新 block内存共享多个序列可共享相同的 prompt KV Cache高吞吐量支持更大的 batch size详细对比详细技术文档HuggingFace Transformers 早期实现与 PageAttention 对比包含早期 Transformers 代码实现分析内存浪费的量化对比75% vs 6.25%不同 batch size 和序列长度的详细对比表vLLM 论文数据来源说明实际代码示例和场景分析FlashAttentionFlashAttention是斯坦福大学提出的IO 感知注意力优化算法通过分块计算和减少 HBM高带宽内存访问来提升性能。核心问题标准 Attention 实现需要存储中间结果注意力矩阵到 HBM导致内存瓶颈HBM 带宽远低于计算速度O(N²) 内存序列长度的平方级内存增长多次数据搬运Q、K、V 需要多次读写 HBM核心创新技术原理效果Tiling分块将 Q、K、V 分块加载到高速 SRAM减少 HBM 访问次数Online Softmax流式计算 softmax无需完整注意力矩阵内存降至 O(N)Recomputation反向传播时重新计算中间值牺牲计算换内存Kernel Fusion多个操作融合为单个 CUDA kernel减少 kernel 启动开销内存层次结构对比GPU 内存层次 ┌─────────────────────────────────────┐ │ HBM (High Bandwidth Memory) │ ← 1.5 TB/s容量大但速度慢 │ - 容量40-80 GB │ ← 标准 Attention 在此频繁读写 │ - 延迟高 │ ├─────────────────────────────────────┤ │ SRAM (Static RAM / Shared Memory) │ ← 19 TB/s容量小但速度快 │ - 容量~100 KB per SM │ ← FlashAttention 主要在此计算 │ - 延迟极低 │ └─────────────────────────────────────┘计算流程对比标准 Attention1. 从 HBM 加载 Q, K, V 2. 计算 S QK^T → 写入 HBM 3. 计算 P softmax(S) → 写入 HBM 4. 计算 O PV → 写入 HBM ❌ 多次 HBM 读写内存占用 O(N²)FlashAttention1. 分块加载 Qᵢ, Kⱼ, Vⱼ 到 SRAM 2. 在 SRAM 中计算 softmax 3. 累加结果到输出 4. 丢弃中间结果重复直到完成 ✅ 仅需 O(N) 内存大幅减少 HBM 访问代码实现/* by 01022.hk - online tools website : 01022.hk/zh/whois.html */ # nanovllm/layers/attention_ori.py from flash_attn import ( flash_attn_varlen_func, # Prefill 阶段 flash_attn_with_kvcache # Decode 阶段 ) # Prefill - 处理变长序列支持 PageAttention flash_attn_varlen_func( q, k, v, max_seqlen_qmax_seqlen_q, cu_seqlens_qcu_seqlens_q, # 累计长度支持变长 causalTrue, # 因果掩码 block_tableblock_table # PageAttention block table ) # Decode - 单 token 推理复用分页 KV Cache flash_attn_with_kvcache( q.unsqueeze(1), # [batch, 1, num_heads, head_dim] k_cache, v_cache, # 分页 KV 缓存 cache_seqlenscontext_lens, # 实际序列长度 block_tableblock_table # block table 映射 )性能收益内存效率从 O(N²) 降至 O(N)计算速度A100 上可达2-4 倍加速序列长度支持更长的上下文如 100K tokens三种 Attention 实现对比本项目包含三种 Attention 实现适用于不同场景特性attention_ori.pyattention.pyattention_torch_native.py底层实现Flash Attention 库NPU 原生算子PyTorch 原生适用平台CUDA GPU华为昇腾 NPU通用CPU/GPU性能⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐可读性⭐⭐⭐⭐⭐⭐⭐⭐⭐用途生产环境GPU生产环境NPU学习调试选择建议生产环境GPU使用attention_ori.pyFlash Attention生产环境昇腾 NPU使用attention.pyNPU 算子学习/调试使用attention_torch_native.py易于理解文档索引本目录包含项目各模块的详细流程图文档使用 Mermaid 语法绘制。Engine 模块流程图文档描述docs/engine/sequence_flowchart.mdSequence 序列状态管理流程docs/engine/block_manager_flowchart.mdBlockManager KV缓存块管理流程docs/engine/scheduler_flowchart.mdScheduler 调度器流程docs/engine/model_runner_flowchart.mdModelRunner 模型运行器流程docs/engine/llm_engine_flowchart.mdLLMEngine 主引擎流程Layers 模块流程图文档描述docs/layers/linear_flowchart.md线性层与张量并行策略docs/layers/attention_torch_native_flowchart.mdPyTorch原生Attention实现docs/layers/attention_flowchart.mdNPU专用Attention实现docs/layers/attention_ori_flowchart.mdFlash Attention优化实现docs/layers/sampler_flowchart.md采样器温度采样docs/layers/rotary_embedding_flowchart.mdRoPE位置编码docs/layers/layernorm_flowchart.mdRMSNorm归一化层docs/layers/embed_head_flowchart.md词嵌入与LM Headdocs/layers/activation_flowchart.mdSwiGLU激活函数Models 模块流程图文档描述docs/models/llama_flowchart.mdLlama模型架构docs/models/qwen3_flowchart.mdQwen3模型架构docs/models/qwen3_vl_flowchart.mdQwen3-VL多模态模型架构docs/models/qwen3_moe_flowchart.mdQwen3-MoE稀疏专家模型架构docs/models/mini_cpm4_flowchart.mdMiniCPM4模型架构docs/models/models_map_flowchart.md模型注册映射关系bench数据仅供参考硬软条件不同跑出的数据也会有差异不同模型对比modelOutput TokensTime (s)Throughput (tokens/s)TPQwen3-0.6B143,77036.823904.201Qwen2-0.5B143,77020.716940.841Qwen2.5-0.5B-Instruct143,77019.827252.671Llama-3.2-1B-Instruct143,77025.455648.501Qwen3-32B143,770206.69695.592Qwen3-32B143,770119.861199.504其他框架对比(2025-12-30)vLLM Nano-vLLM 数据来源 https://github.com/GeeeekExplorer/nano-vllmInference EngineOutput TokensTime (s)Throughput (tokens/s)vLLM133,96698.371361.84Nano-vLLM133,96693.411434.13Nano-vLLM-Ascend python torch原生实现4805257.4918.66Nano-vLLM-Ascend 融合算子图编译bs256133,96633.883954.20图模式不同bs对比(2025-12-30)Batch SizeOutput TokensTime (s)Throughput (tokens/s)bs16133,966107.231249.37bs32133,96675.891765.35bs48133,96664.842066.22bs64133,96654.062478.31bs128133,96643.083109.56bs256133,96633.883954.20单算子Padding和Non-padding对比(2025-12-30)bs256Prepare StrategyOutput TokensTime (s)Throughput (tokens/s)Padding133,966158.46845.41Non-padding133,966152.14880.55环境搭建参考vllm-ascendhttps://docs.vllm.ai/projects/vllm-ascend-cn/zh-cn/latest/quick_start.htmlubuntu# Update DEVICE according to your device (/dev/davinci[0-7]) export DEVICE/dev/davinci0 # Update the vllm-ascend image # Atlas A2: # export IMAGEquay.io/ascend/vllm-ascend:v0.14.0rc1 # Atlas A3: # export IMAGEquay.io/ascend/vllm-ascend:v0.14.0rc1-a3 export IMAGEquay.io/ascend/vllm-ascend:v0.14.0rc1 docker run --rm \ --name vllm-ascend \ --shm-size1g \ --device $DEVICE \ --device /dev/davinci_manager \ --device /dev/devmm_svm \ --device /dev/hisi_hdc \ -v /usr/local/dcmi:/usr/local/dcmi \ -v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \ -v /usr/local/Ascend/driver/lib64/:/usr/local/Ascend/driver/lib64/ \ -v /usr/local/Ascend/driver/version.info:/usr/local/Ascend/driver/version.info \ -v /etc/ascend_install.info:/etc/ascend_install.info \ -v /root/.cache:/root/.cache \ -p 8000:8000 \ -it $IMAGE bash # Install curl apt-get update -y apt-get install -y curl安装依赖pip install .模型下载huggingface-cli download --resume-download Qwen/Qwen3-0.6B \ --local-dir ~/huggingface/Qwen3-0.6B/ \ --local-dir-use-symlinks False快速开始请参见 example.py 了解用法。该 API 与 vLLM 的接口基本一致仅在 LLM.generate 方法上存在一些细微差异from nanovllm import LLM, SamplingParams llm LLM(/YOUR/MODEL/PATH, enforce_eagerTrue, tensor_parallel_size1) sampling_params SamplingParams(temperature0.6, max_tokens256) prompts [Hello, Nano-vLLM.] outputs llm.generate(prompts, sampling_params) outputs[0][text]example运行结果bench环境仅供参考ascend-dmi -c #查看硬件环境1.显卡:A3 910C2.驱动版本:24.1.rc3.103.固件版本:7.5.0.109.220软件环境1.CANN包 8.3.RC12.PTA版本torch-npu 2.5.1.post2gitd7a85f8torch 2.5.1BenchmarkSeebench.pyfor benchmark.Test Configuration:Model: Qwen3-0.6BTotal Requests: 256 sequencesInput Length: Randomly sampled between 100–1024 tokensOutput Length: Randomly sampled between 100–1024 tokensPerformance Results:Nano-vLLM-Ascend 实在太慢了只跑了10条seqInference EngineOutput TokensTime (s)Throughput (tokens/s)vLLM133,96698.371361.84Nano-vLLM133,96693.411434.13Nano-vLLM-Ascend4805257.4918.66qwen3-0.6B layersModuleList( (0-27): 28 x Qwen3DecoderLayer( (self_attn): Qwen3Attention( (qkv_proj): QKVParallelLinear() (o_proj): RowParallelLinear() (rotary_emb): RotaryEmbedding() (attn): Attention() (q_norm): RMSNorm() (k_norm): RMSNorm() ) (mlp): Qwen3MLP( (gate_up_proj): MergedColumnParallelLinear() (down_proj): RowParallelLinear() (act_fn): SiluAndMul() ) (input_layernorm): RMSNorm() (post_attention_layernorm): RMSNorm() ) )蓝天和白云是标配。