isaac commited on
Commit ·
978cbc0
1
Parent(s): 96d95b8
Trim v5 shim to imports plus find_pruneable; fix README outputs
Browse files
RADAR_inference/dynamic_network_architectures/med.py
CHANGED
|
@@ -43,69 +43,14 @@ try:
|
|
| 43 |
find_pruneable_heads_and_indices,
|
| 44 |
prune_linear_layer,
|
| 45 |
)
|
| 46 |
-
except ImportError: # transformers>=5 dropped
|
| 47 |
-
import
|
| 48 |
-
from typing import Callable, List, Set, Union
|
| 49 |
|
| 50 |
from transformers.modeling_utils import PreTrainedModel
|
|
|
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
# (Apache-2.0, HuggingFace Team)
|
| 54 |
-
# feed-forward chunking. Keeps this module working on transformers>=5.
|
| 55 |
-
def prune_linear_layer(layer: nn.Linear, index: torch.LongTensor, dim: int = 0) -> nn.Linear:
|
| 56 |
-
"""Prune a linear layer to keep only entries in index. Used to remove heads."""
|
| 57 |
-
index = index.to(layer.weight.device)
|
| 58 |
-
W = layer.weight.index_select(dim, index).clone().detach()
|
| 59 |
-
if layer.bias is not None:
|
| 60 |
-
if dim == 1:
|
| 61 |
-
b = layer.bias.clone().detach()
|
| 62 |
-
else:
|
| 63 |
-
b = layer.bias[index].clone().detach()
|
| 64 |
-
new_size = list(layer.weight.size())
|
| 65 |
-
new_size[dim] = len(index)
|
| 66 |
-
new_layer = nn.Linear(new_size[1], new_size[0], bias=layer.bias is not None).to(layer.weight.device)
|
| 67 |
-
new_layer.weight.requires_grad = False
|
| 68 |
-
new_layer.weight.copy_(W.contiguous())
|
| 69 |
-
new_layer.weight.requires_grad = True
|
| 70 |
-
if layer.bias is not None:
|
| 71 |
-
new_layer.bias.requires_grad = False
|
| 72 |
-
new_layer.bias.copy_(b.contiguous())
|
| 73 |
-
new_layer.bias.requires_grad = True
|
| 74 |
-
return new_layer
|
| 75 |
-
|
| 76 |
-
def apply_chunking_to_forward(
|
| 77 |
-
forward_fn: Callable[..., torch.Tensor],
|
| 78 |
-
chunk_size: int,
|
| 79 |
-
chunk_dim: int,
|
| 80 |
-
*input_tensors,
|
| 81 |
-
) -> torch.Tensor:
|
| 82 |
-
"""Chunk input_tensors over chunk_dim, apply forward_fn per chunk, concat. No-op when chunk_size<=0."""
|
| 83 |
-
assert len(input_tensors) > 0, f"{input_tensors} has to be a tuple/list of tensors"
|
| 84 |
-
num_args_in_forward_chunk_fn = len(inspect.signature(forward_fn).parameters)
|
| 85 |
-
if num_args_in_forward_chunk_fn != len(input_tensors):
|
| 86 |
-
raise ValueError(
|
| 87 |
-
f"forward_chunk_fn expects {num_args_in_forward_chunk_fn} arguments, but only {len(input_tensors)} input "
|
| 88 |
-
"tensors are given"
|
| 89 |
-
)
|
| 90 |
-
if chunk_size > 0:
|
| 91 |
-
tensor_shape = input_tensors[0].shape[chunk_dim]
|
| 92 |
-
for input_tensor in input_tensors:
|
| 93 |
-
if input_tensor.shape[chunk_dim] != tensor_shape:
|
| 94 |
-
raise ValueError(
|
| 95 |
-
f"All input tenors have to be of the same shape: {tensor_shape}, "
|
| 96 |
-
f"found shape {input_tensor.shape[chunk_dim]}"
|
| 97 |
-
)
|
| 98 |
-
if input_tensors[0].shape[chunk_dim] % chunk_size != 0:
|
| 99 |
-
raise ValueError(
|
| 100 |
-
f"The dimension to be chunked {input_tensors[0].shape[chunk_dim]} has to be a multiple of the chunk "
|
| 101 |
-
f"size {chunk_size}"
|
| 102 |
-
)
|
| 103 |
-
num_chunks = input_tensors[0].shape[chunk_dim] // chunk_size
|
| 104 |
-
input_tensors_chunks = tuple(input_tensor.chunk(num_chunks, dim=chunk_dim) for input_tensor in input_tensors)
|
| 105 |
-
output_chunks = tuple(forward_fn(*input_tensors_chunk) for input_tensors_chunk in zip(*input_tensors_chunks))
|
| 106 |
-
return torch.cat(output_chunks, dim=chunk_dim)
|
| 107 |
-
return forward_fn(*input_tensors)
|
| 108 |
-
|
| 109 |
def find_pruneable_heads_and_indices(
|
| 110 |
heads: List[int], n_heads: int, head_size: int, already_pruned_heads: Set[int]
|
| 111 |
) -> Tuple[Set[int], torch.LongTensor]:
|
|
|
|
| 43 |
find_pruneable_heads_and_indices,
|
| 44 |
prune_linear_layer,
|
| 45 |
)
|
| 46 |
+
except ImportError: # transformers>=5 dropped the modeling_utils re-exports
|
| 47 |
+
from typing import List, Set
|
|
|
|
| 48 |
|
| 49 |
from transformers.modeling_utils import PreTrainedModel
|
| 50 |
+
from transformers.pytorch_utils import apply_chunking_to_forward, prune_linear_layer
|
| 51 |
|
| 52 |
+
# find_pruneable_heads_and_indices was removed outright in transformers>=5
|
| 53 |
+
# (local copy of the transformers 4.x helper, Apache-2.0, HuggingFace Team).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
def find_pruneable_heads_and_indices(
|
| 55 |
heads: List[int], n_heads: int, head_size: int, already_pruned_heads: Set[int]
|
| 56 |
) -> Tuple[Set[int], torch.LongTensor]:
|
README.md
CHANGED
|
@@ -13,7 +13,7 @@ preload_from_hub:
|
|
| 13 |
- radar-generalist/RADAR checkpoint_radar_pretrain.pth,bert-base-chinese/config.json,bert-base-chinese/config_decoder.json,bert-base-chinese/pytorch_model.bin,bert-base-chinese/tokenizer.json,bert-base-chinese/tokenizer_config.json,bert-base-chinese/vocab.txt
|
| 14 |
---
|
| 15 |
|
| 16 |
-
RADAR abdominal-CT findings demo: upload a contrast-enhanced abdominal CT volume (`.nii` / `.nii.gz`), get
|
| 17 |
|
| 18 |
- Paper/model: [radar-generalist/RADAR](https://huggingface.co/radar-generalist/RADAR) (CC BY-NC-SA 4.0, non-commercial research use only)
|
| 19 |
- Source: vendored inference code from the RADAR release (Zenodo `damo-radar.zip`, record `21271172`); weights load at runtime from the public HuggingFace repo, no token needed
|
|
|
|
| 13 |
- radar-generalist/RADAR checkpoint_radar_pretrain.pth,bert-base-chinese/config.json,bert-base-chinese/config_decoder.json,bert-base-chinese/pytorch_model.bin,bert-base-chinese/tokenizer.json,bert-base-chinese/tokenizer_config.json,bert-base-chinese/vocab.txt
|
| 14 |
---
|
| 15 |
|
| 16 |
+
RADAR abdominal-CT findings demo: upload a contrast-enhanced abdominal CT volume (`.nii` / `.nii.gz`), get top-finding scores plus a table of all 146 per-finding scores.
|
| 17 |
|
| 18 |
- Paper/model: [radar-generalist/RADAR](https://huggingface.co/radar-generalist/RADAR) (CC BY-NC-SA 4.0, non-commercial research use only)
|
| 19 |
- Source: vendored inference code from the RADAR release (Zenodo `damo-radar.zip`, record `21271172`); weights load at runtime from the public HuggingFace repo, no token needed
|