Skip to content

Conversation

@lmoneta
Copy link
Member

@lmoneta lmoneta commented Nov 14, 2025

This Pull request provides support for optimal memory allocation of dynamic tensor.
A function to compute the total size and the optimal offset for each tensor given the dynamic input parameters (e.g. batch_size, number of input features, etc..) is added in SOFIE_Common.

Add missing support for Dynamic tensors for some operators.
With this commit a full support for dynamic tensor is available for ParticleNet model.
Fix also a bug in Concat operator when the concat axis is not the first one
Since we use now for boolean tensors a std::vector<uint8_t> it is not needed to
have a special treatment when the output ttype of the operator is a boolean
(e.g. in Comparison)
…ensors

Add a new function in SOFIE_common OrganizeMemory which computes the total memory and the offset for each tensor given tensor begin /end life and size.

Fix also some small issue with dynamic tensor.
One is for the bias of Gemm and Conv. The broadcasting of bias is done for dynamic tensor in the Session constructor only if needed. For the broadcasted tensor  there is no need to create a new tensor, but the existing one is resized to the  broadcasted needed size using vector::resize
… broadcasting

The assert that was generated when broadcasting dynamic tensors was not correct
@lmoneta lmoneta requested a review from sanjibansg November 14, 2025 15:35
@lmoneta lmoneta self-assigned this Nov 14, 2025
@github-actions
Copy link

Test Results

    22 files      22 suites   3d 18h 17m 35s ⏱️
 3 779 tests  3 769 ✅  2 💤  8 ❌
81 179 runs  81 054 ✅ 30 💤 95 ❌

For more details on these failures, see this check.

Results for commit 21f3675.

Copy link
Contributor

@sanjibansg sanjibansg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me overall, just some questions:

for (size_t i = 0; i < fNBroadcastedInputs.size(); i++) {
inputs[i] = fNBroadcastedInputs[i] + "[id]";

// implement operator without broadcasting, but using loos on all indices
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// implement operator without broadcasting, but using loos on all indices
// implement operator without broadcasting, but using loops on all indices

std::copy(inputData, inputData + inputLength, outputData.begin() + offset );
offset += inputLength;
// data do not need to be written as a weight
// data do not need to be written in teh generated code
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// data do not need to be written in teh generated code
// data do not need to be written in the generated code

Comment on lines +778 to +788
//fGC += "std::vector<float> fTensor_" + i.first + ";\n";
fGC += "float * tensor_" + i.first + " = nullptr;\n";
} else if (i.second.type == ETensorType::DOUBLE) {
fGC += "std::vector<double> fTensor_" + i.first + ";\n";
//fGC += "std::vector<double> fTensor_" + i.first + ";\n";
fGC += "double * tensor_" + i.first + " = nullptr;\n";
} else if (i.second.type == ETensorType::INT64) {
fGC += "std::vector<int64_t> fTensor_" + i.first + ";\n";
//fGC += "std::vector<int64_t> fTensor_" + i.first + ";\n";
fGC += "int64_t * tensor_" + i.first + " = nullptr;\n";
} else if (i.second.type == ETensorType::BOOL) {
//fGC += "std::vector<uint8_t> fTensor_" + i.first + ";\n";
fGC += "uint8_t * tensor_" + i.first + " = nullptr;\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we remove the commented out code?

bool modelHasWeights = false;
for (auto &i : fInitializedTensors) {
if (i.second.type() == ETensorType::FLOAT) {
if (i.second.IsWeightTensor()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it be an issue if we do not make type checks here?

Comment on lines +878 to +884
// for (auto &i : fDynamicTensorInfos) {
// auto length = ConvertDynamicShapeToLength(i.second.shape);
// out << SP << "if (" << length << " > 0) {\n";
// out << SP << SP << "fTensor_" << i.first << ".resize(" << length << ");\n";
// out << SP << SP << "tensor_" << i.first << " = fTensor_" << i.first << ".data();\n";
// out << SP << "}\n";
// }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can remove this commented code?


struct MemoryEvent {
int t; // time (i.e. operator index)
int type; // 0 = END first, 1 = START
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does the tensor index signify here?

Comment on lines +247 to +258
// /d to add a new intermediate tensor for broadcasted bias tensor
// fNC2 = fNC + "bcast";
// if (!fIsDynamic) {
// model.AddIntermed/ In case of session add broadcasting code in Session constructor and in GenerateInitCode
// // we neeiateTensor(fNC2, model.GetTensorType(fNC), shapeY);
// }
// else
// model.AddDynamicTensor(fNC2,model.GetTensorType(fNC), fShapeY);
// // do not add to lists of input/output tensors since broadcasted tensors are special
// // and we manage their memory separatly
// //fInputTensorNames.emplace_back(fNC2);
// //fOutputTensorNames.emplace_back(fNC2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this else block is not needed anymore, maybe we can remove the if-else branching completely?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants