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
d660d689
Commit
d660d689
authored
1 year ago
by
Mingchung Xia
Browse files
Options
Downloads
Patches
Plain Diff
Test code cleanup
parent
92c1d9a8
No related branches found
No related tags found
1 merge request
!13
HNSW Implementation with Testcases
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Sources/SwiftNLP/1. Data Collection/HNSWCorpus + Dictionary.swift
+48
-0
48 additions, 0 deletions
...SwiftNLP/1. Data Collection/HNSWCorpus + Dictionary.swift
Tests/SwiftNLPTests/2. Encoding/HNSWTests.swift
+42
-11
42 additions, 11 deletions
Tests/SwiftNLPTests/2. Encoding/HNSWTests.swift
with
90 additions
and
11 deletions
Sources/SwiftNLP/1. Data Collection/HNSWCorpus + Dictionary.swift
0 → 100644
+
48
−
0
View file @
d660d689
//
// File.swift
//
//
// Created by Mingchung Xia on 2024-02-14.
//
import
Foundation
extension
HNSWCorpus
{
/// This extension is used for the dictionary operations
public
struct
DocumentVectorPair
{
var
untokenizedDocument
:
String
var
vector
:
[
Scalar
]
init
(
untokenizedDocument
:
String
,
vector
:
[
Scalar
])
{
self
.
untokenizedDocument
=
untokenizedDocument
self
.
vector
=
vector
}
}
@inlinable
func
getUntokenizedDocument
(
at
key
:
Int
)
->
String
{
if
let
pair
=
dictionary
[
key
]
{
return
pair
.
untokenizedDocument
}
else
{
fatalError
(
"Key
\(
key
)
not found in HNSW dictionary"
)
}
}
@inlinable
func
getVector
(
at
key
:
Int
)
->
[
Scalar
]
{
if
let
pair
=
dictionary
[
key
]
{
return
pair
.
vector
}
else
{
fatalError
(
"Key
\(
key
)
not found in HNSW dictionary"
)
}
}
@inlinable
func
getDictionary
()
->
[
Int
:
DocumentVectorPair
]
{
return
dictionary
}
private
func
addDocumentVectorPair
(
at
key
:
Int
,
document
:
String
,
vector
:
[
Scalar
])
{
dictionary
[
key
]
=
DocumentVectorPair
(
untokenizedDocument
:
document
,
vector
:
vector
)
}
}
This diff is collapsed.
Click to expand it.
Tests/SwiftNLPTests/2. Encoding/HNSWTests.swift
+
42
−
11
View file @
d660d689
...
@@ -14,9 +14,6 @@ final class HNSWTests: XCTestCase {
...
@@ -14,9 +14,6 @@ final class HNSWTests: XCTestCase {
"that enable us to train deep learning algorithms to learn like the human brain."
"that enable us to train deep learning algorithms to learn like the human brain."
]
]
// let encoder = ContextFreeEncoder<Double>(source: .glove6B50d)
// let encoder = ContextFreeEncoder<Double>(source: .hnswindex)
// var corpus = HNSWCorpus(_documentEncoder: encoder)
var
corpus
=
HNSWCorpus
(
encoding
:
.
glove6B50d
)
var
corpus
=
HNSWCorpus
(
encoding
:
.
glove6B50d
)
corpus
.
addUntokenizedDocuments
(
docs
)
corpus
.
addUntokenizedDocuments
(
docs
)
...
@@ -57,9 +54,6 @@ final class HNSWTests: XCTestCase {
...
@@ -57,9 +54,6 @@ final class HNSWTests: XCTestCase {
"All science is either physics or stamp collecting. - Ernest Rutherford"
"All science is either physics or stamp collecting. - Ernest Rutherford"
]
]
// let encoder = ContextFreeEncoder<Double>(source: .glove6B50d)
// let encoder = ContextFreeEncoder<Double>(source: .hnswindex)
// var corpus = HNSWCorpus(_documentEncoder: encoder)
var
corpus
=
HNSWCorpus
(
encoding
:
.
glove6B50d
)
var
corpus
=
HNSWCorpus
(
encoding
:
.
glove6B50d
)
corpus
.
addUntokenizedDocuments
(
twentyQuotes
)
corpus
.
addUntokenizedDocuments
(
twentyQuotes
)
...
@@ -85,9 +79,6 @@ final class HNSWTests: XCTestCase {
...
@@ -85,9 +79,6 @@ final class HNSWTests: XCTestCase {
let
(
submissions
,
_
):
([
Submission
],[
Data
])
=
try
await
loadFromRedditArchive
(
submissionsData
)
let
(
submissions
,
_
):
([
Submission
],[
Data
])
=
try
await
loadFromRedditArchive
(
submissionsData
)
// let encoder = ContextFreeEncoder<Double>(source: .glove6B50d)
// let encoder = ContextFreeEncoder<Double>(source: .hnswindex)
// var corpus = HNSWCorpus(_documentEncoder: encoder)
var
corpus
=
HNSWCorpus
(
encoding
:
.
glove6B50d
)
var
corpus
=
HNSWCorpus
(
encoding
:
.
glove6B50d
)
for
submission
in
submissions
{
for
submission
in
submissions
{
...
@@ -172,7 +163,6 @@ final class HNSWTests: XCTestCase {
...
@@ -172,7 +163,6 @@ final class HNSWTests: XCTestCase {
do
{
do
{
print
(
"Attempting to query corpus.encodedDocuments.find()..."
)
print
(
"Attempting to query corpus.encodedDocuments.find()..."
)
// TODO: Print this as a readable result - reverse encoding?
let
queryVector
:
[
Double
]
=
_documentEncoder
.
encodeToken
(
query
)
.
map
{
Double
(
$0
)
}
let
queryVector
:
[
Double
]
=
_documentEncoder
.
encodeToken
(
query
)
.
map
{
Double
(
$0
)
}
let
results
=
try
corpus
.
encodedDocuments
.
find
(
near
:
queryVector
,
limit
:
8
)
let
results
=
try
corpus
.
encodedDocuments
.
find
(
near
:
queryVector
,
limit
:
8
)
...
@@ -223,7 +213,48 @@ final class HNSWTests: XCTestCase {
...
@@ -223,7 +213,48 @@ final class HNSWTests: XCTestCase {
do
{
do
{
print
(
"Attempting to query corpus.encodedDocuments.find()..."
)
print
(
"Attempting to query corpus.encodedDocuments.find()..."
)
// TODO: Print this as a readable result - reverse encoding?
let
queryVector
:
[
Double
]
=
_documentEncoder
.
encodeToken
(
query
)
.
map
{
Double
(
$0
)
}
let
results
=
try
corpus
.
encodedDocuments
.
find
(
near
:
queryVector
,
limit
:
8
)
for
result
in
results
{
print
(
corpus
.
getUntokenizedDocument
(
at
:
result
.
id
))
}
print
(
"Query completed!"
)
}
catch
{
print
(
"Error when trying corpus.encodedDocuments.find():
\(
error
)
"
)
}
}
// TODO: Get HNSWCorpus from memory map
func
testSubredditQueryExample
()
async
throws
{
guard
let
submissionsURL
=
Bundle
.
module
.
url
(
forResource
:
"Guelph_submissions"
,
withExtension
:
"zst"
)
else
{
fatalError
(
"Failed to find waterloo_submissions.zst in test bundle."
)
}
guard
let
submissionsData
=
try
?
Data
(
contentsOf
:
submissionsURL
)
else
{
fatalError
(
"Failed to load waterloo_submissions.zst from test bundle."
)
}
let
(
submissions
,
_
):
([
Submission
],[
Data
])
=
try
await
loadFromRedditArchive
(
submissionsData
)
let
_documentEncoder
=
ContextFreeEncoder
<
Double
>
(
source
:
.
glove6B50d
)
var
corpus
=
HNSWCorpus
(
encoder
:
_documentEncoder
)
for
submission
in
submissions
{
if
let
text
=
submission
.
selftext
{
corpus
.
addUntokenizedDocument
(
text
)
}
}
let
query
=
"Mr. Goose is a very important figure at the University of Waterloo."
let
size
=
MemoryLayout
.
size
(
ofValue
:
corpus
)
print
(
"Approximate memory footprint:
\(
size
)
bytes"
)
do
{
print
(
"Attempting to query corpus.encodedDocuments.find()..."
)
let
queryVector
:
[
Double
]
=
_documentEncoder
.
encodeToken
(
query
)
.
map
{
Double
(
$0
)
}
let
queryVector
:
[
Double
]
=
_documentEncoder
.
encodeToken
(
query
)
.
map
{
Double
(
$0
)
}
let
results
=
try
corpus
.
encodedDocuments
.
find
(
near
:
queryVector
,
limit
:
8
)
let
results
=
try
corpus
.
encodedDocuments
.
find
(
near
:
queryVector
,
limit
:
8
)
...
...
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