Giter Site home page Giter Site logo

Comments (5)

LaurentMazare avatar LaurentMazare commented on May 18, 2024

Indeed that's actually an issue and you will notice that [Gc.full_major] is actually called pretty often in the code. I'm not sure whether this is actually necessary, a [Gc.minor] may be enough and quite faster.
I would not expect this to be very long compared to running some backward step on your gpu but maybe in some use case it's significant.
Anyway there is no perfect way around it, rust has these linear/affine types that help a lot but there is nothing like this in ocaml. That said I think that some recent version of ocaml added a way to tell the gc about how much 'resource' some opaque pointer is consuming and that should work well for this use case, I haven't played with it yet but that would probably be a nice thing to have.

from ocaml-torch.

zeroexcuses avatar zeroexcuses commented on May 18, 2024

I would not expect this to be very long compared to running some backward step on your gpu but maybe in some use case it's significant.

You're right, currently "Gc.full_major" seems slow, but only relative to doing backpass on MNIST. I'm sure on larger datasets it'll be better.

Do you know how PyTorch handles this problem? My OCaml code, without manual Gc.full_major, will often throw exception of unable to allocate mem on CUDA. I haven't run into this issue in PyTorch. Does PyTorch catch the exception, run a full GC, and continue? [I'm not sure, just guessing.]

from ocaml-torch.

zeroexcuses avatar zeroexcuses commented on May 18, 2024

Speaking of which -- can the OCaml bindings do this? Where if something fails to allocate it does:

fn handle_fail_alloc =
  run full gc;
  try alloc again;
  if still fail, die; else continue;

from ocaml-torch.

LaurentMazare avatar LaurentMazare commented on May 18, 2024

Python uses ref counting for memory management rather than only a gc (there may be a gc involved to find cycles?) so this is not an issue there.
Handling failed allocation by running the gc is probably not very straightforward do I'm afraid. Most code invariants break when you don't have memory. When it's gpu memory that may be less of an issue but it's still probably quite difficult.

from ocaml-torch.

zeroexcuses avatar zeroexcuses commented on May 18, 2024
  1. Right, IIRC Python is RefCount + cycle detector.

  2. I know close to nothing about OCaml VM and defer to you on this issue.

Thanks for the detailed response, looks like Caml.Gc.full_major it is.

from ocaml-torch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.