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

support mps device along with cpu and gpu #5

Merged
merged 1 commit into from
Oct 11, 2024

Conversation

rbavery
Copy link
Contributor

@rbavery rbavery commented Oct 7, 2024

writing up an attempt to get MPS support faster than CPU, marking as draft because it seems slower (?)

SAM2 supports some MPS acceleration: facebookresearch/sam2#192 Samgeo supports this version of SAM2, so I made a small change to how this project chooses the device by using SamGeo's choose_device function.

Tested this locally on my mac m1 with ds-annotate running locally, pointing to my local samgeo-service

→ uv run uvicorn main:app --host 0.0.0.0 --port 8000 --reload --log-level debug
INFO:     Will watch for changes in these directories: ['/Users/ryanavery/samgeo-service/app']
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [19664] using WatchFiles
2024-10-06 20:43:59 - INFO - Using device: mps
Screenshot 2024-10-06 at 8 49 21 PM

This takes about 8 seconds. I'm not sure but I think this doesn't include computing image embeddings.

2024-10-06 20:48:38 - INFO - {'method': 'POST', 'path': '/segment_predictor', 'duration': 8.586161125000217}

this seemed kinda slow so I tried a similar multi point query and hard coded the cpu device

INFO:     127.0.0.1:59286 - "POST /segment_predictor HTTP/1.1" 200 OK
2024-10-06 20:59:24 - INFO - {'method': 'GET', 'path': '/', 'duration': 1.006952417003049}

So both device work but cpu seems faster? So anyway this PR might not actually be useful if cpu is actually faster than mps. SAM2 folks say MPS should be faster than cpu so I'm a bit confused. Thought I'd post this up just so folks can see that MPs in theory does work and I might come back to take a look later and see why mps is slower.

Awesome work getting multi-point support implemented!

@rbavery rbavery marked this pull request as draft October 7, 2024 04:04
@Rub21
Copy link
Contributor

Rub21 commented Oct 10, 2024

This takes about 8 seconds. I'm not sure but I think this doesn't include computing image embeddings.

The part that encodes the image simply uploads the canvas to the server, which then saves it as a GeoTIFF. Later, during the decoding process, the TIFF image is used for predictions with HTR prompt inputs.

I think this PR is good to merge, The code works fine, and i start using the choose_device function to get the info about he gpu or cpu. #6

from samgeo import SamGeo2, choose_device
device = choose_device()

CPU works in a considerable amount of time; in this case, I tested it on an m5.large machine. The example below takes 25 seconds, which I think is good.

image

@rbavery rbavery marked this pull request as ready for review October 10, 2024 20:40
@rbavery
Copy link
Contributor Author

rbavery commented Oct 10, 2024

Sweet !

I'm curious why I saw CPU inference run so quickly then but maybe it had cached the embeddings from the previous run I did on the mac m1. 25 seconds makes more sense.

@Rub21 Rub21 merged commit 94e7697 into GeoCompas:main Oct 11, 2024
@rbavery rbavery deleted the cpu-mps-support branch October 11, 2024 19:34
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