ultralytics icon indicating copy to clipboard operation
ultralytics copied to clipboard

Allow selection of different TPU device

Open lalayants opened this issue 1 year ago β€’ 14 comments

Allows one to choose TPU device, if multiple are available. I needed this for launching multiple docker containers with Coral Dual Edge TPU with two cores. Without modification, second container raises error, because both are trying to access the first core

πŸ› οΈ PR Summary

Made with ❀️ by Ultralytics Actions

🌟 Summary

Allows one to choose TPU device This PR improves code readability by refactoring and restructuring the autobackend.py file.

πŸ“Š Key Changes

  • Allows one to choose TPU device
  • Reformatted and reorganized code for enhanced clarity.
  • Added detailed comments to improve code understanding.
  • Managed imports for smoother readability.

🎯 Purpose & Impact

  • Improved Readability: The changes make the code easier to read and maintain, benefiting developers.
  • Functional Changes: This update allows users to choose TPU device.

πŸ› οΈ PR Summary

Made with ❀️ by Ultralytics Actions

🌟 Summary

Improved guides and backend support for running Coral Edge TPU models on Raspberry Pi with better TPU management.

πŸ“Š Key Changes

  • Updated documentation for exporting and running models on Coral Edge TPU.
  • Enhanced TPU library handling and version checks in the backend.
  • Improved code formatting for better readability and maintenance.

🎯 Purpose & Impact

  • User Guidance: Clarifies steps for setting up Coral Edge TPU with Raspberry Pi, ensuring smoother installation and usage.
  • Performance Enhancement: Allows selecting multiple TPUs for inference, optimizing resource usage.
  • Code Quality: Improved readability and maintainability of backend code, contributing to easier future updates.

lalayants avatar Sep 30 '24 22:09 lalayants

All Contributors have signed the CLA. βœ…
Posted by the CLA Assistant Lite bot.

github-actions[bot] avatar Sep 30 '24 22:09 github-actions[bot]

I have read the CLA Document and I sign the CLA

lalayants avatar Sep 30 '24 22:09 lalayants

πŸ‘‹ Hello @lalayants, thank you for submitting an ultralytics/ultralytics πŸš€ PR! This is an automated response, and an Ultralytics engineer will assist you soon.

To ensure a seamless integration of your work, please review the following checklist:

  • βœ… Define a Purpose: Clearly explain the purpose in your PR description, and link to any relevant issues. Ensure your commit messages are clear, concise, and adhere to the project's conventions.
  • βœ… Synchronize with Source: Confirm your PR is synchronized with the ultralytics/ultralytics main branch. If it's behind, update it using the 'Update branch' button or by running git pull and git merge main locally.
  • βœ… Ensure CI Checks Pass: Verify all Ultralytics Continuous Integration (CI) checks are passing. If any checks fail, please address the issues.
  • βœ… Update Documentation: Update relevant documentation for any new or modified features.
  • βœ… Add Tests: If applicable, include or update tests to cover your changes, and confirm that all tests are passing.
  • βœ… Sign the CLA: Please ensure you have signed our Contributor License Agreement by writing "I have read the CLA Document and I sign the CLA" in a new message.
  • βœ… Minimize Changes: Focus your changes on the minimum necessary for your addition. "It is not daily increase but daily decrease, hack away the unessential." β€” Bruce Lee

For bug reports, if applicable, please provide a minimum reproducible example (MRE).

For more guidance, please refer to our Contributing Guide. Don’t hesitate to leave a comment if you have any questions. Thank you for contributing to Ultralytics! πŸš€

UltralyticsAssistant avatar Sep 30 '24 22:09 UltralyticsAssistant

Codecov Report

Attention: Patch coverage is 40.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 72.09%. Comparing base (ccda7ff) to head (a95055c). Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
ultralytics/nn/autobackend.py 0.00% 3 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16576      +/-   ##
==========================================
- Coverage   72.11%   72.09%   -0.02%     
==========================================
  Files         126      126              
  Lines       16506    16507       +1     
==========================================
- Hits        11903    11901       -2     
- Misses       4603     4606       +3     
Flag Coverage Ξ”
Benchmarks 35.43% <40.00%> (-0.01%) :arrow_down:
GPU 29.63% <40.00%> (-0.01%) :arrow_down:
Tests 68.57% <40.00%> (-0.02%) :arrow_down:

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Sep 30 '24 22:09 codecov[bot]

Do you have a code example how someone would actually use this to select a different tpu?

Skillnoob avatar Oct 07 '24 07:10 Skillnoob

Do you have a code example how someone would actually use this to select a different tpu?

Yeah, below is an example of docker-compose setup. As you can see, you just need to add environment variable TPU_DEVICE. For non-docker usage, just setup it any preferable way.

services:
  yolo_1:
    build:
      context: .
    image: image_name
    container_name: yolo_1
    command: [ "/bin/bash", "-ci", "python3 yolo_test/v8_tpu.py" ]
    ipc: host
    stdin_open: true
    tty: true
    restart: always
    network_mode: host
    working_dir: /workspace/
    privileged: true
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./yolo_test:/workspace/yolo_test
    environment:
      TPU_DEVICE: ":0"

   yolo_2:
    image: image_name
    container_name: yolo_2
    command: [ "/bin/bash", "-ci", "python3 yolo_test/v8_tpu.py" ]
    ipc: host
    stdin_open: true
    tty: true
    restart: always
    network_mode: host
    working_dir: /workspace/
    privileged: true
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./yolo_test:/workspace/yolo_test
    environment:
      TPU_DEVICE: ":1"

lalayants avatar Oct 07 '24 08:10 lalayants

@lalayants I've updated the relevant documentation for your changes.

Skillnoob avatar Oct 07 '24 13:10 Skillnoob

The device selection now uses the device argument instead of setting a environment variable, which should be much more convenient to use.

Skillnoob avatar Oct 10 '24 16:10 Skillnoob

@Laughing-q this is ready for review.

Skillnoob avatar Oct 11 '24 06:10 Skillnoob

@lalayants @Skillnoob I noticed besides the actual changes this PR are making there are a lot code format updates, for example: pic-241011-1542-40 Can we revert that?

Laughing-q avatar Oct 11 '24 07:10 Laughing-q

Yeah, the code formats can be reverted, they weren't done by me. I'm able to do that in a few hours when I can get on my pc.

Skillnoob avatar Oct 11 '24 07:10 Skillnoob

@Laughing-q I've removed the unnecessary reformat.

Skillnoob avatar Oct 11 '24 12:10 Skillnoob

@Skillnoob @lalayants Looks good to me!

Laughing-q avatar Oct 14 '24 08:10 Laughing-q

@glenn-jocher This is ready to merge

Skillnoob avatar Oct 14 '24 13:10 Skillnoob

@Skillnoob @Laughing-q @lalayants thank you all! PR merged :)

glenn-jocher avatar Oct 17 '24 00:10 glenn-jocher