Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Curio
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jim Wallace
Curio
Commits
91a131bb
Commit
91a131bb
authored
1 year ago
by
Henry Tian
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
ea7bfe7d
No related branches found
No related tags found
1 merge request
!5
Allminilm
Pipeline
#108448
failed
1 year ago
Stage: build
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Sources/SwiftNLP/2. Embeddings/MiniLMAllEmbeddings.swift
+55
-0
55 additions, 0 deletions
Sources/SwiftNLP/2. Embeddings/MiniLMAllEmbeddings.swift
with
55 additions
and
0 deletions
Sources/SwiftNLP/2. Embeddings/MiniLMAllEmbeddings.swift
0 → 100644
+
55
−
0
View file @
91a131bb
import
Foundation
import
CoreML
@available
(
macOS
12.0
,
iOS
15.0
,
*
)
public
class
MiniLMEmbeddings
{
public
let
model
:
all_MiniLM_L6_v2
public
let
tokenizer
:
BertTokenizer
public
let
inputDimention
:
Int
=
512
public
let
outputDimention
:
Int
=
384
public
init
()
{
let
modelConfig
=
MLModelConfiguration
()
modelConfig
.
computeUnits
=
.
all
do
{
self
.
model
=
try
all_MiniLM_L6_v2
(
configuration
:
modelConfig
)
}
catch
{
fatalError
(
"Failed to load the Core ML model. Error:
\(
error
.
localizedDescription
)
"
)
}
self
.
tokenizer
=
BertTokenizer
()
}
// MARK: - Dense Embeddings
public
func
encode
(
sentence
:
String
)
async
->
[
Float
]?
{
// Encode input text as bert tokens
let
inputTokens
=
tokenizer
.
buildModelTokens
(
sentence
:
sentence
)
let
(
inputIds
,
attentionMask
)
=
tokenizer
.
buildModelInputs
(
from
:
inputTokens
)
// Send tokens through the MLModel
let
embeddings
=
generateEmbeddings
(
inputIds
:
inputIds
,
attentionMask
:
attentionMask
)
return
embeddings
}
public
func
generateEmbeddings
(
inputIds
:
MLMultiArray
,
attentionMask
:
MLMultiArray
)
->
[
Float
]?
{
let
inputFeatures
=
all_MiniLM_L6_v2Input
(
input_ids
:
inputIds
,
attention_mask
:
attentionMask
)
let
output
=
try
?
model
.
prediction
(
input
:
inputFeatures
)
guard
let
embeddings
=
output
?
.
embeddings
else
{
return
nil
}
var
embeddingsArray
=
[
Float
]()
for
index
in
0
..<
embeddings
.
count
{
let
value
=
embeddings
[
index
]
.
floatValue
embeddingsArray
.
append
(
Float
(
value
))
}
return
embeddingsArray
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment