Share via

Error initializing model :Microsoft.ML.OnnxRuntime.OnnxRuntimeException

洵 王 10 Reputation points
2023-06-10T14:45:34.8+00:00

I encountered a problem while using ML.NET

An error occurred while calling ONNX for an open source project, and I don't know how to handle it

System.InvalidOperationException:“Error initializing model :Microsoft.ML.OnnxRuntime.OnnxRuntimeException: [ErrorCode:Fail] Deserialize tensor transformer.word_embeddings.weight failed.open file transformer.word_embeddings.weight fail, errcode = 2 - ϵͳ�Ҳ���ָ�����ļ���
   at Microsoft.ML.OnnxRuntime.NativeApiStatus.VerifySuccess(IntPtr nativeStatus)
   at Microsoft.ML.OnnxRuntime.InferenceSession.Init(String modelPath, SessionOptions options, PrePackedWeightsContainer prepackedWeightsContainer)
   at Microsoft.ML.OnnxRuntime.InferenceSession..ctor(String modelPath, SessionOptions options)
   at Microsoft.ML.Transforms.Onnx.OnnxModel..ctor(String modelFile, Nullable`1 gpuDeviceId, Boolean fallbackToCpu, Boolean ownModelFile, IDictionary`2 shapeDictionary, Int32 recursionLimit, Nullable`1 interOpNumThreads, Nullable`1 intraOpNumThreads)
   at Microsoft.ML.Transforms.Onnx.OnnxTransformer..ctor(IHostEnvironment env, Options options, Byte[] modelBytes)”
 public static EmbeddingInputEmbeds Gen_embedding(List<Int64> ids, bool useGpu)
    {
        if (ids.Count > 0)
        {
            EmbeddingInputids _input_id = new();
            MLContext _mlContext = new();
            _input_id.input_ids = ids.ToArray();
            var pipeline = _mlContext.Transforms.ApplyOnnxModel(modelFile: modelfile + "embedding.onnx",
                                                                gpuDeviceId: useGpu ? 0 : (int?)null,
                                                                inputColumnNames: new[] { "input_ids" },
                                                                outputColumnNames: new[] { "inputs_embeds" },
                                                                fallbackToCpu: true
            );

            IDataView data = _mlContext.Data.LoadFromEnumerable(new List<EmbeddingInputids>() { _input_id });
            var model = pipeline.Fit(data);
            IDataView transformedValues = model.Transform(data);
            IEnumerable<EmbeddingInputEmbeds> results = transformedValues.GetColumn<EmbeddingInputEmbeds>("inputs_embeds");
            if (results.Count() > 0)
                return results?.ElementAtOrDefault(0);
        }
        return new EmbeddingInputEmbeds();
    }

Onnx fromhttps://github.com/wangzhaode/ChatGLM-MNN/releases/tag/v0.4

Developer technologies | .NET | .NET Machine Learning
Developer technologies | .NET | .NET Machine Learning

.NET: Microsoft Technologies based on the .NET software framework. Machine learning: A type of artificial intelligence focused on enabling computers to use observed data to evolve new behaviors that have not been explicitly programmed.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.