Add model code for trust_remote_code
Browse files- conditioned_gpt2.py +31 -1
conditioned_gpt2.py
CHANGED
|
@@ -11,6 +11,23 @@ NUM_EVO_STAGES = 4 # 3 etapas + 1
|
|
| 11 |
NUM_HAS_EVOLUTION = 2
|
| 12 |
NUM_COLOR_SHIFTS = 6 # 0 = no shift, 1-5 = ColorShift permutations
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
class ConditionedGPT2(GPT2LMHeadModel):
|
| 16 |
def __init__(
|
|
@@ -344,8 +361,14 @@ class ConditionedGPT2(GPT2LMHeadModel):
|
|
| 344 |
temperature: float = 1.2,
|
| 345 |
top_p: float = 0.95,
|
| 346 |
verbose: bool = False,
|
|
|
|
|
|
|
| 347 |
) -> np.ndarray:
|
| 348 |
-
"""Generate a 64×64 RGB sprite as a numpy array.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
device = next(self.parameters()).device
|
| 350 |
|
| 351 |
cond = self.sample_novel_conditioning(device=str(device))
|
|
@@ -357,6 +380,13 @@ class ConditionedGPT2(GPT2LMHeadModel):
|
|
| 357 |
device=device,
|
| 358 |
)
|
| 359 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
row_marker_ids = [
|
| 361 |
tokenizer.convert_tokens_to_ids(f"[ROW_{i:02d}]") for i in range(64)
|
| 362 |
]
|
|
|
|
| 11 |
NUM_HAS_EVOLUTION = 2
|
| 12 |
NUM_COLOR_SHIFTS = 6 # 0 = no shift, 1-5 = ColorShift permutations
|
| 13 |
|
| 14 |
+
_TYPES = [
|
| 15 |
+
"normal", "fire", "water", "electric", "grass", "ice",
|
| 16 |
+
"fighting", "poison", "ground", "flying", "psychic", "bug",
|
| 17 |
+
"rock", "ghost", "dragon", "dark", "steel", "fairy",
|
| 18 |
+
]
|
| 19 |
+
_TYPE1_UNK = 18 # unknown primary type
|
| 20 |
+
_TYPE2_NONE = 18 # no secondary type
|
| 21 |
+
_TYPE2_UNK = 19 # unknown secondary type
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _resolve_type(val: "str | int | None", default_none_idx: int) -> "int | None":
|
| 25 |
+
if val is None:
|
| 26 |
+
return None
|
| 27 |
+
if isinstance(val, str):
|
| 28 |
+
return _TYPES.index(val.lower()) if val.lower() in _TYPES else default_none_idx
|
| 29 |
+
return int(val)
|
| 30 |
+
|
| 31 |
|
| 32 |
class ConditionedGPT2(GPT2LMHeadModel):
|
| 33 |
def __init__(
|
|
|
|
| 361 |
temperature: float = 1.2,
|
| 362 |
top_p: float = 0.95,
|
| 363 |
verbose: bool = False,
|
| 364 |
+
type1: "str | int | None" = None,
|
| 365 |
+
type2: "str | int | None" = None,
|
| 366 |
) -> np.ndarray:
|
| 367 |
+
"""Generate a 64×64 RGB sprite as a numpy array.
|
| 368 |
+
|
| 369 |
+
type1 / type2 accept a type name ("fire", "water", …) or its integer
|
| 370 |
+
index. Pass None to pick a random value.
|
| 371 |
+
"""
|
| 372 |
device = next(self.parameters()).device
|
| 373 |
|
| 374 |
cond = self.sample_novel_conditioning(device=str(device))
|
|
|
|
| 380 |
device=device,
|
| 381 |
)
|
| 382 |
|
| 383 |
+
t1 = _resolve_type(type1, _TYPE1_UNK)
|
| 384 |
+
t2 = _resolve_type(type2, _TYPE2_NONE)
|
| 385 |
+
if t1 is not None:
|
| 386 |
+
cond["type1"] = torch.tensor([t1], dtype=torch.long, device=device)
|
| 387 |
+
if t2 is not None:
|
| 388 |
+
cond["type2"] = torch.tensor([t2], dtype=torch.long, device=device)
|
| 389 |
+
|
| 390 |
row_marker_ids = [
|
| 391 |
tokenizer.convert_tokens_to_ids(f"[ROW_{i:02d}]") for i in range(64)
|
| 392 |
]
|