mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-07-27 20:02:15 +00:00
Fix unused registered buffers issue on ORTModule (#7525)
This commit is contained in:
parent
54db6648af
commit
9ba9da0c95
2 changed files with 38 additions and 5 deletions
|
|
@ -65,7 +65,7 @@ def _combine_input_buffers_initializers(param_names, onnx_input_names, input_inf
|
|||
|
||||
# User inputs
|
||||
non_none_inputs = [inp for inp in inputs if inp is not None]
|
||||
named_buffers_iter = iter(buffer_names)
|
||||
buffer_names_dict = {buffer_name: inp for buffer_name, inp in buffer_names}
|
||||
result = []
|
||||
|
||||
for input_idx, name in enumerate(onnx_input_names):
|
||||
|
|
@ -82,16 +82,17 @@ def _combine_input_buffers_initializers(param_names, onnx_input_names, input_inf
|
|||
|
||||
elif input_idx >= len(non_none_inputs):
|
||||
# Registered buffers are translated to user_input+initializer in ONNX
|
||||
buffer_name, inp = next(named_buffers_iter)
|
||||
assert buffer_name == name, f'Input name {name} expected, but {buffer_name} found!'
|
||||
try:
|
||||
inp = buffer_names_dict[name]
|
||||
except KeyError:
|
||||
raise KeyError(f'Registered buffer name {name} not found.')
|
||||
|
||||
if inp is not None:
|
||||
result.append(inp)
|
||||
else:
|
||||
raise RuntimeError(f'Input is present in ONNX graph but not provided: {name}.')
|
||||
# Initializers
|
||||
for param in param_names:
|
||||
result.append(param[1])
|
||||
result.extend([param[1] for param in param_names])
|
||||
return result
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1585,6 +1585,38 @@ def test_model_with_registered_buffers():
|
|||
output = ort_model(x)
|
||||
assert output is not None
|
||||
|
||||
|
||||
def test_model_with_unused_registered_buffers():
|
||||
class UnusedBufferNet(torch.nn.Module):
|
||||
def __init__(self, input_size, hidden_size, num_classes):
|
||||
super(UnusedBufferNet, self).__init__()
|
||||
|
||||
self.fc1 = torch.nn.Linear(input_size, hidden_size)
|
||||
self.relu = torch.nn.ReLU()
|
||||
self.fc2 = torch.nn.Linear(hidden_size, num_classes)
|
||||
self.register_buffer("buffer1s", torch.ones(num_classes))
|
||||
self.register_buffer("buffer2s", 1+torch.ones(num_classes))
|
||||
self.register_buffer("buffer3s", 2+torch.ones(num_classes))
|
||||
|
||||
def forward(self, input1):
|
||||
out = self.fc1(input1)
|
||||
out = self.relu(out)
|
||||
out = self.fc2(out)
|
||||
out += self.buffer3s
|
||||
return out
|
||||
device = 'cuda'
|
||||
|
||||
N, D_in, H, D_out = 64, 784, 500, 10
|
||||
model = UnusedBufferNet(D_in, H, D_out).to(device)
|
||||
ort_model = ORTModule(model)
|
||||
# Check that the original forward signature is preserved.
|
||||
assert signature(model.forward) == signature(ort_model.forward)
|
||||
x = torch.randn(N, D_in, device=device)
|
||||
# Make sure model runs without any exception
|
||||
output = ort_model(x)
|
||||
assert output is not None
|
||||
|
||||
|
||||
def test_model_with_constant_and_registered_parameters():
|
||||
class NeuralNetWithRegisteredParamsWithConstant(torch.nn.Module):
|
||||
def __init__(self, input_size, hidden_size, num_classes):
|
||||
|
|
|
|||
Loading…
Reference in a new issue