hshatti / TONNXRuntime

TOnnxRuntime is a Microsoft ONNXRuntime AI and Machine Learning Library for Freepascal / Delphi
MIT License
48 stars 10 forks source link

access violation after second time execute session.Run(Inputs); #6

Closed armgong closed 10 months ago

armgong commented 10 months ago

write very simple test (mod from fastcnn-10 demo), use onnxruntime master branch build , on lazarus for windows x64

the model is add.onnx (https://github.com/microsoft/onnxruntime/blob/main/onnxruntime/test/testdata/cloud_models/addf.onnx). which is two input X and Y both are float[-1] , one output Z is float[-1] .

first run is ok , but second run failed with access violation.

confusing now, is this a bug or my fault ?

{$mode delphi}{$H+}

{$ifdef fpc}
{$PACKRECORDS 32}
  {$ifdef CPUX86_64}
    {$asmmode intel}
  {$endif}
  {$PointerMath On}
{$endif}

//{$define NO_SMARTPTR}

procedure TFrmMain.BitBtn1Click(Sender: TObject);
var
  inputs,OutPuts:TORTNameValueList;
  Inputx_s,inputy_s,output_s:TOrtTensor<single>;
  i:int64_t;
begin
try
  ModelPath:='model\'+ComboBox1.Text;
  Inputx_s:=TOrtTensor<single>.Create([10]);
  Inputy_s:=TOrtTensor<single>.Create([10]);
  for i:=0 to Inputx_s.Shape[0]-1 do
    begin
     Inputx_s.index1[i]:=(i+1)*0.2;
     Inputy_S.index1[i]:=(i+1);
    end;
  inputs['X']:=inputx_s;
  inputs['Y']:=inputy_S;
  Outputs:=session.Run(Inputs);
  output_s:= Outputs.Values[0];
  log('result : '+join(output_s.shape)+' '+output_s.ToString) ;
finally

end;
end;         

more test with copy run code twice in one run, still failed.

{$mode delphi}{$H+}

{$ifdef fpc}
{$PACKRECORDS 32}
  {$ifdef CPUX86_64}
    {$asmmode intel}
  {$endif}
  {$PointerMath On}
{$endif}

//{$define NO_SMARTPTR}

procedure TFrmMain.BitBtn1Click(Sender: TObject);
var
  inputs,OutPuts:TORTNameValueList;
  Inputx_s,inputy_s,output_s:TOrtTensor<single>;
  i:int64_t;
begin
try
  Inputx_s:=TOrtTensor<single>.Create([10]);
  Inputy_s:=TOrtTensor<single>.Create([10]);
  for i:=0 to Inputx_s.Shape[0]-1 do
    begin
     Inputx_s.index1[i]:=(i+1)*0.2;
     Inputy_S.index1[i]:=(i+1);
    end;
  inputs['X']:=inputx_s;
  inputs['Y']:=inputy_S;
  Outputs:=session.Run(Inputs);
  output_s:= Outputs.Values[0];
  log('result : '+join(output_s.shape)+' '+output_s.ToString) ;
  for i:=0 to Inputx_s.Shape[0]-1 do
    begin
     Inputx_s.index1[i]:=(i+1)*0.3;
     Inputy_S.index1[i]:=(i+2);
    end;
  inputs['X']:=inputx_s;
  inputs['Y']:=inputy_S;//will failed on this line 
  Outputs:=session.Run(Inputs); 
finally

end;
end;         
hshatti commented 10 months ago

Hi , Thanks for the observation, indeed this is a strange behaviour impacting only FPC , I have tried to replicate it on Delphi and it works with no issues, debugging this case shows that there could be a bug with FPC memory allocator/reallocator, I managed to implement a quick work around that solves this issue, try to git pull the repo again and let me know if it works.

appreciate if you star the repo it will encourage me to work on more native and interesting AI for pascal libraries. Best

armgong commented 10 months ago

hi, thanks for your quick fix , already starred your repo on last month , thanks you for your work on this .

now the access violation issue solved.

armgong commented 10 months ago

now close this issue