Skip to content

Commit d04ab05

Browse files
committed
Support modern uv project management (uv init + uv add)
Update MaxText's installation scripts and setup process to detect 'uv.lock' in the current working directory. When detected, the scripts now use 'uv add --frozen' to correctly track extra GitHub-based dependencies in the user's pyproject.toml. Key changes: - Updated install_pre_train_deps.py and install_post_train_deps.py to support 'uv add'. - Updated setup.sh to use 'uv add' for requirements while maintaining 'uv pip install -e .' for the package itself to avoid conflicts. - Updated docs/install_maxtext.md with instructions for the modern uv workflow and fixed various outdated command names and paths.
1 parent 34995f4 commit d04ab05

4 files changed

Lines changed: 103 additions & 39 deletions

File tree

docs/install_maxtext.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ install_maxtext_tpu_github_deps
4545

4646
# Option 2: Installing maxtext[cuda12]
4747
uv pip install maxtext[cuda12] --resolution=lowest
48-
install_maxtext_cuda12_github_dep
48+
install_maxtext_cuda12_github_deps
4949

5050
# Option 3: Installing maxtext[tpu-post-train]
5151
uv pip install maxtext[tpu-post-train] --resolution=lowest
@@ -55,10 +55,27 @@ install_maxtext_tpu_post_train_extra_deps
5555
uv pip install maxtext[runner] --resolution=lowest
5656
```
5757

58-
> **Note:** The `install_maxtext_tpu_github_deps`, `install_maxtext_cuda12_github_dep`, and
58+
> **Note:** The `install_maxtext_tpu_github_deps`, `install_maxtext_cuda12_github_deps`, and
5959
> `install_maxtext_tpu_post_train_extra_deps` commands are temporarily required to install dependencies directly from GitHub
6060
> that are not yet available on PyPI. As shown above, choose the one that corresponds to your use case.
6161
62+
## Modern UV Project (Recommended for New Projects)
63+
64+
If you are starting a new project and want to use `uv`'s project management features (with a `pyproject.toml` and `uv.lock` in your own project), you can use `uv add`. MaxText's helper scripts will detect your `uv.lock` and correctly add their extra dependencies to your `pyproject.toml`.
65+
66+
```bash
67+
# 1. Initialize your project
68+
mkdir my-maxtext-project && cd my-maxtext-project
69+
uv init
70+
71+
# 2. Add MaxText as a dependency
72+
uv add maxtext[tpu] --resolution=lowest
73+
74+
# 3. Install MaxText's extra GitHub dependencies
75+
# These will be automatically added to your pyproject.toml
76+
install_maxtext_tpu_github_deps
77+
```
78+
6279
> **Note:** The maxtext package contains a comprehensive list of all direct and transitive dependencies, with lower bounds, generated by [seed-env](https://github.com/google-ml-infra/actions/tree/main/python_seed_env). We highly recommend the `--resolution=lowest` flag. It instructs `uv` to install the specific, tested versions of dependencies defined by MaxText, rather than the latest available ones. This ensures a consistent and reproducible environment, which is critical for stable performance and for running benchmarks.
6380
6481
## macOS Installation
@@ -88,7 +105,7 @@ install_maxtext_tpu_github_deps
88105

89106
# Option 2: Installing .[cuda12]
90107
uv pip install -e .[cuda12] --resolution=lowest
91-
install_maxtext_cuda12_github_dep
108+
install_maxtext_cuda12_github_deps
92109

93110
# Option 3: Installing .[tpu-post-train]
94111
uv pip install -e .[tpu-post-train] --resolution=lowest
@@ -114,7 +131,7 @@ To update dependencies, you will follow these general steps:
114131

115132
1. **Modify Base Requirements**: Update the desired dependencies in `base_requirements/requirements.txt` or the hardware-specific files (`base_requirements/tpu-base-requirements.txt`, `base_requirements/gpu-base-requirements.txt`).
116133
2. **Generate New Files**: Run the `seed-env` CLI tool to generate new, fully-pinned requirements files based on your changes.
117-
3. **Update Project Files**: Copy the newly generated files into the `generated_requirements/` directory.
134+
3. **Update Project Files**: Copy the newly generated files into the `src/dependencies/requirements/generated_requirements/` directory.
118135
4. **Handle GitHub Dependencies**: Move any dependencies that are installed directly from GitHub from the generated files to `src/dependencies/github_deps/pre_train_deps.txt`.
119136
5. **Verify**: Test the new dependencies to ensure the project installs and runs correctly.
120137

@@ -170,8 +187,8 @@ After generating the new requirements, you need to update the files in the MaxTe
170187

171188
1. **Copy the generated files:**
172189

173-
- Move `generated_tpu_artifacts/tpu-requirements.txt` to `generated_requirements/tpu-requirements.txt`.
174-
- Move `generated_gpu_artifacts/cuda12-requirements.txt` to `generated_requirements/cuda12-requirements.txt`.
190+
- Move `generated_tpu_artifacts/tpu-requirements.txt` to `src/dependencies/requirements/generated_requirements/tpu-requirements.txt`.
191+
- Move `generated_gpu_artifacts/cuda12-requirements.txt` to `src/dependencies/requirements/generated_requirements/cuda12-requirements.txt`.
175192

176193
2. **Update `pre_train_deps.txt` (if necessary):**
177194
Currently, MaxText uses a few dependencies, such as `mlperf-logging` and `google-jetstream`, that are installed directly from GitHub source. These are defined in `base_requirements/requirements.txt`, and the `seed-env` tool will carry them over to the generated requirements files.

src/dependencies/github_deps/install_post_train_deps.py

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,48 @@ def main():
4949
print(f"Stderr: {e.stderr.decode()}")
5050
sys.exit(1)
5151

52-
command = [
53-
sys.executable, # Use the current Python executable's pip to ensure the correct environment
54-
"-m",
55-
"uv",
56-
"pip",
57-
"install",
58-
"-r",
59-
str(extra_deps_path),
60-
"--no-deps",
61-
]
62-
63-
local_vllm_install_command = [
64-
sys.executable, # Use the current Python executable's pip to ensure the correct environment
65-
"-m",
66-
"uv",
67-
"pip",
68-
"install",
69-
f"{repo_root}/maxtext/integration/vllm", # MaxText on vllm installations
70-
"--no-deps",
71-
]
52+
# Check for uv.lock in the current directory to decide between 'uv add' and 'uv pip install'
53+
if os.path.exists("uv.lock"):
54+
print("Found uv.lock. Using 'uv add' to track dependencies in pyproject.toml.")
55+
command = [
56+
sys.executable,
57+
"-m",
58+
"uv",
59+
"add",
60+
"--frozen",
61+
"-r",
62+
str(extra_deps_path),
63+
]
64+
local_vllm_install_command = [
65+
sys.executable,
66+
"-m",
67+
"uv",
68+
"add",
69+
"--frozen",
70+
"--editable",
71+
f"{repo_root}/maxtext/integration/vllm",
72+
]
73+
else:
74+
print("uv.lock not found. Using 'uv pip install' for direct environment installation.")
75+
command = [
76+
sys.executable, # Use the current Python executable's pip to ensure the correct environment
77+
"-m",
78+
"uv",
79+
"pip",
80+
"install",
81+
"-r",
82+
str(extra_deps_path),
83+
"--no-deps",
84+
]
85+
local_vllm_install_command = [
86+
sys.executable, # Use the current Python executable's pip to ensure the correct environment
87+
"-m",
88+
"uv",
89+
"pip",
90+
"install",
91+
f"{repo_root}/maxtext/integration/vllm", # MaxText on vllm installations
92+
"--no-deps",
93+
]
7294

7395
try:
7496
# Run the command to install Github dependencies

src/dependencies/github_deps/install_pre_train_deps.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,30 @@ def main():
4646
print(f"Stderr: {e.stderr.decode()}")
4747
sys.exit(1)
4848

49-
command = [
50-
sys.executable, # Use the current Python executable's pip to ensure the correct environment
51-
"-m",
52-
"uv",
53-
"pip",
54-
"install",
55-
"-r",
56-
str(extra_deps_path),
57-
"--no-deps",
58-
]
49+
# Check for uv.lock in the current directory to decide between 'uv add' and 'uv pip install'
50+
if os.path.exists("uv.lock"):
51+
print("Found uv.lock. Using 'uv add' to track dependencies in pyproject.toml.")
52+
command = [
53+
sys.executable,
54+
"-m",
55+
"uv",
56+
"add",
57+
"--frozen",
58+
"-r",
59+
str(extra_deps_path),
60+
]
61+
else:
62+
print("uv.lock not found. Using 'uv pip install' for direct environment installation.")
63+
command = [
64+
sys.executable, # Use the current Python executable's pip to ensure the correct environment
65+
"-m",
66+
"uv",
67+
"pip",
68+
"install",
69+
"-r",
70+
str(extra_deps_path),
71+
"--no-deps",
72+
]
5973

6074
try:
6175
# Run the command

src/dependencies/scripts/setup.sh

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ install_maxtext_package_without_deps() {
199199
# to significantly faster image builds.
200200
if [ -f 'pyproject.toml' ]; then
201201
echo "Installing MaxText package without installing the dependencies (already installed)"
202+
# We use 'uv pip install' for the editable install to avoid self-dependency
203+
# conflicts that can occur with 'uv add' when the package name matches
204+
# the project name in pyproject.toml.
202205
python3 -m uv pip install --no-deps -e .
203206
fi
204207
}
@@ -215,8 +218,12 @@ install_maxtext_with_deps() {
215218
dep_name='src/dependencies/requirements/generated_requirements/tpu-requirements.txt'
216219
fi
217220
echo "Installing requirements from $dep_name"
218-
python3 -m uv pip install --resolution=lowest -r "$dep_name" \
219-
-r 'src/dependencies/github_deps/pre_train_deps.txt'
221+
if [ -f "uv.lock" ]; then
222+
python3 -m uv add --frozen -r "$dep_name" -r 'src/dependencies/github_deps/pre_train_deps.txt'
223+
else
224+
python3 -m uv pip install --resolution=lowest -r "$dep_name" \
225+
-r 'src/dependencies/github_deps/pre_train_deps.txt'
226+
fi
220227

221228
install_maxtext_package_without_deps
222229
}
@@ -229,7 +236,11 @@ install_post_training_deps() {
229236
echo "Setting up MaxText post-training workflow for $DEVICE device"
230237
dep_name='src/dependencies/requirements/generated_requirements/tpu-post-train-requirements.txt'
231238
echo "Installing requirements from $dep_name"
232-
python3 -m uv pip install --resolution=lowest -r "$dep_name"
239+
if [ -f "uv.lock" ]; then
240+
python3 -m uv add --frozen -r "$dep_name"
241+
else
242+
python3 -m uv pip install --resolution=lowest -r "$dep_name"
243+
fi
233244
python3 -m src.dependencies.github_deps.install_post_train_deps
234245
}
235246

0 commit comments

Comments
 (0)