argos-translate icon indicating copy to clipboard operation
argos-translate copied to clipboard

How do I enable "cuda" from within a .py script, rather than CLI?

Open solkad opened this issue 2 years ago • 4 comments

The Readme gives this CLI example for using cuda:

ARGOS_DEVICE_TYPE=cuda argos-translate --from-lang en --to-lang es "Hello World"

I tested this with a 3000 word string instead of Hello World, and it works. The Python example takes 60 seconds for the 3000 word string, but the CLI takes only 7 seconds.

However I wasn't able to figure out how to enable cuda from within the Python script. I tried using 'import os' and 'os.environ["ARGOS_DEVICE_TYPE"]="cuda"' to set the ARGOS_DEVICE_TYPE environment variable, but that didn't work. The script still took 60 seconds, the same as if the line setting ARGOS_DEVICE_TYPE to cuda was not present. Here is my code, with the 3000 word string snipped:

import argostranslate.package
import argostranslate.translate
import os

os.environ["ARGOS_DEVICE_TYPE"]="cuda"
from_code = "en"
to_code = "es"

# Download and install Argos Translate package
argostranslate.package.update_package_index()
available_packages = argostranslate.package.get_available_packages()
package_to_install = next(
    filter(
        lambda x: x.from_code == from_code and x.to_code == to_code, available_packages
    )
)
argostranslate.package.install_from_path(package_to_install.download())

# Translate
translatedText = argostranslate.translate.translate("...snipped...", from_code, to_code)
print(translatedText)

What is my error here? How can I enable Cuda from within the .py script? I am using "python3 scriptname.py" to execute.

solkad avatar Mar 10 '23 15:03 solkad

Maybe you should set an environment variable "ARGOS_DEVICE_TYPE" in your operation system.

Take windows for example:

2023-03-31

More details are in this issue: #210

By the way, cuda must be installed at first if you want to enable "cuda", and it is resource-consuming job, so large VRAM of GPU is recommended.

doveg avatar Mar 31 '23 05:03 doveg

If you put os.environ["ARGOS_DEVICE_TYPE"]="cuda" before importing the package it works fine. Like this:

import os
os.environ["ARGOS_DEVICE_TYPE"]="cuda"

import argostranslate.package
import argostranslate.translate

maximxlss avatar Aug 18 '23 08:08 maximxlss

If you put os.environ["ARGOS_DEVICE_TYPE"]="cuda" before importing the package it works fine. Like this:

import os
os.environ["ARGOS_DEVICE_TYPE"]="cuda"

import argostranslate.package
import argostranslate.translate

Great thanks, that works! I wouldn't have thought of that.

Though it seems that Python doesn't work as fast as the CLI. The way I was doing it before, 3000 words took 62-63 seconds. After this suggestion, it took 27-28 seconds. But the CLI only takes 7-8 seconds, if I do this instead of using a Python script:

ARGOS_DEVICE_TYPE=cuda argos-translate --from-lang en --to-lang es "...3000 words snipped..."

Anyone have any idea why the CLI is faster, and if there's any way to get a Python script to work just as fast as the CLI?

solkad avatar Aug 18 '23 15:08 solkad