Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call Tensor::alias() when returning an input Tensor #1433

Merged
merged 4 commits into from
Apr 7, 2025

Conversation

ds5678
Copy link
Contributor

@ds5678 ds5678 commented Jan 10, 2025

This ensures that disposing the result has the same impact as it would with a different size.

@ds5678
Copy link
Contributor Author

ds5678 commented Jan 10, 2025

The continuous integration failed, but I don't see how I did anything wrong.

Copy link
Contributor

@yueyinqiu yueyinqiu left a comment

Choose a reason for hiding this comment

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

TestLoadJIT_3 sometimes just fail. That's not your problem.

By the way, you could edit RELEASENOTES.md as well. (I don't know, since we didn't do that in #1421.)

@ds5678 ds5678 changed the title Call Tensor::alias() when concatenating a list of count 1 Call Tensor::alias() when returning an input Tensor Jan 10, 2025
@ds5678
Copy link
Contributor Author

ds5678 commented Jan 14, 2025

By the way, you could edit RELEASENOTES.md as well. (I don't know, since we didn't do that in #1421.)

I haven't done this because the wording was ambiguous on whether or not I should, and I didn't want to cause merge conflicts with any other pull requests.

@ds5678
Copy link
Contributor Author

ds5678 commented Jan 15, 2025

Is there anything I need to do in order for this to be merged? It's blocking my third pull request.

@ds5678 ds5678 force-pushed the alias-when-concat-single-tensor branch from 4799ca4 to f06fcc0 Compare March 8, 2025 07:45
@alinpahontu2912 alinpahontu2912 force-pushed the alias-when-concat-single-tensor branch from f06fcc0 to d11ec0e Compare March 25, 2025 13:32
@alinpahontu2912
Copy link
Member

Hey @ds5678, I rebased your branch, if tests pass, can you also add an explanation in releasenotes ? We can then merge the pr :)

@ds5678
Copy link
Contributor Author

ds5678 commented Mar 25, 2025

Thanks for the review!

@ozanMSFT ozanMSFT force-pushed the alias-when-concat-single-tensor branch from 6fb8ebb to c77f71c Compare March 27, 2025 12:19
@ozanMSFT
Copy link
Contributor

@ds5678 , thanks for your input and the PR.

Overall your change seems ok however I'm trying to understand the justification behind this code change.

This ensures that disposing the result has the same impact as it would with a different size

Could you provide further clarification in the description or perhaps a simple, reproducible code example with a brief before-and-after explanation? For instance, what was the issue, and how does this change resolve it?

@ds5678
Copy link
Contributor Author

ds5678 commented Mar 27, 2025

My pull request is based on the opinionated stance that disposing the tensor output from any function should never affect the inputs to that function.

I originally encountered this issue while concatenating tokens into a sequence during auto regression. I use the using Tensor approach to memory management because I find it to be the most predictable and performant. The only footgun I encountered with this approach was:

using Tensor sequence = torch.concat(tokens);

It disposed my first token (when my token list had count 1). Then, I auto regressed, so the list had count 2. It throws an error on the second pass because you can't concatenate a disposed tensor.

@ds5678
Copy link
Contributor Author

ds5678 commented Mar 27, 2025

Here's another example:

public override Tensor forward(IReadOnlyList<CilInstructionData> instructions)
{
	Tensor[] rentedArray = ArrayPool<Tensor>.Shared.Rent(instructions.Count);
	ArraySegment<Tensor> tensors = new(rentedArray, 0, instructions.Count);

	for (int i = 0; i < instructions.Count; i++)
	{
		tensors[i] = tokenAssembler.forward(instructions[i]);
	}

	Tensor result = concat(tensors);

	tensors.Dispose(); // extension method that iterates over the segment disposing each element
	ArrayPool<Tensor>.Shared.Return(rentedArray, true);

	return result; // If "instructions" has length 1, this will have already been disposed.
}

@ds5678
Copy link
Contributor Author

ds5678 commented Apr 4, 2025

Is there anything else I need to do?

Copy link
Contributor

@ozanMSFT ozanMSFT left a comment

Choose a reason for hiding this comment

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

LGTM with the additional explanation

@ozanMSFT ozanMSFT merged commit b056ca5 into dotnet:main Apr 7, 2025
2 checks passed
@ozanMSFT
Copy link
Contributor

ozanMSFT commented Apr 7, 2025

@ds5678 , thanks for the contribution and sorry for the late reply.

It is always helpful to see the example and justification.

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.

4 participants