Merged
fix(operator): Make ScatterUpdate update dst inplace and preserve slot symbol #6120
fix(operator): Make ScatterUpdate update dst inplace and preserve slot symbol #6120
Merged
gaoxingwangAdd on Sep 10
Total: 9 files changed+40-8
@@ -310,7 +310,7 @@ Tensor Unsqueeze(const Tensor& old, int unsqueezeDimNum);
310Tensor Squeeze(const Tensor& input, const std::vector<int>& dim = {});310Tensor Squeeze(const Tensor& input, const std::vector<int>& dim = {});
311 311 
312Tensor TensorIndex(const Tensor& params, const Tensor& indices);312Tensor TensorIndex(const Tensor& params, const Tensor& indices);
313-Tensor ScatterUpdate(const Tensor& dst, const Tensor& index, const Tensor& src, int axis = -2,313+Tensor ScatterUpdate(Tensor& dst, const Tensor& index, const Tensor& src, int axis = -2,
314 std::string cacheMode = "PA_BNSD", int chunkSize = 1);314 std::string cacheMode = "PA_BNSD", int chunkSize = 1);
315 315 
316Tensor Expand(const Tensor& self, const std::vector<int64_t>& dstShape, std::vector<SymbolicScalar> validShape = {});316Tensor Expand(const Tensor& self, const std::vector<int64_t>& dstShape, std::vector<SymbolicScalar> validShape = {});
@@ -358,6 +358,10 @@ enum class ParamDirection {
358struct DyndevFunctionAttribute {358struct DyndevFunctionAttribute {
359 std::vector<std::reference_wrapper<const Tensor>> startArgsInputTensorList;359 std::vector<std::reference_wrapper<const Tensor>> startArgsInputTensorList;
360 std::vector<std::reference_wrapper<const Tensor>> startArgsOutputTensorList;360 std::vector<std::reference_wrapper<const Tensor>> startArgsOutputTensorList;
361+ // Holds shared ownership of the input slot tensors so they outlive the references
362+ // in startArgsInputTensorList; slot tensors may be migrated by SetSameSlot and
363+ // would otherwise be destructed before the function attribute is consumed.
364+ std::vector<std::shared_ptr<Tensor>> startArgsInputSlotTensorList;
361 365 
362 std::vector<std::shared_ptr<LogicalTensor>> startArgsInputLogicalTensorList;366 std::vector<std::shared_ptr<LogicalTensor>> startArgsInputLogicalTensorList;
363 std::vector<std::shared_ptr<LogicalTensor>> startArgsOutputLogicalTensorList;367 std::vector<std::shared_ptr<LogicalTensor>> startArgsOutputLogicalTensorList;
@@ -295,7 +295,7 @@ static void CheckScatterUpdateInvalid(const Tensor& dst, const Tensor& index, co
295 CheckScatterUpdateInput(dst);295 CheckScatterUpdateInput(dst);
296}296}
297 297 
298-Tensor ScatterUpdate(const Tensor& dst, const Tensor& index, const Tensor& src, int axis, std::string cacheMode,298+Tensor ScatterUpdate(Tensor& dst, const Tensor& index, const Tensor& src, int axis, std::string cacheMode,
299 int chunkSize)299 int chunkSize)
300{300{
301 DECLARE_TRACER();301 DECLARE_TRACER();
@@ -325,6 +325,8 @@ Tensor ScatterUpdate(const Tensor& dst, const Tensor& index, const Tensor& src,
325 CALL(ScatterUpdate, *Program::GetInstance().GetCurrentFunction(), result.GetStorage(), dst.GetStorage(),325 CALL(ScatterUpdate, *Program::GetInstance().GetCurrentFunction(), result.GetStorage(), dst.GetStorage(),
326 index.GetStorage(), src.GetStorage(), axis, cacheMode, chunkSize);326 index.GetStorage(), src.GetStorage(), axis, cacheMode, chunkSize);
327 }327 }
328+ 
329+ dst = result;
328 return result;330 return result;
329}331}
330 332 
@@ -157,12 +157,17 @@ void RootFunctionBuilder::InitDynFunc(const ir::FunctionPtr& irFunc)
157 dynFunc_->SetSpan(irFunc->span_);157 dynFunc_->SetSpan(irFunc->span_);
158 dynFunc_->SetDyndevAttribute(std::make_shared<DyndevFunctionAttribute>());158 dynFunc_->SetDyndevAttribute(std::make_shared<DyndevFunctionAttribute>());
159 auto attr = dynFunc_->GetDyndevAttribute();159 auto attr = dynFunc_->GetDyndevAttribute();
160+ auto slotManager = program_.GetTensorSlotManager();
160 161 
161 for (const auto& param : irFunc->params_) {162 for (const auto& param : irFunc->params_) {
162 auto lt = AsLogicalTensor(param);163 auto lt = AsLogicalTensor(param);
163 ASSERT(lt) << "RootFunctionBuilder: param is not a LogicalTensor: " << param->name_;164 ASSERT(lt) << "RootFunctionBuilder: param is not a LogicalTensor: " << param->name_;
164 logicalParams_.push_back(lt);165 logicalParams_.push_back(lt);
165 attr->startArgsInputLogicalTensorList.push_back(lt);166 attr->startArgsInputLogicalTensorList.push_back(lt);
167+ auto tensor = slotManager->GetSlotTensor(lt);
168+ attr->startArgsInputSlotTensorList.emplace_back(tensor);
169+ attr->startArgsInputTensorList.emplace_back(*tensor);
170+ slotManager->MarkInput(*tensor);
166 }171 }
167 program_.SetCurrentDynamicFunction(dynFunc_.get());172 program_.SetCurrentDynamicFunction(dynFunc_.get());
168}173}
@@ -465,8 +470,9 @@ void RootFunctionBuilder::BuildDynSlotScope()
465 470 
466 for (size_t idx = 0; idx < logicalParams_.size(); idx++) {471 for (size_t idx = 0; idx < logicalParams_.size(); idx++) {
467 auto tensor = slotManager->GetSlotTensor(logicalParams_[idx]);472 auto tensor = slotManager->GetSlotTensor(logicalParams_[idx]);
468- attr->startArgsInputTensorList.emplace_back(*tensor);473+ slotManager->UpdateInputSlot(idx, *tensor);
469- slotManager->MarkInput(*tensor);474+ attr->startArgsInputTensorList[idx] = *tensor;
475+ attr->startArgsInputSlotTensorList[idx] = tensor;
470 }476 }
471 477 
472 dynFunc_->InferParamDirection();478 dynFunc_->InferParamDirection();
@@ -466,6 +466,7 @@ void TensorSlotManager::SetSameSlot(const LogicalTensorPtr& src, const LogicalTe
466 if (srcTensor->Id() == dstTensor->Id()) {466 if (srcTensor->Id() == dstTensor->Id()) {
467 return;467 return;
468 }468 }
469+ 
469 // Migrate all tensors pointing to dst's old slot to src's slot, so no prior470 // Migrate all tensors pointing to dst's old slot to src's slot, so no prior
470 // links are lost regardless of link order or direction.471 // links are lost regardless of link order or direction.
471 for (auto& [lt, tensor] : slotTensorDict) {472 for (auto& [lt, tensor] : slotTensorDict) {
@@ -493,6 +494,23 @@ void TensorSlotManager::MarkInput(const Tensor& tensor)
493 LogOperation(slot, "input");494 LogOperation(slot, "input");
494}495}
495 496 
497+void TensorSlotManager::UpdateInputSlot(size_t idx, const Tensor& tensor)
498+{
499+ FE_ASSERT(idx < inputSlotList.size())
500+ << "UpdateInputSlot idx: " << idx << " out of range, size: " << inputSlotList.size();
501+ TensorSlot slot = TensorSlot::CreateTensor(tensor);
502+ TensorSlot oldSlot = inputSlotList[idx];
503+ if (slot == oldSlot) {
504+ return;
505+ }
506+ inputSlotDict.erase(oldSlot);
507+ inputSlotList[idx] = slot;
508+ if (inputSlotDict.count(slot) == 0) {
509+ inputSlotDict[slot] = idx;
510+ }
511+ LogOperation(slot, "updateInput");
512+}
513+ 
496void TensorSlotManager::MarkOutput(const Tensor& tensor)514void TensorSlotManager::MarkOutput(const Tensor& tensor)
497{515{
498 TensorSlot slot = TensorSlot::CreateTensor(tensor);516 TensorSlot slot = TensorSlot::CreateTensor(tensor);
@@ -272,6 +272,7 @@ struct TensorSlotManager {
272 std::vector<int> LookupSlotIndexConst(const std::vector<std::reference_wrapper<const Tensor>>& tensorList);272 std::vector<int> LookupSlotIndexConst(const std::vector<std::reference_wrapper<const Tensor>>& tensorList);
273 273 
274 void MarkInput(const Tensor& tensor);274 void MarkInput(const Tensor& tensor);
275+ void UpdateInputSlot(size_t idx, const Tensor& tensor);
275 void MarkOutput(const Tensor& tensor);276 void MarkOutput(const Tensor& tensor);
276 void MarkInplace(const Tensor& out, const Tensor& in);277 void MarkInplace(const Tensor& out, const Tensor& in);
277 278 
@@ -37,6 +37,7 @@ _patch_methods = [
37 (pypto, "index_put_", 0),37 (pypto, "index_put_", 0),
38 (pypto, "index_add_", 0),38 (pypto, "index_add_", 0),
39 (pypto, "atomic_add", 2),39 (pypto, "atomic_add", 2),
40+ (pypto, "scatter_update", 0),
40]41]
41 42 
42_orig_assemble = pypto.assemble43_orig_assemble = pypto.assemble
@@ -424,7 +424,7 @@ void BindOperation(py::module_& m)
424 "Tensor index_select.");424 "Tensor index_select.");
425 m.def(425 m.def(
426 "ScatterUpdate",426 "ScatterUpdate",
427- [](const Tensor& dst, const Tensor& index, const Tensor& src, int axis, std::string cacheMode, int chunkSize) {427+ [](Tensor& dst, const Tensor& index, const Tensor& src, int axis, std::string cacheMode, int chunkSize) {
428 return npu::tile_fwk::ScatterUpdate(dst, index, src, axis, cacheMode, chunkSize);428 return npu::tile_fwk::ScatterUpdate(dst, index, src, axis, cacheMode, chunkSize);
429 },429 },
430 py::arg("dst"), py::arg("index"), py::arg("src"), py::arg("axis") = SCATTER_UPDATE_DIM,430 py::arg("dst"), py::arg("index"), py::arg("src"), py::arg("axis") = SCATTER_UPDATE_DIM,
@@ -1,7 +1,7 @@
1function foo incast(v0_logical_tensor %target@0 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]), v0_logical_tensor %index@1 #dtype(int32_t) #shape([2, 2]) #offset([0, 0]) #dynvalidshape([2, 2]), v0_logical_tensor %source@2 #dtype(float) #shape([4, 16]) #offset([0, 0]) #dynvalidshape([4, 16]), v0_logical_tensor %out@3 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16])) outcast() #type(Opaque) #entry(false) {1function foo incast(v0_logical_tensor %target@0 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]), v0_logical_tensor %index@1 #dtype(int32_t) #shape([2, 2]) #offset([0, 0]) #dynvalidshape([2, 2]), v0_logical_tensor %source@2 #dtype(float) #shape([4, 16]) #offset([0, 0]) #dynvalidshape([4, 16]), v0_logical_tensor %out@3 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16])) outcast() #type(Opaque) #entry(false) {
2 v0_logical_tensor %$0@4 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]), token %target_r = !10006 ADDS(%target@0) token();2 v0_logical_tensor %$0@4 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]), token %target_r = !10006 ADDS(%target@0) token();
3- v0_logical_tensor %$1@5 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]) = !10007 INDEX_OUTCAST(%source@2, %index@1, %target@0) token(%target_r);3+ v0_logical_tensor %target_0@5 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]) = !10007 INDEX_OUTCAST(%source@2, %index@1, %target@0) token(%target_r);
4- v0_logical_tensor %$2@6 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]) = !10008 ADD(%$0@4, %$1@5) token();4+ v0_logical_tensor %$2@6 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]) = !10008 ADD(%$0@4, %target_0@5) token();
5 v0_logical_tensor %out_1@3 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]) = !10009 ASSEMBLE(%$2@6) token() #toOffset([0, 0]);5 v0_logical_tensor %out_1@3 #dtype(float) #shape([16, 16]) #offset([0, 0]) #dynvalidshape([16, 16]) = !10009 ASSEMBLE(%$2@6) token() #toOffset([0, 0]);
6- return %target@0, %index@1, %source@2, %out_1@3;6+ return %target_0@5, %index@1, %source@2, %out_1@3;
7}7}