function [t,y] = internal_dlode(y0,tspan,tracedOdefun,~,options,...
    numericalOdeSolver,~,varargin)
%

%   Copyright 2021-2022 The MathWorks, Inc.

% Retrieve cached odefun trace
dlodefun = deep.internal.AcceleratedFunctionManager.getGeneratedFunction(tracedOdefun,1);

theta = varargin;

batchedOdeFun = @(t,y) dlodefun(t,y,theta{:});

t0 = tspan(1);
t1 = tspan(2);
tfinal = tspan(end);

f0 = batchedOdeFun(t0,y0);

[ty,y] = numericalOdeSolver(batchedOdeFun,tspan,y0,options,...
                            t0,t1,tfinal,f0);
t = ty';

if numel(tspan) == 2
    % If tspan has two elements, disregard the internal timesteps.
    y = y(end,:);
else
    % Don't return the first timestep.
    y = y(2:end,:);
end

% Output has size of y0 with additional time dim as last dim.
y = y.';
end
