Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/support inf1 #6

Merged
merged 10 commits into from
Dec 11, 2024
37 changes: 33 additions & 4 deletions ultralytics/engine/neuron_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def export_formats():
["PyTorch", "-", ".pt", True, True],
["TorchScript", "torchscript", ".torchscript", True, True],
["AWS NeuronX", "neuronx", ".neuronx", True, True],
["AWS Neuron", "neuron", ".neuron", True, True],
["ONNX", "onnx", ".onnx", True, True],
["OpenVINO", "openvino", "_openvino_model", True, False],
["TensorRT", "engine", ".engine", False, True],
Expand Down Expand Up @@ -173,7 +174,22 @@ def __call__(self, model=None) -> str:
flags = [x == fmt for x in fmts]
if sum(flags) != 1:
raise ValueError(f"Invalid export format='{fmt}'. Valid formats are {fmts}")
jit, neuronx, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs, paddle, ncnn = flags # export booleans
(
jit,
neuronx,
neuron,
onnx,
xml,
engine,
coreml,
saved_model,
pb,
tflite,
edgetpu,
tfjs,
paddle,
ncnn,
) = flags # export booleans
is_tf_format = any((saved_model, pb, tflite, edgetpu, tfjs))

# Device
Expand Down Expand Up @@ -315,6 +331,8 @@ def __call__(self, model=None) -> str:
f[11], _ = self.export_ncnn()
if neuronx: # NeuronX
f[12], _ = self.export_neuronx()
if neuron: # Neuron
f[13], _ = self.export_neuron()

# Finish
f = [str(x) for x in f if x] # filter out '' and None
Expand Down Expand Up @@ -354,6 +372,17 @@ def export_neuronx(self, prefix=colorstr("AWS NeuronX:")):
from torch.utils.mobile_optimizer import optimize_for_mobile

optimize_for_mobile(ts)._save_for_lite_interpreter(str(f), _extra_files=extra_files)
else:
ts.save(str(f), _extra_files=extra_files)
return f, None
extra_files = {"config.txt": json.dumps(self.metadata)}
ts.save(str(f), _extra_files=extra_files)
return f, None

@try_export
def export_neuron(self, prefix=colorstr("AWS Neuron:")):
import torch_neuron
"""YOLOv8 Neuron model export."""
LOGGER.info(f"\n{prefix} starting export with torch {torch_neuron.__version__}...")
f = self.file.with_suffix(".neuron")
ts = torch_neuron.trace(self.model, self.im, strict=False)
extra_files = {"config.txt": json.dumps(self.metadata)}
ts.save(str(f), _extra_files=extra_files)
return f, None
13 changes: 13 additions & 0 deletions ultralytics/nn/neuron_autobackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def __init__(
pt,
jit,
neuronx,
neuron,
onnx,
xml,
engine,
Expand Down Expand Up @@ -182,6 +183,16 @@ def __init__(
model.half() if fp16 else model.float()
if extra_files["config.txt"]: # load metadata dict
metadata = json.loads(extra_files["config.txt"], object_hook=lambda x: dict(x.items()))
## Neuron
elif neuron:
import torch_neuron
LOGGER.info(f"Loading {w} for Neuron inference...")
extra_files = {"config.txt": ""} # model metadata
model = torch.jit.load(w, _extra_files=extra_files, map_location=device)
model.half() if fp16 else model.float()
if extra_files["config.txt"]: # load metadata dict
metadata = json.loads(extra_files["config.txt"], object_hook=lambda x: dict(x.items()))

# ONNX OpenCV DNN
elif dnn:
LOGGER.info(f"Loading {w} for ONNX OpenCV DNN inference...")
Expand Down Expand Up @@ -469,6 +480,8 @@ def forward(self, im, augment=False, visualize=False, embed=None):

elif self.neuronx:
y = self.model(im)
elif self.neuron:
y = self.model(im)
# ONNX OpenCV DNN
elif self.dnn:
im = im.cpu().numpy() # torch to numpy
Expand Down
Loading