Merged
fix(ir): Support get_tensor_data #5466
lidongshengAdd on Aug 15
fix(ir): Support get_tensor_data #5466
Merged
Merged fromtsungl4/pypto-opentensor_dataintocann/pyptomaster
Total: 5 files changed+94-1
| @@ -375,7 +375,7 @@ void RootFunctionBuilder::ComputeOutcast(Function& pathFunc) | |||
| 375 | for (auto& oOperand : op.GetOOperands()) { | 375 | for (auto& oOperand : op.GetOOperands()) { |
| 376 | bool neededByConsumer = consumedTensors_.count(oOperand) > 0; | 376 | bool neededByConsumer = consumedTensors_.count(oOperand) > 0; |
| 377 | bool isFuncOutput = paramTensors_.count(oOperand) > 0; | 377 | bool isFuncOutput = paramTensors_.count(oOperand) > 0; |
| 378 | - if (neededByConsumer || isFuncOutput) { | 378 | + if (neededByConsumer || isFuncOutput || oOperand->IsGetTensorDataOutcast()) { |
| 379 | pathFunc.AddOriginOutcast(oOperand); | 379 | pathFunc.AddOriginOutcast(oOperand); |
| 380 | } | 380 | } |
| 381 | } | 381 | } |
| @@ -590,6 +590,7 @@ std::pair<LogicalTensors, LogicalTensors> RootFunctionBuilder::FinalizeHiddenFun | |||
| 590 | 590 | ||
| 591 | BuildPathFuncSlotScope(hiddenFunc, hiddenScope, hiddenFunc->GetOriginIncast(), hiddenFunc->GetOriginOutcast()); | 591 | BuildPathFuncSlotScope(hiddenFunc, hiddenScope, hiddenFunc->GetOriginIncast(), hiddenFunc->GetOriginOutcast()); |
| 592 | 592 | ||
| 593 | + hiddenFunc->CreateTensorDataImportOp(); | ||
| 593 | auto hiddenInArgs = hiddenFunc->MakeIncasts(hiddenScope); | 594 | auto hiddenInArgs = hiddenFunc->MakeIncasts(hiddenScope); |
| 594 | auto hiddenOutArgs = hiddenFunc->MakeOutcasts(hiddenScope); | 595 | auto hiddenOutArgs = hiddenFunc->MakeOutcasts(hiddenScope); |
| 595 | for (size_t idx = 0; idx < hiddenFunc->GetOutcast().size(); idx++) { | 596 | for (size_t idx = 0; idx < hiddenFunc->GetOutcast().size(); idx++) { |
| @@ -632,6 +633,8 @@ ir::StmtPtr RootFunctionBuilder::FinalizePathFunc(const ir::StmtPtr& placeholder | |||
| 632 | pathFunc->funcType_ = ir::FunctionType::IN_CORE; | 633 | pathFunc->funcType_ = ir::FunctionType::IN_CORE; |
| 633 | 634 | ||
| 634 | // 2. hiddenFunc 设为 hidden,parent 改为 pathFunc | 635 | // 2. hiddenFunc 设为 hidden,parent 改为 pathFunc |
| 636 | + auto origFunc = program_.GetCurrentFunction(); | ||
| 637 | + program_.SetCurrentFunction(hiddenFunc); | ||
| 635 | hiddenFunc->SetHiddenFunction(true); | 638 | hiddenFunc->SetHiddenFunction(true); |
| 636 | hiddenFunc->SetParent(pathFunc.get()); | 639 | hiddenFunc->SetParent(pathFunc.get()); |
| 637 | 640 | ||
| @@ -652,6 +655,8 @@ ir::StmtPtr RootFunctionBuilder::FinalizePathFunc(const ir::StmtPtr& placeholder | |||
| 652 | } | 655 | } |
| 653 | CreateAndFinalizePathFunc(pathFunc.get(), hiddenFunc, hiddenFuncArgs.first, hiddenFuncArgs.second, placeholder); | 656 | CreateAndFinalizePathFunc(pathFunc.get(), hiddenFunc, hiddenFuncArgs.first, hiddenFuncArgs.second, placeholder); |
| 654 | 657 | ||
| 658 | + program_.SetCurrentFunction(origFunc); | ||
| 659 | + | ||
| 655 | // 5. dynFunc 的 CALL op | 660 | // 5. dynFunc 的 CALL op |
| 656 | auto& callOperation = dynFunc_->AddRawOperation(Opcode::OP_CALL, originalIncasts, originalOutcasts, | 661 | auto& callOperation = dynFunc_->AddRawOperation(Opcode::OP_CALL, originalIncasts, originalOutcasts, |
| 657 | placeholder->span_); | 662 | placeholder->span_); |
| @@ -61,6 +61,18 @@ std::vector<ir::VarPtr>& IRContext::GetDependToken(const ir::ExprPtr& val) | |||
| 61 | return token_map_[val]; | 61 | return token_map_[val]; |
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | +std::vector<ir::VarPtr> IRContext::GetDependToken(Operation& op) | ||
| 65 | +{ | ||
| 66 | + std::set<ir::VarPtr> tokenSet; | ||
| 67 | + for (auto& scalar : op.GetDynamicAttributeList()) { | ||
| 68 | + auto token = GetDependToken(scalar.get().AsExpr()); | ||
| 69 | + tokenSet.insert(token.begin(), token.end()); | ||
| 70 | + } | ||
| 71 | + std::vector<ir::VarPtr> tokenList(tokenSet.begin(), tokenSet.end()); | ||
| 72 | + std::sort(tokenList.begin(), tokenList.end(), [](ir::VarPtr a, ir::VarPtr b) { return a->name_ < b->name_; }); | ||
| 73 | + return tokenList; | ||
| 74 | +} | ||
| 75 | + | ||
| 64 | Function& DummyFunc() | 76 | Function& DummyFunc() |
| 65 | { | 77 | { |
| 66 | static auto func = []() { | 78 | static auto func = []() { |
| @@ -275,6 +287,7 @@ void IRBuilder::EmitTensorStmts() | |||
| 275 | for (auto& op : func->Operations(false)) { | 287 | for (auto& op : func->Operations(false)) { |
| 276 | auto stmt = std::dynamic_pointer_cast<ir::TensorOpStmt>(op.shared_from_this()); | 288 | auto stmt = std::dynamic_pointer_cast<ir::TensorOpStmt>(op.shared_from_this()); |
| 277 | stmt->span_ = ir::Span::Current(); | 289 | stmt->span_ = ir::Span::Current(); |
| 290 | + stmt->tokens_ = irContext_.GetDependToken(op); | ||
| 278 | Emit(stmt); | 291 | Emit(stmt); |
| 279 | } | 292 | } |
| 280 | func->ResetOperations(); | 293 | func->ResetOperations(); |
| @@ -103,6 +103,9 @@ public: | |||
| 103 | 103 | ||
| 104 | void AddDependToken(const ir::ExprPtr& val, const ir::VarPtr& token) { token_map_[val].push_back(token); } | 104 | void AddDependToken(const ir::ExprPtr& val, const ir::VarPtr& token) { token_map_[val].push_back(token); } |
| 105 | std::vector<ir::VarPtr>& GetDependToken(const ir::ExprPtr& val); | 105 | std::vector<ir::VarPtr>& GetDependToken(const ir::ExprPtr& val); |
| 106 | + | ||
| 107 | + std::vector<ir::VarPtr> GetDependToken(Operation& op); | ||
| 108 | + | ||
| 106 | int AddTensorDataDesc(const std::shared_ptr<Tensor>& assembleTensor) | 109 | int AddTensorDataDesc(const std::shared_ptr<Tensor>& assembleTensor) |
| 107 | { | 110 | { |
| 108 | int index = static_cast<int>(getTensorDataDescList.size()); | 111 | int index = static_cast<int>(getTensorDataDescList.size()); |
| @@ -232,6 +235,8 @@ public: | |||
| 232 | 235 | ||
| 233 | void AddDependToken(ir::ExprPtr expr, ir::VarPtr token) { irContext_.AddDependToken(expr, token); } | 236 | void AddDependToken(ir::ExprPtr expr, ir::VarPtr token) { irContext_.AddDependToken(expr, token); } |
| 234 | std::vector<ir::VarPtr>& GetDependToken(ir::ExprPtr expr) { return irContext_.GetDependToken(expr); } | 237 | std::vector<ir::VarPtr>& GetDependToken(ir::ExprPtr expr) { return irContext_.GetDependToken(expr); } |
| 238 | + std::vector<ir::VarPtr> GetDependToken(Operation& op) { return irContext_.GetDependToken(op); } | ||
| 239 | + | ||
| 235 | int AddTensorDataDesc(const std::shared_ptr<Tensor>& assembleTensor) | 240 | int AddTensorDataDesc(const std::shared_ptr<Tensor>& assembleTensor) |
| 236 | { | 241 | { |
| 237 | return irContext_.AddTensorDataDesc(assembleTensor); | 242 | return irContext_.AddTensorDataDesc(assembleTensor); |
| @@ -34,6 +34,14 @@ def _ssa_verify(verifier, prog, name): | |||
| 34 | raise SyntaxError(f"IR verification failed after {name}") | 34 | raise SyntaxError(f"IR verification failed after {name}") |
| 35 | 35 | ||
| 36 | 36 | ||
| 37 | +def ssa_verify(func, desc=""): | ||
| 38 | + """Verify that the function is in SSA form.""" | ||
| 39 | + b = ir.IRBuilder() | ||
| 40 | + prog = b.create_program([func], "main", ir.Span.unknown()) | ||
| 41 | + verifier = ir.IRVerifier.create_default() | ||
| 42 | + _ssa_verify(verifier, prog, desc) | ||
| 43 | + | ||
| 44 | + | ||
| 37 | def run_merge_pass(func, *args): | 45 | def run_merge_pass(func, *args): |
| 38 | """Compile a kernel and run canonicalize + dce + merge_stmts_into_if, stopping before lowering | 46 | """Compile a kernel and run canonicalize + dce + merge_stmts_into_if, stopping before lowering |
| 39 | so the resulting if-tree (func.body) is inspectable. Mirrors compile_new_ir's first half.""" | 47 | so the resulting if-tree (func.body) is inspectable. Mirrors compile_new_ir's first half.""" |
| @@ -0,0 +1,62 @@ | |||
| 1 | +# -*- coding: utf-8 -*- | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + | ||
| 10 | +import pypto | ||
| 11 | +from pypto.pil.compile_pipeline import compile_new_ir | ||
| 12 | + | ||
| 13 | +from .test_common import ssa_verify | ||
| 14 | + | ||
| 15 | + | ||
| 16 | +def grouped_matmul( | ||
| 17 | + tensor_x: pypto.Tensor[[pypto.DYNAMIC, ...]], | ||
| 18 | + tensor_w: pypto.Tensor[[...]], | ||
| 19 | + output_tensor: pypto.Tensor[[pypto.DYNAMIC, ...]], | ||
| 20 | +): | ||
| 21 | + m, k = tensor_x.shape | ||
| 22 | + n = tensor_w.shape[1] | ||
| 23 | + pypto.set_vec_tile_shapes(32, 32) | ||
| 24 | + m_tile = 32 | ||
| 25 | + m_loop = (m + m_tile - 1) // m_tile | ||
| 26 | + | ||
| 27 | + assert isinstance(k, int) | ||
| 28 | + assert isinstance(n, int) | ||
| 29 | + | ||
| 30 | + for _ in pypto.loop(1): | ||
| 31 | + group_list = pypto.arange(100) | ||
| 32 | + | ||
| 33 | + for m_idx in pypto.loop(m_loop, name="LOOP_GROUP", idx_name="m_loop"): | ||
| 34 | + | ||
| 35 | + def inner(): | ||
| 36 | + actual_seq_len = group_list[m_idx] | ||
| 37 | + | ||
| 38 | + valid_shape1 = [pypto.min(m_tile, m - m_idx * m_tile), actual_seq_len] | ||
| 39 | + m_view = pypto.view(tensor_x, shape=[m_tile, k], offsets=[m_idx * m_tile, 0], valid_shape=valid_shape1) | ||
| 40 | + | ||
| 41 | + valid_shape_2 = [actual_seq_len, n] | ||
| 42 | + w_view = pypto.view(tensor_w, shape=[k, n], offsets=[0, 0], valid_shape=valid_shape_2) | ||
| 43 | + | ||
| 44 | + pypto.set_cube_tile_shapes([64, 64], [64, 64], [64, 64]) | ||
| 45 | + matmul_out = pypto.matmul(m_view, w_view, out_dtype=pypto.DT_FP32) | ||
| 46 | + | ||
| 47 | + output_tensor[m_idx * m_tile:(m_idx + 1) * m_tile, :] = matmul_out | ||
| 48 | + | ||
| 49 | + inner() | ||
| 50 | + | ||
| 51 | + | ||
| 52 | +def test_get_tensor_data(): | ||
| 53 | + | ||
| 54 | + m = pypto.SymbolicScalar("m") | ||
| 55 | + n = 256 | ||
| 56 | + k = 128 | ||
| 57 | + x = pypto.Tensor([m, k], pypto.DT_BF16, "x") | ||
| 58 | + y = pypto.Tensor([k, n], pypto.DT_BF16, "y") | ||
| 59 | + out = pypto.Tensor([m, n], pypto.DT_FP32, "out") | ||
| 60 | + | ||
| 61 | + func = compile_new_ir(grouped_matmul, x, y, out) | ||
| 62 | + ssa_verify(func, "get_tensor_data") | ||