Skip to content

[Mobile] Model output is different in React Native compared to NodeJS #19353

Open
@pax-k

Description

@pax-k

Describe the issue

I'm using Xenova/all-MiniLM-L6-v2 to extract embeddings from sentences. Given this inference code, I execute it as is in both NodeJS and React-Native (in RN with a slight difference in how loading the model is made).

The NodeJS outputs are good. The problem is that I get slightly different vector embeddings in React Native, using the same code.

Things to note from the inference code:

  • the model was downloaded locally as seen in the imports
  • uses BertTokenizer
  • tokenizer inputs are padded and truncated
  • no pooling, no normalization, no quantized model
  • returns raw data from the model as is, without any transformations:
let output = await session.run(input);
return Array.from(output.last_hidden_state.data);

To reproduce

Setup iOS:

git clone https://github.com/pax-k/expo-onnx
cd expo-onnx
yarn
npx expo prebuild
npx pod-install
yarn ios
// copy the sortedSentences JSON object from the console

Setup NodeJS:

git clone https://github.com/pax-k/nodejs-onnx
cd nodejs-onnx
yarn
npx ts-node --esm onnx-minilm.ts
// copy the sortedSentences JSON object from the console

I combined and analyzed the 2 JSONs in this project:

  • open it
  • check the browser console
  • you can see this table:
Screenshot 2024-01-31 at 14 17 42

a is from NodeJS
b is from React Native

Observations:

  • none of the NodeJS embeddings match the React Native embeddings, because React Native embeddings have 17 decimals, while in NodeJS there are 16 decimals (see this)
  • manhattan distance should be zero
  • worth noting that dot product is prone to floating point errors (if you run this code in both contexts, you will get different results), but this shouldn't influence the model's raw output

Urgency

It's pretty urgent

Platform

React Native

OS Version

iOS 17.0.1, iPhone 15 Pro Simulator

ONNX Runtime Installation

Released Package

Compiler Version (if 'Built from Source')

No response

Package Name (if 'Released Package')

onnxruntime-react-native

ONNX Runtime Version or Commit ID

1.16.3

ONNX Runtime API

JavaScript

Architecture

ARM64

Execution Provider

Default CPU

Execution Provider Library Version

No response

Metadata

Metadata

Assignees

Labels

api:Javascriptissues related to the Javascript APImodel:transformerissues related to a transformer model: BERT, GPT2, Hugging Face, Longformer, T5, etc.platform:mobileissues related to ONNX Runtime mobile; typically submitted using template

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions