ykyao commited on
Commit
43ed691
·
verified ·
1 Parent(s): 0a80698

Default to BF16 weights for inference

Browse files
Files changed (3) hide show
  1. README.md +3 -1
  2. config.json +3 -2
  3. modeling_arloop.py +1 -1
README.md CHANGED
@@ -33,7 +33,7 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
33
  model_id = "osunlp/ARLoop-1.3B" # Or the local ARLOOP_hf directory.
34
  tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
35
  model = AutoModelForCausalLM.from_pretrained(
36
- model_id, trust_remote_code=True, dtype=torch.float32,
37
  ).to("cuda").eval()
38
  inputs = tokenizer("The capital of France is", return_tensors="pt").to("cuda")
39
  with torch.inference_mode():
@@ -41,6 +41,8 @@ with torch.inference_mode():
41
  print(tokenizer.decode(output[0], skip_special_tokens=True))
42
  ```
43
 
 
 
44
  ## Architecture
45
 
46
  | Property | Value |
 
33
  model_id = "osunlp/ARLoop-1.3B" # Or the local ARLOOP_hf directory.
34
  tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
35
  model = AutoModelForCausalLM.from_pretrained(
36
+ model_id, trust_remote_code=True, dtype=torch.bfloat16,
37
  ).to("cuda").eval()
38
  inputs = tokenizer("The capital of France is", return_tensors="pt").to("cuda")
39
  with torch.inference_mode():
 
41
  print(tokenizer.decode(output[0], skip_special_tokens=True))
42
  ```
43
 
44
+ Weights load in BF16 for inference. The stored checkpoint retains the original FP32 weights; routing and normalization statistics use FP32 internally.
45
+
46
  ## Architecture
47
 
48
  | Property | Value |
config.json CHANGED
@@ -8,7 +8,7 @@
8
  "AutoModelForCausalLM": "modeling_arloop.ARLoopForCausalLM"
9
  },
10
  "bos_token_id": 0,
11
- "dtype": "float32",
12
  "embedding_scale": 1.0,
13
  "eos_token_id": 0,
14
  "head_dim": 128,
@@ -31,5 +31,6 @@
31
  "tie_word_embeddings": true,
32
  "transformers_version": "5.12.1",
33
  "use_cache": true,
34
- "vocab_size": 49152
 
35
  }
 
8
  "AutoModelForCausalLM": "modeling_arloop.ARLoopForCausalLM"
9
  },
10
  "bos_token_id": 0,
11
+ "dtype": "bfloat16",
12
  "embedding_scale": 1.0,
13
  "eos_token_id": 0,
14
  "head_dim": 128,
 
31
  "tie_word_embeddings": true,
32
  "transformers_version": "5.12.1",
33
  "use_cache": true,
34
+ "vocab_size": 49152,
35
+ "torch_dtype": "bfloat16"
36
  }
modeling_arloop.py CHANGED
@@ -182,7 +182,7 @@ class ARLoopForCausalLM(PreTrainedModel, GenerationMixin):
182
  if not bool(attention_mask.all()):
183
  mask = (key_indices[None, :] <= query_indices[:, None])[None, None, :, :]
184
  mask = mask & attention_mask[:, None, None, :].bool()
185
- # Match the training model's BF16 residual stream with FP32 master weights.
186
  with torch.autocast(device_type=input_ids.device.type, dtype=torch.bfloat16):
187
  embedded = (self.transformer.wte(input_ids) * self.config.embedding_scale).to(torch.bfloat16)
188
  ve = self.shared_value_embedding(input_ids)
 
182
  if not bool(attention_mask.all()):
183
  mask = (key_indices[None, :] <= query_indices[:, None])[None, None, :, :]
184
  mask = mask & attention_mask[:, None, None, :].bool()
185
+ # Use BF16 activations; parameters follow the requested loading dtype.
186
  with torch.autocast(device_type=input_ids.device.type, dtype=torch.bfloat16):
187
  embedded = (self.transformer.wte(input_ids) * self.config.embedding_scale).to(torch.bfloat16)
188
  ve = self.shared_value_embedding(input_ids)