Download set_schema.py from VynoDePal/kev: direct link, hf CLI and curl.
- Browser
- Download file 4.39 kB
-
https://huggingface.co/VynoDePal/kev/resolve/main/set_schema.py
- Command line
-
hf download hf://VynoDePal/kev/set_schema.py
-
curl -L -o set_schema.py https://huggingface.co/VynoDePal/kev/resolve/main/set_schema.py
4.39 kB
| """Laya Mini v2 encoding. | |
| set_mode=True (default for choice questions): every option block [MASK]+description gets the SAME | |
| position ids and its own segment id; option tokens see shared tokens and their own block only, so | |
| the per-option output does not depend on the listed order. set_mode=False reproduces v1 exactly | |
| (sequential positions, all segments 0, full attention) with identical input_ids. | |
| """ | |
| import numpy as np | |
| from schema import QTYPES, options_for, state_text | |
| SET_INPUT_NAMES = ("input_ids", "attention_mask", "position_ids", "segment_ids", "marker_pos", "marker_mask", "qtype") | |
| INPUT_SCHEMA_VERSION = 2 | |
| def encode_question_v2(tokenizer, state, question, max_length=512, set_mode=None): | |
| if set_mode is None: | |
| set_mode = question["type"] == "choice" | |
| if not 32 <= max_length <= 512: | |
| raise ValueError("max_length must be between 32 and 512") | |
| labels, descriptions = options_for(question) | |
| special = {name: tokenizer.token_to_id(name) for name in ["[CLS]", "[SEP]", "[MASK]"]} | |
| if any(value is None for value in special.values()): | |
| raise ValueError("The tokenizer is missing required BERT special tokens") | |
| def tokenize(text): | |
| return tokenizer.encode(str(text).replace("[MASK]", " "), add_special_tokens=False).ids | |
| instructions = question.get("instructions", "") | |
| if not isinstance(instructions, str) or not instructions.strip(): | |
| raise ValueError("A nonempty instructions string is required") | |
| prefix = [special["[CLS]"]] + tokenize(f"{question['type']} question: {instructions}") + [special["[SEP]"]] | |
| ids, positions, segments = list(prefix), list(range(len(prefix))), [0] * len(prefix) | |
| base = len(prefix) | |
| blocks = [[special["[MASK]"]] + tokenize(description) for description in descriptions] | |
| width = max(len(block) for block in blocks) | |
| markers = [] | |
| for segment, block in enumerate(blocks, start=1): | |
| markers.append(len(ids)) | |
| ids.extend(block) | |
| positions.extend(range(base, base + len(block))) | |
| segments.extend([segment] * len(block)) | |
| next_position = base + width | |
| ids.append(special["[SEP]"]) | |
| positions.append(next_position) | |
| segments.append(0) | |
| if len(ids) > min(192, max_length - 16): | |
| raise ValueError("Question/options exceed the prompt budget; shorten them explicitly") | |
| content = tokenize(state_text(state)) | |
| room = max_length - len(ids) - 1 | |
| kept = content[:room] + [special["[SEP]"]] | |
| ids.extend(kept) | |
| positions.extend(range(next_position + 1, next_position + 1 + len(kept))) | |
| segments.extend([0] * len(kept)) | |
| if not set_mode: | |
| positions = list(range(len(ids))) | |
| segments = [0] * len(ids) | |
| return { | |
| "input_ids": ids, "position_ids": positions, "segment_ids": segments, | |
| "marker_pos": markers, "qtype": QTYPES[question["type"]], "labels": labels, | |
| "truncated_state": len(content) > room, "set_mode": bool(set_mode), | |
| } | |
| def encode_question_set(tokenizer, state, question, max_length=512): | |
| return encode_question_v2(tokenizer, state, question, max_length, set_mode=True) | |
| def collate_set(features): | |
| length = max(len(feature["input_ids"]) for feature in features) | |
| option_count = max(len(feature["marker_pos"]) for feature in features) | |
| batch_size = len(features) | |
| arrays = { | |
| "input_ids": np.zeros((batch_size, length), dtype=np.int64), | |
| "attention_mask": np.zeros((batch_size, length), dtype=np.int64), | |
| "position_ids": np.zeros((batch_size, length), dtype=np.int64), | |
| "segment_ids": np.zeros((batch_size, length), dtype=np.int64), | |
| "marker_pos": np.zeros((batch_size, option_count), dtype=np.int64), | |
| "marker_mask": np.zeros((batch_size, option_count), dtype=np.bool_), | |
| "qtype": np.zeros(batch_size, dtype=np.int64), | |
| } | |
| for index, feature in enumerate(features): | |
| n = len(feature["input_ids"]) | |
| count = len(feature["marker_pos"]) | |
| arrays["input_ids"][index, :n] = feature["input_ids"] | |
| arrays["attention_mask"][index, :n] = 1 | |
| arrays["position_ids"][index, :n] = feature["position_ids"] | |
| arrays["segment_ids"][index, :n] = feature["segment_ids"] | |
| arrays["marker_pos"][index, :count] = feature["marker_pos"] | |
| arrays["marker_mask"][index, :count] = True | |
| arrays["qtype"][index] = feature["qtype"] | |
| return arrays | |