Device: use CUDA when available; model.to(device) after loading works
Browse files- README.md +3 -2
- modeling_klon8.py +2 -2
- src/scripts/twolayer_canvas.py +3 -2
README.md
CHANGED
|
@@ -75,8 +75,9 @@ Notes:
|
|
| 75 |
- The checkpoint is not a standard architecture, so it has no `forward` for general use: a poem is produced by the
|
| 76 |
project's own decoder (`src/` in this repository, unchanged from the paper's code), which the wrapper calls.
|
| 77 |
- The first load also downloads the WangchanBERTa encoder the model is built on.
|
| 78 |
-
- Device:
|
| 79 |
-
|
|
|
|
| 80 |
- Decoding is deterministic given the weights and the seed, and the wrapper returns exactly the stanzas of the paper's
|
| 81 |
decode. A stanza takes about 1.6 seconds on a laptop GPU.
|
| 82 |
- `src/` holds the generation code and the model and training code (`src/scripts/twolayer_canvas.py`). The data-building and
|
|
|
|
| 75 |
- The checkpoint is not a standard architecture, so it has no `forward` for general use: a poem is produced by the
|
| 76 |
project's own decoder (`src/` in this repository, unchanged from the paper's code), which the wrapper calls.
|
| 77 |
- The first load also downloads the WangchanBERTa encoder the model is built on.
|
| 78 |
+
- Device: an NVIDIA GPU (cuda) when available, then the Apple GPU (mps), otherwise CPU; pass `device="cuda"`,
|
| 79 |
+
`"mps"` or `"cpu"` to `from_pretrained` to choose, or call `model.to(device)` after loading. Tested on mps and CPU;
|
| 80 |
+
CUDA has not been tested. The decoder's masks and sampling run on the CPU, so a GPU speeds up only the network pass.
|
| 81 |
- Decoding is deterministic given the weights and the seed, and the wrapper returns exactly the stanzas of the paper's
|
| 82 |
decode. A stanza takes about 1.6 seconds on a laptop GPU.
|
| 83 |
- `src/` holds the generation code and the model and training code (`src/scripts/twolayer_canvas.py`). The data-building and
|
modeling_klon8.py
CHANGED
|
@@ -11,8 +11,8 @@ loads the checkpoint with the project's code, shipped unchanged in `src/` of thi
|
|
| 11 |
`generate_poem`. With the same seed it returns exactly the stanzas of the paper's decode.
|
| 12 |
|
| 13 |
Requirements: torch, transformers, sentencepiece, pythainlp, tltk. The first load also downloads the WangchanBERTa
|
| 14 |
-
encoder (`config.base_model`), which the model is built on. Device:
|
| 15 |
-
`device=
|
| 16 |
"""
|
| 17 |
import importlib
|
| 18 |
import os
|
|
|
|
| 11 |
`generate_poem`. With the same seed it returns exactly the stanzas of the paper's decode.
|
| 12 |
|
| 13 |
Requirements: torch, transformers, sentencepiece, pythainlp, tltk. The first load also downloads the WangchanBERTa
|
| 14 |
+
encoder (`config.base_model`), which the model is built on. Device: NVIDIA GPU (cuda) when available, then Apple GPU (mps), else CPU;
|
| 15 |
+
pass `device=...` (or set KLON8_DEVICE) to choose, or call `model.to(device)` after loading. One klon8 checkpoint can be loaded per Python process.
|
| 16 |
"""
|
| 17 |
import importlib
|
| 18 |
import os
|
src/scripts/twolayer_canvas.py
CHANGED
|
@@ -28,7 +28,7 @@ import torch, torch.nn as nn, torch.nn.functional as F
|
|
| 28 |
from klon8 import prosody as P
|
| 29 |
|
| 30 |
WC = "airesearch/wangchanberta-base-att-spm-uncased"
|
| 31 |
-
DEV = torch.device(os.environ.get("KLON8_DEVICE") or ("mps" if torch.backends.mps.is_available() else "cpu"))
|
| 32 |
C, TL, L = 10, 12, 4
|
| 33 |
T = TL + L * C
|
| 34 |
PAD, MASK, UNK, SEP = 0, 1, 2, 3 # word ids AND token ids (specials have k=0, n=1)
|
|
@@ -547,7 +547,8 @@ class Decoder:
|
|
| 547 |
"""masked log-probs [T, NT] for the current state"""
|
| 548 |
X, tk, tn, tw = self.X, self.X.tok_k, self.X.tok_n, self.X.tok_word
|
| 549 |
self.n_fwd += 1
|
| 550 |
-
|
|
|
|
| 551 |
lg = logits[0].float().cpu() + self.base[None, :]
|
| 552 |
for i in range(L):
|
| 553 |
lg[self.idx(i, 0)] += torch.where(tk == 0, 0.0, -1e9)
|
|
|
|
| 28 |
from klon8 import prosody as P
|
| 29 |
|
| 30 |
WC = "airesearch/wangchanberta-base-att-spm-uncased"
|
| 31 |
+
DEV = torch.device(os.environ.get("KLON8_DEVICE") or ("cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"))
|
| 32 |
C, TL, L = 10, 12, 4
|
| 33 |
T = TL + L * C
|
| 34 |
PAD, MASK, UNK, SEP = 0, 1, 2, 3 # word ids AND token ids (specials have k=0, n=1)
|
|
|
|
| 547 |
"""masked log-probs [T, NT] for the current state"""
|
| 548 |
X, tk, tn, tw = self.X, self.X.tok_k, self.X.tok_n, self.X.tok_word
|
| 549 |
self.n_fwd += 1
|
| 550 |
+
dev = self.m.word_bias.device # the model's own device, so model.to(...) after loading works
|
| 551 |
+
logits, _ = self.m(self.tok[None].to(dev), self.snd[None].to(dev), self.ri_in[None].to(dev))
|
| 552 |
lg = logits[0].float().cpu() + self.base[None, :]
|
| 553 |
for i in range(L):
|
| 554 |
lg[self.idx(i, 0)] += torch.where(tk == 0, 0.0, -1e9)
|