• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..--

README.mdD23-Nov-20236.1 KiB147105

README.md

1# Smart Reply Model
2
3## What is On-Device Smart Reply Model?
4
5Smart Replies are contextually relevant, one-touch responses that help the user
6to reply to an incoming text message (or email) efficiently and effortlessly.
7Smart Replies have been highly successful across several Google products
8including
9[Gmail](https://www.blog.google/products/gmail/save-time-with-smart-reply-in-gmail/),
10[Inbox](https://www.blog.google/products/gmail/computer-respond-to-this-email/)
11and
12[Allo](https://blog.google/products/allo/google-allo-smarter-messaging-app/).
13
14The On-device Smart Reply model is targeted towards text chat use cases. It has
15a completely different architecture from its cloud-based counterparts, and is
16built specifically for memory constraints devices such as phones & watches. It
17has been successfully used to provide [Smart Replies on Android
18Wear](https://research.googleblog.com/2017/02/on-device-machine-intelligence.html)
19to all first- & third-party apps.
20
21The on-device model comes with several benefits. It is:
22
23*   **Faster**: The model resides on the device and does not require internet
24    connectivity. Thus, the inference is very fast and has an average latency of
25    only a few milliseconds.
26*   **Resource efficient**: The model has a small memory footprint on
27    the device.
28*   **Privacy-friendly**: The user data never leaves the device and this
29    eliminates any privacy restrictions.
30
31A caveat, though, is that the on-device model has lower triggering rate than its
32cloud counterparts (triggering rate is the percentage of times the model
33suggests a response for an incoming message).
34
35## When to use this Model?
36
37The On-Device Smart Reply model is aimed towards improving the messaging
38experience for day-to-day conversational chat messages. We recommend using this
39model for similar use cases. Some sample messages on which the model does well
40are provided in this [tsv
41file](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/models/testdata/smartreply_samples.tsv)
42for reference. The file format is:
43
44```
45   {incoming_message  smart_reply1   [smart_reply2]   [smart_reply3]}
46```
47
48For the current model, we see a triggering rate of about 30-40% for messages
49which are similar to those provided in the tsv file above.
50
51In case the model does not trigger any response, the system falls back to
52suggesting replies from a fixed back-off set that was compiled from popular
53response intents observed in chat conversations. Some of the fallback responses
54are `Ok, Yes, No, ��, ☺`.
55
56The model can only be used for inference at this time (i.e. it cannot be custom
57trained). If you are interested to know how the model was trained, please refer
58to this [blog
59post](https://research.googleblog.com/2017/02/on-device-machine-intelligence.html)
60and [research paper](https://arxiv.org/pdf/1708.00630).
61
62## How to use this Model?
63
64We have provided a pre-built demo APK that you can download, install and test on
65your phone ([demo APK
66here](http://download.tensorflow.org/deps/tflite/SmartReplyDemo.apk)).
67
68The On-Device Smart Reply demo App works in the following way:
69
701.  Android app links to the JNI binary with a predictor library.
71
722.  In the predictor library, `GetSegmentPredictions` is called with a list of input
73    strings.
74
75    2.1 The input string can be 1-3 most recent messages of the conversations in
76    form of string vector. The model will run on these input sentences and
77    provide Smart Replies corresponding to them.
78
79    2.2 The function performs some preprocessing on input data which includes:
80
81    *   Sentence splitting: The input message will be split into sentences if
82        message has more than one sentence. Eg: a message like “How are you?
83        Want to grab lunch?” will be broken down into 2 different sentences.
84    *   Normalization: The individual sentences will be normalized by converting
85        them into lower cases, removing unnecessary punctuations, etc. Eg: “how
86        are you????” will be converted to “how are you?” (refer for NORMALIZE op
87        for more details).
88
89        The input string content will be converted to tensors.
90
91    2.3 The function then runs the prediction model on the input tensors.
92
93    2.4 The function also performs some post-processing which includes
94    aggregating the model predictions for the input sentences from 2.2 and
95    returning the appropriate responses.
96
973.  Finally, it gets response(s) from `std::vector<PredictorResponse>`, and
98    returns back to Android app. Responses are sorted in descending order of
99    confidence score.
100
101## Ops and Functionality Supported
102
103Following are the ops supported for using On-Device Smart Reply model:
104
105*   **NORMALIZE**
106
107    This is a custom op which normalizes the sentences by:
108
109    *   Converting all sentences into lower case.
110    *   Removing unnecessary punctuations (eg: “how are you????” → “how are
111        you?”).
112    *   Expanding sentences wherever necessary (eg: “ I’m home” → “I am home”).
113
114*   **SKIP_GRAM**
115
116    This is an op inside TensorFlow Lite that converts sentences into a list of
117    skip grams. The configurable parameters are `ngram_size` and
118    `max_skip_size`. For the model provided, the values for these parameters are
119    set to 3 & 2 respectively.
120
121*   **EXTRACT_FEATURES**
122
123    This is a custom op that hashes skip grams to features represented as
124    integers. Longer skip-grams are allocated higher weights.
125
126*   **LSH_PROJECTION**
127
128    This is an op inside TensorFlow Lite that projects input features to a
129    corresponding bit vector space using Locality Sensitive Hashing (LSH).
130
131*   **PREDICT**
132
133    This is a custom op that runs the input features through the projection
134    model (details [here](https://arxiv.org/pdf/1708.00630.pdf)), computes the
135    appropriate response labels along with weights for the projected features,
136    and aggregates the response labels and weights together.
137
138*   **HASHTABLE_LOOKUP**
139
140    This is an op inside TensorFlow Lite that uses label id from predict op and
141    looks up the response text from the given label id.
142
143## Further Information
144
145*   Open source code
146    [here](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/models/smartreply/).
147