Skip to content

[WIP] Dynamic Tensor Rematerialization(Checkpointing) - #42056

Closed
MarisaKirisame wants to merge 1 commit into
pytorch:masterfrom
MarisaKirisame:dtr
Closed

MarisaKirisame wants to merge 1 commit into
pytorch:masterfrom
MarisaKirisame:dtr

Conversation

@MarisaKirisame

Copy link
Copy Markdown
Contributor

We had been designing a checkpointing algorithm, Dynamic Tensor Rematerialization, that work for dynamic graph.
With our work, we had successfully save 40%-80% memory with less then double the computation cost.
WNU@F4%KJ@XNJR2M698`L
We want to contribute to pytorch by upstreaming the algorithm, however, we are not pytorch developers and this is our first pytorch pr. Can anyone help and discuss what should be done?
Dynamic Tensor Rematerialization is the paper and https://github.com/uwsampl/dtr-prototype is the artifact evaluation.

Implements CheckpointTensor backend

Hook up the overload with a passing test. (pytorch#15)

* save

* save

* review

* review

* add back files

Adding some overload (pytorch#16)

* save

* save

* review

* remove outdated file

* save

* can compile again

* save

* save

* up up array

* finsih the overloads

* replace vec[n] with vec.at(n)

make resnet pass again, adding more logs for logging simulator (pytorch#17)

* commit

* add more overloads

* fix log

* save

Logging everything for resnet (pytorch#18)

* save

* save

* save

* save

* use release instead of free

Fix logger (special handling for small constants) (pytorch#19)

* save

* fix comment

TreeLSTM overloads (pytorch#21)

Overloads for mean and mean.dim (needed for densenet)

Add overloads for U-Net

Add ability to annotate log and make new log file (pytorch#25)

Restore overloads needed for LSTM and GRU

Implemented unrolled_gan (pytorch#23)

* save

* save

* save

Overload bitwise and for ParityACT

Add various overloads for ACT model

Log aliases of operator outputs

Overloads and changes needed for transformer

Overloads for topk functions

Add overloads for deepspeech

More overloads for ACT

DTR implementation first pass (pytorch#37)

save before change

save before change

add aliaspool design, and add weak/strong pointer discussion

add more code

rebase

add allocator hook

save metadata to prepare for eviction

save

refactor: move log to a seprate file

add file

raii

save

save comment

save

save

save

save

find error, bisecting code

save

save

address review comment

address comment

address comment

fix segfault

save

save

pin

save

save

save

save

save

refactor - remove stitch

save

save

[ impl ] overload diag & mv

refactor - remove stitch

save

save

restore equivalentclassnode

save

save

save

50% resnet here we go

50% resnet here we go

save

save

save

save

save

save

More opts

save

Try removing pop-ing from loop

save

save

save

save

mike try this

mike try this

save

undo system optimization for now

save

save

save

fix compile error

save

save
@MarisaKirisame
MarisaKirisame requested a review from apaszke as a code owner July 25, 2020 11:02
@MarisaKirisame
MarisaKirisame marked this pull request as draft July 25, 2020 11:02
@dr-ci

dr-ci Bot commented Jul 25, 2020 •

Copy link
Copy Markdown

🔗 Helpful links

💊 CI failures summary and remediations

As of commit 00f8475 (more details on the Dr. CI page):


Commit 00f8475 was recently pushed. Waiting for builds...


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions to the (internal) Dr. CI Users group.

Click here to manually regenerate this comment.

@ezyang

ezyang commented Aug 3, 2020

Copy link
Copy Markdown
Contributor

(Haven't forgotten about this, but need time to digest)

@MarisaKirisame

Copy link
Copy Markdown
Contributor Author

We are working on other things so we arent blocked on this (yet). Let us know if there is anything we can do to help!

@ezyang

ezyang commented Aug 19, 2020

Copy link
Copy Markdown
Contributor

Hi @MarisaKirisame, this is a really interesting PR. The biggest current problem with the PR is that the recording of replay has all been written out by hand; this will be a hard sell from a maintenance perspective. I think what's realistic is for us to wait until full boxing/unboxing support, and then reimplement the manual codegen part as a boxed fallback. From there, the question is in-tree versus out-of-tree; there are very few other "invasive" changes and so I think this could fairly easily live out of tree. I can help you guys set this up, when boxed fallback is running. If the extension gets quite popular we can then talk about inclusion in core.

If you want to take a crack at boxed fallback today (which will actually be a decent thing to try, since we do have a decent amount of op coverage), the best code to look at are these:

  • ./aten/src/ATen/core/dispatch/backend_fallback_test.cpp is a simple implementation of fallback, that doesn't do anything but forward on to the next function. After you setup the checkpoint information, you're going to do this, so this will give you some information about how to set that up
  • vmap fallback kernel #41943 which adds boxed fallback for vmap; basically, it looks at the IValues, finds tensors, and then manually loops over them. It should give you some idea about what kinds of idioms you can do with IValues
  • For some IValue manipulation code that looks similar to what you would have to do, check out torch/csrc/jit/runtime/register_c10_ops.cpp . This implements tracing on IValues; your rematerialization traces are basically a version of this.

@MarisaKirisame

Copy link
Copy Markdown
Contributor Author

@ezyang is there any way to have it out of tree while not requiring user to recompile pytorch from scratch? compiling pytorch take lots of time and is quite hard to setup for a typical ML researcher that only use python.

@ezyang

ezyang commented Aug 19, 2020

Copy link
Copy Markdown
Contributor

Yeah, out of tree would be a C++ extension like torchvision, no need to compile PyTorch from scratch, you can just build based off of the release version of PyTorch.

@MarisaKirisame

Copy link
Copy Markdown
Contributor Author

@ezyang for the dtr to work we must hook all inplace operation. how can i do that with fallback?

@ezyang

ezyang commented Aug 28, 2020

Copy link
Copy Markdown
Contributor

Can you tell me more about what kind of hooking you need to do? This may be hard to do.

@MarisaKirisame

Copy link
Copy Markdown
Contributor Author

@ezyang the current way DTR handle mutation rn, is that it pretend all Tensor is a Ref, all inplace operation as clone and inplace on the cloned result, and DTR only work on ImmutableTensor. So I have to hook all mutable call, and figure out what argument is getting changed, and how is it related to the output.

@ezyang

ezyang commented Aug 31, 2020

Copy link
Copy Markdown
Contributor

OK. With DTR we should be able to interpose in on calls to inplace (in the same way we interpose on functional calls to record the trace). So you would just need a way to get information about (1) is the op inplace and (2) the correspondence of inputs to outputs. This information can all be derived from the function schema. So yes, in principle it is doable. Not sure how easy it is in practice right now.

@MarisaKirisame

Copy link
Copy Markdown
Contributor Author

So I can get the schema? I think that's what I need - most inplace only modify a single thing and it is easy to figure out. I will see if I hit any problem.

@ezyang

ezyang commented Sep 1, 2020

Copy link
Copy Markdown
Contributor

Yup. In backend fallback you'll get an OperatorHandle which you can use to retrieve the schema. I think we even have a few examples of doing this already.

@shenh10

shenh10 commented Jan 11, 2022

Copy link
Copy Markdown

It seems that the above PR is not successfully compiled. Is there a fix for it?

@shenh10

shenh10 commented Jan 11, 2022

Copy link
Copy Markdown

Btw, this implementation adapts global variables to record memory usage, which only supports a single device and might cause a problem for distributed case.

@MarisaKirisame

MarisaKirisame commented Jan 12, 2022 •

Copy link
Copy Markdown
Contributor Author

Hi @shenh10, I’ve chatted with Nuno Lopes and @ezyang about this and long term it would be nice to get this feature into PyTorch but it looks like the best path forward involves some refactoring to related systems first.

Happy to chat more if you’re interested, and while there’s no roadmap right now for getting DTR into PyTorch, I think there’s interest in moving in that direction.

And yes, the code is garbage, we are just trying to make it work for our evaluation, and thus we do not handle multiple device.

@MarisaKirisame MarisaKirisame mentioned this pull request Jan 12, 2022
@shenh10

shenh10 commented Jan 17, 2022

Copy link
Copy Markdown

@MarisaKirisame Sure, it's my pleasure to have a deeper chat. Except for checkpointing, it would be nice to have some automatic memory-saving choices in PyTorch ecosystem. The current codebase is indeed far from ready for a formal integration and release, cause I have faced some segfaults during testing your codes. Btw, I would like to help if you need more people engaged in.

@MarisaKirisame

Copy link
Copy Markdown
Contributor Author

@shenh10 sure, email me to figure out a time to chat!

@pytorchbot

Copy link
Copy Markdown
Collaborator

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
Stale pull requests will automatically be closed 30 days after being marked Stale

@Jack47

Jack47 commented Jul 25, 2022 •

Copy link
Copy Markdown

Hi @ezyang , I'm developer from SenseTime, we really needs this MR to allow pytorch training bigger models. we have spent almost half a year to move it forward on pytorch 1.11, we found some pains when developing dtr on pytorch, may you guys give some advice?

  1. compiling dtr implementation with pytorch is hard, not to say distribute it as a wheel with staticlly linked libs. it greatly decrease developing efficiency, is there any chance we can developing this feature as a pytorch plugin to build and test separately?
  2. is there more documents and scripts to help developing pytorch? such as ut and ci to help find errors quickly
  3. we want to using backend fallback to avoid wraping every operations in pytorch. Is there any news on performance of backend fallback? Or is there any benchmarks I can test in my env
  4. No easy way to support other pytorch extensions which implment customized operations. Because they usually use pybind11, which doesn't go dispatcher?

@ezyang ezyang reopened this Jul 25, 2022
@shenh10

shenh10 commented Jul 26, 2022 •

Copy link
Copy Markdown

@Jack47 , did you test the MR and run a model with this feature successfully? I came across certain segfaults before and no longer look into it anymore. It's grateful if there are more people engaged-in to fix this feature. Any experience could share?

@Jack47

Jack47 commented Jul 26, 2022 •

Copy link
Copy Markdown

shenh10

This PR is DTR v2 which uses backend fallback, I'm currently working on DTR v1, trying to decrease DTR overhead and make it more efficiency. Several months later, I will use backend fallback to implement DTR v2

@albanD

albanD commented Jul 26, 2022

Copy link
Copy Markdown
Collaborator

Hey @Jack47

  1. Given that this PR is a very early draft, there is no plan to package it in a specific way no. One thing that can be done though is to add the ciflow/binaries label to the PR so that the CI binary generation runs and you will be able to manually download wheels from github.
  2. There are a lot of details in https://github.com/pytorch/pytorch/blob/master/CONTRIBUTING.md for CI, you can simply send a PR (leave it as draft if not ready for review) and you will get CI signal from there.
  3. No specific news that I know of. But for any workload that is not framework-overhead bound, it should be pretty minimal. I am not aware of any benchmark on this but things like Conjugate View are implemented with that.
  4. Not sure what you mean by that? Creating new ops from c++ is rather simple via the TORCH_LIBRARY macro and there is now a python binding for it at torch.library.

I also wanted to mention that if you're seriously interested in reviving this PR, we might want to sit down and discuss the overall design of this PR. I think there are ways to do this in a much simpler way (with higher overhead) that might be enough for experimentation and/or non-overhead dominated models.

@Jack47

Jack47 commented Jul 26, 2022 •

Copy link
Copy Markdown

Hey @Jack47

  1. Given that this PR is a very early draft, there is no plan to package it in a specific way no. One thing that can be done though is to add the ciflow/binaries label to the PR so that the CI binary generation runs and you will be able to manually download wheels from github.
  2. There are a lot of details in https://github.com/pytorch/pytorch/blob/master/CONTRIBUTING.md for CI, you can simply send a PR (leave it as draft if not ready for review) and you will get CI signal from there.
  3. No specific news that I know of. But for any workload that is not framework-overhead bound, it should be pretty minimal. I am not aware of any benchmark on this but things like Conjugate View are implemented with that.
  4. Not sure what you mean by that? Creating new ops from c++ is rather simple via the TORCH_LIBRARY macro and there is now a python binding for it at torch.library.

I also wanted to mention that if you're seriously interested in reviving this PR, we might want to sit down and discuss the overall design of this PR. I think there are ways to do this in a much simpler way (with higher overhead) that might be enough for experimentation and/or non-overhead dominated models.

Hey @albanD , thanks for quick response, those advices are useful, I'll read CONTRIBUTING.md carefully and use github ci to help me building wheel.

for 4, I mean there are existing old pytorch plugins that are using pybind11, so those plugins are not using dispatcher.

I'm seriously interested in reviving this PR, so eager to known more about simpler way. I'm currently working on DTR v1 which doesn't use backend fallback, I've spent too much time on wraping pytorch op :(. So I want to try this PR. My goal is to use DTR in production env(A100 10 nodes) finally, so I want to find a little overhead design.

@MarisaKirisame

Copy link
Copy Markdown
Contributor Author

I also wanted to mention that if you're seriously interested in reviving this PR, we might want to sit down and discuss the overall design of this PR. I think there are ways to do this in a much simpler way (with higher overhead) that might be enough for experimentation and/or non-overhead dominated models.

Can I chime in on that? I aint maintaining this heavily anymore, as I moved onto other research project, but I'd love to share some knowledge/insight and get some design feedback.

@albanD

albanD commented Jul 27, 2022

Copy link
Copy Markdown
Collaborator

Created a slack channel as a higher bandwidth channel to sync on this.
Anyone interested on this is welcome to join #dynamic-tensor-rematerialization on slack.

@github-actions github-actions Bot closed this Aug 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants