Run this model with HF Jobs
Run nvidia/RADIO on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/nvidia/RADIO. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/nvidia/RADIO on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use nvidia/RADIO with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use nvidia/RADIO with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-feature-extraction", model="nvidia/RADIO", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("nvidia/RADIO", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload model
#2
by mranzinger - opened
Files changed (6)
- config.json+1-1
- enable_spectral_reparam.pyAdded+227
- eradio_model.py+3
- hf_model.py+11-1
- model.safetensors+2-2
- radio_model.py+15
353 unmodified lines354355356357357358359360353 unmodified lines 432 ], "torch_dtype": "bfloat16", "transformers_version": "4.37.2", "transformers_version": "4.40.1", "version": "radio_v2.1", "vitdet_window_size": null}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227from logging import getLoggerimport mathimport osfrom typing import Union, Tuplefrom types import MethodType
import torchfrom torch import nnfrom torch.nn import functional as Ffrom torch.nn.utils import parametrizefrom torch.nn.utils.parametrizations import _SpectralNorm
from timm.models.vision_transformer import Attention, Mlp
_EPS = 1e-5
class _SNReweight(_SpectralNorm): def __init__(self, weight: torch.Tensor, *args, init_norm_to_current: bool = False, alpha: float = 0.05, version: int = 2, **kwargs): super().__init__(weight, *args, **kwargs)
self.alpha = alpha self.version = version self.register_buffer('_sn_version', torch.tensor(version))
if init_norm_to_current: # This will set the numerator to match the denominator, which should preserve the original values init_scale = self._get_sigma(weight).item() else: init_scale = 1.0
if version == 1: init_value = init_scale elif version == 2: t = init_scale - alpha if t < _EPS: getLogger("spectral_reparam").warn(f'The initialized spectral norm {init_scale} is too small to be represented. Setting to {_EPS} instead.') t = _EPS
init_value = math.log(math.exp(t) - 1) else: raise ValueError(f'Unsupported version: {version}')
# Make 2D so that weight decay gets applied self.scale = nn.Parameter(torch.tensor([[init_value]], dtype=torch.float32, device=weight.device))
# Re-implementing this because we need to make division by sigma safe def _get_sigma(self, weight: torch.Tensor) -> torch.Tensor: if weight.ndim == 1: # Faster and more exact path, no need to approximate anything sigma = weight.norm() else: weight_mat = self._reshape_weight_to_matrix(weight) if self.training: self._power_method(weight_mat, self.n_power_iterations) # See above on why we need to clone u = self._u.clone(memory_format=torch.contiguous_format) v = self._v.clone(memory_format=torch.contiguous_format) # The proper way of computing this should be through F.bilinear, but # it seems to have some efficiency issues: # https://github.com/pytorch/pytorch/issues/58093 sigma = torch.dot(u, torch.mv(weight_mat, v))
return sigma + self.eps
def forward(self, weight: torch.Tensor, *args, **kwargs): dtype = weight.dtype sigma = self._get_sigma(weight, *args, **kwargs)
if self.version == 1: scale = self.scale elif self.version == 2: scale = F.softplus(self.scale) + self.alpha else: raise ValueError(f'Unsupported version: {self.version}')
scale = scale.float() / sigma.float()
y = weight * scale
if dtype in (torch.float16, torch.bfloat16): y = y.to(dtype) return y
def _load_from_state_dict(self, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs): version_key = f'{prefix}_sn_version' if version_key not in state_dict: self.version = 1 state_dict[version_key] = torch.tensor(1) return super()._load_from_state_dict(state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs)
class _AttnSNReweight(nn.Module): def __init__(self, weight: torch.Tensor, *args, init_norm_to_current: bool = False, renorm_values: bool = False, **kwargs): super().__init__()
parts = weight.split(weight.shape[0] // 3, dim=0)
ct = 2 if not renorm_values else 3
self.parts = nn.ModuleList([ _SNReweight(p, *args, init_norm_to_current=init_norm_to_current, **kwargs) if i < ct else nn.Identity() for i, p in enumerate(parts) ])
def forward(self, weight: torch.Tensor, *args, **kwargs): parts = weight.split(weight.shape[0] // 3, dim=0)
parts = [ fn(p) for fn, p in zip(self.parts, parts) ]
return torch.cat(parts, dim=0)
def enable_spectral_reparam(model: nn.Module, n_power_iterations: int = 1, eps: float = 1e-6, init_norm_to_current: bool = False, renorm_values: bool = True, renorm_mlp: bool = True): # print('Enabling spectral reparametrization') for mod in model.modules(): if isinstance(mod, Attention): parametrize.register_parametrization( mod.qkv, 'weight', _AttnSNReweight(mod.qkv.weight, n_power_iterations, dim=0, eps=eps, init_norm_to_current=init_norm_to_current, renorm_values=renorm_values), ) pass elif isinstance(mod, Mlp) and renorm_mlp: parametrize.register_parametrization( mod.fc1, 'weight', _SNReweight(mod.fc1.weight, n_power_iterations, dim=0, eps=eps, init_norm_to_current=init_norm_to_current), ) parametrize.register_parametrization( mod.fc2, 'weight', _SNReweight(mod.fc2.weight, n_power_iterations, dim=0, eps=eps, init_norm_to_current=init_norm_to_current), ) pass
def configure_spectral_reparam_from_args(model: nn.Module, args): spectral_reparam = getattr(args, 'spectral_reparam', False) if isinstance(spectral_reparam, bool) and spectral_reparam: enable_spectral_reparam(model, init_norm_to_current=args.pretrained) elif isinstance(spectral_reparam, dict): enable_spectral_reparam( model, n_power_iterations=spectral_reparam.get('n_power_iterations', 1), eps=spectral_reparam.get('eps', 1e-12), init_norm_to_current=args.pretrained, )
def disable_spectral_reparam(model: nn.Module): for mod in model.modules(): if isinstance(mod, Attention): parametrize.remove_parametrizations(mod.qkv, 'weight') pass elif isinstance(mod, Mlp): parametrize.remove_parametrizations(mod.fc1, 'weight') parametrize.remove_parametrizations(mod.fc2, 'weight') pass
if __name__ == '__main__': import argparse from . import radio_model as create_model
parser = argparse.ArgumentParser(description='Remove parametrization from state dict') parser.add_argument('--checkpoint', type=str, required=True, help='The checkpoint to load') parser.add_argument('--output', type=str, default='', help='Where to store the checkpoint') parser.add_argument('--release', default=False, action='store_true', help='Prune extraneous checkpoint fields') parser.add_argument('--strict', default=False, action='store_true', help='Strictly load the state dict')
args = parser.parse_args()
if not args.output: chk_dir, chk_name = os.path.split(args.checkpoint) args.output = os.path.join(chk_dir, f'clean_{chk_name}') print(f'Set output to "{args.output}"')
chk = torch.load(args.checkpoint, map_location='cpu', mmap=True)
model = create_model.create_model_from_args(chk['args'])
key = 'base_model.' mod_state = dict() extra_state = dict() for k, v in chk['state_dict'].items(): if k.startswith(key): mod_state[k[len(key):]] = v else: extra_state[k] = v
chk_load_info = model.load_state_dict(mod_state, strict=args.strict) if chk_load_info.unexpected_keys or chk_load_info.missing_keys: print(chk_load_info)
if chk['args'].spectral_reparam: disable_spectral_reparam(model)
if hasattr(chk['args'], 'dtype'): model.to(dtype=chk['args'].dtype)
mod_state = model.state_dict() final_state = dict() final_state.update({f'{key}{k}': v for k, v in mod_state.items()}) final_state.update(extra_state)
chk['state_dict'] = final_state chk['args'].spectral_reparam = False
if args.release: chk = { 'arch': chk['arch'], 'epoch': chk['epoch'], 'state_dict': chk['state_dict'], 'args': chk['args'], }
torch.save(chk, args.output) pass1161 unmodified lines1162116311641165116611671168116911701161 unmodified lines return {'rpb'}
def forward_features(self, x): _, _, H, W = x.shape if H % 32 != 0 or W % 32 != 0: raise ValueError(f"E-RADIO requires input dimensions to be divisible by 32 but got H x W: {H} x {W}") x = self.patch_embed(x) full_features = None for il, level in enumerate(self.levels):11 unmodified lines12131415151617181 unmodified line2021222324252696 unmodified lines12312412512612712812913013113213313413513611 unmodified lines# See the License for the specific language governing permissions and# limitations under the License.from collections import namedtuplefrom typing import Optional, List, Unionfrom typing import Callable, Optional, List, Union
from timm.models import VisionTransformerimport torch1 unmodified line
from .common import RESOURCE_MAP, DEFAULT_VERSION
# Force import of eradio_model in order to register it.from .eradio_model import eradiofrom .radio_model import create_model_from_args96 unmodified lines def input_conditioner(self) -> InputConditioner: return self.radio_model.input_conditioner
@input_conditioner.setter def input_conditioner(self, v: InputConditioner): self.radio_model.input_conditioner = v
def make_preprocessor_external(self) -> Callable[[torch.Tensor], torch.Tensor]: ret = self.input_conditioner self.input_conditioner = nn.Identity() return ret
def forward(self, x: torch.Tensor): return self.radio_model.forward(x)12323version https://git-lfs.github.com/spec/v1oid sha256:df75c4351ef558af885acbf0d21ad53fd273e3720b5ae3d1e7d4a23df1ca9ed1size 1306581088oid sha256:03534ca8b7a26b0cbf69073b944fdd47f41aedad1b3b01c1e387c27191abc8desize 130401888017 unmodified lines1819202122232482 unmodified lines10710810911011111211311411511611711868 unmodified lines1871881891901911921931941951961976 unmodified lines20420520620720820921017 unmodified linesfrom . import extra_timm_modelsfrom .adaptor_base import AdaptorBase, RadioOutput, AdaptorInputfrom . import eradio_modelfrom .enable_spectral_reparam import configure_spectral_reparam_from_args
class Resolution(NamedTuple):82 unmodified lines fn()
def forward(self, x: torch.Tensor) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]: res_step = self.min_resolution_step if res_step is not None and (x.shape[-2] % res_step != 0 or x.shape[-1] % res_step != 0): raise ValueError('The input resolution must be a multiple of `self.min_resolution_step`. ' '`self.get_nearest_supported_resolution(<height>, <width>) is provided as a convenience API. ' f'Input: {x.shape[-2:]}, Nearest: {self.get_nearest_supported_resolution(*x.shape[-2:])}')
x = self.input_conditioner(x) y = self.model.forward_features(x)
68 unmodified lines **args.model_kwargs, )
if hasattr(model, 'norm') and not getattr(args, 'model_norm', False): model.norm = nn.Identity()
model.head = nn.Identity()
assert ( not args.cls_token_per_teacher or args.cpe_max_size is not None ), "CPE must be enabled for multiple CLS tokens!"6 unmodified lines register_multiple=args.register_multiple, )
if args.spectral_reparam: configure_spectral_reparam_from_args(model, args)
return modelconfig.json+1-1
enable_spectral_reparam.pyAdded+227
eradio_model.py+3
hf_model.py+11-1
model.safetensors+2-2
radio_model.py+15