Detects in env.py if apple silicon can be used and sets pytorch to it (and sorry my linter added some other commas and reformatting, I can fix if needed and disable that).
The changes are:
env.py: detects if the pytorch mps device is available and if pytorch is built for it
env.py: cleans up the current detection code which includes a try and except and replaces it with a simple if.
env.py: adds a try and except when importing pytorch in case the backend is built without it
No documentation change made since Apple Silicon support is not specific listed and judging from Discussion, its assumed it works but could add if needed
No new dependencies added uses existing pytorch checks
Note: I do not have a Windows machine nor a CUDA machine, but looking at the code it should perform the same in setting env.py properly
Note: I did not fix but can if desired fixed two other places where there code looks wrong. I just grepped for "coda". I'm not sure why they ignore the logic in env.py which basically ignores CUDA in Docker unless DOCKER_CUDA is set
./backend/open_webui/routes/audio.py:99 This is setting device type but only allows cuda and cpu but not mps. I don't know the audio code well enough to know why it is overriding what is set in ./env.py
"device": DEVICE_TYPE if DEVICE_TYPE and DEVICE_TYPE == "cuda" else "cpu",
# should become
"device": DEVICE_TYPE
./backend/open_webui/retrieval/models/colbert.py:11 this is ignoring DEVICE_TYPE. Again, I didn't change it because I don't know the Colbert code well enough. Does if fail on Apple Silicon. Is this because RAG does not have access to env.py, I don't know the code well enough to say
self.device = "cuda" if torch.cuda.is_available() else "cpu"
# should become
self.device = DEVICE_TYPE
Pull Request Checklist
Note to first-time contributors: Please open a discussion post in Discussions and describe your changes before submitting a pull request.
Before submitting, make sure you've checked the following:
Target branch: Please verify that the pull request targets the dev branch.
Description: Provide a concise description of the changes made in this pull request.
Changelog: Ensure a changelog entry following the format of Keep a Changelog is added at the bottom of the PR description.
Documentation: Have you updated relevant documentation Open WebUI Docs, or other documentation sources?
Dependencies: Are there any new dependencies? Have you updated the dependency versions in the documentation?
Testing: Have you written and run sufficient tests for validating the changes?
[x ] Code review: Have you performed a self-review of your code, addressing any coding standard issues and ensuring adherence to the project's coding standards?
Prefix: To cleary categorize this pull request, prefix the pull request title, using one of the following:
BREAKING CHANGE: Significant changes that may affect compatibility
build: Changes that affect the build system or external dependencies
ci: Changes to our continuous integration processes or workflows
chore: Refactor, cleanup, or other non-functional code changes
docs: Documentation update or addition
feat: Introduces a new feature or enhancement to the codebase
fix: Bug fix or error correction
i18n: Internationalization or localization changes
perf: Performance improvement
refactor: Code restructuring for better maintainability, readability, or scalability
style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc.)
test: Adding missing tests or correcting existing tests
WIP: Work in progress, a temporary label for incomplete or ongoing work
Changelog Entry
Description
[Concisely describe the changes made in this pull request, including any relevant motivation and impact (e.g., fixing a bug, adding a feature, or improving performance)]
Added
Added. Check for Apple Silicon and the pytorch mps device
Added. README on how to use uv in a venv to debug the backend
Changed
Changed. Detection in env.py of docker, cuda and cpu only so mps is added easily
Deprecated
None
Removed
None
Fixed
None
Security
None
Breaking Changes
None
Additional Information
The current implementation runs sentence transformers in RAG on CPU only when Apple Silicon support is available
Screenshots or Videos
Hard to capture the speed increases, but this speeds up RAG by 100x at least on my M4 Max. I can also see the GPU is being used in the Stats.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.
## 📋 Pull Request Information
**Original PR:** https://github.com/open-webui/open-webui/pull/8328
**Author:** [@richtong](https://github.com/richtong)
**Created:** 1/5/2025
**Status:** ❌ Closed
**Base:** `dev` ← **Head:** `rich-mps-dev`
---
### 📝 Commits (1)
- [`3b3b389`](https://github.com/open-webui/open-webui/commit/3b3b389d6c7c108fde0b67d7e76ea64c57593e66) fix: mps detected in env.py
### 📊 Changes
**1 file changed** (+23 additions, -23 deletions)
<details>
<summary>View changed files</summary>
📝 `backend/open_webui/env.py` (+23 -23)
</details>
### 📄 Description
Detects in env.py if apple silicon can be used and sets pytorch to it (and sorry my linter added some other commas and reformatting, I can fix if needed and disable that).
The changes are:
1. env.py: detects if the pytorch mps device is available and if pytorch is built for it
2. env.py: cleans up the current detection code which includes a try and except and replaces it with a simple if.
3. env.py: adds a try and except when importing pytorch in case the backend is built without it
5. No documentation change made since Apple Silicon support is not specific listed and judging from Discussion, its assumed it works but could add if needed
6. No new dependencies added uses existing pytorch checks
Note: I do not have a Windows machine nor a CUDA machine, but looking at the code it should perform the same in setting env.py properly
Note: I did not fix but can if desired fixed two other places where there code looks wrong. I just grepped for "coda". I'm not sure why they ignore the logic in env.py which basically ignores CUDA in Docker unless DOCKER_CUDA is set
./backend/open_webui/routes/audio.py:99 This is setting device type but only allows cuda and cpu but not mps. I don't know the audio code well enough to know why it is overriding what is set in ./env.py
```
"device": DEVICE_TYPE if DEVICE_TYPE and DEVICE_TYPE == "cuda" else "cpu",
# should become
"device": DEVICE_TYPE
```
./backend/open_webui/retrieval/models/colbert.py:11 this is ignoring DEVICE_TYPE. Again, I didn't change it because I don't know the Colbert code well enough. Does if fail on Apple Silicon. Is this because RAG does not have access to env.py, I don't know the code well enough to say
```
self.device = "cuda" if torch.cuda.is_available() else "cpu"
# should become
self.device = DEVICE_TYPE
```
# Pull Request Checklist
### Note to first-time contributors: Please open a discussion post in [Discussions](https://github.com/open-webui/open-webui/discussions) and describe your changes before submitting a pull request.
**Before submitting, make sure you've checked the following:**
- [x] **Target branch:** Please verify that the pull request targets the `dev` branch.
- [x] **Description:** Provide a concise description of the changes made in this pull request.
- [x] **Changelog:** Ensure a changelog entry following the format of [Keep a Changelog](https://keepachangelog.com/) is added at the bottom of the PR description.
- [x] **Documentation:** Have you updated relevant documentation [Open WebUI Docs](https://github.com/open-webui/docs), or other documentation sources?
- [x] **Dependencies:** Are there any new dependencies? Have you updated the dependency versions in the documentation?
- [x] **Testing:** Have you written and run sufficient tests for validating the changes?
- [x ] **Code review:** Have you performed a self-review of your code, addressing any coding standard issues and ensuring adherence to the project's coding standards?
- [x] **Prefix:** To cleary categorize this pull request, prefix the pull request title, using one of the following:
- **BREAKING CHANGE**: Significant changes that may affect compatibility
- **build**: Changes that affect the build system or external dependencies
- **ci**: Changes to our continuous integration processes or workflows
- **chore**: Refactor, cleanup, or other non-functional code changes
- **docs**: Documentation update or addition
- **feat**: Introduces a new feature or enhancement to the codebase
- **fix**: Bug fix or error correction
- **i18n**: Internationalization or localization changes
- **perf**: Performance improvement
- **refactor**: Code restructuring for better maintainability, readability, or scalability
- **style**: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc.)
- **test**: Adding missing tests or correcting existing tests
- **WIP**: Work in progress, a temporary label for incomplete or ongoing work
# Changelog Entry
### Description
- [Concisely describe the changes made in this pull request, including any relevant motivation and impact (e.g., fixing a bug, adding a feature, or improving performance)]
### Added
Added. Check for Apple Silicon and the pytorch mps device
Added. README on how to use uv in a venv to debug the backend
### Changed
Changed. Detection in env.py of docker, cuda and cpu only so mps is added easily
### Deprecated
None
### Removed
None
### Fixed
None
### Security
None
### Breaking Changes
None
---
### Additional Information
The current implementation runs sentence transformers in RAG on CPU only when Apple Silicon support is available
### Screenshots or Videos
Hard to capture the speed increases, but this speeds up RAG by 100x at least on my M4 Max. I can also see the GPU is being used in the Stats.
---
<sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
📋 Pull Request Information
Original PR: https://github.com/open-webui/open-webui/pull/8328
Author: @richtong
Created: 1/5/2025
Status: ❌ Closed
Base:
dev← Head:rich-mps-dev📝 Commits (1)
3b3b389fix: mps detected in env.py📊 Changes
1 file changed (+23 additions, -23 deletions)
View changed files
📝
backend/open_webui/env.py(+23 -23)📄 Description
Detects in env.py if apple silicon can be used and sets pytorch to it (and sorry my linter added some other commas and reformatting, I can fix if needed and disable that).
The changes are:
Note: I do not have a Windows machine nor a CUDA machine, but looking at the code it should perform the same in setting env.py properly
Note: I did not fix but can if desired fixed two other places where there code looks wrong. I just grepped for "coda". I'm not sure why they ignore the logic in env.py which basically ignores CUDA in Docker unless DOCKER_CUDA is set
./backend/open_webui/routes/audio.py:99 This is setting device type but only allows cuda and cpu but not mps. I don't know the audio code well enough to know why it is overriding what is set in ./env.py
./backend/open_webui/retrieval/models/colbert.py:11 this is ignoring DEVICE_TYPE. Again, I didn't change it because I don't know the Colbert code well enough. Does if fail on Apple Silicon. Is this because RAG does not have access to env.py, I don't know the code well enough to say
Pull Request Checklist
Note to first-time contributors: Please open a discussion post in Discussions and describe your changes before submitting a pull request.
Before submitting, make sure you've checked the following:
devbranch.Changelog Entry
Description
Added
Added. Check for Apple Silicon and the pytorch mps device
Added. README on how to use uv in a venv to debug the backend
Changed
Changed. Detection in env.py of docker, cuda and cpu only so mps is added easily
Deprecated
None
Removed
None
Fixed
None
Security
None
Breaking Changes
None
Additional Information
The current implementation runs sentence transformers in RAG on CPU only when Apple Silicon support is available
Screenshots or Videos
Hard to capture the speed increases, but this speeds up RAG by 100x at least on my M4 Max. I can also see the GPU is being used in the Stats.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.