Python SDK¶
Contents
Overview¶
The Python SDK is available at genv.sdk
.
This SDK can be used by Python applications to use Genv features such as activating environments, attaching and detaching devices, and locking over-subscribed devices.
Installing the SDK¶
The Python SDK comes as part of the Genv installation out of the box.
Importing the SDK¶
To use the Python SDK, add the following import
command to your Python script:
import genv.sdk
Using Environments¶
If you are not familiar with the concept of environments in Genv, it is recommended to go over the shell usage guide.
Activating an Environment¶
To activate an environment, use the method genv.sdk.activate
.
This method acts as a context manager and provides an easy way to activate a scoped Genv environment. Upon entering, an environment will be created, and upon exiting, the environment will be deactivated.
You can also configure the environment straight from the genv.sdk.activate()
call by passing the argument config: genv.sdk.Env.Config
.
You can activate an environment for the entire script by wrapping your main
.
For example:
import genv.sdk
def main():
pass
if __name__ == "__main__":
with genv.sdk.activate(config=genv.sdk.Env.Config(gpus=1, gpu_memory="4g")):
main()
You can also activate an environment for a specific method. For example when using a Ray task:
import genv.sdk
import ray
@ray.remote
def my_function():
with genv.sdk.activate(config=genv.sdk.Env.Config(name="my-function", gpus=1)):
pass
Configuring the Environment¶
You can configure your environment after activating it using genv.sdk.configure
.
For example:
import genv.sdk
with genv.sdk.activate():
name = input("How should I call the Genv environment?\n")
genv.sdk.configure(genv.sdk.Env.Config(name=name))
Querying the Environment Configuration¶
You can query the configuration of your environment using genv.sdk.configuration
.
For example:
import os
import genv.sdk
with genv.sdk.activate(config=genv.sdk.Env.Config(name=str(os.getpid()))):
print(genv.sdk.configuration())
Using Devices¶
If you are not familiar with the concept of device attachments in Genv environments, it is recommended to go over the shell usage guide.
Attach Devices¶
When activating an environment, it will be attached to devices if its device count is configured.
For example, by specifying the environment configuration argument gpus
, Genv will attach this amount of devices to the newly activated environment:
import genv.sdk
with genv.sdk.activate(config=genv.sdk.Env.Config(gpus=1)):
print(genv.sdk.attached())
You can also attach devices later using genv.sdk.attach()
.
For example:
import genv.sdk
with genv.sdk.activate():
print(genv.sdk.attached())
genv.sdk.configure(genv.sdk.Env.Config(gpus=1))
genv.sdk.attach()
print(genv.sdk.attached())
Reference¶
-
genv.sdk.
active
()¶ Returns whether running in an active environment.
- Return type
bool
-
genv.sdk.
activate
(*, eid: Optional[str] = None, config: Optional[Env.Config] = None)¶ A context manager for activating an environment for the current process.
Configures the environment if configuration is specified. Attaches devices if device count is configured.
Raises
RuntimeError
if already running in an active environment.- Parameters
eid – Environment identifier
config – Environment configuration
- Return type
None
-
genv.sdk.
attached
()¶ Returns the indices of devices attached to the current environment.
Indices are in host namespace even when running in a container.
Raises
RuntimeError
if not running in an active environment.- Return type
Iterable[int]
-
genv.sdk.
configuration
()¶ Returns the current environment configuration.
Raises
RuntimeError
if not running in an active environment.- Return type
Env.Config
-
genv.sdk.
configure
(config: Env.Config)¶ Configures the current environment.
- Parameters
config – Environment configuration
- Return type
None
-
genv.sdk.
eid
()¶ Returns the current environment identifier if running in an active environment.
- Return type
Optional[str]
-
genv.sdk.
lock
()¶ A context manager for obtaining exclusive access to the attached devices.
Does nothing if not running in an active environment or not attached to devices.
- Return type
None