Instructions to use aehrc/cxrmate-rrg24 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use aehrc/cxrmate-rrg24 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="aehrc/cxrmate-rrg24", trust_remote_code=True)# Load model directly from transformers import AutoTokenizer, AutoModel tokenizer = AutoTokenizer.from_pretrained("aehrc/cxrmate-rrg24", trust_remote_code=True) model = AutoModel.from_pretrained("aehrc/cxrmate-rrg24", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload model
Browse files- modelling_cxrrg.py +8 -12
modelling_cxrrg.py
CHANGED
|
@@ -201,7 +201,6 @@ class CXRRGModel(VisionEncoderDecoderModel):
|
|
| 201 |
self,
|
| 202 |
input_ids,
|
| 203 |
special_token_ids,
|
| 204 |
-
token_type_id_sections=None,
|
| 205 |
past_key_values=None,
|
| 206 |
use_cache=None,
|
| 207 |
encoder_outputs=None,
|
|
@@ -227,7 +226,7 @@ class CXRRGModel(VisionEncoderDecoderModel):
|
|
| 227 |
# `inputs_embeds` are only to be used in the 1st generation step:
|
| 228 |
inputs_embeds = torch.cat([encoder_outputs[0], self.decoder.get_input_embeddings()(input_ids)], dim=1)
|
| 229 |
|
| 230 |
-
decoder_token_type_ids = self.token_ids_to_token_type_ids(input_ids, special_token_ids
|
| 231 |
decoder_token_type_ids = torch.cat(
|
| 232 |
[
|
| 233 |
torch.full(
|
|
@@ -256,7 +255,7 @@ class CXRRGModel(VisionEncoderDecoderModel):
|
|
| 256 |
decoder_position_ids.masked_fill_(report_attention_mask == 0, 1)
|
| 257 |
|
| 258 |
# Always place token_ids_to_token_type_ids_past before input_ids = input_ids[:, remove_prefix_length:]:
|
| 259 |
-
decoder_token_type_ids = self.token_ids_to_token_type_ids_past(input_ids, special_token_ids
|
| 260 |
decoder_position_ids = decoder_position_ids[:, -1:]
|
| 261 |
|
| 262 |
past_length = past_key_values[0][0].shape[2]
|
|
@@ -283,7 +282,7 @@ class CXRRGModel(VisionEncoderDecoderModel):
|
|
| 283 |
)
|
| 284 |
return input_dict
|
| 285 |
|
| 286 |
-
def token_ids_to_token_type_ids(self, token_ids, special_token_ids
|
| 287 |
"""
|
| 288 |
Extract token type identifiers from the token identifiers.
|
| 289 |
|
|
@@ -296,10 +295,8 @@ class CXRRGModel(VisionEncoderDecoderModel):
|
|
| 296 |
token_type_ids - token type identifiers.
|
| 297 |
"""
|
| 298 |
|
| 299 |
-
token_type_id_sections = token_type_id_sections if token_type_id_sections is not None else list(range(len(special_token_ids) + 1))
|
| 300 |
-
|
| 301 |
mbatch_size, seq_len = token_ids.shape
|
| 302 |
-
token_type_ids = torch.full_like(token_ids,
|
| 303 |
|
| 304 |
for i, j in enumerate(special_token_ids):
|
| 305 |
# Find first occurrence of special tokens that indicate the boundary between sections:
|
|
@@ -322,11 +319,11 @@ class CXRRGModel(VisionEncoderDecoderModel):
|
|
| 322 |
)
|
| 323 |
])
|
| 324 |
|
| 325 |
-
token_type_ids[ids[:, 0], ids[:, 1]] =
|
| 326 |
|
| 327 |
return token_type_ids
|
| 328 |
|
| 329 |
-
def token_ids_to_token_type_ids_past(self, token_ids, special_token_ids
|
| 330 |
"""
|
| 331 |
Extract token type identifiers from the token identifiers if past != None. Make sure to input all the
|
| 332 |
token_ids (e.g., do not input input_ids = input_ids[:, remove_prefix_length:] from prepare_inputs_for_generation).
|
|
@@ -339,8 +336,7 @@ class CXRRGModel(VisionEncoderDecoderModel):
|
|
| 339 |
token_type_ids - token type identifiers.
|
| 340 |
"""
|
| 341 |
|
| 342 |
-
|
| 343 |
-
token_type_ids = torch.full([token_ids.shape[0], 1], token_type_id_sections[0], dtype=torch.long, device=token_ids.device)
|
| 344 |
|
| 345 |
# https://huggingface.co/docs/transformers/model_doc/bert#transformers.BertTokenizer.create_token_type_ids_from_sequences.example
|
| 346 |
token_ids = token_ids[:, :-1]
|
|
@@ -349,7 +345,7 @@ class CXRRGModel(VisionEncoderDecoderModel):
|
|
| 349 |
|
| 350 |
# Find first occurrence of special token, which indicates the boundary between sections:
|
| 351 |
exists = torch.any(token_ids == j, dim=1, keepdim=True)
|
| 352 |
-
token_type_ids[exists] =
|
| 353 |
|
| 354 |
return token_type_ids
|
| 355 |
|
|
|
|
| 201 |
self,
|
| 202 |
input_ids,
|
| 203 |
special_token_ids,
|
|
|
|
| 204 |
past_key_values=None,
|
| 205 |
use_cache=None,
|
| 206 |
encoder_outputs=None,
|
|
|
|
| 226 |
# `inputs_embeds` are only to be used in the 1st generation step:
|
| 227 |
inputs_embeds = torch.cat([encoder_outputs[0], self.decoder.get_input_embeddings()(input_ids)], dim=1)
|
| 228 |
|
| 229 |
+
decoder_token_type_ids = self.token_ids_to_token_type_ids(input_ids, special_token_ids)
|
| 230 |
decoder_token_type_ids = torch.cat(
|
| 231 |
[
|
| 232 |
torch.full(
|
|
|
|
| 255 |
decoder_position_ids.masked_fill_(report_attention_mask == 0, 1)
|
| 256 |
|
| 257 |
# Always place token_ids_to_token_type_ids_past before input_ids = input_ids[:, remove_prefix_length:]:
|
| 258 |
+
decoder_token_type_ids = self.token_ids_to_token_type_ids_past(input_ids, special_token_ids)
|
| 259 |
decoder_position_ids = decoder_position_ids[:, -1:]
|
| 260 |
|
| 261 |
past_length = past_key_values[0][0].shape[2]
|
|
|
|
| 282 |
)
|
| 283 |
return input_dict
|
| 284 |
|
| 285 |
+
def token_ids_to_token_type_ids(self, token_ids, special_token_ids):
|
| 286 |
"""
|
| 287 |
Extract token type identifiers from the token identifiers.
|
| 288 |
|
|
|
|
| 295 |
token_type_ids - token type identifiers.
|
| 296 |
"""
|
| 297 |
|
|
|
|
|
|
|
| 298 |
mbatch_size, seq_len = token_ids.shape
|
| 299 |
+
token_type_ids = torch.full_like(token_ids, self.config.section_ids[0], dtype=torch.long, device=token_ids.device)
|
| 300 |
|
| 301 |
for i, j in enumerate(special_token_ids):
|
| 302 |
# Find first occurrence of special tokens that indicate the boundary between sections:
|
|
|
|
| 319 |
)
|
| 320 |
])
|
| 321 |
|
| 322 |
+
token_type_ids[ids[:, 0], ids[:, 1]] = self.config.section_ids[i + 1]
|
| 323 |
|
| 324 |
return token_type_ids
|
| 325 |
|
| 326 |
+
def token_ids_to_token_type_ids_past(self, token_ids, special_token_ids):
|
| 327 |
"""
|
| 328 |
Extract token type identifiers from the token identifiers if past != None. Make sure to input all the
|
| 329 |
token_ids (e.g., do not input input_ids = input_ids[:, remove_prefix_length:] from prepare_inputs_for_generation).
|
|
|
|
| 336 |
token_type_ids - token type identifiers.
|
| 337 |
"""
|
| 338 |
|
| 339 |
+
token_type_ids = torch.full([token_ids.shape[0], 1], self.config.section_ids[0], dtype=torch.long, device=token_ids.device)
|
|
|
|
| 340 |
|
| 341 |
# https://huggingface.co/docs/transformers/model_doc/bert#transformers.BertTokenizer.create_token_type_ids_from_sequences.example
|
| 342 |
token_ids = token_ids[:, :-1]
|
|
|
|
| 345 |
|
| 346 |
# Find first occurrence of special token, which indicates the boundary between sections:
|
| 347 |
exists = torch.any(token_ids == j, dim=1, keepdim=True)
|
| 348 |
+
token_type_ids[exists] = self.config.section_ids[i + 1]
|
| 349 |
|
| 350 |
return token_type_ids
|
| 351 |
|