-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ML] Natural Language Processing tasks and models #73523
Merged
davidkyle
merged 21 commits into
elastic:feature/pytorch-inference
from
davidkyle:bert-tokenizer
Jun 2, 2021
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e3fafa2
WIP
davidkyle 7b74033
Add the tokenization pipeline
davidkyle ea665e4
Pass 'inputs' to infer request instead of the big whole doc
davidkyle d38a054
Add special tokens and do_lower_case setting
davidkyle 558ce9f
Add pipeline post processor
davidkyle 568fabd
Fixing tests
davidkyle 8ce7ede
Implement NER result processor
dimitris-athanasiou f3aef86
Add fill_mask processor
dimitris-athanasiou 5edff63
Move results into core and add tests
davidkyle b76f14e
Drop Pipeline terminology
davidkyle 5d2491f
Remove big config file
davidkyle 4b26720
Use a common BERT request builder
davidkyle 9aa0457
Add top k function
davidkyle 0f0424b
Handle punctuation chars next to the [MASK] token
davidkyle bc050ae
Ner Processor tests
davidkyle 1788374
tidy up
davidkyle 92c4123
Heap based top k
davidkyle b7a4a7f
Implement top k using a priority queue
davidkyle b744aa3
Fixes
davidkyle 5abec25
Fill Mask test
davidkyle ff2a6c1
Check for error from pytorch results
davidkyle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
...core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/FillMaskResults.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.ml.inference.results; | ||
|
||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.common.xcontent.ToXContentObject; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
public class FillMaskResults implements InferenceResults { | ||
|
||
public static final String NAME = "fill_mask_result"; | ||
public static final String DEFAULT_RESULTS_FIELD = "results"; | ||
|
||
private final List<Prediction> predictions; | ||
|
||
public FillMaskResults(List<Prediction> predictions) { | ||
this.predictions = predictions; | ||
} | ||
|
||
public FillMaskResults(StreamInput in) throws IOException { | ||
this.predictions = in.readList(Prediction::new); | ||
} | ||
|
||
public List<Prediction> getPredictions() { | ||
return predictions; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startArray(); | ||
for (Prediction prediction : predictions) { | ||
prediction.toXContent(builder, params); | ||
} | ||
builder.endArray(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public String getWriteableName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeList(predictions); | ||
} | ||
|
||
@Override | ||
public Map<String, Object> asMap() { | ||
Map<String, Object> map = new LinkedHashMap<>(); | ||
map.put(DEFAULT_RESULTS_FIELD, predictions.stream().map(Prediction::toMap).collect(Collectors.toList())); | ||
return map; | ||
} | ||
|
||
@Override | ||
public Object predictedValue() { | ||
if (predictions.isEmpty()) { | ||
return null; | ||
} | ||
return predictions.get(0).token; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
FillMaskResults that = (FillMaskResults) o; | ||
return Objects.equals(predictions, that.predictions); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(predictions); | ||
} | ||
|
||
public static class Prediction implements ToXContentObject, Writeable { | ||
|
||
private static final ParseField TOKEN = new ParseField("token"); | ||
private static final ParseField SCORE = new ParseField("score"); | ||
private static final ParseField SEQUENCE = new ParseField("sequence"); | ||
|
||
private final String token; | ||
private final double score; | ||
private final String sequence; | ||
|
||
public Prediction(String token, double score, String sequence) { | ||
this.token = Objects.requireNonNull(token); | ||
this.score = score; | ||
this.sequence = Objects.requireNonNull(sequence); | ||
} | ||
|
||
public Prediction(StreamInput in) throws IOException { | ||
token = in.readString(); | ||
score = in.readDouble(); | ||
sequence = in.readString(); | ||
} | ||
|
||
public double getScore() { | ||
return score; | ||
} | ||
|
||
public String getSequence() { | ||
return sequence; | ||
} | ||
|
||
public String getToken() { | ||
return token; | ||
} | ||
|
||
public Map<String, Object> toMap() { | ||
Map<String, Object> map = new LinkedHashMap<>(); | ||
map.put(TOKEN.getPreferredName(), token); | ||
map.put(SCORE.getPreferredName(), score); | ||
map.put(SEQUENCE.getPreferredName(), sequence); | ||
return map; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field(TOKEN.getPreferredName(), token); | ||
builder.field(SCORE.getPreferredName(), score); | ||
builder.field(SEQUENCE.getPreferredName(), sequence); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(token); | ||
out.writeDouble(score); | ||
out.writeString(sequence); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Prediction result = (Prediction) o; | ||
return Double.compare(result.score, score) == 0 && | ||
Objects.equals(token, result.token) && | ||
Objects.equals(sequence, result.sequence); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(token, score, sequence); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this also be
predictions
?