Spaces:
Running
Running
Fix bugs
Browse files- translate.py +13 -4
translate.py
CHANGED
|
@@ -54,26 +54,35 @@ def main(
|
|
| 54 |
if tensorrt:
|
| 55 |
import torch_tensorrt
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
traced_model = torch.jit.trace(
|
| 58 |
-
model, [torch.randn((batch_size, max_length)).to("cuda")]
|
| 59 |
)
|
| 60 |
model = torch_tensorrt.compile(
|
| 61 |
traced_model,
|
| 62 |
-
inputs=[torch_tensorrt.Input((batch_size, max_length), dtype=
|
| 63 |
enabled_precisions={dtype},
|
| 64 |
)
|
| 65 |
else:
|
| 66 |
if torch.cuda.is_available():
|
| 67 |
-
|
|
|
|
| 68 |
else:
|
| 69 |
-
|
| 70 |
print("CUDA not available. Using CPU. This will be slow.")
|
|
|
|
| 71 |
|
| 72 |
with tqdm(total=total_lines, desc="Dataset translation") as pbar, open(
|
| 73 |
output_path, "w+", encoding="utf-8"
|
| 74 |
) as output_file:
|
| 75 |
with torch.no_grad():
|
| 76 |
for batch in data_loader:
|
|
|
|
|
|
|
|
|
|
| 77 |
generated_tokens = model.generate(
|
| 78 |
**batch, forced_bos_token_id=lang_code_to_idx
|
| 79 |
)
|
|
|
|
| 54 |
if tensorrt:
|
| 55 |
import torch_tensorrt
|
| 56 |
|
| 57 |
+
device = "cuda"
|
| 58 |
+
|
| 59 |
+
model.to(device)
|
| 60 |
+
|
| 61 |
traced_model = torch.jit.trace(
|
| 62 |
+
model, [torch.randn((batch_size, max_length)).to("cuda", dtype=torch.long)]
|
| 63 |
)
|
| 64 |
model = torch_tensorrt.compile(
|
| 65 |
traced_model,
|
| 66 |
+
inputs=[torch_tensorrt.Input((batch_size, max_length), dtype=torch.long)],
|
| 67 |
enabled_precisions={dtype},
|
| 68 |
)
|
| 69 |
else:
|
| 70 |
if torch.cuda.is_available():
|
| 71 |
+
device = "cuda"
|
| 72 |
+
|
| 73 |
else:
|
| 74 |
+
device = "cpu"
|
| 75 |
print("CUDA not available. Using CPU. This will be slow.")
|
| 76 |
+
model.to(device, dtype=dtype)
|
| 77 |
|
| 78 |
with tqdm(total=total_lines, desc="Dataset translation") as pbar, open(
|
| 79 |
output_path, "w+", encoding="utf-8"
|
| 80 |
) as output_file:
|
| 81 |
with torch.no_grad():
|
| 82 |
for batch in data_loader:
|
| 83 |
+
batch["input_ids"] = batch["input_ids"].to(device)
|
| 84 |
+
batch["attention_mask"] = batch["attention_mask"].to(device)
|
| 85 |
+
|
| 86 |
generated_tokens = model.generate(
|
| 87 |
**batch, forced_bos_token_id=lang_code_to_idx
|
| 88 |
)
|