Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| technical:recipes:tensorflow-in-virtualenv [2021-01-27 09:38] – [Create TensorFlow Virtualenv] anita | technical:recipes:tensorflow-in-virtualenv [2025-11-06 15:30] (current) – [Create TensorFlow Virtualenv] thuachen | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== TensorFlow Python Virtual Environment ====== | ||
| + | <note warning> This page is under construction. | ||
| + | </ | ||
| + | This page documents the creation of a Python virtual environment (virtualenv) containing the TensorFlow software for machine learning on the Caviness HPC system((The steps should also work on the DARWIN HPC system, though with different package versions.)). | ||
| + | |||
| + | ===== Prepare Workgroup Directory ===== | ||
| + | |||
| + | Prepare to add software in the standard sub-directories of the workgroup storage: | ||
| + | |||
| + | <code bash> | ||
| + | [user@login01 ~]$ workgroup -g my_workgroup | ||
| + | [(my_workgroup: | ||
| + | [(my_workgroup: | ||
| + | </ | ||
| + | |||
| + | These commands create any missing directories. | ||
| + | |||
| + | ===== Create TensorFlow Virtualenv ===== | ||
| + | First, we will need to load the Miniconda VALET package and create the virtual environment: | ||
| + | |||
| + | <code bash> | ||
| + | [(my_workgroup: | ||
| + | Adding package `miniconda/ | ||
| + | </ | ||
| + | |||
| + | The '' | ||
| + | |||
| + | <code bash> | ||
| + | [(my_workgroup: | ||
| + | Loading channels: done | ||
| + | # Name | ||
| + | tensorflow | ||
| + | tensorflow | ||
| + | tensorflow | ||
| + | |||
| + | ... | ||
| + | |||
| + | tensorflow | ||
| + | tensorflow | ||
| + | tensorflow | ||
| + | </ | ||
| + | All versions of the TensorFlow virtual environment will be stored in the directory '' | ||
| + | |||
| + | <code bash> | ||
| + | [(my_workgroup: | ||
| + | 2.17.0-cpu | ||
| + | [(my_workgroup: | ||
| + | |||
| + | [(my_workgroup: | ||
| + | 2.17.0-cuda | ||
| + | [(my_workgroup: | ||
| + | </ | ||
| + | |||
| + | The virtualenvs are created using the '' | ||
| + | |||
| + | <code bash> | ||
| + | [(my_workgroup: | ||
| + | WARNING: A directory already exists at the target location '/ | ||
| + | but it is not a conda environment. | ||
| + | Continue creating environment (y/[n])? y | ||
| + | |||
| + | : | ||
| + | |||
| + | Preparing transaction: | ||
| + | Verifying transaction: | ||
| + | Executing transaction: | ||
| + | # | ||
| + | # To activate this environment, | ||
| + | # | ||
| + | # $ conda activate / | ||
| + | # | ||
| + | # To deactivate an active environment, | ||
| + | # | ||
| + | # $ conda deactivate | ||
| + | </ | ||
| + | |||
| + | We're **not** going to activate that virtualenv -- we will install the other one next: | ||
| + | |||
| + | <code bash> | ||
| + | [(my_workgroup: | ||
| + | WARNING: A directory already exists at the target location '/ | ||
| + | but it is not a conda environment. | ||
| + | Continue creating environment (y/[n])? y | ||
| + | |||
| + | : | ||
| + | |||
| + | Preparing transaction: | ||
| + | Verifying transaction: | ||
| + | Executing transaction: | ||
| + | # | ||
| + | # To activate this environment, | ||
| + | # | ||
| + | # $ conda activate / | ||
| + | # | ||
| + | # To deactivate an active environment, | ||
| + | # | ||
| + | # $ conda deactivate | ||
| + | </ | ||
| + | We will need to run the '' | ||
| + | <code bash> | ||
| + | [(my_workgroup: | ||
| + | (/ | ||
| + | </ | ||
| + | Use '' | ||
| + | |||
| + | <code bash> | ||
| + | (/ | ||
| + | [(my_workgroup: | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== VALET Package Definition ===== | ||
| + | |||
| + | Assuming the workgroup does //not// already have a TensorFlow VALET package definition, the following text: | ||
| + | |||
| + | <file tensorflow.vpkg_yaml> | ||
| + | tensorflow: | ||
| + | prefix: / | ||
| + | description: | ||
| + | flags: | ||
| + | - no-standard-paths | ||
| + | actions: | ||
| + | - action: source | ||
| + | script: | ||
| + | sh: miniconda-activate.sh | ||
| + | order: failure-first | ||
| + | success: 0 | ||
| + | versions: | ||
| + | " | ||
| + | description: | ||
| + | dependencies: | ||
| + | - miniconda/ | ||
| + | " | ||
| + | description: | ||
| + | dependencies: | ||
| + | - miniconda/ | ||
| + | </ | ||
| + | |||
| + | would be added to '' | ||
| + | |||
| + | <file tensorflow.vpkg_yaml> | ||
| + | tensorflow: | ||
| + | prefix: / | ||
| + | description: | ||
| + | flags: | ||
| + | - no-standard-paths | ||
| + | actions: | ||
| + | - action: source | ||
| + | script: | ||
| + | sh: miniconda-activate.sh | ||
| + | order: failure-first | ||
| + | success: 0 | ||
| + | versions: | ||
| + | " | ||
| + | description: | ||
| + | dependencies: | ||
| + | - miniconda/ | ||
| + | " | ||
| + | description: | ||
| + | dependencies: | ||
| + | - miniconda/ | ||
| + | " | ||
| + | description: | ||
| + | dependencies: | ||
| + | - miniconda/ | ||
| + | </ | ||
| + | |||
| + | <note warning> | ||
| + | |||
| + | <note tip>On Caviness after a user has used the '' | ||
| + | |||
| + | With a properly-constructed package definition file, you can now check for your versions of TensorFlow: | ||
| + | |||
| + | <code bash> | ||
| + | [(it_nss: | ||
| + | |||
| + | Available versions in package (* = default version): | ||
| + | |||
| + | [/ | ||
| + | tensorflow | ||
| + | * 2.17.0: | ||
| + | 2.17.0: | ||
| + | | ||
| + | : | ||
| + | </ | ||
| + | |||
| + | ===== Job Scripts ===== | ||
| + | |||
| + | Any job scripts you submit that want to run scripts using this virtualenv should include something like the following toward its end: | ||
| + | |||
| + | < | ||
| + | # | ||
| + | # Setup TensorFlow virtualenv: | ||
| + | # | ||
| + | vpkg_require tensorflow/ | ||
| + | |||
| + | # | ||
| + | # Run a Python script in that virtualenv: | ||
| + | # | ||
| + | python3 my_tf_work.py | ||
| + | rc=$? | ||
| + | |||
| + | # | ||
| + | # Do cleanup work, etc.... | ||
| + | # | ||
| + | |||
| + | # | ||
| + | # Exit with whatever exit code our Python script handed back: | ||
| + | # | ||
| + | exit $rc | ||
| + | </ | ||