WhiteBoxModel provides an interface for interacting with locally-hosted language models using the Hugging Face Transformers library. This class enables direct access to model weights, embeddings, and internal state, allowing for gradient-based methods and other white-box techniques.
Constructor
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
model_name | string | (Required) | Name of the model on Hugging Face Hub |
device | string | "cpu" | Device to load the model on (“cpu”, “cuda”, “cuda:0”, etc.) |
dtype | torch.dtype | torch.bfloat16 | Data type for model weights |
load_in_8bit | bool | False | Whether to load the model in 8-bit quantization |
load_in_4bit | bool | False | Whether to load the model in 4-bit quantization |
Methods
generate_from_ids
Generates text from tokenized input IDs.Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
input_ids | torch.Tensor | (Required) | Tensor of token IDs |
attention_mask | torch.Tensor | None | Attention mask tensor (created automatically if not provided) |
max_new_tokens | int | 100 | Maximum number of tokens to generate |
temperature | float | 0.7 | Controls randomness in generation |
skip_special_tokens | bool | True | Whether to remove special tokens from output |
return_decoded | bool | True | Whether to return text or token IDs |
return_only_generated_tokens | bool | True | Whether to return only new tokens or all tokens |
Returns
Ifreturn_decoded is True, returns generated text as a string or list of strings. Otherwise, returns tensor of token IDs.
generate_with_chat_template
Generates responses using the model’s chat template format.Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
prompts | List[string] | (Required) | List of prompts to send to the model |
max_new_tokens | int | 100 | Maximum number of tokens to generate |
temperature | float | 0.7 | Controls randomness in generation |
skip_special_tokens | bool | True | Whether to remove special tokens from output |
return_decoded | bool | True | Whether to return text or token IDs |
return_only_generated_tokens | bool | True | Whether to return only new tokens or all tokens |
Returns
Ifreturn_decoded is True, returns generated text as a list of strings. Otherwise, returns tensor of token IDs.
get_input_embeddings
Retrieves the model’s input embedding layer.Returns
Returns the model’s input embedding layer, which can be used for token manipulation or gradient-based methods.save_to_hub
Saves the model and tokenizer to Hugging Face Hub.Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
repo_id | string | (Required) | Repository ID on Hugging Face Hub |
commit_message | string | "Model saved from WhiteBoxModel" | Commit message for the upload |

